Diffusion model

From HandWiki
Short description: Deep learning algorithm

In machine learning, diffusion models, also known as diffusion probabilistic models or score-based generative models, are a class of latent variable generative models. A diffusion model consists of three major components: the forward process, the reverse process, and the sampling procedure.[1] The goal of diffusion models is to learn a diffusion process that generates the probability distribution of a given dataset. They learn the latent structure of a dataset by modeling the way in which data points diffuse through their latent space.[2]

In the case of computer vision, diffusion models can be applied to a variety of tasks, including image denoising, inpainting, super-resolution, and image generation. This typically involves training a neural network to sequentially denoise images blurred with Gaussian noise.[2][3] The model is trained to reverse the process of adding noise to an image. After training to convergence, it can be used for image generation by starting with an image composed of random noise for the network to iteratively denoise. Announced on 13 April 2022, OpenAI's text-to-image model DALL-E 2 is an example that uses diffusion models for both the model's prior (which produces an image embedding given a text caption) and the decoder that generates the final image.[4] Diffusion models have recently found applications in natural language processing (NLP),[5] particularly in areas like text generation[6][7] and summarization.[8]

Diffusion models are typically formulated as markov chains and trained using variational inference.[9] Examples of generic diffusion modeling frameworks used in computer vision are denoising diffusion probabilistic models, noise conditioned score networks, and stochastic differential equations.[10]

Denoising diffusion model

Non-equilibrium thermodynamics

Diffusion models were introduced in 2015 as a method to learn a model that can sample from a highly complex probability distribution. They used techniques from non-equilibrium thermodynamics, especially diffusion.[11]

Consider, for example, how one might model the distribution of all naturally-occurring photos. Each image is a point in the space of all images, and the distribution of naturally-occurring photos is a "cloud" in space, which, by repeatedly adding noise to the images, diffuses out to the rest of the image space, until the cloud becomes all but indistinguishable from a Gaussian distribution [math]\displaystyle{ N(0, I) }[/math]. A model that can approximately undo the diffusion can then be used to sample from the original distribution. This is studied in "non-equilibrium" thermodynamics, as the starting distribution is not in equilibrium, unlike the final distribution.

The equilibrium distribution is the Gaussian distribution [math]\displaystyle{ N(0, I) }[/math], with pdf [math]\displaystyle{ \rho(x) \propto e^{-\frac 12 \|x\|^2} }[/math]. This is just the Boltzmann distribution of particles in a potential well [math]\displaystyle{ V(x) = \frac 12 \|x\|^2 }[/math] at temperature 1. The initial distribution, being very much out of equilibrium, would diffuse towards the equilibrium distribution, making biased random steps that are a sum of pure randomness (like a Brownian walker) and gradient descent down the potential well. The randomness is necessary: if the particles were to undergo only gradient descent, then they will all fall to the origin, collapsing the distribution.

Denoising Diffusion Probabilistic Model (DDPM)

The 2020 paper proposed the Denoising Diffusion Probabilistic Model (DDPM), which improves upon the previous method by variational inference.[9]

Forward diffusion

To present the model, we need some notation.

  • [math]\displaystyle{ \beta_1, ..., \beta_T \in (0, 1) }[/math] are fixed constants.
  • [math]\displaystyle{ \alpha_t := 1-\beta_t }[/math]
  • [math]\displaystyle{ \bar \alpha_t := \alpha_1 \cdots \alpha_t }[/math]
  • [math]\displaystyle{ \tilde \beta_t := \frac{1-\bar \alpha_{t-1}}{1-\bar \alpha_{t}}\beta_t }[/math]
  • [math]\displaystyle{ \tilde\mu_t(x_t, x_0) :=\frac{\sqrt{\alpha_{t}}(1-\bar \alpha_{t-1})x_t +\sqrt{\bar\alpha_{t-1}}(1-\alpha_{t})x_0}{1-\bar\alpha_{t}} }[/math]
  • [math]\displaystyle{ N(\mu, \Sigma) }[/math] is the normal distribution with mean [math]\displaystyle{ \mu }[/math] and variance [math]\displaystyle{ \Sigma }[/math], and [math]\displaystyle{ N(x | \mu, \Sigma) }[/math] is the probability density at [math]\displaystyle{ x }[/math].
  • A vertical bar denotes conditioning.

A forward diffusion process starts at some starting point [math]\displaystyle{ x_0 \sim q }[/math], where [math]\displaystyle{ q }[/math] is the probability distribution to be learned, then repeatedly adds noise to it by[math]\displaystyle{ x_t = \sqrt{1-\beta_t} x_{t-1} + \sqrt{\beta_t} z_t }[/math]where [math]\displaystyle{ z_1, ..., z_T }[/math] are IID samples from [math]\displaystyle{ N(0, I) }[/math]. This is designed so that for any starting distribution of [math]\displaystyle{ x_0 }[/math], we have [math]\displaystyle{ \lim_t x_t|x_0 }[/math] converging to [math]\displaystyle{ N(0, I) }[/math].

