项目作者: lucidrains

项目描述 :
Implementation of Denoising Diffusion Probabilistic Model in Pytorch
高级语言: Python
项目地址: git://github.com/lucidrains/denoising-diffusion-pytorch.git
创建时间: 2020-08-26T02:22:10Z
项目社区:https://github.com/lucidrains/denoising-diffusion-pytorch

开源协议:MIT License

下载


Denoising Diffusion Probabilistic Model, in Pytorch

Implementation of Denoising Diffusion Probabilistic Model in Pytorch. It is a new approach to generative modeling that may have the potential to rival GANs. It uses denoising score matching to estimate the gradient of the data distribution, followed by Langevin sampling to sample from the true distribution.

This implementation was inspired by the official Tensorflow version here

Youtube AI Educators - Yannic Kilcher | AI Coffeebreak with Letitia | Outlier

Flax implementation from YiYi Xu

Annotated code by Research Scientists / Engineers from 🤗 Huggingface

Update: Turns out none of the technicalities really matters at all | “Cold Diffusion” paper | Muse

PyPI version

Install

  1. $ pip install denoising_diffusion_pytorch

Usage

  1. import torch
  2. from denoising_diffusion_pytorch import Unet, GaussianDiffusion
  3. model = Unet(
  4. dim = 64,
  5. dim_mults = (1, 2, 4, 8),
  6. flash_attn = True
  7. )
  8. diffusion = GaussianDiffusion(
  9. model,
  10. image_size = 128,
  11. timesteps = 1000 # number of steps
  12. )
  13. training_images = torch.rand(8, 3, 128, 128) # images are normalized from 0 to 1
  14. loss = diffusion(training_images)
  15. loss.backward()
  16. # after a lot of training
  17. sampled_images = diffusion.sample(batch_size = 4)
  18. sampled_images.shape # (4, 3, 128, 128)

Or, if you simply want to pass in a folder name and the desired image dimensions, you can use the Trainer class to easily train a model.

  1. from denoising_diffusion_pytorch import Unet, GaussianDiffusion, Trainer
  2. model = Unet(
  3. dim = 64,
  4. dim_mults = (1, 2, 4, 8),
  5. flash_attn = True
  6. )
  7. diffusion = GaussianDiffusion(
  8. model,
  9. image_size = 128,
  10. timesteps = 1000, # number of steps
  11. sampling_timesteps = 250 # number of sampling timesteps (using ddim for faster inference [see citation for ddim paper])
  12. )
  13. trainer = Trainer(
  14. diffusion,
  15. 'path/to/your/images',
  16. train_batch_size = 32,
  17. train_lr = 8e-5,
  18. train_num_steps = 700000, # total training steps
  19. gradient_accumulate_every = 2, # gradient accumulation steps
  20. ema_decay = 0.995, # exponential moving average decay
  21. amp = True, # turn on mixed precision
  22. calculate_fid = True # whether to calculate fid during training
  23. )
  24. trainer.train()

Samples and model checkpoints will be logged to ./results periodically

Multi-GPU Training

The Trainer class is now equipped with 🤗 Accelerator. You can easily do multi-gpu training in two steps using their accelerate CLI

At the project root directory, where the training script is, run

  1. $ accelerate config

Then, in the same directory

  1. $ accelerate launch train.py

Miscellaneous

1D Sequence

By popular request, a 1D Unet + Gaussian Diffusion implementation.

  1. import torch
  2. from denoising_diffusion_pytorch import Unet1D, GaussianDiffusion1D, Trainer1D, Dataset1D
  3. model = Unet1D(
  4. dim = 64,
  5. dim_mults = (1, 2, 4, 8),
  6. channels = 32
  7. )
  8. diffusion = GaussianDiffusion1D(
  9. model,
  10. seq_length = 128,
  11. timesteps = 1000,
  12. objective = 'pred_v'
  13. )
  14. training_seq = torch.rand(64, 32, 128) # features are normalized from 0 to 1
  15. loss = diffusion(training_seq)
  16. loss.backward()
  17. # Or using trainer
  18. dataset = Dataset1D(training_seq) # this is just an example, but you can formulate your own Dataset and pass it into the `Trainer1D` below
  19. trainer = Trainer1D(
  20. diffusion,
  21. dataset = dataset,
  22. train_batch_size = 32,
  23. train_lr = 8e-5,
  24. train_num_steps = 700000, # total training steps
  25. gradient_accumulate_every = 2, # gradient accumulation steps
  26. ema_decay = 0.995, # exponential moving average decay
  27. amp = True, # turn on mixed precision
  28. )
  29. trainer.train()
  30. # after a lot of training
  31. sampled_seq = diffusion.sample(batch_size = 4)
  32. sampled_seq.shape # (4, 32, 128)

