This is the official code for the paper "MindDiffuser: Controlled Image Reconstruction from Human Brain Activity with Semantic and Structural Diffusion"(ACMMM2023)
Schematic diagram of MindDiffuser.
(a) Decoders are trained to fit fMRI with averaged CLIP text embeddings π, CLIP image feature πππΆπΏπΌπ, and VQ-VAE latent feature π§.
(b) The two-stage image reconstruction process. In stage 1, an initial reconstructed image is generated using the decoded CLIP text feature π and VQ-VAE latent feature π§. In stage 2, the decoded CLIP image feature is used as a constraint to iteratively adjust π and π§ until the final reconstruction result matches the original image in terms of both semantic and structure.
A brief comparison of image reconstruction results.
Reconstruction results of MindDiffuser on multiple subjects
During the feature decoding process, we use L2-regularized linear regression model to automatically select voxels to fit three types
of feature: semantic feature π, detail feature π§, and structural feature ππΆπΏπΌπ. We ultilize pycortex to project the weights of each
voxel in the fitted model onto the corresponding 3D coordinates in the visual cortex.
This code was developed and tested with:
- Python version 3.8.5
- PyTorch version 1.11.0
- A100 40G
- The conda environment defined in environment_1.yml
NSD dataset
http://naturalscenesdataset.org/
Data preparation
https://github.com/styvesg/nsd
First, set up the conda enviroment as follows:
conda env create -f environment_1.yml # create conda env
conda activate MindDiffuser # activate conda env
cd your_folder
python Feature extractor/Semantic_feature_extraction.py
python Feature extractor/detail_extracttion.py
python Feature extractor/Structural_feature_extraction.py
python Feature extractor/Structural_feature_selection.py
cd your_folder
python Feature decoding/Semantic_feature_decoding.py
python Feature decoding/Structural_feature_decoding.py
python Feature decoding/detail_decoding.py
I will upload files such as features.npy
,mask.npy
, checkpoints of decoders
, etc. to the checkpoints folder.
cd your_folder
python Image reconstruction/Reconstruction.py
Reproduce the results of "High-resolution image reconstruction with latent diffusion models from human brain activity"(CVPR2023)
After extracting and decoding the features, run the following codeοΌ
cd your_folder
python Reproduce Takagi's results/image_reconstruction.py
Reproduce the results of "Reconstruction of Perceived Images from fMRI Patterns and Semantic Brain Exploration using Instance-Conditioned GANs"
After configuring the environment and codes provided by Ozcelik, run the following codes:
cd your_folder
python Reproduce Ozcelik's results/extract_features.py
python Reproduce Ozcelik's results/train_regression.py
python Reproduce Ozcelik's results/reconstruct_images.py