Lesson 09 of 10 /Builds on 2

How does an AI image generator turn noise into a picture? Diffusion, step by step

Most AI images start life as random static. A model wipes a little of it away, again and again, and your prompt nudges every wipe. In this lesson you drag a picture out of the noise yourself and turn the guidance dial until it breaks.

By the end you can
  • Say in one sentence how a diffusion model makes a picture
  • Explain what the model learns in training: to guess the noise in a noisy picture
  • Say what the steps setting and the guidance scale change, and pick sensible values
  • Explain why hands and written words went wrong for years, and what made them much better
In this lesson
  1. 1What happens when you type a prompt into an image generator?
  2. LabHow do you train a model to remove noise?
  3. 3Why does the picture appear so late?
  4. 4How does the prompt steer each step?
  5. 5Steps and guidance scale: what should you set them to?
  6. QuizWhy were hands and words so hard for image AI?
Part 1

What happens when you type a prompt into an image generator?

The short answer: the tool starts with a square of random static, and a model cleans it up a little at a time, usually 20 to 50 times. Each time, it looks at the static and your prompt and guesses which part is noise. It takes some of that noise away. After the last step, what is left is your picture.

That is called diffusion, and it is how Stable Diffusion and many of the well-known image tools of the last few years work inside. The model does not search the web for a photo. It does not cut and paste bits of pictures it has seen, either. It makes new pixels by removing noise, guided by what it learned from millions of pictures with captions.

To see why that works, we start with how the model is trained.

A grid of tiny pictures. Each row starts as grey static on the left and turns into a clear small image on the right: a plane, a frog, a deer, a ship, a car, a horse.
Real diffusion at work: each row is one picture being made, from pure noise on the left to the finished image on the right. Notice the rough shapes show up well before the details. /Ho, Jain and Abbeel, Denoising Diffusion Probabilistic Models (2020), figure 14. Screenshot from arXiv
Lab

How do you train a model to remove noise?

Take a real photo. Add a little random noise. Now add a bit more, and more, a thousand times, until nothing of the photo is left. The 2020 paper that made diffusion work well, by Jonathan Ho and two colleagues at Berkeley, used exactly 1,000 of these noise steps, with the amount added each time growing slowly from 0.0001 to 0.02.

Training runs the other way. We show the model a photo at some random noise level and ask one question: which part of this is the noise? It makes a guess, the training code tells it how wrong it was, and it adjusts. Repeat that across millions of pictures and every noise level, and you get a model that is good at one small job, which is cleaning up a noisy picture by a little bit.

Once the model can clean up any noise level, you can hand it pure static and ask it to clean that up too. It guesses what picture could be under there, removes a little noise, and does it again on the result. After enough rounds you have a picture that was never in the training data.

The lab lets you step through that process. It is a simulation, and it says so on the lab. The amount of noise at each step follows the real 2020 schedule. The pictures are not made by a model: they are three small scenes we drew ourselves, so the "guess" panel shows our drawing, blurred the way a real model's early guesses are blurry.

Lab 01Watch a picture come out of noiseReady

A simulation: the noise schedule is the real one from the 2020 paper, the pictures are ours. Drag the Step slider from 0 to the end. The left square is what the image looks like at that step; the right square is the model's guess of the finished picture. Then try a guidance of 0, 1 and 20, and try 4 total steps.

Part 3

Why does the picture appear so late?

Drag the slider slowly and you will see that halfway through, the left square is still mostly static. The picture only shows up near the end. That is the real noise schedule at work: with the 2020 settings, only about 8% of the picture is there at step 500 of 1,000, and half of it arrives only by step 741. Going forward, each step adds a bit more noise than the last, and by the halfway point about 92% of the picture is buried. Run backwards, the first half of the steps barely changes what you see.

Now look at the right square again. Even at the early steps, when the left side is nearly pure static, the guess already has the big shapes: a dark sky, a bright tower, a light patch where the moon will be. The 2020 paper saw the same thing in real models, and showed it with rows of tiny pictures, each row one image being made from left to right. Its authors wrote that "large scale image features appear first and details appear last." The model settles the layout early, when the picture is still noise, and fills in edges and small parts at the end.

This is why the first few steps matter so much. Once the layout is set, later steps mostly add detail. And it is why a picture made with too few steps looks soft and smudgy: with only 4 steps, each one has to remove a big chunk of noise at once, and fine detail is what gets lost. Try 4 total steps in the lab and look at the result.

