A Complete, Hands-On Guide to Building, Training, Fine-Tuning and Deploying Your Own Diffusion Models from Scratch
Introduction: From Noise to Meaning
- What This Book Covers
- How to Use This Book
- Prerequisites and Setup
- A Note on the Mathematics
Chapter 1: Generative Modeling and the Path to Diffusion
- The Generative Modeling Problem
- From VAEs to GANs: What Came Before
- The Intuitive Picture of Adding and Removing Noise
- Why Diffusion Won
- What This Book Will Teach You
Chapter 2: Linear Algebra for Diffusion Models
- Vectors, Matrices and Tensors: Notation and Conventions
- Matrix Multiplication and Convolution as Linear Maps
- Eigendecomposition and Singular Value Decomposition
- Low-Rank Approximation and Dimensionality Reduction
- Tensors in Deep Learning Frameworks
Chapter 3: Calculus and Optimization Fundamentals
- Gradients, Jacobians and the Chain Rule
- Optimization Landscapes and Local Minima
- Gradient Descent and Its Variants
- Adam, AdamW and Modern Optimizers
- Learning Rate Schedules and Warmup
Chapter 4: Probability, Statistics and Bayesian Thinking
- Random Variables, Distributions and Expectations
- The Gaussian Distribution and Multivariate Gaussians
- Bayes Rule and Posterior Inference
- Kullback-Leibler Divergence and Related Metrics
- Monte Carlo Estimation and Score Functions
Chapter 5: Neural Networks, Backpropagation and Generative Architectures
- Layers, Activations and Forward Computation
- Backpropagation and Automatic Differentiation
- Loss Functions and Training Dynamics
- Convolutional Neural Networks for Images
- Attention Mechanisms and Self-Attention
Chapter 6: The Forward Diffusion Process
- The Core Idea: Gradual Corruption of Data
- Defining the Markov Chain of Noise Transitions
- The Variance Schedule and Noise Levels
- Closed-Form Sampling at Arbitrary Times
- What the Diffused Data Looks Like
Chapter 7: The Reverse Diffusion Process
- Reversing the Markov Chain in Theory
- The Intractable Reverse Transition
- Approximating the Reverse Process with a Neural Network
- Noise Prediction as a Learning Objective
- Why Denoising Works as a Generative Process
Chapter 8: The Variational Lower Bound and ELBO
- Evidence Lower Bound as the Training Objective
- Deriving the ELBO for Diffusion Models
- The Three ELBO Terms: KL Divergences and Log Likelihood
- Simplifying the ELBO to a Practical Loss Function
- What the Simplification Means for Training
Chapter 9: Score Matching and the Score Function Perspective
- The Score Function: Gradient of the Log-Density
- Denoising Score Matching
- Equivalence Between Noise Prediction and Score Matching
- Multi-Scale Score Matching
- Practical Implications of the Score View
Chapter 10: Stochastic Differential Equations and Probability Flow
- From Discrete Steps to Continuous-Time Processes
- Stochastic Differential Equations and Diffusion
- The Probability Flow Ordinary Differential Equation
- Diverse Sampling via SDEs vs Deterministic Paths via ODEs
- Numerical Solvers and Their Trade-offs
Chapter 11: Building the Minimal Diffusion Model
- Environment Setup and Dependencies
- The Dataset: Loading and Preprocessing Images
- A Simple MLP for Noise Prediction
- The Forward Diffusion Step
- Training the Minimal Model
- Sampling and Generating Images
Chapter 12: The U-Net Architecture
- Anatomy of a U-Net: Encoders, Decoders and Skip Connections
- Residual Blocks and Normalization
- Time Embeddings: Encoding the Diffusion Step
- Cross-Attention and Conditioning
- Implementing U-Net in PyTorch
Chapter 13: Noise Schedules and Their Impact
- Linear, Cosine and Other Schedules
- How the Schedule Shapes the Training Distribution
- Implementing Flexible Schedulers
- Empirical Comparison of Schedules
- Choosing a Schedule for Your Problem
Chapter 14: The Complete Training Pipeline
- The Training Loop: Batch-by-Batch Mechanics
- Mixed Precision and Gradient Accumulation
- Checkpointing, Logging and Experiment Tracking
- Validation Metrics and Qualitative Evaluation
- Distributed Training for Scale
Chapter 15: DDIM and Faster, Deterministic Sampling
- The DDIM Process: Non-Markovian Reverse Diffusion
- Deriving the DDIM Sampling Step
- Speedups: Fewer Steps Without Quality Loss
- Implementation and Comparison with DDPM
- When to Use DDIM vs DDPM
Chapter 16: EDM and Preconditioning
- The EDM Formulation: Preconditioning and Scaling
- Training Stability and Quality Gains
- The EDM Noise Schedule
- Implementing EDM in Practice
- EDM vs DDPM: A Detailed Comparison
Chapter 17: Latent Diffusion Models and Stable Diffusion
- Why Diffuse in Latent Space?
- The Variational Autoencoder Encoder-Decoder
- The Stable Diffusion Pipeline
- Reproducing Stable Diffusion Behavior
- Practical Considerations for Latent Diffusion
Chapter 18: Text-to-Image Generation and Conditioning
- Encoding Text: CLIP and T5
- Classifier Guidance: Training a Separate Classifier
- Classifier-Free Guidance: Unified Conditioning
- Cross-Attention Between Text and Image Features
- Building a Text-Conditional Diffusion Model
Chapter 19: ControlNet and Structured Control
- The Need for Fine-Grained Control
- ControlNet Architecture: The Frozen Pretrained Backbone
- Training ControlNet on Pose, Edge and Depth Conditions
- Implementing ControlNet
- Combining Multiple Controls
Chapter 20: Image Inpainting and Image-to-Image Translation
- Inpainting: Masking and Local Generation
- Implementation: Masked Diffusion
- Image-to-Image Translation with Diffusion
- Controlling Strength and Fidelity
- Practical Examples and Results
Chapter 21: Super-Resolution with Diffusion
- The Super-Resolution Problem Formulation
- Conditioning on Low-Resolution Inputs
- SR3 and Related Approaches
- Implementation Details
- Quality Metrics and Comparisons
Chapter 22: Video Diffusion and Temporal Generation
- Extending Diffusion to the Temporal Dimension
- 3D Convolutions and Temporal Attention
- Efficient Video Diffusion Architectures
- Training and Sampling Challenges
- Current State and Open Problems
Chapter 23: Fine-Tuning Diffusion Models
- When to Fine-Tune vs Train from Scratch
- Dataset Preparation and Curation
- Fine-Tuning the U-Net
- Avoiding Catastrophic Forgetting
- Domain-Specific Results and Evaluation
Chapter 24: Parameter-Efficient Fine-Tuning and LoRA
- The Problem: Too Many Parameters
- Low-Rank Adaptation (LoRA): Theory and Implementation
- DreamBooth and Personalization
- Textual Inversion and Token Learning
- Comparing PEFT Methods
Chapter 25: Deployment, Optimization and Production Engineering
- Model Quantization and Pruning
- TensorRT, ONNX and Runtime Optimization
- Serving Diffusion Models at Scale
- Latency, Throughput and Cost Trade-offs
- Monitoring and Reliability in Production
Chapter 26: The Current Landscape and What Comes Next
- Where Diffusion Models Stand Today
- Unifying Views: SDEs, Flow Matching and Score Models
- Open Research Problems
- Practical Advice for Researchers and Engineers
- A Vision for the Next Generation