The entire diffusion process then satisfies[math]\displaystyle{ q(x_{0:T}) = q(x_0)q(x_1|x_0) \cdots q(x_T|x_{T-1}) = q(x_0) N(x_1 | \sqrt{\alpha_1} x_0, \beta_1 I) \cdots N(x_T | \sqrt{\alpha_T} x_{T-1}, \beta_T I) }[/math]or[math]\displaystyle{ \ln q(x_{0:T}) = \ln q(x_0) - \sum_{t=1}^T \frac{1}{2\beta_t} \| x_t - \sqrt{1-\beta_t}x_{t-1}\|^2 + C }[/math]where [math]\displaystyle{ C }[/math] is a normalization constant and often omitted. In particular, we note that [math]\displaystyle{ x_{1:T}|x_0 }[/math] is a gaussian process, which affords us considerable freedom in reparameterization. For example, by standard manipulation with gaussian process, [math]\displaystyle{ x_{t}|x_0 \sim N\left(\sqrt{\bar\alpha_t} x_{0}, (1-\bar\alpha_t) I \right) }[/math][math]\displaystyle{ x_{t-1} | x_t, x_0 \sim N(\tilde\mu_t(x_t, x_0), \tilde \beta_t I) }[/math]In particular, notice that for large [math]\displaystyle{ t }[/math], the variable [math]\displaystyle{ x_{t}|x_0 \sim N\left(\sqrt{\bar\alpha_t} x_{0}, (1-\bar\alpha_t) I \right) }[/math] converges to [math]\displaystyle{ N(0, I) }[/math]. That is, after a long enough diffusion process, we end up with some [math]\displaystyle{ x_T }[/math] that is very close to [math]\displaystyle{ N(0, I) }[/math], with all traces of the original [math]\displaystyle{ x_0 \sim q }[/math] gone.

For example, since[math]\displaystyle{ x_{t}|x_0 \sim N\left(\sqrt{\bar\alpha_t} x_{0}, (1-\bar\alpha_t) I \right) }[/math]we can sample [math]\displaystyle{ x_{t}|x_0 }[/math] directly "in one step", instead of going through all the intermediate steps [math]\displaystyle{ x_1, x_2, ..., x_{t-1} }[/math].

Backward diffusion

The key idea of DDPM is to use a neural network parametrized by [math]\displaystyle{ \theta }[/math]. The network takes in two arguments [math]\displaystyle{ x_t, t }[/math], and outputs a vector [math]\displaystyle{ \mu_\theta(x_t, t) }[/math] and a matrix [math]\displaystyle{ \Sigma_\theta(x_t, t) }[/math], such that each step in the forward diffusion process can be approximately undone by [math]\displaystyle{ x_{t-1} \sim N(\mu_\theta(x_t, t), \Sigma_\theta(x_t, t)) }[/math]. This then gives us a backward diffusion process [math]\displaystyle{ p_\theta }[/math] defined by[math]\displaystyle{ p_\theta(x_T) = N(x_T | 0, I) }[/math][math]\displaystyle{ p_\theta(x_{t-1} | x_t) = N(x_{t-1} | \mu_\theta(x_t, t), \Sigma_\theta(x_t, t)) }[/math]The goal now is to learn the parameters such that [math]\displaystyle{ p_\theta(x_0) }[/math] is as close to [math]\displaystyle{ q(x_0) }[/math] as possible. To do that, we use maximum likelihood estimation with variational inference.

Variational inference

The ELBO inequality states that [math]\displaystyle{ \ln p_\theta(x_0) \geq E_{x_{1:T}\sim q(\cdot | x_0)}[ \ln p_\theta(x_{0:T}) - \ln q(x_{1:T}|x_0)] }[/math], and taking one more expectation, we get[math]\displaystyle{ E_{x_0 \sim q}[\ln p_\theta(x_0)] \geq E_{x_{0:T}\sim q}[ \ln p_\theta(x_{0:T}) - \ln q(x_{1:T}|x_0)] }[/math]We see that maximizing the quantity on the right would give us a lower bound on the likelihood of observed data. This allows us to perform variational inference.

Define the loss function[math]\displaystyle{ L(\theta) := -E_{x_{0:T}\sim q}[ \ln p_\theta(x_{0:T}) - \ln q(x_{1:T}|x_0)] }[/math]and now the goal is to minimize the loss by stochastic gradient descent. The expression may be simplified to[12][math]\displaystyle{ L(\theta) = \sum_{t=1}^T E_{x_{t-1}, x_t\sim q}[-\ln p_\theta(x_{t-1} | x_t)] + E_{x_0 \sim q}[D_{KL}(q(x_T|x_0) \| p_\theta(x_T))] + C }[/math]where [math]\displaystyle{ C }[/math] does not depend on the parameter, and thus can be ignored. Since [math]\displaystyle{ p_\theta(x_T) = N(x_T | 0, I) }[/math] also does not depend on the parameter, the term [math]\displaystyle{ E_{x_0 \sim q}[D_{KL}(q(x_T|x_0) \| p_\theta(x_T))] }[/math] can also be ignored. This leaves just [math]\displaystyle{ L(\theta ) = \sum_{t=1}^T L_t }[/math] with [math]\displaystyle{ L_t = E_{x_{t-1}, x_t\sim q}[-\ln p_\theta(x_{t-1} | x_t)] }[/math] to be minimized.

Noise prediction network

Since [math]\displaystyle{ x_{t-1} | x_t, x_0 \sim N(\tilde\mu_t(x_t, x_0), \tilde \beta_t I) }[/math], this suggests that we should use [math]\displaystyle{ \mu_\theta(x_t, t) = \tilde \mu_t(x_t, x_0) }[/math]; however, the network does not have access to [math]\displaystyle{ x_0 }[/math], and so it has to estimate it instead. Now, since [math]\displaystyle{ x_{t}|x_0 \sim N\left(\sqrt{\bar\alpha_t} x_{0}, (1-\bar\alpha_t) I \right) }[/math], we may write [math]\displaystyle{ x_t = \sqrt{\bar\alpha_t} x_{0} + \sqrt{1-\bar\alpha_t} z }[/math], where [math]\displaystyle{ z }[/math] is some unknown gaussian noise. Now we see that estimating [math]\displaystyle{ x_0 }[/math] is equivalent to estimating [math]\displaystyle{ z }[/math].

