0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Watch a mug dissolve into static
Watch a photo of a mug slowly dissolve into static, a little more noise at every step, until nothing of the mug remains. Destroying an image like this is easy. Now watch the film run backwards. From pure static, shapes slowly emerge: a blur, a silhouette, then a sharp, detailed mug. But this mug was never photographed. It's brand new. That's the idea behind diffusion models, the method behind many of today's leading image generators. Learn to undo noise, one small step at a time, and you can start from random static and create something that never existed.
Today we'll build the mathematics, which turns out to be surprisingly simple.
0:57Where we are: diffusion models
Season Three, Module Twelve, stop three: diffusion models.
1:03The forward process: adding noise
Let's start with the easy direction: destroying an image. At each step, we shrink the image slightly and add a tiny amount of random Gaussian noise, the bell-curve noise from episode four. The amount is set by a schedule: very little at first, a fraction of a percent, rising gradually over a thousand steps. Watch any single pixel. Its value drifts randomly, and gradually loses any connection to the original image. After a thousand steps, every pixel is just random noise from a standard normal distribution. The photo is gone.
This idea was first proposed in two thousand fifteen, and in two thousand twenty a paper on denoising diffusion probabilistic models showed it could generate high-quality images.
1:54The shortcut: jump to any step
We don't actually need to add noise step by step. There's a handy shortcut: at any step t, the noisy image is just a blend of the original image and fresh noise. Multiply the original by one coefficient, the noise by another, and add. Look at how the blend changes. At step fifty, the signal coefficient is zero point nine eight five, and the noise coefficient only zero point one seven. Around step two hundred and fifty, they're roughly equal: zero point seven two and zero point six nine. By step five hundred, the noise dominates.
By step seven hundred and fifty, barely any signal remains. And at step one thousand, it's essentially pure noise. Let's do one pixel by hand. A pixel of value zero point eight, at step two hundred and fifty, with a noise sample of minus zero point five: zero point seven two four times zero point eight, plus zero point six nine times minus zero point five. The noisy value is zero point two three four.
3:02Learning to denoise
Now the clever part. We train a neural network to look at a noisy image, along with the step number, and predict the noise that was added. Training is beautifully simple. Take a real image. Pick a random step. Generate some noise, and use the shortcut to make the noisy version. Ask the network to predict the noise, and measure its error with ordinary mean squared error. Repeat millions of times, with images and steps chosen at random. Why predict the noise? Because if you know the noise, you know the image. Rearrange the shortcut, and the original is the noisy value minus the noise part, divided by the signal coefficient.
For our pixel, a perfect noise prediction of minus zero point five recovers exactly zero point eight. An imperfect guess of minus zero point four gives zero point seven zero five, close but not exact. Notice the loss: just a mean squared error, the very first cost function we ever studied. No adversarial contest, no fragile balance. That's a big reason diffusion models are so stable to train.
4:14The U-Net that knows the time
What network does the denoising? Traditionally, the U-Net from episode thirty-one: an encoder–decoder with skip connections, whose output is the same size as its input, perfect for predicting a noise value for every pixel. It also needs to know how noisy its input is, since removing a little grain at step fifty is very different from finding structure in near-static at step nine hundred. So the step number is turned into a vector, using the same kind of sine and cosine waves as the transformer's positional encodings, and fed into every block.
Many models also include attention layers, which will matter next episode when we steer generation with text. And some newer models replace the U-Net with a transformer. One caveat: this corner of AI moves quickly. These examples were current when we made this video, and they may look different on the day you watch it.
5:18Generating: from static to an image
Now we can create. Start with pure random noise. Ask the network to predict the noise in it, and take a small step towards a cleaner image, adding back a little fresh randomness so we don't commit too early. Then repeat, with the next step number, hundreds of times. Watch what happens. In the first steps, a vague blob of colour appears: the overall layout is decided early. Then a silhouette, then edges, and in the final steps, fine texture and reflections. Coarse to fine. Start from a different patch of random noise, and you get a different mug.
And because diffusion is trained to cover the whole range of training images, rather than to fool a judge, it doesn't collapse onto a few favourite images the way GANs can.
6:08Why does it work?
Why does repeatedly removing predicted noise produce realistic images? Here's the intuition. Optional deep dive. Picture the space of all images as a landscape, where realistic images form bright ridges and peaks of high probability. A noisy image sits somewhere off the ridges. The noise the network predicts, when reversed, points back towards nearby realistic images. In fact, researchers showed that predicting the noise is closely related to estimating the direction in which the probability of the data increases, called the score.
So generation is like a ball climbing towards the bright ridges, with a little random jitter, until it settles on a realistic image. Different starting points, different ridges, different images.
7:00Speed, and diffusion vs GANs and VAEs
Diffusion's main cost is speed. The original method needed a thousand passes through the network for every single image, where a GAN needs just one. Researchers have since developed much faster samplers that take bigger, smarter steps, often needing just twenty to fifty, and further tricks can reduce that even more. Here's how the three approaches compare. VAEs are stable and fast, but often blurry. GANs are sharp and fast, but unstable and prone to mode collapse. Diffusion models are sharp, varied and stable to train, with slower sampling as the trade-off.
That combination is why diffusion and its close relatives became the foundation of many leading image generators.
7:50Try it yourself: a jump to step 500
Let's jump straight to step five hundred. Here, the signal coefficient is about zero point two eight, and the noise coefficient about zero point nine six. Our pixel is zero point eight, and the noise is plus a half. What's the noisy value, and how much of it comes from the original pixel? Pause.
Zero point two eight times zero point eight is zero point two two four. Zero point nine six times a half is zero point four eight. Together: zero point seven zero four. Less than a third of it is the original pixel; the rest is noise. Halfway through the schedule, the image is mostly static already.
8:38Why many small steps?
Why not jump from noise to a clean image in one go? Suppose the network's noise prediction is off by just zero point one. At step two hundred and fifty, the recovered pixel is off by about zero point zero nine five. At step five hundred, the same small mistake becomes an error of about zero point three four, more than three times worse, because there's so little signal left to work with. So diffusion takes many small steps, refining its estimate as the picture slowly emerges.
9:12Pause & Predict: what's decided first?
When a diffusion model generates a mug, what appears first: the fine pattern on the handle, or the overall layout? Pause. The layout. The early, high-noise steps decide the big picture: where the mug sits and its rough colours. The middle steps shape its form. The final, low-noise steps add texture and crisp edges. This gives us a handy editing trick: add just a little noise to an existing photo and denoise it, and you keep the layout while changing the details.
9:52Making diffusion fast
Speed matters. If one network pass takes fifty milliseconds, a thousand steps take fifty seconds per image. Better samplers can produce good images in around fifty steps, two and a half seconds. And distillation, training a student model to reproduce many of the teacher's steps in just a few, can bring it down to a handful of steps, a fifth of a second. Quality usually drops a little as steps fall, so the right number depends on whether you need speed or perfection.
10:26A gentler noise schedule
The noise schedule turns out to matter. With the original linear schedule, the image is mostly gone by the halfway point, so many steps are spent on what's almost pure static. Later research proposed schedules that destroy information more evenly, such as a cosine-shaped schedule, so the middle steps still contain useful signal. Same idea, better use of every step.
10:53Editing with diffusion
Diffusion is also a powerful editor. To clean up a product photo, Dev masks the cluttered background and lets the model regenerate only that region, while the mug itself stays exactly as photographed. Or start from a rough sketch, add some noise, and denoise it into a realistic image that follows the sketch's layout. For a shop, the first trick matters most: keep the real product real, and only generate what's around it.
11:25Beyond pictures: flow matching
The diffusion idea travels well. It generates audio and video, as we'll see in two episodes' time, and scientists use it to design protein and molecule structures, and even to plan robot movements. A close cousin called flow matching learns to carry noise towards data along nearly straight paths, which can make generation faster, and some recent image generators use it. The core idea stays the same: start from noise, and learn the way back.
11:56Pause & Predict: why a random step each time?
Each training example uses a randomly chosen step, anywhere from one to a thousand. Why not train on step one thousand first, then nine hundred and ninety-nine, and so on? Pause. Because a single network must handle every noise level, from light grain to pure static, and if it practised them in order, it would forget the early levels while learning the late ones. Mixing them in every batch keeps it good at all of them, and the time embedding tells it which level it's facing. Thanks to the shortcut formula, we can jump straight to any step, so training never has to add noise a thousand times over.
12:40Myth vs Fact
Myth: image generators work by cutting and pasting pieces of their training images. Fact: a diffusion model starts from random noise and refines it, step by step, using patterns it learned. That said, researchers have shown that models can occasionally reproduce images that appeared many times in their training data, so memorisation is a real risk to be aware of, just like with our tiny GPT.
13:07Recap
Let's recap. The forward process gradually adds Gaussian noise until an image becomes pure static, and a shortcut jumps straight to any step as a blend of signal and noise. A U-Net, told the step number, learns to predict the added noise, trained with plain mean squared error. To generate, start from random noise and remove predicted noise step by step, from layout to fine detail. The result is sharp, varied images from a stable training process, at the cost of many steps, which faster samplers reduce. One sentence to remember: learn to remove a little noise, and you can sculpt an image out of static.
13:52Next: text-to-image
Mission complete. Brightcart can now generate sharp, varied product images by sculpting them out of noise, with training that doesn't fall apart. But Meera wants control. Not just "a mug", but "a blue ceramic mug on a wooden table, in morning light". How do we steer diffusion with words? Next episode: CLIP, latent diffusion and guidance, the three ideas behind text-to-image. See you on the map.