Notes

Diffusion models

The forward pass (image to noise)

The forward process can be described with a sequence of gaussian distributions:

q(xtxt1)=N(xt;1βtxt1,βtI)q(x_t|x_{t-1})=\mathcal N(x_t; \sqrt{1 - \beta_t}x_{t-1}, \beta_tI)

Assume βt\beta_t is the weight for gaussian noise (variance) at step tt, and αt=1βt, αˉt=i=1tαi\alpha_t = 1 - \beta_t,~ \bar\alpha_t= \prod_{i=1}^t\alpha_i.

The forward step gradually adds a sequence of gaussian noise {ϵt}N(0,I)\{\epsilon_t\} \sim \mathcal N(0, I) to the real image x0x_0, so the corrupted image at step tt is:

xt=αtxt1+1αtϵ1=αt(αt1xt2+1αt1ϵ2)+1αtϵ1=αtαt1xt2+αt(1αt1)ϵ2+1αtϵ1=αtαt1xt2+1αtαt1ϵ2==αˉtx0+1αˉtϵt\begin{aligned} x_t &= \sqrt{\alpha_t}x_{t-1}+ \sqrt{1-\alpha_t}\epsilon_{1} \\ &= \sqrt{\alpha_t}(\sqrt{\alpha_{t-1}}x_{t-2}+ \sqrt{1-\alpha_{t-1}}\epsilon_{2})+ \sqrt{1-\alpha_t}\epsilon_{1} \\ &= \sqrt{\alpha_t\alpha_{t-1}}x_{t-2}+\sqrt{\alpha_t(1-\alpha_{t-1})}\epsilon_{2}+ \sqrt{1-\alpha_t}\epsilon_{1} \\ &= \sqrt{\alpha_t\alpha_{t-1}}x_{t-2}+\sqrt{1-\alpha_t\alpha_{t-1}} \epsilon_{2} \\ &= \cdots \\ &= \sqrt{\bar \alpha_{t}}x_{0}+\sqrt{1-\bar \alpha_{t}} \epsilon_{t} \\ \end{aligned}

Note that two independent gaussian noise ϵ1,ϵ2\epsilon_{1}, \epsilon_{2} can be merged into one:

N(0,αt(1αt1))+N(0,1αt)N(0,1αtαt1)\mathcal N(0, {\alpha_t(1-\alpha_{t-1})})+\mathcal N(0, {1-\alpha_{t}}) \rightarrow \mathcal N(0, {1-\alpha_t\alpha_{t-1}})

This means we can express any xtx_t with x0x_0, {β1,β2,,βt}\{\beta_1, \beta_2, \cdots, \beta_t\}, and a random noise ϵt\epsilon_{t}.

The backward pass (noise to image)

Given xtx_{t}, we want to get xt1x_{t-1}, and eventually restore the real image x0x_0.

It's proven that if βt\beta_t is small enough, the backward process is also a sequence of gaussian.

q(xt1xt)=N(xt1;μ~t(xt,t),β~tI)q(x_{t-1}|x_t) = \mathcal N(x_{t-1}; \tilde\mu_t(x_t, t), \tilde\beta_tI)

which gives the denoising step with another sequence of gaussian noise {zt}N(0,I)\{z_t\} \sim \mathcal N(0, I):

xt1=μ~t(xt,t)+β~tztx_{t-1} = \tilde\mu_t(x_t, t)+\tilde \beta_t z_t

Through some math we could get:

β~t=1αˉt11αˉtβtμ~t(xt,t)=1αt(xt1αt1αˉtϵt)\tilde\beta_t = \frac {1 - \bar \alpha_{t-1}} {1 - \bar \alpha_{t}} \beta_t\\ \tilde\mu_t(x_t, t)=\frac {1} {\sqrt{\alpha_t}} (x_t-\frac{1-\alpha_t}{\sqrt{1-\bar \alpha_t}}\epsilon_t)

Note that here ϵt\epsilon_t is the exact noise added in the forward step!

And all we need is a neural network that predicts this noise ϵt\epsilon_t given xt(x0,t)=αˉtx0+1αˉtϵtx_t(x_0, t) = \sqrt{\bar \alpha_{t}}x_{0}+\sqrt{1-\bar \alpha_{t}}\epsilon_t.

Assume the predicted noise is ϵθ\epsilon_\theta, we need to minimize the following loss:

L=Et,x0,ϵtϵtϵθ(x0,t)2\mathcal L=\mathbb E_{t,x_0,\epsilon_t}||\epsilon_t -\epsilon_\theta(x_0, t)||^2

The neural network is usually an U-Net with attention since we need same-size output as input.

DDPM (Denoising Diffusion Probabilistic Model)

DDPM exactly describes the above idea.

Note that DDPM use β~t=βt\tilde \beta_t = \beta_t as an approximation.

image-20221212213705784

DDIM (Denoising Diffusion Implicit Model)

DDIM use a different formula for the backward step:

xt1=αˉt1x0+1αˉt1ϵt1=αˉt1x0+1αˉt1σt2ϵt+σtzt=αˉt1x0+1αˉt1σt2xtαtx01αt+σtzt\begin{aligned} x_{t-1} &= \sqrt{\bar \alpha_{t-1}}x_{0}+\sqrt{1-\bar \alpha_{t-1}} \epsilon_{t-1} \\ &= \sqrt{\bar \alpha_{t-1}}x_{0}+\sqrt{1-\bar \alpha_{t-1} -\sigma_t^2} \epsilon_{t} +\sigma_t z_t \\ &= \sqrt{\bar \alpha_{t-1}}x_{0}+\sqrt{1-\bar \alpha_{t-1} -\sigma_t^2} \frac {x_t-\sqrt{\alpha_t}x_0}{\sqrt{1-\alpha_t}} +\sigma_t z_t \\ \end{aligned}

According to xt1=μ~t(xt,t)+β~tztx_{t-1} = \tilde\mu_t(x_t, t)+\tilde \beta_t z_t, we have

μ~t(xt,t)=αˉt1x0+1αˉt1σt2xtαtx01αtβ~t=σt2=1αˉt11αˉtβt\tilde\mu_t(x_t, t)=\sqrt{\bar \alpha_{t-1}}x_{0}+\sqrt{1-\bar \alpha_{t-1} -\sigma_t^2} \frac {x_t-\sqrt{\alpha_t}x_0}{\sqrt{1-\alpha_t}} \\ \tilde\beta_t = \sigma_t^2= \frac {1 - \bar \alpha_{t-1}} {1 - \bar \alpha_{t}} \beta_t

Another hyper-parameter η0\eta \ge 0 is introduced as ηβ~t=σt2\eta \tilde \beta_t = \sigma_t^2.

DDPM usually requires T=1000T=1000 steps for both training and inference (S=T=1000S = T = 1000), which makes generation (inference) too slow.

To speed up inference, DDIM use strided sampling where S<TS <T, and shows when η=0\eta = 0, S=50100S=50\sim100 steps are enough to generate good images.

LDM (Latent Diffusion Model)

Instead of diffusing in the pixel space, LDM first use a VQVAE to compress the image space (e.g., 512x512x3) into a latent space (e.g., 64x64x4).

This greatly reduces the computation cost for both training and testing.

Conditioned Generation

Assume we want to condition the generation to an input yy which may be class labels or text.

Classifier-Guided Diffusion

A pretrained classifier is required, not flexible.

Classifier-Free Guidance

Train both conditioned denoiser ϵθ(x0,t)\epsilon_\theta(x_0, t) and unconditioned denoiser ϵθ(x0,t,y)\epsilon_\theta(x_0, t, y) in the same neural network together.

The final denoiser is simply a weighted combination of these two terms:

ϵˉθ(x0,t,y)=ϵθ(x0,t,y)+w(ϵθ(x0,t,y)ϵθ(x0,t))=(w+1)ϵθ(x0,t,y)wϵθ(x0,t)\begin{aligned} \bar\epsilon_\theta(x_0, t, y) &= \epsilon_\theta(x_0, t, y) + w(\epsilon_\theta(x_0, t, y) - \epsilon_\theta(x_0, t)) \\ &=(w+1)\epsilon_\theta(x_0, t, y) - w\epsilon_\theta(x_0, t) \end{aligned}

where a larger ww leads to stronger class guidance.

Type to search.