Therefore, let the network output a noise vector [math]\displaystyle{ \epsilon_\theta(x_t, t) }[/math], and let it predict[math]\displaystyle{ \mu_\theta(x_t, t) =\tilde\mu_t\left(x_t, \frac{x_t - \sqrt{1-\bar\alpha_t} \epsilon_\theta(x_t, t)}{\sqrt{\bar\alpha_t}}\right) = \frac{x_t - \epsilon_\theta(x_t, t) \beta_t/\sqrt{1-\bar\alpha_t}}{\sqrt{\alpha_t}} }[/math]It remains to design [math]\displaystyle{ \Sigma_\theta(x_t, t) }[/math]. The DDPM paper suggested not learning it (since it resulted in "unstable training and poorer sample quality"), but fixing it at some value [math]\displaystyle{ \Sigma_\theta(x_t, t) = \sigma_t^2 I }[/math], where either [math]\displaystyle{ \sigma_t^2 = \beta_t \text{ or } \tilde \beta_t }[/math] yielded similar performance.

With this, the loss simplifies to [math]\displaystyle{ L_t = \frac{\beta_t^2}{2\alpha_t(1-\bar\alpha_t)\sigma_t^2} E_{x_0\sim q; z \sim N(0, I)}\left[ \left\| \epsilon_\theta(x_t, t) - z \right\|^2\right] + C }[/math]which may be minimized by stochastic gradient descent. The paper noted empirically that an even simpler loss function[math]\displaystyle{ L_{simple, t} = E_{x_0\sim q; z \sim N(0, I)}\left[ \left\| \epsilon_\theta(x_t, t) - z \right\|^2\right] }[/math]resulted in better models.

Score-based generative model

Score-based generative model is another formulation of diffusion modelling. They are also called noise conditional score network (NCSN) or score-matching with Langevin dynamics (SMLD).[13][14]

Score matching

The idea of score functions

Consider the problem of image generation. Let [math]\displaystyle{ x }[/math] represent an image, and let [math]\displaystyle{ q(x) }[/math] be the probability distribution over all possible images. If we have [math]\displaystyle{ q(x) }[/math] itself, then we can say for certain how likely a certain image is. However, this is intractable in general.

Most often, we are uninterested in knowing the absolute probability of a certain image. Instead, we are usually only interested in knowing how likely a certain image is compared to its immediate neighbors — e.g. how much more likely is an image of cat compared to some small variants of it? Is it more likely if the image contains two whiskers, or three, or with some Gaussian noise added?

Consequently, we are actually quite uninterested in [math]\displaystyle{ q(x) }[/math] itself, but rather, [math]\displaystyle{ \nabla_x \ln q(x) }[/math]. This has two major effects:

  • One, we no longer need to normalize [math]\displaystyle{ q(x) }[/math], but can use any [math]\displaystyle{ \tilde q(x) = Cq(x) }[/math], where [math]\displaystyle{ C = \int \tilde q(x) dx \gt 0 }[/math] is any unknown constant that is of no concern to us.
  • Two, we are comparing [math]\displaystyle{ q(x) }[/math] neighbors [math]\displaystyle{ q(x + dx) }[/math], by [math]\displaystyle{ \frac{q(x)}{q(x+dx)} =e^{-\langle \nabla_x \ln q, dx \rangle} }[/math]

Let the score function be [math]\displaystyle{ s(x) := \nabla_x \ln q(x) }[/math]; then consider what we can do with [math]\displaystyle{ s(x) }[/math].

As it turns out, [math]\displaystyle{ s(x) }[/math] allows us to sample from [math]\displaystyle{ q(x) }[/math] using thermodynamics. Specifically, if we have a potential energy function [math]\displaystyle{ U(x) = -\ln q(x) }[/math], and a lot of particles in the potential well, then the distribution at thermodynamic equilibrium is the Boltzmann distribution [math]\displaystyle{ q_U(x) \propto e^{-U(x)/k_B T} = q(x)^{1/k_BT} }[/math]. At temperature [math]\displaystyle{ k_BT=1 }[/math], the Boltzmann distribution is exactly [math]\displaystyle{ q(x) }[/math].

Therefore, to model [math]\displaystyle{ q(x) }[/math], we may start with a particle sampled at any convenient distribution (such as the standard gaussian distribution), then simulate the motion of the particle forwards according to the Langevin equation[math]\displaystyle{ dx_{t}= -\nabla_{x_t}U(x_t) d t+d W_t }[/math]and the Boltzmann distribution is, by Fokker-Planck equation, the unique thermodynamic equilibrium. So no matter what distribution [math]\displaystyle{ x_0 }[/math] has, the distribution of [math]\displaystyle{ x_t }[/math] converges in distribution to [math]\displaystyle{ q }[/math] as [math]\displaystyle{ t\to \infty }[/math].

Learning the score function

Given a density [math]\displaystyle{ q }[/math], we wish to learn a score function approximation [math]\displaystyle{ f_\theta \approx \nabla \ln q }[/math]. This is score matching.[15] Typically, score matching is formalized as minimizing Fisher divergence function [math]\displaystyle{ E_q[\|f_\theta(x) - \nabla \ln q(x)\|^2] }[/math]. By expanding the integral, and performing an integration by parts, [math]\displaystyle{ E_q[\|f_\theta(x) - \nabla \ln q(x)\|^2] = E_q[\|f_\theta\|^2 + 2\nabla^2\cdot f_\theta] + C }[/math]giving us a loss function, also known as the Hyvärinen scoring rule, that can be minimized by stochastic gradient descent.

Annealing the score function

Suppose we need to model the distribution of images, and we want [math]\displaystyle{ x_0 \sim N(0, I) }[/math], a white-noise image. Now, most white-noise images do not look like real images, so [math]\displaystyle{ q(x_0) \approx 0 }[/math] for large swaths of [math]\displaystyle{ x_0 \sim N(0, I) }[/math]. This presents a problem for learning the score function, because if there are no samples around a certain point, then we can't learn the score function at that point. If we do not know the score function [math]\displaystyle{ \nabla_{x_t}\ln q(x_t) }[/math] at that point, then we cannot impose the time-evolution equation on a particle:[math]\displaystyle{ dx_{t}= \nabla_{x_t}\ln q(x_t) d t+d W_t }[/math]To deal with this problem, we perform annealing. If [math]\displaystyle{ q }[/math] is too different from a white-noise distribution, then progressively add noise until it is indistinguishable from one. That is, we perform a forward diffusion, then learn the score function, then use the score function to perform a backward diffusion.

