Diffusion Models
Learn to reverse a gradual noising process; the technique behind Stable Diffusion and most state-of-the-art image, audio and video generators.
Advanced · about 12 hours · difficulty 5/5
Learn first: Autoencoders and Variational Autoencoders, Attention and the Transformer Architecture
Diffusion models generate data by learning to remove noise. During training you take a real image, add a random amount of Gaussian noise, and teach a network to predict that noise. At generation time you start from pure noise and denoise step by step until an image appears.
Core pieces
- Forward process: a fixed schedule that gradually adds noise over many timesteps.
- Denoising network: usually a U-Net (or a diffusion transformer) that takes the noisy image and the timestep and predicts the noise.
- Loss: simple mean squared error between predicted and actual noise, which makes training far more stable than GANs.
- Samplers and schedulers (DDPM, DDIM, DPM-Solver and others) trade the number of steps against quality.
Controlling what you get
- Conditioning on text via embeddings from a text encoder injected through cross-attention.
- Classifier-free guidance strengthens the effect of the prompt; the guidance scale is a key knob.
- Latent diffusion runs the whole process in a compressed VAE latent space, making high-resolution generation affordable.
- ControlNet, LoRA and DreamBooth add spatial control or teach new concepts with a few images.
Using them in practice
The Hugging Face diffusers library gives you pipelines, schedulers and training scripts. Start by running a pretrained pipeline, then train a tiny unconditional model on small images so you understand the loop, then try a LoRA fine-tune.
Be thoughtful about licensing of models and training images, consent for likeness, and labelling generated content.
Good enough to move on when you can explain the forward and reverse processes, train a small diffusion model from scratch, and tune guidance and steps in a pretrained pipeline.
Key concepts
- Forward noising process
- Noise-prediction training objective
- U-Net denoiser and timesteps
- Samplers and schedulers
- Classifier-free guidance
- Latent diffusion
- diffusers library
- LoRA, DreamBooth and ControlNet
Mini project
Train a small unconditional diffusion model on 32x32 images with diffusers, then use a pretrained text-to-image pipeline to compare 10, 25 and 50 sampling steps and three guidance scales on the same prompts.
Check yourself
What exactly is the network trained to predict in a DDPM, and what does raising the classifier-free guidance scale trade off?
Free resources
- Diffusers documentation (Hugging Face)
- Hugging Face Learn (Hugging Face)
- Practical Deep Learning for Coders (fast.ai)