Trainer1D does not evaluate the generated samples in any way since the type of data is not known.

You could consider adding a suitable metric to the training loop yourself after doing an editable install of this package
pip install -e ..

Citations

  1. @inproceedings{NEURIPS2020_4c5bcfec,
  2. author = {Ho, Jonathan and Jain, Ajay and Abbeel, Pieter},
  3. booktitle = {Advances in Neural Information Processing Systems},
  4. editor = {H. Larochelle and M. Ranzato and R. Hadsell and M.F. Balcan and H. Lin},
  5. pages = {6840--6851},
  6. publisher = {Curran Associates, Inc.},
  7. title = {Denoising Diffusion Probabilistic Models},
  8. url = {https://proceedings.neurips.cc/paper/2020/file/4c5bcfec8584af0d967f1ab10179ca4b-Paper.pdf},
  9. volume = {33},
  10. year = {2020}
  11. }
  1. @InProceedings{pmlr-v139-nichol21a,
  2. title = {Improved Denoising Diffusion Probabilistic Models},
  3. author = {Nichol, Alexander Quinn and Dhariwal, Prafulla},
  4. booktitle = {Proceedings of the 38th International Conference on Machine Learning},
  5. pages = {8162--8171},
  6. year = {2021},
  7. editor = {Meila, Marina and Zhang, Tong},
  8. volume = {139},
  9. series = {Proceedings of Machine Learning Research},
  10. month = {18--24 Jul},
  11. publisher = {PMLR},
  12. pdf = {http://proceedings.mlr.press/v139/nichol21a/nichol21a.pdf},
  13. url = {https://proceedings.mlr.press/v139/nichol21a.html},
  14. }
  1. @inproceedings{kingma2021on,
  2. title = {On Density Estimation with Diffusion Models},
  3. author = {Diederik P Kingma and Tim Salimans and Ben Poole and Jonathan Ho},
  4. booktitle = {Advances in Neural Information Processing Systems},
  5. editor = {A. Beygelzimer and Y. Dauphin and P. Liang and J. Wortman Vaughan},
  6. year = {2021},
  7. url = {https://openreview.net/forum?id=2LdBqxc1Yv}
  8. }
  1. @article{Karras2022ElucidatingTD,
  2. title = {Elucidating the Design Space of Diffusion-Based Generative Models},
  3. author = {Tero Karras and Miika Aittala and Timo Aila and Samuli Laine},
  4. journal = {ArXiv},
  5. year = {2022},
  6. volume = {abs/2206.00364}
  7. }
  1. @article{Song2021DenoisingDI,
  2. title = {Denoising Diffusion Implicit Models},
  3. author = {Jiaming Song and Chenlin Meng and Stefano Ermon},
  4. journal = {ArXiv},
  5. year = {2021},
  6. volume = {abs/2010.02502}
  7. }
  1. @misc{chen2022analog,
  2. title = {Analog Bits: Generating Discrete Data using Diffusion Models with Self-Conditioning},
  3. author = {Ting Chen and Ruixiang Zhang and Geoffrey Hinton},
  4. year = {2022},
  5. eprint = {2208.04202},
  6. archivePrefix = {arXiv},
  7. primaryClass = {cs.CV}
  8. }
  1. @article{Salimans2022ProgressiveDF,
  2. title = {Progressive Distillation for Fast Sampling of Diffusion Models},
  3. author = {Tim Salimans and Jonathan Ho},
  4. journal = {ArXiv},
  5. year = {2022},
  6. volume = {abs/2202.00512}
  7. }
  1. @article{Ho2022ClassifierFreeDG,
  2. title = {Classifier-Free Diffusion Guidance},
  3. author = {Jonathan Ho},
  4. journal = {ArXiv},
  5. year = {2022},
  6. volume = {abs/2207.12598}
  7. }
  1. @article{Sunkara2022NoMS,
  2. title = {No More Strided Convolutions or Pooling: A New CNN Building Block for Low-Resolution Images and Small Objects},
  3. author = {Raja Sunkara and Tie Luo},
  4. journal = {ArXiv},
  5. year = {2022},
  6. volume = {abs/2208.03641}
  7. }
  1. @inproceedings{Jabri2022ScalableAC,
  2. title = {Scalable Adaptive Computation for Iterative Generation},
  3. author = {A. Jabri and David J. Fleet and Ting Chen},
  4. year = {2022}
  5. }
  1. @article{Cheng2022DPMSolverPlusPlus,
  2. title = {DPM-Solver++: Fast Solver for Guided Sampling of Diffusion Probabilistic Models},
  3. author = {Cheng Lu and Yuhao Zhou and Fan Bao and Jianfei Chen and Chongxuan Li and Jun Zhu},
  4. journal = {NeuRips 2022 Oral},
  5. year = {2022},
  6. volume = {abs/2211.01095}
  7. }
  1. @inproceedings{Hoogeboom2023simpleDE,
  2. title = {simple diffusion: End-to-end diffusion for high resolution images},
  3. author = {Emiel Hoogeboom and Jonathan Heek and Tim Salimans},
  4. year = {2023}
  5. }
  1. @misc{https://doi.org/10.48550/arxiv.2302.01327,
  2. doi = {10.48550/ARXIV.2302.01327},
  3. url = {https://arxiv.org/abs/2302.01327},
  4. author = {Kumar, Manoj and Dehghani, Mostafa and Houlsby, Neil},
  5. title = {Dual PatchNorm},
  6. publisher = {arXiv},
  7. year = {2023},
  8. copyright = {Creative Commons Attribution 4.0 International}
  9. }
  1. @inproceedings{Hang2023EfficientDT,
  2. title = {Efficient Diffusion Training via Min-SNR Weighting Strategy},
  3. author = {Tiankai Hang and Shuyang Gu and Chen Li and Jianmin Bao and Dong Chen and Han Hu and Xin Geng and Baining Guo},
  4. year = {2023}
  5. }
  1. @misc{Guttenberg2023,
  2. author = {Nicholas Guttenberg},
  3. url = {https://www.crosslabs.org/blog/diffusion-with-offset-noise}
  4. }
  1. @inproceedings{Lin2023CommonDN,
  2. title = {Common Diffusion Noise Schedules and Sample Steps are Flawed},
  3. author = {Shanchuan Lin and Bingchen Liu and Jiashi Li and Xiao Yang},
  4. year = {2023}
  5. }
  1. @inproceedings{dao2022flashattention,
  2. title = {Flash{A}ttention: Fast and Memory-Efficient Exact Attention with {IO}-Awareness},
  3. author = {Dao, Tri and Fu, Daniel Y. and Ermon, Stefano and Rudra, Atri and R{\'e}, Christopher},
  4. booktitle = {Advances in Neural Information Processing Systems},
  5. year = {2022}
  6. }
  1. @article{Bondarenko2023QuantizableTR,
  2. title = {Quantizable Transformers: Removing Outliers by Helping Attention Heads Do Nothing},
  3. author = {Yelysei Bondarenko and Markus Nagel and Tijmen Blankevoort},
  4. journal = {ArXiv},
  5. year = {2023},
  6. volume = {abs/2306.12929},
  7. url = {https://api.semanticscholar.org/CorpusID:259224568}
  8. }
  1. @article{Karras2023AnalyzingAI,
  2. title = {Analyzing and Improving the Training Dynamics of Diffusion Models},
  3. author = {Tero Karras and Miika Aittala and Jaakko Lehtinen and Janne Hellsten and Timo Aila and Samuli Laine},
  4. journal = {ArXiv},
  5. year = {2023},
  6. volume = {abs/2312.02696},
  7. url = {https://api.semanticscholar.org/CorpusID:265659032}
  8. }
  1. @article{Li2024ImmiscibleDA,
  2. title = {Immiscible Diffusion: Accelerating Diffusion Training with Noise Assignment},
  3. author = {Yiheng Li and Heyang Jiang and Akio Kodaira and Masayoshi Tomizuka and Kurt Keutzer and Chenfeng Xu},
  4. journal = {ArXiv},
  5. year = {2024},
  6. volume = {abs/2406.12303},
  7. url = {https://api.semanticscholar.org/CorpusID:270562607}
  8. }
  1. @article{Chung2024CFGMC,
  2. title = {CFG++: Manifold-constrained Classifier Free Guidance for Diffusion Models},
  3. author = {Hyungjin Chung and Jeongsol Kim and Geon Yeong Park and Hyelin Nam and Jong Chul Ye},
  4. journal = {ArXiv},
  5. year = {2024},
  6. volume = {abs/2406.08070},
  7. url = {https://api.semanticscholar.org/CorpusID:270391454}
  8. }
  1. @inproceedings{Sadat2024EliminatingOA,
  2. title = {Eliminating Oversaturation and Artifacts of High Guidance Scales in Diffusion Models},
  3. author = {Seyedmorteza Sadat and Otmar Hilliges and Romann M. Weber},
  4. year = {2024},
  5. url = {https://api.semanticscholar.org/CorpusID:273098845}
  6. }