Continuous diffusion processes

Forward diffusion process

Consider again the forward diffusion process, but this time in continuous time:[math]\displaystyle{ x_t = \sqrt{1-\beta_t} x_{t-1} + \sqrt{\beta_t} z_t }[/math]By taking the [math]\displaystyle{ \beta_t \to \beta(t)dt, \sqrt{dt}z_t \to dW_t }[/math] limit, we obtain a continuous diffusion process, in the form of a stochastic differential equation:[math]\displaystyle{ dx_t = -\frac 12 \beta(t) x_t dt + \sqrt{\beta(t)} dW_t }[/math]where [math]\displaystyle{ W_t }[/math] is a Wiener process (multidimensional Brownian motion).

Now, the equation is exactly a special case of the overdamped Langevin equation[math]\displaystyle{ dx_t = -\frac{D}{k_BT} (\nabla_x U)dt + \sqrt{2D}dW_t }[/math]where [math]\displaystyle{ D }[/math] is diffusion tensor, [math]\displaystyle{ T }[/math] is temperature, and [math]\displaystyle{ U }[/math] is potential energy field. If we substitute in [math]\displaystyle{ D= \frac 12 \beta(t)I, k_BT = 1, U = \frac 12 \|x\|^2 }[/math], we recover the above equation. This explains why the phrase "Langevin dynamics" is sometimes used in diffusion models.

Now the above equation is for the stochastic motion of a single particle. Suppose we have a cloud of particles distributed according to [math]\displaystyle{ q }[/math] at time [math]\displaystyle{ t=0 }[/math], then after a long time, the cloud of particles would settle into the stable distribution of [math]\displaystyle{ N(0, I) }[/math]. Let [math]\displaystyle{ \rho_t }[/math] be the density of the cloud of particles at time [math]\displaystyle{ t }[/math], then we have[math]\displaystyle{ \rho_0 = q; \quad \rho_T \approx N(0, I) }[/math]and the goal is to somehow reverse the process, so that we can start at the end and diffuse back to the beginning.

By Fokker-Planck equation, the density of the cloud evolves according to[math]\displaystyle{ \partial_t \ln \rho_t = \frac 12 \beta(t) \left( n + (x+ \nabla\ln\rho_t) \cdot \nabla \ln\rho_t + \Delta\ln\rho_t \right) }[/math]where [math]\displaystyle{ n }[/math] is the dimension of space, and [math]\displaystyle{ \Delta }[/math] is the Laplace operator.

Backward diffusion process

If we have solved [math]\displaystyle{ \rho_t }[/math] for time [math]\displaystyle{ t\in [0, T] }[/math], then we can exactly reverse the evolution of the cloud. Suppose we start with another cloud of particles with density [math]\displaystyle{ \nu_0 = \rho_T }[/math], and let the particles in the cloud evolve according to[math]\displaystyle{ dy_t = \frac{1}{2} \beta(T-t) y_{t} d t + \beta(T-t) \underbrace{\nabla_{y_{t}} \ln \rho_{T-t}\left(y_{t}\right)}_{\text {score function }} d t+\sqrt{\beta(T-t)} d W_t }[/math]then by plugging into the Fokker-Planck equation, we find that [math]\displaystyle{ \partial_t \rho_{T-t} = \partial_t \nu_t }[/math]. Thus this cloud of points is the original cloud, evolving backwards.[16]

Noise conditional score network (NCSN)

At the continuous limit, [math]\displaystyle{ \bar \alpha_t = (1-\beta_1) \cdots (1-\beta_t) = e^{\sum_i \ln(1-\beta_i)} \to e^{-\int_0^t \beta(t)dt} }[/math]and so [math]\displaystyle{ x_{t}|x_0 \sim N\left(e^{-\frac 12\int_0^t \beta(t)dt} x_{0}, \left(1- e^{-\int_0^t \beta(t)dt}\right) I \right) }[/math]In particular, we see that we can directly sample from any point in the continuous diffusion process without going through the intermediate steps, by first sampling [math]\displaystyle{ x_0 \sim q, z \sim N(0, I) }[/math], then get [math]\displaystyle{ x_t = e^{-\frac 12\int_0^t \beta(t)dt} x_{0} + \left(1- e^{-\int_0^t \beta(t)dt}\right) z }[/math]. That is, we can quickly sample [math]\displaystyle{ x_t \sim \rho_t }[/math] for any [math]\displaystyle{ t \geq 0 }[/math].

Now, define a certain probability distribution [math]\displaystyle{ \gamma }[/math] over [math]\displaystyle{ [0, \infty) }[/math], then the score-matching loss function is defined as the expected Fisher divergence:[math]\displaystyle{ L(\theta) = E_{t\sim \gamma, x_t \sim \rho_t}[\|f_\theta(x_t, t)\|^2 + 2\nabla\cdot f_\theta(x_t, t)] }[/math]After training, [math]\displaystyle{ f_\theta(x_t, t) \approx \nabla \ln\rho_t }[/math], so we can perform the backwards diffusion process by first sampling [math]\displaystyle{ x_T \sim N(0, I) }[/math], then integrating the SDE from [math]\displaystyle{ t=T }[/math] to [math]\displaystyle{ t=0 }[/math]:[math]\displaystyle{ x_{t-dt}=x_t + \frac{1}{2} \beta(t) x_{t} d t + \beta(t) f_\theta(x_t, t) d t+\sqrt{\beta(t)} d W_t }[/math]This may be done by any SDE integration method, such as Euler–Maruyama method.

The name "noise conditional score network" is explained thus:

  • "network", because [math]\displaystyle{ f_\theta }[/math] is implemented as a neural network.
  • "score", because the output of the network is interpreted as approximating the score function [math]\displaystyle{ \nabla\ln\rho_t }[/math].
  • "noise conditional", because [math]\displaystyle{ \rho_t }[/math] is equal to [math]\displaystyle{ \rho_0 }[/math] blurred by an added gaussian noise that increases with time, and so the score function depends on the amount of noise added.

Their equivalence

DDPM and score-based generative model are equivalent.[17] This means that a network trained using DDPM can be used as a NCSN, and vice versa.

We know that [math]\displaystyle{ x_{t}|x_0 \sim N\left(\sqrt{\bar\alpha_t} x_{0}, (1-\bar\alpha_t) I\right) }[/math], so by Tweedie's formula, we have[math]\displaystyle{ \nabla_{x_t}\ln q(x_t) = \frac{1}{1-\bar\alpha_t}(-x_t + \sqrt{\bar\alpha_t} E_q[x_0|x_t]) }[/math]As described previously, the DDPM loss function is [math]\displaystyle{ \sum_t L_{simple, t} }[/math] with[math]\displaystyle{ L_{simple, t} = E_{x_0\sim q; z \sim N(0, I)}\left[ \left\| \epsilon_\theta(x_t, t) - z \right\|^2\right] }[/math]where [math]\displaystyle{ x_t =\sqrt{\bar\alpha_t} x_{0} + \sqrt{1-\bar\alpha_t}z }[/math]. By a change of variables,[math]\displaystyle{ L_{simple, t} = E_{x_0, x_t\sim q}\left[ \left\| \epsilon_\theta(x_t, t) - \frac{x_t -\sqrt{\bar\alpha_t} x_{0}}{\sqrt{1-\bar\alpha_t}} \right\|^2\right] = E_{x_t\sim q, x_0\sim q(\cdot | x_t)}\left[ \left\| \epsilon_\theta(x_t, t) - \frac{x_t -\sqrt{\bar\alpha_t} x_{0}}{\sqrt{1-\bar\alpha_t}} \right\|^2\right] }[/math]and the term inside becomes a least squares regression, so if the network actually reaches the global minimum of loss, then we have [math]\displaystyle{ \epsilon_\theta(x_t, t) = \frac{x_t -\sqrt{\bar\alpha_t} E_q[x_0|x_t]}{\sqrt{1-\bar\alpha_t}} = -\sqrt{1-\bar\alpha_t}\nabla_{x_t}\ln q(x_t) }[/math].

Now, the continuous limit [math]\displaystyle{ x_{t-1} = x_{t-dt}, \beta_t = \beta(t) dt, z_t\sqrt{dt} = dW_t }[/math] of the backward equation[math]\displaystyle{ x_{t-1} = \frac{x_t}{\sqrt{\alpha_t}}- \frac{ \beta_t}{\sqrt{\alpha_t (1-\bar\alpha_t)}} \epsilon_\theta(x_t, t) + \sqrt{\beta_t} z_t; \quad z_t \sim N(0, I) }[/math]gives us precisely the same equation as score-based diffusion:[math]\displaystyle{ x_{t-dt} = x_t(1+\beta(t)dt / 2) + \beta(t) \nabla_{x_t}\ln q(x_t) dt + \sqrt{\beta(t)}dW_t }[/math]

Main variants

Denoising Diffusion Implicit Model (DDIM)

The original DDPM method for generating images is slow, since the forward diffusion process usually takes [math]\displaystyle{ T \sim 1000 }[/math] to make the distribution of [math]\displaystyle{ x_T }[/math] to appear close to gaussian. However this means the backward diffusion process also take 1000 steps. Unlike the forward diffusion process, which can skip steps as [math]\displaystyle{ x_t | x_0 }[/math] is gaussian for all [math]\displaystyle{ t \geq 1 }[/math], the backward diffusion process does not allow skipping steps. For example, to sample [math]\displaystyle{ x_{t-2}|x_{t-1} \sim N(\mu_\theta(x_{t-1}, t-1), \Sigma_\theta(x_{t-1}, t-1)) }[/math] requires the model to first sample [math]\displaystyle{ x_{t-1} }[/math]. Attempting to directly sample [math]\displaystyle{ x_{t-2}|x_t }[/math] would require us to marginalize out [math]\displaystyle{ x_{t-1} }[/math], which is generally intractable.

DDIM[18] is a method to take any model trained on DDPM loss, and use it to sample with some steps skipped, sacrificing an adjustable amount of quality. The original DDPM is a special case of DDIM.

Latent diffusion model (LDM)

Since the diffusion model is a general method for modelling probability distributions, if one wants to model a distribution over images, one can first encode the images into a lower-dimensional space by an encoder, then use a diffusion model to model the distribution over encoded images. Then to generate an image, one can sample from the diffusion model, then use a decoder to decode it into an image.[19]

The encoder-decoder pair is most often a variational autoencoder (VAE).

Classifier guidance

Suppose we wish to sample not from the entire distribution of images, but conditional on the image description. We don't want to sample a generic image, but an image that fits the description "black cat with red eyes". Generally, we want to sample from the distribution [math]\displaystyle{ p(x|y) }[/math], where [math]\displaystyle{ x }[/math] ranges over images, and [math]\displaystyle{ y }[/math] ranges over classes of images (a description "black cat with red eyes" is just a very detailed class, and a class "cat" is just a very vague description).

Taking the perspective of the noisy channel model, we can understand the process as follows: To generate an image [math]\displaystyle{ x }[/math] conditional on description [math]\displaystyle{ y }[/math], we imagine that the requester really had in mind an image [math]\displaystyle{ x }[/math], but the image is passed through a noisy channel and came out garbled, as [math]\displaystyle{ y }[/math]. Image generation is then nothing but inferring which [math]\displaystyle{ x }[/math] the requester had in mind.

