How Diffusion Models Work: The Science Behind AI Image Generation
Diffusion models generate images by gradually denoising random noise into coherent structure. This is the technical explanation of how they actually work — the forward process, denoising, guidance, and training.
View all image ai depths →Depth ladder for this topic:
Diffusion models have produced some of the most striking capabilities in AI: photorealistic images from text, coherent video sequences, music generation, protein structure prediction. The underlying mechanism is conceptually unusual — these models learn by destroying data and then learning to reverse the destruction.
This article explains how diffusion models work technically: the mathematical framework, the key design choices, and why this approach outperformed the GANs that previously dominated image generation.
The core idea: learning to denoise
The training process for a diffusion model involves two phases:
The forward process (adding noise): Take a real image and gradually add Gaussian noise over T steps. At step 0, you have the clean image. At step T (typically 1000), you have pure random noise — indistinguishable from Gaussian noise, containing no discernible image structure.
The reverse process (learning to denoise): Train a neural network to reverse this process — given a noisy image at step t, predict what it looks like at step t-1 (slightly less noisy).
If the model learns to do this well, you can generate new images by: starting with pure random noise and applying the learned reverse process 1000 times, progressively resolving structure until you have a coherent image.
The mathematical framework that makes this work is rooted in thermodynamics (diffusion of particles) and variational inference, but the intuition is accessible: learn to denoise well, and you can generate by iterative denoising from noise.
The forward process in detail
The forward process is defined analytically (no learning required). Given an image x₀, the noisy image at step t is:
q(xₜ | x₀) = N(xₜ; √ᾱₜ x₀, (1-ᾱₜ)I)
Where:
- ᾱₜ is a cumulative noise schedule parameter that decreases from ~1 (at t=0, clean image) to ~0 (at t=T, pure noise)
- N denotes a Gaussian distribution
Crucially, you can sample xₜ directly from x₀ without going step by step: xₜ = √ᾱₜ x₀ + √(1-ᾱₜ) ε, where ε ~ N(0,I).
This means training is efficient: for any training image x₀, you can sample a random timestep t, add the appropriate amount of noise, and immediately create a training example (noisy image → target prediction).
The noise schedule
The noise schedule controls how quickly noise is added across the T steps. The original DDPM paper used a linear schedule. Stable Diffusion and most modern models use a cosine schedule, which adds noise more slowly early in the process (preserving more structure for longer) and gives better generation quality.
Getting the noise schedule right matters: too aggressive early in the schedule and the model loses structure before learning meaningful patterns; too conservative at the end and the model can’t fully convert noise to signal.
The denoising neural network
The model itself (ε_θ) is a neural network trained to predict the noise ε that was added to produce xₜ from x₀. The training objective is:
L = E[t, x₀, ε] [||ε - ε_θ(xₜ, t)||²]
In plain terms: for a randomly chosen timestep t, noisy image xₜ, and actual noise ε, minimize the L2 distance between the predicted noise and the actual noise.
Architecture: The standard architecture for image diffusion models is a U-Net — an encoder-decoder network with skip connections. The encoder progressively downsamples the noisy image to a compact latent representation; the decoder upsamples back to the original resolution. Skip connections (residual connections between encoder and decoder at each scale) allow the decoder to access fine-grained spatial information.
The current timestep t is embedded and injected into the network via cross-attention or feature modulation, allowing the model to adapt its denoising strategy based on how much noise is present.
For text-to-image models, the text condition is also injected via cross-attention — at each layer, the model attends over the text embedding alongside the image features.
Conditional generation: how text-to-image works
Generating an image conditioned on a text prompt requires connecting the visual and language domains.
Text encoding: The prompt is encoded by a pretrained text model (CLIP or T5). This produces a sequence of vectors, one per token, that captures the semantic content of the prompt.
Cross-attention in the denoiser: At each layer of the U-Net, the image features attend to the text embedding via cross-attention. This allows the model to selectively incorporate information from the prompt while denoising.
Classifier-free guidance (CFG): This is the key technique that makes text-to-image work well in practice. The denoising model is trained to handle two conditions: with text (“a photo of a cat”) and without text (unconditioned). At inference time, both predictions are computed and combined:
ε_guided = ε_uncond + w × (ε_text - ε_uncond)
The guidance scale w controls how strongly the output is pushed toward the text condition. Low w: more varied, less text-aligned. High w: strongly text-aligned but sometimes oversaturated and artifact-prone. Typical values: 7-12. CFG is why “guidance scale” is an exposed parameter in Stable Diffusion.
Latent diffusion: making it practical
Early diffusion models operated directly in pixel space — the U-Net worked on full-resolution images. This was computationally expensive and limited to low resolutions.
Latent diffusion models (LDM) — the architecture behind Stable Diffusion — work in a compressed latent space:
-
Encode: A pretrained autoencoder (specifically, a Variational Autoencoder) compresses the image to a lower-dimensional latent representation. A 512×512 image becomes a 64×64×4 latent.
-
Diffuse in latent space: The diffusion process runs entirely in the compressed latent space. The denoising U-Net operates on 64×64×4 tensors, not 512×512×3 images — roughly 4× fewer dimensions.
-
Decode: After the full denoising process, the VAE decoder expands the latent back to pixel space.
This delivers the same perceptual quality as pixel-space diffusion at a fraction of the compute cost, enabling training and inference at practical resolution and speed.
Sampling algorithms: from DDPM to faster methods
The original DDPM (Denoising Diffusion Probabilistic Models) required 1000 denoising steps at inference — slow for practical use. The research community developed faster alternatives:
DDIM (Denoising Diffusion Implicit Models): Achieves similar quality in 20-50 steps by using a deterministic (non-stochastic) reverse process. Same trained model; different sampling algorithm.
DPM-Solver / DPM-Solver++: Derived from treating diffusion as an ODE and applying high-order numerical solvers. 10-25 steps with good quality.
SDXL Turbo, Turbo Distillation: Train a student model to replicate the output of many-step inference in 1-4 steps. Significant quality-versus-speed tradeoff, but enables real-time generation.
The sampler and number of steps are key quality knobs in image generation — choosing the right combination for your application is part of the engineering of image AI systems.
Why diffusion outperformed GANs
Before diffusion models, Generative Adversarial Networks (GANs) were the dominant image generation approach. GANs use a generator (produces images) competing against a discriminator (distinguishes real from fake). Training is notoriously unstable — mode collapse, training oscillation, and sensitivity to hyperparameters are fundamental GAN challenges.
Diffusion models have several advantages:
- Training stability: The denoising objective is a simple MSE regression. It’s stable and scales well.
- Mode coverage: GANs often learn a subset of the true data distribution. Diffusion models cover the full distribution more faithfully, producing more diverse outputs.
- Latent space interpolation: Diffusion’s latent space (when projected through DDIM) supports meaningful interpolation and editing operations.
- Scalability: Diffusion training scales smoothly with data and compute — a core reason the frontier image models are consistently trained on larger and larger datasets.
Where the field is going
Video diffusion: Extend the architecture to temporal sequences. The core challenge is ensuring consistency across frames — current state-of-art handles this with 3D attention across time and space.
Flow matching: An alternative generalization of diffusion that defines the training objective as a flow between noise and data distributions. Faster convergence than DDPM and increasingly competitive generation quality. Flux and SD3 use flow matching.
Consistency models: Train the denoising model to always predict the clean image directly (not just one step ahead), enabling 1-step generation with full-quality results. Still active research.
Understanding diffusion models gives you a foundation for reading research papers, evaluating new image AI systems, and understanding why certain design choices (number of steps, guidance scale, sampler) affect outputs the way they do. The framework is powerful and increasingly general — most of the interesting generative AI outside language is built on it.
Simplify
← Image AI Evaluation Guide — How Teams Measure Quality Beyond “Looks Good”
Go deeper
Image AI: Understanding Vision Transformers (ViTs) →
Related reads
Stay ahead of the AI curve
Weekly insights on AI — explained at the level that's right for you. No hype, no jargon, just what matters.
No spam. Unsubscribe anytime. We respect your inbox.