A comprehensive repository covering both the theoretical foundations and practical implementations of diffusion models in PyTorch. This project bridges mathematical theory with code, guiding you from foundational concepts to advanced techniques in generative AI.
Diffusion models have become one of the most exciting and powerful approaches in generative AI. This repository provides a comprehensive journey from mathematical foundations to practical implementations, featuring rigorous theory alongside executable code. By working through the materials, you'll gain both deep theoretical understanding and hands-on experience implementing various diffusion model techniques on image datasets.
The content is organized into a series of modules, each building on the previous ones:
- Introduction & Motivation - Overview of generative models and the motivation behind diffusion models
- DDPM (Denoising Diffusion Probabilistic Models) - Implementation of the foundational diffusion model approach
- DDIM (Denoising Diffusion Implicit Models) - Extension to non-Markovian sampling for faster generation
- Classifier Guidance & Conditional Generation - Methods to guide the generation process
- Diffusion Models on CelebA - Applying diffusion models to human face generation
├── 0X-module.md # Theory explanations for each module
├── 0X-module.ipynb # Interactive notebook implementations
├── requirements.txt # Required Python packages
├── train_celeba_ddpm.py # Training script for CelebA dataset
├── src/ # Source code modules
│ ├── data.py # Data loading utilities
│ ├── diffusion.py # Core diffusion process implementations
│ ├── model.py # Model architectures
│ ├── scheduler.py # Noise scheduling algorithms
│ ├── training.py # Training loop utilities
│ └── models/ # Additional model implementations
├── data/ # Dataset storage
│ ├── MNIST/ # MNIST dataset
│ └── celeba/ # CelebA dataset
├── experiments/ # Outputs from training runs
│ └── celeba_64x64/ # Example experiment outputs
└── images/ # Supporting images for documentation
-
Clone the repository:
git clone <repository-url> cd DiffusionModels-Theory2Code
-
Create a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the required packages:
pip install -r requirements.txt
-
Download pre-trained models (optional):
All pre-trained models from the
models/directory can be downloaded from the following Google Drive link:Download and place the models in the
models/directory to skip training and use them directly.
For the best learning experience, follow these steps:
- Read each
0X-module.mdfile to understand the theory - Complete the corresponding
0X-module.ipynbnotebook to implement concepts - Run experiments with different parameters to build intuition
To train a diffusion model on the CelebA dataset:
python train_celeba_ddpm.py --image_size 64 --batch_size 64 --epochs 100Check the script for additional command line arguments to customize your training.
The Jupyter notebooks contain both theoretical explanations and practical implementations. To run them:
jupyter notebookThen navigate to the desired notebook in the browser interface.
The workshop covers essential mathematical concepts:
- Forward and reverse diffusion processes
- Evidence Lower Bound (ELBO) optimization
- Variance scheduling techniques
- Non-Markovian sampling strategies
- Classifier guidance for conditional generation
The workshop uses several datasets:
- MNIST: Handwritten digits (basic examples)
- CelebA: Human faces (high-resolution generation)
- Theoretical Foundation: Comprehensive explanation of diffusion model mathematics with complete derivations
- Theory-to-Code Translation: Direct mapping between mathematical concepts and their PyTorch implementations
- Step-by-Step Implementation: Guided coding exercises with progressive complexity
- Visualization Tools: Helper functions to visualize the diffusion process
- Modular Design: Code structured for clarity and reusability
- Scalable Approaches: From simple to complex datasets
- Ho, J., Jain, A., & Abbeel, P. (2020). Denoising diffusion probabilistic models. Advances in Neural Information Processing Systems.
- Song, J., Meng, C., & Ermon, S. (2020). Denoising diffusion implicit models. International Conference on Learning Representations.
- Dhariwal, P., & Nichol, A. (2021). Diffusion models beat GANs on image synthesis. Advances in Neural Information Processing Systems.
Contributions to improve the workshop materials are welcome! Please feel free to submit issues or pull requests.