A line chart. The share of the picture that is visible stays near zero for the first half of the 1,000 steps, reaches 10% at step 525 and 50% at step 741, then climbs to 100% at the end.
How much of the picture is there at each step, worked out from the real 2020 noise schedule. Half the steps pass before even 10% of it shows. /Our chart, from the schedule in Ho et al. (2020)
Part 4

How does the prompt steer each step?

First, a text model turns your prompt into numbers. In the first Stable Diffusion that text model is CLIP. It always turns your prompt into 77 tokens, and each token is a list of 768 numbers. Inside the image model, a step called cross-attention lets each part of the picture look at those words and pick the ones that matter to it. It is the same attention idea as in our transformers lesson, but here the picture is looking at the text.

Stable Diffusion adds one more trick to save time. It does not clean up a full 512 by 512 picture. It works on a small, packed version of it, a grid of 64 by 64, and a separate decoder turns that grid into full-size pixels at the end. The 2021 paper behind it called this latent diffusion. It is a big reason the model can run on a home graphics card.

The setting you can control here is guidance. At every step the model makes two guesses. One uses your prompt, one uses an empty prompt. The tool then pushes the result away from the empty-prompt guess and toward the prompt guess. The guidance scale is how hard it pushes. This method, called classifier-free guidance, came from a 2022 paper by Ho and Tim Salimans, and the latent diffusion paper says it "greatly boosts sample quality".

Fourteen small made-up pictures in two rows, two for each of seven prompts. They include a street sign that says Latent Diffusion, a squirrel eating a burger, a chair shaped like an octopus and a T-shirt with a heart.
Seven prompts, two tries each, from the 2021 latent diffusion paper, the research Stable Diffusion is built on. Look at the street sign: one try spells it right, the other writes LATETEN. /Rombach et al., High-Resolution Image Synthesis with Latent Diffusion Models (2021), figure 5. Screenshot from arXiv
Part 5

Steps and guidance scale: what should you set them to?

Most image tools show these two settings somewhere, often under "advanced". Here is what they do and the values we use.

Steps is how many times the model removes noise. The popular diffusers library sets 50 as the default, and its docs say more steps usually give a better picture but take longer. In our use the gain past about 30 is small, so we start at 25 to 30 and only go higher for fine detail.

Guidance scale is how hard the prompt pulls. The diffusers default is 7.5, and its docs warn that higher values follow the prompt more closely "at the expense of lower image quality". At 1 the model adds no extra push, and low values let the picture drift away from what you asked for. The Hugging Face guide calls values around 7 to 8.5 usually good. High values make colours oversaturated, as the 2022 guidance paper showed and our lab imitates at 15 or 20.

Seed is the number that picks the starting static. The same prompt, seed and settings give the same picture again. A new seed gives a new picture from the same prompt, which is what "New noise" did in the lab. A negative prompt takes the place of the empty prompt in the guidance step, so the model pushes away from it. That is why "blurry, extra fingers" in a negative prompt can help.

Two groups of six generated volcano pictures. The left group is murky and mixed; the right group shows clear, bright, classic volcano shapes.
Volcanoes made with no guidance (left) and with classifier-free guidance at a weight of 3 (right). In the paper's units that is 4 on the guidance scale most tools show. Guidance makes them clearer and more alike, with stronger colours. /Ho and Salimans, Classifier-Free Diffusion Guidance (2022), part of figure 3. Screenshot from arXiv
Check yourself

Why were hands and words so hard for image AI?

For years the easiest way to spot an AI picture was to count the fingers or read the sign in the background. The two failures have different causes.

Hands are small. Say a hand is about 60 pixels wide in a 512 pixel image. In Stable Diffusion's packed 64 by 64 grid, that is about 7 or 8 cells for a whole hand, so a finger is barely one cell. That is our own arithmetic, but it shows how little room there is. A hand can also bend into a huge number of shapes, and in everyday photos it is often half hidden, in a pocket, behind a cup or curled into a fist. Our reading is that the model learned what fingers look like side by side, and nothing in training made it check how many there are. One 2024 paper on fixing hands, HanDiffuser, lists the faults: odd poses, odd shapes and the wrong number of fingers.

