πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Diffusion Models

🟒 Free Lesson

Advertisement

Diffusion Models

Diffusion models are the architecture powering modern image generators like Stable Diffusion, DALL-E, and Midjourney. They work by learning to reverse a gradual noising process - like learning to restore a corrupted photo step by step. Think of it as an art restorer who can take a damaged painting and meticulously remove each layer of damage until the original masterpiece is revealed.

Forward Diffusion Process

Forward Diffusion: Adding Noise Over T Stepst=0t=1t=T/3t=2T/3t=T-1t=Txβ‚€x₁CleanNoisyPure NoiseForward Process Formulaq(xβ‚œ | xβ‚œβ‚‹β‚) = N(xβ‚œ; √(1-Ξ²β‚œ) Β· xβ‚œβ‚‹β‚, Ξ²β‚œ Β· I)Each step adds Gaussian noise with variance Ξ²β‚œ, controlled by a noise schedule

Reverse Denoising Process

Reverse Process: Denoising Step by Stept=Tt=T-1t=2T/3t=T/3t=1t=0xβ‚€Neural Network (UNet) Predicts Noise Ρθ(xβ‚œ, t)Input: Noisy image xβ‚œ + timestep tOutput: Predicted noise Ξ΅Iterative Refinement Loopxβ‚œβ‚‹β‚ = (1/βˆšΞ±β‚œ)(xβ‚œ - Ξ²β‚œ/√(1-αΎ±β‚œ) Β· Ρθ(xβ‚œ,t)) + Οƒβ‚œz

UNet Architecture for Denoising

UNet Architecture for Diffusion ModelsEncoder (Downsampling)Conv 64256x256Conv 128128x128Conv 25664x64Conv 51232x32BottleneckConv 102416x16Timestep EmbDecoder (Upsampling)UpConv 51232x32UpConv 25664x64UpConv 128128x128UpConv 64256x256PredictedNoise Ξ΅Noisy Image xβ‚œ+ timestep tKey Featuresβ–  Encoder layersβ–  Decoder layersβ–  Skip connectionsβ–  Timestep embeddingβ–  Output (noise)

DDPM Sampling Loop

DDPM Sampling Algorithm1. Sample Noisexβ‚œ ~ N(0, I)Start from pure noise2. For t = T to 1:Loop through timestepsReverse the process3. Predict NoiseΡθ = UNet(xβ‚œ, t)Neural net estimates noise4. Remove Noisexβ‚œβ‚‹β‚ = denoise(xβ‚œ, Ρθ)Step toward clean imageReturnxβ‚€Repeat T times (typically T=1000)def ddpm_sample(model, T=1000): x = torch.randn(1, 3, 64, 64) # Start from noise for t in reversed(range(T)): eps = model(x, t) # Predict noise

How Diffusion Models Learn

Imagine you have a pristine photograph. First, you progressively corrupt it by adding static - a little at first, then more until it's pure noise (forward process). The neural network learns to reverse each corruption step. Given a slightly noisy image, it predicts what noise was added, then subtracts that prediction. By chaining thousands of these tiny corrections, pure noise transforms into a coherent image.

The genius is in the simplicity: instead of learning to generate images directly, the model learns the much simpler task of "what noise was added to this image?" This decomposition makes training stable and scalable.

Python Example: Simple Diffusion

Hands-on Project: Visualize Noise Schedules

Summary

Diffusion models achieve state-of-the-art image generation by learning to reverse a gradual noising process. The forward process corrupts data with Gaussian noise, while the reverse process uses a UNet neural network to predict and remove noise iteratively. Key innovations include learned noise schedules, efficient sampling via DDIM, and conditioning mechanisms for text-guided generation.

Next: We explore GANs, the adversarial framework that revolutionized generative modeling.

Need Expert Generative AI Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement