Diffusion Models: How AI Image Generation Actually Works

Every striking AI-generated image you have seen — Midjourney portraits, DALL-E illustrations, Stable Diffusion wallpapers — was produced by the same family of models: diffusion models. They replaced GANs almost entirely within about two years. The core idea is strange at first glance: systematically destroy an image with noise, then train a neural network to reverse the destruction. This article walks through forward diffusion with real math, explains why the network predicts noise rather than pixels, shows where the “creativity” comes from, and explains the latent-space trick that made Stable Diffusion fast enough for a laptop.

The core idea: destroy, then learn to un-destroy

A diffusion model learns from a process with two directions. The forward process takes a real image and gradually adds Gaussian noise over hundreds of steps until nothing remains but static. This direction needs no learning — it is a fixed recipe. The reverse process is the learned part: a neural network that, given a noisy image and the current step number, predicts how to remove a little of the noise. Chain that denoising step hundreds of times starting from pure static, and an image emerges.

The intuition for why this works: removing a tiny amount of noise is an easy, well-defined regression task — far easier than “draw a cat” in one shot. The network never has to imagine a whole image at once. It only ever answers the local question: “this patch looks noisy in this way; what was probably underneath?” Iterated hundreds of times, those small, confident corrections accumulate into global structure. Training is just supervised learning on known noise, which is why diffusion training is famously stable compared to the adversarial tug-of-war of GANs.

Forward diffusion, concretely

The forward process is a Markov chain. At each step t, a little Gaussian noise is blended in:

q(x_t | x_{t-1}) = N(x_t; sqrt(1 − β_t) · x_{t-1}, β_t · I)

The noise schedule β1…βT controls how much noise enters at each step — typically ramping from ~10−4 to ~0.02 over T = 1,000 steps. A neat algebraic property means you never need to simulate the chain step by step during training. Defining αt = 1 − βt and ᾱt = α1·α2·…·αt, the noisy image at any step can be sampled directly from the clean image:

x_t = sqrt(ᾱ_t) · x_0 + sqrt(1 − ᾱ_t) · ε, where ε ~ N(0, I)

Read it as a crossfade: at t = 0, ᾱ0 = 1 and you see the clean image; as t grows, ᾱt decays toward 0 and the image crossfades into pure Gaussian noise. The schedule is tuned so that at t = T the signal is effectively gone. Every training example is then trivially manufactured: take a real image, pick a random t, sample fresh noise ε, blend. No labels, no adversarial discriminator, no mode collapse — just noise with a known answer.

Why the network predicts noise, not the image