In other words, conditional image generation is simply "translating from a textual language into a pictorial language". Then, as in noisy-channel model, we use Bayes theorem to get[math]\displaystyle{ p(x|y) \propto p(y|x)p(x) }[/math]in other words, if we have a good model of the space of all images, and a good image-to-class translator, we get a class-to-image translator "for free". In the equation for backward diffusion, the score [math]\displaystyle{ \nabla \ln p(x) }[/math] can be replaced by[math]\displaystyle{ \nabla_x \ln p(x|y) = \nabla_x \ln p(y|x) + \nabla_x \ln p(x) }[/math]where [math]\displaystyle{ \nabla_x \ln p(x) }[/math] is the score function, trained as previously described, and [math]\displaystyle{ \nabla_x \ln p(y|x) }[/math] is found by using a differentiable image classifier.

With temperature

The classifier-guided diffusion model samples from [math]\displaystyle{ p(x|y) }[/math], which is concentrated around the maximum a posteriori estimate [math]\displaystyle{ \arg\max_x p(x|y) }[/math]. If we want to force the model to move towards the maximum likelihood estimate [math]\displaystyle{ \arg\max_x p(y|x) }[/math], we can use [math]\displaystyle{ p_\beta(x|y) \propto p(y|x)^\beta p(x) }[/math]where [math]\displaystyle{ \beta \gt 0 }[/math] is interpretable as inverse temperature. In the context of diffusion models, it is usually called the guidance scale. A high [math]\displaystyle{ \beta }[/math] would force the model to sample from a distribution concentrated around [math]\displaystyle{ \arg\max_x p(y|x) }[/math]. This often improves quality of generated images.[20]

This can be done simply by SGLD with[math]\displaystyle{ \nabla_x \ln p_\beta(x|y) = \beta\nabla_x \ln p(y|x) + \nabla_x \ln p(x) }[/math]

Classifier-free guidance (CFG)

If we do not have a classifier [math]\displaystyle{ p(y|x) }[/math], we could still extract one out of the image model itself:[21][math]\displaystyle{ \nabla_x \ln p_\beta(x|y) = (1-\beta) \nabla_x \ln p(x) + \beta \nabla_x \ln p(x|y) }[/math]Such a model is usually trained by presenting it with both [math]\displaystyle{ (x, y) }[/math] and [math]\displaystyle{ (x, {\rm None}) }[/math], allowing it to model both [math]\displaystyle{ \nabla_x\ln p(x|y) }[/math] and [math]\displaystyle{ \nabla_x\ln p(x) }[/math].

Samplers

Given a diffusion model, one may regard it either as a continuous process, and sample from it by integrating a SDE, or one can regard it as a discrete process, and sample from it by iterating the discrete steps. The choice of the "noise schedule" [math]\displaystyle{ \beta_t }[/math] can also affect the quality of samples. In the DDPM perspective, one can use the DDPM itself (with noise), or DDIM (with adjustable amount of noise). The case where one adds noise is sometimes called ancestral sampling.[22] One can interpolate between noise and no noise. The amount of noise is denoted [math]\displaystyle{ \eta }[/math] ("eta value") in the DDIM paper, with [math]\displaystyle{ \eta = 0 }[/math] denoting no noise (as in deterministic DDIM), and [math]\displaystyle{ \eta = 1 }[/math] denoting full noise (as in DDPM).

In the perspective of SDE, one can use any of the numerical integration methods, such as Euler–Maruyama method, Heun's method, linear multistep methods, etc. Just as in the discrete case, one can add an adjustable amount of noise during the integration.

A survey and comparison of samplers in the context of image generation is in.[23]

Choice of architecture

File:U-net-architecture.png
The standard U-Net architecture.
Architecture of Stable Diffusion.
The denoising process used by Stable Diffusion.

Diffusion model

For generating images by DDPM, we need a neural network that takes a time [math]\displaystyle{ t }[/math] and a noisy image [math]\displaystyle{ x_t }[/math], and predicts a noise [math]\displaystyle{ \epsilon_\theta(x_t, t) }[/math] from it. Since predicting the noise is the same as predicting the denoised image, then subtracting it from [math]\displaystyle{ x_t }[/math], denoising architectures tend to work well. For example, the most common architecture is U-Net, which is also good at denoising images.[24]

For non-image data, we can use other architectures. For example,[25] models human motion trajectory by DDPM. Each human motion trajectory is a sequence of poses, represented by either joint rotations or positions. It uses a Transformer network to generate a less noisy trajectory out of a noisy one.

Conditioning

The base diffusion model can only generate unconditionally from the whole distribution. For example, a diffusion model learned on ImageNet would generate images that look like a random image from ImageNet. To generate images from just one category, one would need to impose the condition. Whatever condition one wants to impose, one needs to first convert the conditioning into a vector of floating point numbers, then feed it into the underlying diffusion model neural network. However, one has freedom in choosing how to convert the conditioning into a vector.

Stable Diffusion, for example, imposes conditioning in the form of cross-attention mechanism, where the query is an intermediate representation of the image in the U-Net, and both key and value are the conditioning vectors.[26] The conditioning can be selectively applied to only parts of an image, and new kinds of conditionings can be finetuned upon the base model, as used in ControlNet.[26]

As a particularly simple example, consider image inpainting. The conditions are [math]\displaystyle{ \tilde x }[/math], the reference image, and [math]\displaystyle{ m }[/math], the inpainting mask. The conditioning is imposed at each step of the backward diffusion process, by first sampling [math]\displaystyle{ \tilde x_t \sim N\left(\sqrt{\bar\alpha_t} \tilde x, (1-\bar\alpha_t) I \right) }[/math], a noisy version of [math]\displaystyle{ \tilde x }[/math], then replacing [math]\displaystyle{ x_t }[/math] with [math]\displaystyle{ (1-m) \odot x_t + m \odot \tilde x_t }[/math], where [math]\displaystyle{ \odot }[/math] means elementwise multiplication.[27]

