Skip to content

Jarhatz/brain_diffusion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Brain Diffusion

BrainDiffusion: Reconstructing Visual Semantics from Non-Invasive Neural Activity Readings

The files that have been heavily modified / newly created are:

code/sc_mbm/mae_for_eeg.py

code/eeg_ldm.py

code/dc_ldm/ldm_for_eeg.py

code/dc_ldm/models/diffusion/ddpm.py

code/cluster_analysis.py

code/gen_eval_eeg.py

code/eval_generations.py

In order to replicate findings, please make sure all data has been downloaded from their respective links and organized in the mentioned file structure format at the bottom of the page.

  • EEG Waves : Please download and place them in /datasets and /pretrains folders in the project root dir
  • Image Pairs : Please download ImageNet subset of shown images
  • Stable Diffusion 1.5 Checkpoint : Please download SD 1.5 checkpoint and place in /pretrains/models

Once all of these dependencies have been downloaded and loaded into their correct paths, follow the steps for reproducing the results.

Environment Setup:

Create and activate conda environment named dreamdiffusion from the environment.yaml

conda env create -f environment.yaml
conda activate dreamdiffusion

Finetuning the Stable Diffusion with Pre-trained EEG Encoder:

In this stage, the cross-attention heads and pre-trained EEG encoder will be jointly optimized with EEG-image pairs.

python code/eeg_ldm.py --dataset EEG --batch_size 10 --num_epoch 100 --lr 1e-5

Optionally you can also provide a checkpoint file to resume from:

python code/eeg_ldm.py --dataset EEG --batch_size 10 --num_epoch 100 --lr 1e-5 --checkpoint_path [CHECKPOINT_PATH]

Cluster Analysis:

After fine-tuning, EEG Encoder's embedding space can be visually plotted with t-SNE dimensional reduction.

python code/cluster_analysis.py --checkpoint_path [CHECKPOINT_PATH] -o [OUTPUT_PATH]

Visualizing Loss:

Plot loss curves.

python code/visualize_loss.py --loss_path [LOSS_PATH]

Generating Images:

Generate images based on the held out test EEG dataset.

python code/gen_eval_eeg.py --dataset EEG --model_path [MODEL_PATH]

Evaluate Generated Images:

Run evaluation metrics on the generated images from the test set. ViT ImageNet-1K classifier used for Top1-Acc and Top3-Acc scores.

python code/eval_generations.py --results_path [RESULTS_PATH]

Project Directory Structure:

/pretrains
┣ πŸ“‚ models
┃   β”— πŸ“œ config.yaml
┃   β”— πŸ“œ v1-5-pruned.ckpt

┣ πŸ“‚ generation  
┃   β”— πŸ“œ checkpoint_best.pth 

┣ πŸ“‚ eeg_pretain
┃   β”— πŸ“œ checkpoint.pth  (pre-trained EEG encoder)

/datasets
┣ πŸ“‚ imageNet_images (subset of Imagenet)
β”—  πŸ“œ imagenet_label_map.csv
β”—  πŸ“œ block_splits_by_image_all.pth
β”—  πŸ“œ block_splits_by_image_single.pth 
β”—  πŸ“œ eeg_5_95_std.pth  

/code
┣ πŸ“‚ sc_mbm
┃   β”— πŸ“œ mae_for_eeg.py
┃   β”— πŸ“œ trainer.py
┃   β”— πŸ“œ utils.py

┣ πŸ“‚ dc_ldm
┃   β”— πŸ“œ ldm_for_eeg.py
┃   β”— πŸ“œ utils.py
┃   ┣ πŸ“‚ models
┃   ┃   β”— (adopted from LDM)
┃   ┣ πŸ“‚ modules
┃   ┃   β”— (adopted from LDM)

β”—  πŸ“œ stageA1_eeg_pretrain.py   (main script for EEG pre-training)
β”—  πŸ“œ eeg_ldm.py    (main script for fine-tuning stable diffusion)
β”—  πŸ“œ gen_eval_eeg.py               (main script for generating images)
β”—  πŸ“œ eval_generations.py               (main script for evaluating generated images)
β”—  πŸ“œ cluster_analysis.py                (functions for embedding alignment analysis)
β”—  πŸ“œ visualize_loss.py                (functions for visualizing losses)
β”—  πŸ“œ dataset.py                (functions for loading datasets)
β”—  πŸ“œ eval_metrics.py           (functions for evaluation metrics)
β”—  πŸ“œ config.py                 (configurations for the main scripts)

Acknowledgement

This code is built upon the publicly available code DreamDiffusion. Thanks to these authors for making their excellent work and codes publicly available.

About

BrainDiffusion: Generate Images With Your Mind!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages