SeriesDeep Learning14 / 20

Denoising Diffusion Probabilistic Models

Supplement to Module 13 of CS 7643 - Deep Learning @ Georgia Tech.

Introduction#

(notes for this intro section obtained from IBM: What are Diffusion Models?)

Diffusion Models are a class of generative models, which broadly seek to estimate the joint probability distribution of our data Pr(X)\Pr(X) in order to sample new (synthetic) instances. Diffusion models are most commonly applied to image generation and other computer vision tasks.

The primary idea behind diffusion modeling is that we will progressively “diffuse” samples with random noise, eventually destroying the original input instance. The diffusion model learns to perform denoising to probabilistically reconstruct the original instance. A trained diffusion model takes in random noise as input, and produces an instance of the target class.

Denoising Diffusion Probabilistic Models (DDPMs) were introduced in 2020 as competitors to state-of-the-art image generation methods such as Generative Adversarial Networks (GANs). As of today, DDPMs are the most dominant family of models for image generation.

DDPMs#

Overview of Modeling Process#

DDPM process image data in two main steps:

  1. Forward Diffusion: iteratively add noise to an input image, eventually producing an image of simply noise. Noise is typically assumed to be Gaussian N(0,I)\mathcal{N}(0, I).
    • Diffusion is repeated over many time steps TT, with intermediate representations of the image xtx_t produced at each step.
    • x0x_0 is always the input image, and xTx_T is always the pure noise version of the image.
    • NOT a neural network. Instead, we are simply adding Gaussian noise to the input.
  2. Denoising: generate an image from noise by iteratively removing noise from the final noised image xTx_T.
    • Implemented as a neural network.
DDPM

Compared to other generative models, diffusion models are relatively unique in their methods for generation.

generative-models

Whereas the forward diffusion process is well-defined, the denoising diffusion process is the learned part of DDPMs. Let’s step through the math of each step more formally.

Step 1: Forward Diffusion#

As previously mentioned, the forward process of a diffusion model iteratively adds noise to the original input image x0x_0 to produce some final image of only noise xTx_T. Forward diffusion is sometimes referred to as encoding, even though we don’t use a neural network to transform the input.

We can represent the forward diffusion process as a Markov Chain - a stochastic process describing a sequence of possible events, in which the probability of the current event only depends on the previous event (Markov Property). Note that a Markov Chain is a special case of the Chain Rule of Probability, which states that we can decompose a joint probability distribution into a product over all conditionals.

CHAIN RULE:   Pr(e1,e2,e3)=Pr(e3  e2,e1)×Pr(e2  e1)×Pr(e1)\text{CHAIN RULE}: ~~~ \Pr(e_1, e_2, e_3) = \Pr(e_3 ~ | ~ e_2, e_1) \times \Pr(e_2 ~ | ~ e_1) \times \Pr(e_1)

MARKOV CHAIN:   Pr(e1,e2,e3)=Pr(e3  e2)×Pr(e2  e1)×Pr(e1)\text{MARKOV CHAIN}: ~~~ \Pr(e_1, e_2, e_3) = \Pr(e_3 ~ | ~ e_2) \times \Pr(e_2 ~ | ~ e_1) \times \Pr(e_1)

Applied to the forward diffusion process, we have…

q(x1:T  x0)=t=1TPr(xt  xt1)q(x_{1:T} ~ | ~ x_0) = \prod_{t=1}^T \Pr(x_t ~ | ~ x_{t-1})

… where each conditional distribution is estimated by a conditional Gaussian as follows:

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

We can actually unroll our computations over time to produce a distribution of any intermediate output xtx_t conditioned on the original input x0x_0.

q(xt  x0)=N(xt;αˉtx0,(1αˉt)I)q(x_t ~ | ~ x_0) = \mathcal{N}(x_t; \sqrt{\bar{\alpha}}_t x_0, (1 - \bar{\alpha}_t)I)

αt=(1βt),   αˉt=s=1tαs\alpha_t = (1 - \beta_t), ~~~ \bar{\alpha}_t = \prod_{s=1}^t \alpha_s

Recall in the case of VAEs, we introduced the Reparameterization Trick to avoid sampling directly within our computation graph (which would prevent backpropagation, since sampling is a non-differentiable operation). We apply the same trick in the case of DDPMs.