Conditioning is not limited to just generating images from a specific category, or according to a specific caption (as in text-to-image). For example,[25] demonstrated generating human motion, conditioned on an audio clip of human walking (allowing syncing motion to a soundtrack), or video of human running, or a text description of human motion, etc.

Upscaling

As generating an image takes a long time, one can try to generate a small image by a base diffusion model, then upscale it by other models. Upscaling can be done by GAN,[28] Transformer,[29] or signal processing methods like Lanczos resampling.

Diffusion models themselves can be used to perform upscaling. Cascading diffusion model stacks multiple diffusion models one after another, in the style of Progressive GAN. The lowest level is a standard diffusion model that generate 32x32 image, then the image would be upscaled by a diffusion model specifically trained for upscaling, and the process repeats.[24]

Examples

This section collects some notable diffusion models, and briefly describes their architecture.

OpenAI

Main page: Software:DALL-E

The DALL-E series by OpenAI are text-conditional diffusion models of images.

The first version of DALL-E (2021) is not actually a diffusion model. Instead, it uses a Transformer architecture that generates a sequence of tokens, which is then converted to an image by the decoder of a discrete VAE. Released with DALL-E was the CLIP classifier, which was used by DALL-E to rank generated images according to how close the image fits the text.

GLIDE (2022-03)[30] is a 3.5-billion diffusion model, and a small version was released publicly.[4] Soon after, DALL-E 2 was released (2022-04).[31] DALL-E 2 is a 3.5-billion cascaded diffusion model that generates images from text by "inverting the CLIP image encoder", the technique which they termed "unCLIP".

Stability AI

Main page: Software:Stable Diffusion

Stable Diffusion (2022-08), released by Stability AI, consists of a latent diffusion model (860 million parameters), a VAE, and a text encoder. The diffusion model is a U-Net, with cross-attention blocks to allow for conditional image generation.[32][19]

See also

Further reading

