liveBook · Manning

Diffusion model

As explained in Build a Text-to-Image Generator (from Scratch)

A denoising diffusion probabilistic model is a diffusion generative model that samples through a probabilistic sequence of noise-removal steps.

A diffusion model generates an image by starting with random noise and repeatedly removing some of it. Training supplies the reverse task: take clean images, add noise at known time steps, and teach a denoising network to predict that noise. Unlike image generation that predicts patches in sequence, diffusion refines a noisy image over many steps. section 1.3 section 1.7

Build a denoising diffusion probabilistic model (DDPM) by connecting four parts:

  • Set a noise schedule. At each forward time step, combine an image with normally distributed noise; after enough steps, the image approaches random noise. A schedule controls how quickly this happens. section 5.1.1 section 5.4.1
  • Train a U-Net on noisy images and their time steps. Sample a time step for each training image, inject known noise, and minimize mean squared error between that noise and the U-Net’s prediction. For conditional generation, also supply a class label. section 5.2 section 6.3.2
  • Sample in reverse. Begin with a random-noise image, use the network’s prediction to take a denoising step, and repeat toward time step zero. Classifier-free guidance combines conditional and unconditional predictions to control label influence. section 1.3.1 section 6.3.2

Generate clothing items

Train on normalized Fashion MNIST clothing images and their ten class labels. A DDPM can sample different clothing categories from noise; changing guidance strength changes how strongly the chosen label steers generation. A basic unconditional version can instead generate clothing-like images without selecting a category. section 5.0 section 6.3.2 section 6.3.3

Diffusion can also run in a compact latent space rather than directly on full-resolution pixels. A text-guided model denoises latents using a prompt, then a variational autoencoder decodes the result into an image. This reduces the work of diffusion, but training a latent diffusion model remains computationally expensive; a pretrained model is a practical option. Text-to-image outputs also face geometric and copyright-related concerns. section 1.7 section 9.1.2 section 1.5

Figure 9.3 How a trained LDM generates an image based on a text prompt. The text prompt shown at the top-left corner is first encoded into a text embedding. To generate an image in the lower-dimensional space (latent space), we start with an image of pure noise (bottom left). We use the trained U-Net to iteratively denoise the image, conditional on the text embedding so the generated image matches the text prompt, with the guidance of a trained CLIP model. The generated latent image (bottom right) is presented to a trained VAE to convert it to a high-resolution image, which is the final output (top right).

Trace the text embedding and noisy latent through iterative U-Net denoising, then follow the finished latent through the VAE to the final image. section 9.1.2

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Works through noise scheduling, U-Net noise prediction, training loss, and sampling; uses a pretrained model for computationally costly latent diffusion.
Examples
Uses Fashion MNIST clothing generation, staged image-noising visualizations, and text-guided generation in latent space.
What it assumes
Introduces diffusion from clean images and random noise while using Python and PyTorch tensors for the implementation.
What you can do after
Build it from scratch as a class-conditional Fashion MNIST DDPM, then apply a pretrained latent model to text-to-image generation.
How it connects
Connects forward diffusion and reverse diffusion to noise schedulers, U-Nets, classifier-free guidance, latent diffusion, CLIP, and variational autoencoders.

Also explained in

Compare how 10 books explain diffusion model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.