Generating imagesabout 11 minutes

Diffusion models power today's image generators (Stable Diffusion, DALL·E, Imagen, Midjourney) and increasingly video and audio. The idea, from Sohl-Dickstein et al. (2015) and made practical by Ho, Jain and Abbeel's DDPM (2020), is beautifully simple: noise is easy to make, so learn to remove it.

The forward process: adding noise

Take a training image and add a little Gaussian noise, then a little more, over steps (DDPM uses , with noise variances rising linearly from to ). By the end, is indistinguishable from pure noise. Conveniently, you can jump straight to any step in one shot:

The forward process on a digit. As t grows, ᾱ_t falls toward 0: the signal fades and the noise takes over.

The reverse process: learning to denoise

Train a network to look at a noisy image and the step number, and predict the noise that was added. The DDPM training loss is plain squared error:

  1. Pick a training image, a random step , and random noise .
  2. Make with the formula above.
  3. Train the network (typically a U-Net, a CNN with skip connections, or a transformer) to predict from and .

To generate, start from pure noise and repeatedly subtract a scaled version of the predicted noise (plus a little fresh noise), stepping down to 0. Each step is a small, learnable correction. Faster samplers (DDIM and others) cut the 1,000 steps to tens.

Text to image

  • Conditioning: the denoiser also receives an embedding of a text prompt (from a text encoder such as CLIP or T5), usually through cross-attention, so it learns to denoise toward images matching the text.
  • Classifier-free guidance (Ho & Salimans, 2022): run the denoiser with and without the prompt and push the prediction further in the direction the prompt suggests. Higher guidance gives images that follow the prompt more literally, with less variety.
  • Latent diffusion (Rombach et al., 2022, the basis of Stable Diffusion): run the whole process in the compact latent space of an autoencoder (lesson 1), then decode, making high-resolution generation affordable.
try itedit the code and run it, nothing can break
python sleeps until you press run

Diffusion models learn to predict the noise in a noised image. Generation starts from pure noise and removes it step by step, steered by a text embedding and guidance.

← Generative adversarial networks