Here is the part that confuses newcomers. Given the noisy x_t and the step t, the network ε_θ is trained to predict the noise ε that was added, not the clean image. The training objective (from Ho, Jain, and Abbeel's 2020 DDPM paper) is strikingly simple:

L = E[ || ε − ε_θ(x_t, t) ||² ]

Plain mean-squared error. Why noise? Because the noise is what was randomly sampled — it is the only unknown, and it has a clean, well-behaved distribution. Predicting the clean image directly would force the network to average over all plausible images consistent with the noise, producing blurry mush. Predicting the noise sidesteps that: the network learns the direction from the current noisy point back toward the data manifold.

There is a beautiful reinterpretation due to Song and Ermon (2019): the predicted noise is proportional to the score function — the gradient of the log-probability of the data. Each denoising step is then a small step uphill on the probability landscape, toward regions where real images live. “Removing noise” and “moving toward likely images” are the same operation. Sampling is just many small uphill steps starting from a random location.

Where the “creativity” comes from

A common misconception is that the model “remembers” training images and blends them. What actually happens at generation time: you sample pure Gaussian noise — random static, different every run — and denoise it. The randomness of that starting static is the seed of variation. Same prompt, different seed, different image. The model contributes learned structure; the noise contributes which instance of that structure you get.

The second ingredient is guidance: how strongly the text prompt steers the denoising. The standard technique is classifier-free guidance (Ho & Salimans, 2022). During training, the text conditioning is randomly dropped about 10% of the time, so the network learns both a conditional prediction (“noise, given this prompt”) and an unconditional one (“noise, for any image”). At sampling time, the two are extrapolated:

ε_guided = ε_uncond + w · (ε_cond − ε_uncond)

The guidance scale w (typically ~7 in Stable Diffusion) pushes each step further in the direction the prompt implies, past what the conditional model alone would do. Low w gives diverse, loosely-prompted images; high w gives literal, saturated, prompt-faithful ones — push it too far and artifacts appear. That single slider is responsible for much of the “prompt engineering” feel of these tools.

Latent diffusion: why Stable Diffusion runs on your laptop

The original DDPM diffused in pixel space: a 512×512 image is 786,432 values per step, times a thousand steps. Rombach and colleagues (2022) had the key efficiency insight: perceptual detail does not need the diffusion process. Compress the image first with a variational autoencoder (VAE) into a compact latent — 512×512×3 pixels become 64×64×4 latents, a 48× reduction — run the entire diffusion process in that small space, and decode back to pixels only at the end.

This is the “latent” in Latent Diffusion Models, the architecture behind Stable Diffusion. The VAE handles textures, edges, and fine detail; the diffusion model handles composition and semantics in a space 48 times smaller. Training gets cheaper, sampling gets faster, and quality barely moves. Nearly every open image model since has kept this design.

Text conditioning: cross-attention again

How does the prompt actually reach the denoiser? The text is encoded by a language model — CLIP's text encoder in Stable Diffusion — into a sequence of embeddings. Those embeddings enter the denoising U-Net through cross-attention: the image features supply the queries, the text embeddings supply the keys and values. It is the same query/key/value machinery as the transformer article's self-attention, repurposed so that each image region can ask, “which words describe what should be here?”

This is why prompt word order and emphasis matter: cross-attention weights decide how much each token influences each region. It is also why garbled text appears in generated images — the model attends to the concept of text via CLIP embeddings but never learned letterforms as precise geometry. (DALL-E 3 and later models improved this with better text encoders, but the mechanism is unchanged.)

Why diffusion beat GANs

For most of the 2010s, GANs ruled image generation. Diffusion won on three axes. First, training stability: the objective is plain regression on known noise — no generator/discriminator equilibrium to babysit. Second, mode coverage: GANs famously collapse to a few “safe” outputs; diffusion's likelihood-based training covers the full distribution, which is why one model can draw anything. Third, scalability: the simple objective keeps improving with more data and parameters.

The price is sampling speed: a GAN draws an image in one forward pass; DDPM needs hundreds of sequential denoising steps. DDIM (Song et al., 2020) reformulated sampling as a deterministic process needing far fewer steps — 50 instead of 1,000, with little quality loss — and every practical sampler since (DPM-Solver, LCM, and others) is a variation on spending fewer, smarter steps. Distillation methods now push all the way to one-step generation, closing the speed gap that was diffusion's main weakness.

Putting it together

The full pipeline, end to end: your prompt is encoded by CLIP into text embeddings; pure Gaussian noise is sampled in the VAE's latent space; the U-Net denoiser iterates — predict the noise, subtract a little, repeat — with cross-attention injecting the prompt and classifier-free guidance steering each step; after 20–50 steps the latent is clean; the VAE decoder expands it to pixels. Noise in, photograph out, with the learned probability landscape of “what images look like” doing all the creative work in between.

The deep idea compresses to one sentence: if you can learn to remove a little noise from anything, you can build everything from nothing.

Further reading

  • Ho, J., Jain, A. & Abbeel, P. (2020). Denoising Diffusion Probabilistic Models. arXiv:2006.11239
  • Song, J., Meng, C. & Ermon, S. (2020). Denoising Diffusion Implicit Models. arXiv:2010.02502
  • Rombach, R., et al. (2022). High-Resolution Image Synthesis with Latent Diffusion Models. arXiv:2112.10752
  • Ho, J. & Salimans, T. (2022). Classifier-Free Diffusion Guidance. arXiv:2207.12598

Leave a Comment