71 min total

Generative AI

Diffusion models

Diffusion models

Background

Foundational paper introduced in 2015 by Jascha (UCB, Anthropic) with 3 more co-authors including an Indian. Work was presented in ICML and inspiration was drawn from non-equilibrium thermodynamics and stochastic processes. 2020 Jonathan Ho's (UCB) work refined the original model by introducing a simplified training objective leading to SOTA results and superiority to GANs.

Denoising Diffusion Probabilistic Models (DDPM)

is trained to denoise an image over a series

of very small steps, noise is removed gradually to

GenAI / Final — Diffusion models

reconstruct the original data

Forward Process

We start with real image, we add gaussian noise at each time step, after T steps, image turns into pure noise.

During training, we compare the model’s predicted denoised image at a given timestep with the corresponding clean image from the forward process to compute the loss. Forward process sole purpose is to generate training data pairs for learning.

In forward process at each timestep should have zero mean and unit variance ( = 1) to make training and inference more stable.

Update process equation:

GenAI / Final — Diffusion models

Bbut whats Variance?

How much a set of values are spread out around the mean

e.g.

Data points: [2, 4, 6, 8, 10]

Mean ~ 6

Variance ~ 8

Covariance matrix: .I describes the noise distribution at each step.

8 variance means on average squared distance from each value to the mean (6) is 8

Noise has equal variance on all dimensions, no correlations

How to make it more interpretable?

Standard deviation = = 2.83

On average, each data point is about 2.83 units away from the mean

Reparameterization technique

What the hell is covariance?

Image a Large dataset with millions of images and a large number of diffusion steps, to compare timestep t image at and approaches:-

Variance is a special case where we look at relationship of variable with itself, variance measures spread of one variable against itself

  1. Cache all images during forward process then run backward process (consumes a hell lot of VRAM)

Covariance measures how two different variables vary together. If one variable increases, does the other also increases/decreases/stays the same?

  1. Do a forward process until 0 - for the image for comparision with backward process (a ton of compute required O()
CovarianceWhat it means
> 0Both variables are directly proportional
< 0Both variables are indirectly proportional
= 0Both variables are independent

Introducing reparameterization technique: you can generate directly from and noise e on the fly for any timestep t, without storing any intermediate images (DDPM 2020 Jonathan Ho)

GenAI / Final — Diffusion models

Oi what is Covariance Matrix?

Is a square matrix (2D) contains variance at diagonals and covariances at non-diagonal elements.

GenAI / Final — Diffusion models
GenAI / Final — Diffusion models

Now can just compute and directly sample from without running the whole markov process

represents how much signal remains after t timesteps

This multiplication of may seem trivial xD but paper mein proving that this method is valid through mathematics proofs was the real deal

Schedule in diffusion process

Just like a school or workout schedule, in context of denoising diffusion models a schedule refers to sequence of noise levels applied at each timestep during the forward process.

In original paper authors chose linear diffusion schedule from

Whats wrong with constant scheduling?

If every step adds identical amount of distortion, images gets corrupted too fast, model has no chance to learn denoising steps. Think of it like trying to learn how to unmix a smoothie, if everything’s instantly blended, you can't tell what fruit was there. With variable scheduling, early timesteps use small model learns easy denoising tasks, later timesteps use large making model learn hard noise

Linear Diffusion schedule

This schedule will add noise evenly from start to end

Cosine Diffusion schedule

In the same researchers found out cosine scheduler outperformed liner scheduler in different paper in terms of quality and performance.

t is current step

T is total steps

GenAI / Final — Diffusion models

Reverse Process

We are learning a NN (here means parameters) that can approximate the reverse distribution . The reverse process is similar to VAE, given a noise vector we are denoising an image, forward process in VAEs is parameterized, in diffusion model forward process is not parameterized.

Loss function

GenAI / Final — Diffusion models
GenAI / Final — Diffusion models

UNet for denoising

Unet is the NN which predicts the noise added to a noisy image in diffusion models.

Two halves:

  1. Downsampling half: compresses spatial dimensions, increases channel depth
  2. Upsampling half: expands spatial dimensions, reduces channel depth

UNet inputs noisy image

And outputs a tensor containing noise intensities at each channel

Why not use VAE here?

Unet Uses skip connections to improves pixel level accuracy preserving fine spatial details + allows better gradient flow ~ faster training.

Sinusoidal Embeddings

Ashish Vaswani 2017 (indian) introduced, extended by Mildenhall (NeRF, 2020)

Is a way to convert single scalar input (timestep or variance) into a high dimensional vector. A single scalar value input would have minimal effect on the output, having distinct vector for each scalar value would have more effect on output.

GenAI / Final — Diffusion models
GenAI / Final — Diffusion models

Residual Blocks

A Residual Block is a group of layers where the input is added to the output via a skip connection.

Solves:

  1. Vanishing gradient issue - when gradients become very small during backprop
  2. Degradation problem - as network gets deeper, accuracy worsens despite more layers
GenAI / Final — Diffusion models

Downblock in UNet

Used during encoding stage of Unet

Increases feature channels with multiple Resblocks

Downsamples using pooling

Stores intermediate outputs in some temp variable for skip connection to upblocks

UpBlock in UNet

Used during decoding stage of Unet

Upsamples using upsampling2D

Reduces channels using residual blocks

Concatenate with downblock output to recover spatial details

Sampling process

  1. Unet trained to predict total noise that has been added to noisy image at timestep t. We can compute the original image directly in one step
  2. We reconstruct image by adding noise until 0 … t-1 steps
  3. Unet predicts the noise added until t-1 in a loop until T steps to get clean image at the end
  4. Model was trained with small transitions in mind, jumping from to directly hurts sample quality. We can choose lesser number of steps than T though 😋 balancing compute and quality.
GenAI / Final — Diffusion models

Increasing the number of diffusion steps in reverse process improves image quality as model is able to sharpen its generations

But image quality in book example doesn’t improves as much after 20 steps

Interpolation in DDPMs

Interpolation means to create a smooth transition between two data points by estimating intermediate values between two known points

How to interpolate two images in DDPMS:

  1. Start with two random noise matrix a, b
  2. outputs interpolated noise matrix, we use this matrix to remove noise at timestep t
  3. After n steps we get the original image