Text failed for a related reason. The model had to spell words it only knew as meanings. You saw it in the street sign shown above, from the 2021 latent diffusion paper. The model was asked twice for a sign reading "Latent Diffusion". It got it right once and wrote "LATETEN DIFFUSION" the other time. That model used its own small text model. The first Stable Diffusion used CLIP, which was trained to match pictures with their captions, so its numbers carry much more about a word's meaning than about its letters. Google's 2022 Imagen paper found that making the text model bigger helped overall quality and prompt matching more than making the image model bigger. Stable Diffusion 3 in 2024 used three text models, one of them a large language model called T5. Its paper tested the model with T5 taken out. People liked the overall look as much, a 50% win rate against the full model, but for writing in the picture the stripped model won only 38% of the time.

In our use, newer and much bigger models, with stronger text models built in, get hands and short words right far more often. We still check long passages of text and hands that hold things before we use a picture.

A bar chart. Stable Diffusion 3 without its T5 text model wins 50% for overall look, 46% for following the prompt and 38% for writing in the picture, against the full model.
Take the big T5 text model out of Stable Diffusion 3 and the look holds up. Writing in the picture suffers most. /Our chart, from win rates in Esser et al. (2024)

Q1What does a diffusion model learn to do in training?

Q2Halfway through the steps, the image is still mostly static. Why?

Q3You set the guidance scale to 20. What happens?

Q4Why did early image models get fingers wrong so often?

Remember

What to take with you

  1. An image generator starts from random static. It removes a little noise at a time, usually 20 to 50 times.
  2. In training the model only learns to guess the noise in a noisy picture. Running that skill from pure static is what makes a new picture.
  3. The big shapes are decided early and the details come last, which is why too few steps look smudgy.
  4. The prompt steers every step. Guidance scale sets how hard; around 7.5 is normal and values like 20 oversaturate the picture.
  5. Hands failed because they are small and bend every way. Text failed because the text model knew meanings, not spelling. Bigger text models made spelling much better.
Questions

Questions people are asking

/01How do AI image generators work?

Many of the best-known ones use diffusion. The tool starts with random noise, and a trained model removes a little of it at a time, usually 20 to 50 times. At every step the text prompt steers which way the clean-up goes. What is left at the end is a new picture.

/02What is a diffusion model?

A model trained to guess the noise in a noisy picture. In training, it sees real pictures with different amounts of noise added. To make an image, it starts from pure noise and removes noise step by step until a picture is left.

/03What does the guidance scale do?

It sets how strongly the picture follows the prompt. At each step the model makes one guess with the prompt and one without, and the guidance scale is how hard it pushes toward the prompt guess. The common default is 7.5. High values like 15 or 20 give oversaturated colours.

/04How many steps should I use in Stable Diffusion?

The diffusers library defaults to 50. We start at 25 to 30 for most pictures, because the gain past that is small, and go higher only for fine detail. Too few steps give a soft, smudgy picture.

/05Why is AI bad at drawing hands?

Hands take up few pixels in most images, bend into many shapes and are often half hidden in photos. In our reading the model learned how fingers look next to each other but never had to count them. In our use, newer and larger models get hands right much more often.

/06Does an AI image generator copy existing pictures?

It does not paste parts of saved images. It makes new pixels by removing noise, using patterns it learned from millions of captioned pictures in training. Those patterns come from real images, which is why style and copyright questions around training data are still argued.

Sources 8

  1. Ho, Jain and Abbeel, Denoising Diffusion Probabilistic Models, Jun 2020 arxiv.org/abs/2006.11239
  2. Rombach et al., High-Resolution Image Synthesis with Latent Diffusion Models, Dec 2021 arxiv.org/abs/2112.10752
  3. Ho and Salimans, Classifier-Free Diffusion Guidance, Jul 2022 arxiv.org/abs/2207.12598
  4. Hugging Face, Stable Diffusion text-to-image pipeline docs (steps and guidance defaults) huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/text2img
  5. Hugging Face, Stable Diffusion with Diffusers (latents, CLIP, guidance) huggingface.co/blog/stable_diffusion
  6. Saharia et al., Imagen: Photorealistic Text-to-Image Diffusion Models with Deep Language Understanding, May 2022 arxiv.org/abs/2205.11487
  7. Esser et al., Scaling Rectified Flow Transformers for High-Resolution Image Synthesis (Stable Diffusion 3), Mar 2024 arxiv.org/abs/2403.03206
  8. Narasimhaswamy et al., HanDiffuser: Text-to-Image Generation With Realistic Hand Appearances, CVPR 2024 arxiv.org/abs/2403.01693
sansser weekly

Get the next lesson in your inbox.

New lessons and how-tos, the tools we tested and what we think, in one short email.

Free, every Friday. Unsubscribe in one click.