References

  1. Chang, Ziyi; Koulieris, George Alex; Shum, Hubert P. H. (2023). "On the Design Fundamentals of Diffusion Models: A Survey". arXiv:2306.04542 [cs.LG].
  2. 2.0 2.1 Song, Yang; Sohl-Dickstein, Jascha; Kingma, Diederik P.; Kumar, Abhishek; Ermon, Stefano; Poole, Ben (2021-02-10). "Score-Based Generative Modeling through Stochastic Differential Equations". arXiv:2011.13456 [cs.LG].
  3. Gu, Shuyang; Chen, Dong; Bao, Jianmin; Wen, Fang; Zhang, Bo; Chen, Dongdong; Yuan, Lu; Guo, Baining (2021). "Vector Quantized Diffusion Model for Text-to-Image Synthesis". arXiv:2111.14822 [cs.CV].
  4. 4.0 4.1 GLIDE, OpenAI, 2023-09-22, https://github.com/openai/glide-text2im, retrieved 2023-09-24 
  5. Li, Yifan; Zhou, Kun; Zhao, Wayne Xin; Wen, Ji-Rong (August 2023). "Diffusion Models for Non-autoregressive Text Generation: A Survey". Proceedings of the Thirty-Second International Joint Conference on Artificial Intelligence (California: International Joint Conferences on Artificial Intelligence Organization). doi:10.24963/ijcai.2023/750. http://dx.doi.org/10.24963/ijcai.2023/750. 
  6. Han, Xiaochuang; Kumar, Sachin; Tsvetkov, Yulia (2023). "SSD-LM: Semi-autoregressive Simplex-based Diffusion Language Model for Text Generation and Modular Control". Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers) (Stroudsburg, PA, USA: Association for Computational Linguistics). doi:10.18653/v1/2023.acl-long.647. http://dx.doi.org/10.18653/v1/2023.acl-long.647. 
  7. Xu, Weijie; Hu, Wenxiang; Wu, Fanyou; Sengamedu, Srinivasan (2023). "DeTiME: Diffusion-Enhanced Topic Modeling using Encoder-decoder based LLM". Findings of the Association for Computational Linguistics: EMNLP 2023 (Stroudsburg, PA, USA: Association for Computational Linguistics). doi:10.18653/v1/2023.findings-emnlp.606. http://dx.doi.org/10.18653/v1/2023.findings-emnlp.606. 
  8. Zhang, Haopeng; Liu, Xiao; Zhang, Jiawei (2023). "DiffuSum: Generation Enhanced Extractive Summarization with Diffusion". Findings of the Association for Computational Linguistics: ACL 2023 (Stroudsburg, PA, USA: Association for Computational Linguistics). doi:10.18653/v1/2023.findings-acl.828. http://dx.doi.org/10.18653/v1/2023.findings-acl.828. 
  9. 9.0 9.1 Ho, Jonathan; Jain, Ajay; Abbeel, Pieter (2020). "Denoising Diffusion Probabilistic Models". Advances in Neural Information Processing Systems (Curran Associates, Inc.) 33: 6840–6851. https://proceedings.neurips.cc/paper/2020/hash/4c5bcfec8584af0d967f1ab10179ca4b-Abstract.html. 
  10. Croitoru, Florinel-Alin; Hondru, Vlad; Ionescu, Radu Tudor; Shah, Mubarak (2023). "Diffusion Models in Vision: A Survey". IEEE Transactions on Pattern Analysis and Machine Intelligence 45 (9): 10850–10869. doi:10.1109/TPAMI.2023.3261988. PMID 37030794. 
  11. Sohl-Dickstein, Jascha; Weiss, Eric; Maheswaranathan, Niru; Ganguli, Surya (2015-06-01). "Deep Unsupervised Learning using Nonequilibrium Thermodynamics" (in en). Proceedings of the 32nd International Conference on Machine Learning (PMLR) 37: 2256–2265. http://proceedings.mlr.press/v37/sohl-dickstein15.pdf. 
  12. Weng, Lilian (2021-07-11). "What are Diffusion Models?" (in en). https://lilianweng.github.io/posts/2021-07-11-diffusion-models/. 
  13. "Generative Modeling by Estimating Gradients of the Data Distribution | Yang Song". https://yang-song.net/blog/2021/score/. 
  14. Song, Yang; Sohl-Dickstein, Jascha; Kingma, Diederik P.; Kumar, Abhishek; Ermon, Stefano; Poole, Ben (2021-02-10). "Score-Based Generative Modeling through Stochastic Differential Equations". arXiv:2011.13456 [cs.LG].
  15. "Sliced Score Matching: A Scalable Approach to Density and Score Estimation | Yang Song". https://yang-song.net/blog/2019/ssm/. 
  16. Anderson, Brian D.O. (May 1982). "Reverse-time diffusion equation models". Stochastic Processes and Their Applications 12 (3): 313–326. doi:10.1016/0304-4149(82)90051-5. ISSN 0304-4149. http://dx.doi.org/10.1016/0304-4149(82)90051-5. 
  17. Luo, Calvin (2022). Understanding Diffusion Models: A Unified Perspective. 
  18. Song, Jiaming; Meng, Chenlin; Ermon, Stefano (2020). Denoising Diffusion Implicit Models. 
  19. 19.0 19.1 Rombach, Robin; Blattmann, Andreas; Lorenz, Dominik; Esser, Patrick; Ommer, Björn (2022) (in en). High-Resolution Image Synthesis With Latent Diffusion Models. pp. 10684–10695. https://openaccess.thecvf.com/content/CVPR2022/html/Rombach_High-Resolution_Image_Synthesis_With_Latent_Diffusion_Models_CVPR_2022_paper.html. 
  20. Dhariwal, Prafulla; Nichol, Alex (2021-06-01). "Diffusion Models Beat GANs on Image Synthesis". arXiv:2105.05233 [cs.LG].
  21. Ho, Jonathan; Salimans, Tim (2022-07-25). "Classifier-Free Diffusion Guidance". arXiv:2207.12598 [cs.LG].
  22. Yang, Ling; Zhang, Zhilong; Song, Yang; Hong, Shenda; Xu, Runsheng; Zhao, Yue; Zhang, Wentao; Cui, Bin et al. (2022). Diffusion Models: A Comprehensive Survey of Methods and Applications. 
  23. Karras, Tero; Aittala, Miika; Aila, Timo; Laine, Samuli (2022). Elucidating the Design Space of Diffusion-Based Generative Models. 
  24. 24.0 24.1 Ho, Jonathan; Saharia, Chitwan; Chan, William; Fleet, David J.; Norouzi, Mohammad; Salimans, Tim (2022-01-01). "Cascaded diffusion models for high fidelity image generation". The Journal of Machine Learning Research 23 (1): 47:2249–47:2281. ISSN 1532-4435. https://dl.acm.org/doi/abs/10.5555/3586589.3586636. 
  25. 25.0 25.1 Tevet, Guy; Raab, Sigal; Gordon, Brian; Shafir, Yonatan; Cohen-Or, Daniel; Bermano, Amit H. (2022). Human Motion Diffusion Model. 
  26. 26.0 26.1 Zhang, Lvmin; Rao, Anyi; Agrawala, Maneesh (2023). Adding Conditional Control to Text-to-Image Diffusion Models. 
  27. Lugmayr, Andreas; Danelljan, Martin; Romero, Andres; Yu, Fisher; Timofte, Radu; Van Gool, Luc (2022) (in en). RePaint: Inpainting Using Denoising Diffusion Probabilistic Models. pp. 11461–11471. https://openaccess.thecvf.com/content/CVPR2022/html/Lugmayr_RePaint_Inpainting_Using_Denoising_Diffusion_Probabilistic_Models_CVPR_2022_paper.html. 
  28. Wang, Xintao; Xie, Liangbin; Dong, Chao; Shan, Ying (2021) (in en). Real-ESRGAN: Training Real-World Blind Super-Resolution With Pure Synthetic Data. pp. 1905–1914. https://openaccess.thecvf.com/content/ICCV2021W/AIM/html/Wang_Real-ESRGAN_Training_Real-World_Blind_Super-Resolution_With_Pure_Synthetic_Data_ICCVW_2021_paper.html. 
  29. Liang, Jingyun; Cao, Jiezhang; Sun, Guolei; Zhang, Kai; Van Gool, Luc; Timofte, Radu (2021) (in en). SwinIR: Image Restoration Using Swin Transformer. pp. 1833–1844. https://openaccess.thecvf.com/content/ICCV2021W/AIM/html/Liang_SwinIR_Image_Restoration_Using_Swin_Transformer_ICCVW_2021_paper.html. 
  30. Nichol, Alex; Dhariwal, Prafulla; Ramesh, Aditya; Shyam, Pranav; Mishkin, Pamela; McGrew, Bob; Sutskever, Ilya; Chen, Mark (2022-03-08). "GLIDE: Towards Photorealistic Image Generation and Editing with Text-Guided Diffusion Models". arXiv:2112.10741 [cs.CV].
  31. Ramesh, Aditya; Dhariwal, Prafulla; Nichol, Alex; Chu, Casey; Chen, Mark (2022-04-12). "Hierarchical Text-Conditional Image Generation with CLIP Latents". arXiv:2204.06125 [cs.CV].
  32. Alammar, Jay. "The Illustrated Stable Diffusion". https://jalammar.github.io/illustrated-stable-diffusion/.