xt=αˉtx0+1αˉtϵ,   ϵN(0,1)x_t = \sqrt{\bar{\alpha}_t}x_0 + \sqrt{1 - \bar{\alpha}_t} \epsilon, ~~~ \epsilon \sim \mathcal{N}(0, 1)

Step 2: Denoising Diffusion#

During the reverse process, we denoise our completely-noised instance xtx_t to generate an instance of the original type. Whereas the forward diffusion step is well-defined, the reverse diffusion step is completely learned by our neural network. Denoising as part of DDPMs is sometimes referred to as the decoding portion of the full process.

Similar to the case of forward diffusion, the denoising process can be thought of as a Markov Chain of conditional Gaussians.

pθ(x0:T)=Pr(xT)×t=1Tpθ(xt1  xt)p_{\theta}(x_{0:T}) = \Pr(x_T) \times \prod_{t=1}^{T} p_{\theta}(x_{t-1} ~ | ~ x_t)

pθ(xt1  xt)=N(xt1;μθ(xt,t),Σq(t))p_{\theta}(x_{t-1} ~ | ~ x_t) = \mathcal{N}(x_{t-1}; \mu_{\theta}(x_t, t), \Sigma_q(t))

Note that while the Markov Chain looks familiar, the estimate of each conditional Gaussian distribution has changed. Our distribution parameters are estimated via a neural network. For simplicity, we typically only estimate the mean vector μθ\mu_{\theta} and define covariance as the corresponding covariance from the forward diffusion process Σq(t)\Sigma_q(t).

How does our neural network estimate the conditional distribution at each time step? The high-level intuition is to derive some ground truth denoising distribution q(xt1  xt,x0)q(x_{t-1} ~ \| ~ x_t, x_0), then train a neural network to generate an estimate of this distribution pθ(xt1  xt)p_{\theta}(x_{t-1} ~ \| ~ x_t). As such, our Loss Function for optimization can be formulated as the KL Divergence between our ground truth and estimated probability distributions.

argminθDKL  q(xt1  xt,x0)  pθ(xt1  xt)\arg \min_{\theta} \text{D}_{KL} ~~ q(x_{t-1} ~ | ~ x_t, x_0) ~ || ~ p_{\theta}(x_{t-1} ~ | ~ x_t)

Since we know the noise added at each time step - each conditional distribution of the forward diffusion process was created via our βt\beta_t specification - we can directly calculate our ground truth distributions.

q(xt1  xt,x0)=N(xt1;μq(t),Σq(t))q(x_{t-1} ~ | ~ x_t, x_0) = \mathcal{N}(x_{t-1}; \mu_q(t), \Sigma_q(t))

μq(t)=1αt(xtβt1αˉt)ϵ),   ϵN(0,I)\mu_q(t) = \frac{1}{\sqrt{\alpha_t}}(x_t - \frac{\beta_t}{\sqrt{1 - \bar{\alpha}_t)}}\epsilon), ~~~ \epsilon \sim \mathcal{N}(0, I)

Therefore, assuming identical variance Σq(t)\Sigma_q(t), we can decompose our loss function from KL Divergence between predicted and ground truth denoising conditional distributions, to simple mean squared error between the actual noise added and our network’s prediction of noise added. (this involves a ton of math, so I will not consider the derivation here). This implies we must store the noise added during forward diffusion for use in the final loss function.

argminθDKL=argminθϵϵθ(xt,t)2\arg \min_{\theta} \text{D}_{KL} = \arg \min_{\theta} || \epsilon - \epsilon_{\theta}(x_t, t) || ^2

This implies we design our neural network to predict noise ϵθ\epsilon_{\theta} as opposed to the parameters of the noise distribution.

“Anytime you can make the neural network estimate something smaller or simpler, compared to something that’s more complicated, you should choose the simpler option.”

  • Zsolt Kira

Practical Considerations#

During Model Training, we perform forward diffusion by iteratively adding noise to our input image. Once we have a noisy image, we feed it to our decoder neural network to estimate noise ϵθ\epsilon_{\theta}.

train-alg

For Model Inference, we provide the neural network with a noised image xTx_T, and iteratively feed xtx_t to the denoiser process to generate xt1x_{t-1} until reaching x0x_0.

inf-alg

As a final summary of the diffusion process, consider the following illustration. Forward diffusion progressively noises the original input image until reaching a point of compete noise. Conversely, reverse diffusion progressively denoises the final noised image to reproduce a valid input.

2D-diffusion-example
License

CC BY-NC-SA 4.0 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Related Posts