AI & machine learning / Diffusion models

Diffusion models

How image generators turn random static into a picture: bury images in noise, train a network to predict that noise, then run the process backwards, steered by a text prompt.

Intermediate6 steps~7 min
Image — a grid of pixels, −1 (paper) to +1 (ink)
Noise — random static, a number per pixel
Network — predicts the noise in a noisy image
Prompt — text that steers which image appears
// THE SHORT VERSION

In 30 seconds

A diffusion model learns to make images by learning to remove noise. During training, real images are mixed with random static, and a neural network is asked to work out exactly which static was added.

To generate, the model starts from pure static and repeatedly removes a little of the noise the network predicts. After many small steps a clean, new image is left.

A text prompt, turned into numbers and fed to the network at every step, steers which image emerges.

Diffusion models, step by step

STEPS01 / 06

The 8 × 8 pictures and the frames are illustrations. Many image generators run these steps on a compressed version of the image and decode it at the end, and strengthen the prompt’s pull with a guidance setting.

STEP 01 / 06 Drown an image in noise
THE MATH
x_t = a·x0 + b·noise a shrinks, b grows a² + b² = 1
  1. Step 1: Drown an image in noise. Take a training image and mix in random noise, a little more at each step: the image is scaled down by a and random static is added with weight b. After enough steps nothing of the picture is left. This forward direction needs no learning. It is plain arithmetic, applied to every image in the training set.
  2. Step 2: One pixel, worked through. Follow a single pixel at the middle noise level. Its clean value is 0.8 on a scale from −1 (paper) to +1 (ink), and the random noise drawn for it is −0.5. Keeping 0.8 of the pixel and adding 0.6 of the noise gives 0.34. The whole image is this sum done for every pixel, each with its own random number.
  3. Step 3: Learn to predict the noise. Now the learning. A neural network is shown the noisy image and the noise level, and must output the noise that was added. We added that noise ourselves, so the right answer is known and the guess can be marked: for our pixel the truth is −0.5, the guess −0.4, the squared error 0.01. Training shrinks that error over many images.
  4. Step 4: Run it backwards. A noise guess can be turned around. Subtract the guessed noise from the noisy pixel, rescale, and you get an estimate of the clean pixel: 0.725, close to the true 0.8. At high noise such estimates are rough, so the sampler does not jump there. It moves part of the way, to a slightly less noisy image, and asks the network again.
  5. Step 5: Generate from pure noise. To make a new image, start from pure random noise and repeat the small step: predict the noise, remove a little. Coarse shapes emerge first, then detail. The result is normally not a copy of a training picture but a fresh sample that resembles them. The original recipe used 1,000 steps; later samplers need a few dozen.
  6. Step 6: Steer it with text. A text encoder turns a caption into numbers that are fed to the network at every step. In training each image came with its caption, so the network learned to predict noise given the words. When generating, the prompt pulls each step towards a matching picture: the same starting noise can become a house or a tree.
VALUES SO FAR
forwardimage → noiserule
x0····pixel
noise····random
x_t····mixed
guess····network
loss····error
x0 est.····estimate
reverse····sampling
prompt····steering
// AFTER

Learn next

Nothing builds on this one yet. See every ai & machine learning concept.

Spotted a mistake on this page?