GithubHelp home page GithubHelp logo

dreamdiffusion's Introduction

DreamDiffusion: Generating High-Quality Images from Brain EEG Signals

DreamDiffusion

DreamDiffusion is a framework for generating high-quality images from brain EEG signals. This document introduces the precesedures required for replicating the results in DreamDiffusion: Generating High-Quality Images from Brain EEG Signals

Abstract

This paper introduces DreamDiffusion, a novel method for generating high-quality images directly from brain electroencephalogram (EEG) signals, without the need to translate thoughts into text. DreamDiffusion leverages pre-trained text-to-image models and employs temporal masked signal modeling to pre-train the EEG encoder for effective and robust EEG representations. Additionally, the method further leverages the CLIP image encoder to provide extra supervision to better align EEG, text, and image embeddings with limited EEG-image pairs. Overall, the proposed method overcomes the challenges of using EEG signals for image generation, such as noise, limited information, and individual differences, and achieves promising results. Quantitative and qualitative results demonstrate the effectiveness of the proposed method as a significant step towards portable and low-cost "thoughts-to-image", with potential applications in neuroscience and computer vision.

Overview

pipeline

The datasets folder and pretrains folder are not included in this repository. Please download them from link and put them in the root directory of this repository as shown below. We also provide a copy of the Imagenet subset.

For Stable Diffusion, we just use standard SD1.5. You can download it from the official page of Stability. You want the file "v1-5-pruned.ckpt".

File path | Description


/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)

โ”—  ๐Ÿ“œ 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)

โ”—  ๐Ÿ“œ dataset.py                (functions for loading datasets)
โ”—  ๐Ÿ“œ eval_metrics.py           (functions for evaluation metrics)
โ”—  ๐Ÿ“œ config.py                 (configurations for the main scripts)

Environment setup

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

conda env create -f env.yaml
conda activate dreamdiffusion

Download data and checkpoints

We also provide checkpoints at link to run the finetuing and decoding directly.

Pre-training on EEG data

To perform the pre-training from scratch with defaults parameters, run

python3 code/stageA1_eeg_pretrain.py

Hyper-parameters can be changed with command line arguments,

python3 code/stageA1_eeg_pretrain.py --mask_ratio 0.75 --num_epoch 800 --batch_size 2

Or the parameters can also be changed in code/config.py

Multiple-GPU (DDP) training is supported, run with

python -m torch.distributed.launch --nproc_per_node=NUM_GPUS code/stageA1_eeg_pretrain.py

Finetune the Stable Diffusion with Pre-trained fMRI Encoder

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

python3 code/eeg_ldm.py --dataset EEG  --num_epoch 300 --batch_size 4 --pretrain_mbm_path ../dreamdiffusion/pretrains/eeg_pretrain/checkpoint.pth

Generating Images with Trained Checkpoints

Run this stage with our provided checkpoints:

python3 code/gen_eval_eeg.py --dataset EEG --model_path  pretrains/generation/checkpoint_best.pth

results

Acknowledgement

This code is built upon the publicly available code Mind-vis and StableDiffusion. Thanks these authors for making their excellent work and codes publicly available.

Citation

Please cite the following paper if you use this repository in your reseach.

@article{bai2023dreamdiffusion,
  title={DreamDiffusion: Generating High-Quality Images from Brain EEG Signals},
  author={Bai, Yunpeng and Wang, Xintao and Cao, Yanpei and Ge, Yixiao and Yuan, Chun and Shan, Ying},
  journal={arXiv preprint arXiv:2306.16934},
  year={2023}
}

dreamdiffusion's People

Contributors

bbaaii avatar

Watchers

Joel Navez CT avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.