Diffusion model

Source: Wikipedia, the free encyclopedia.

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 a probability distribution for a given dataset from which we can then sample new images. 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

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 . 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 , with pdf . This is just the Maxwell–Boltzmann distribution of particles in a potential well 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.

  • are fixed constants.
  • is the normal distribution with mean and variance , and is the probability density at .
  • A vertical bar denotes conditioning.

A forward diffusion process starts at some starting point , where is the probability distribution to be learned, then repeatedly adds noise to it by

where are IID samples from . This is designed so that for any starting distribution of , we have converging to .

The entire diffusion process then satisfies

or
where is a normalization constant and often omitted. In particular, we note that is a gaussian process, which affords us considerable freedom in reparameterization. For example, by standard manipulation with gaussian process,
In particular, notice that for large , the variable converges to . That is, after a long enough diffusion process, we end up with some that is very close to , with all traces of the original gone.

For example, since

we can sample directly "in one step", instead of going through all the intermediate steps .

Derivation by reparameterization

We know is a gaussian, and is another gaussian. We also know that these are independent. Thus we can perform a reparameterization:

where are IID gaussians.

There are 5 variables and two linear equations. The two sources of randomness are , which can be reparameterized by rotation, since the IID gaussian distribution is rotationally symmetric.

By plugging in the equations, we can solve for the first reparameterization:

where is a gaussian with mean zero and variance one.

To find the second one, we complete the rotational matrix:

Since rotational matrices are all of the form , we know the matrix must be

and since the inverse of rotational matrix is its transpose,

Plugging back, and simplifying, we have

Backward diffusion

The key idea of DDPM is to use a neural network parametrized by . The network takes in two arguments , and outputs a vector and a matrix , such that each step in the forward diffusion process can be approximately undone by . This then gives us a backward diffusion process defined by

The goal now is to learn the parameters such that is as close to as possible. To do that, we use maximum likelihood estimation with variational inference.

Variational inference

The ELBO inequality states that , and taking one more expectation, we get

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

and now the goal is to minimize the loss by stochastic gradient descent. The expression may be simplified to[12]
where does not depend on the parameter, and thus can be ignored. Since also does not depend on the parameter, the term can also be ignored. This leaves just with to be minimized.

Noise prediction network

Since , this suggests that we should use ; however, the network does not have access to , and so it has to estimate it instead. Now, since , we may write , where is some unknown gaussian noise. Now we see that estimating is equivalent to estimating .

Therefore, let the network output a noise vector , and let it predict

It remains to design . The DDPM paper suggested not learning it (since it resulted in "unstable training and poorer sample quality"), but fixing it at some value , where either yielded similar performance.

With this, the loss simplifies to

which may be minimized by stochastic gradient descent. The paper noted empirically that an even simpler loss function
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 represent an image, and let be the probability distribution over all possible images. If we have 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 itself, but rather, . This has two major effects:

  • One, we no longer need to normalize , but can use any , where is any unknown constant that is of no concern to us.
  • Two, we are comparing neighbors , by

Let the

score function
be ; then consider what we can do with .

As it turns out, allows us to sample from using thermodynamics. Specifically, if we have a potential energy function , and a lot of particles in the potential well, then the distribution at thermodynamic equilibrium is the Boltzmann distribution . At temperature , the Boltzmann distribution is exactly .

Therefore, to model , 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

and the Boltzmann distribution is, by Fokker-Planck equation, the unique thermodynamic equilibrium. So no matter what distribution has, the distribution of converges in distribution to as .

Learning the score function

Given a density , we wish to learn a score function approximation . This is score matching.[15] Typically, score matching is formalized as minimizing Fisher divergence function . By expanding the integral, and performing an integration by parts,

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 , a white-noise image. Now, most white-noise images do not look like real images, so for large swaths of . 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 at that point, then we cannot impose the time-evolution equation on a particle:

To deal with this problem, we perform annealing. If 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:

By taking the limit, we obtain a continuous diffusion process, in the form of a stochastic differential equation:
where is a Wiener process (multidimensional Brownian motion).

Now, the equation is exactly a special case of the overdamped Langevin equation

where is diffusion tensor, is temperature, and is potential energy field. If we substitute in , 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 at time , then after a long time, the cloud of particles would settle into the stable distribution of . Let be the density of the cloud of particles at time , then we have

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

where is the dimension of space, and is the Laplace operator.

Backward diffusion process

If we have solved for time , then we can exactly reverse the evolution of the cloud. Suppose we start with another cloud of particles with density , and let the particles in the cloud evolve according to

then by plugging into the Fokker-Planck equation, we find that . Thus this cloud of points is the original cloud, evolving backwards.[16]

Noise conditional score network (NCSN)

At the continuous limit,

and so
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 , then get . That is, we can quickly sample for any .

Now, define a certain probability distribution over , then the score-matching loss function is defined as the expected Fisher divergence:

After training, , so we can perform the backwards diffusion process by first sampling , then integrating the SDE from to :
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 is implemented as a neural network.
  • "score", because the output of the network is interpreted as approximating the score function .
  • "noise conditional", because is equal to 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 models are equivalent.[17] This means that a network trained using DDPM can be used as a NCSN, and vice versa.

We know that , so by Tweedie's formula, we have

As described previously, the DDPM loss function is with
where . By a change of variables,
and the term inside becomes a least squares regression, so if the network actually reaches the global minimum of loss, then we have .

Now, the continuous limit of the backward equation

gives us precisely the same equation as score-based diffusion:

Main variants

Denoising Diffusion Implicit Model (DDIM)

The original DDPM method for generating images is slow, since the forward diffusion process usually takes to make the distribution of 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 is gaussian for all , the backward diffusion process does not allow skipping steps. For example, to sample requires the model to first sample . Attempting to directly sample would require us to marginalize out , 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. If we generate the Markovian chain case in DDPM to non-Markovian case, DDIM corresponds to the case that the reverse process has variance equals to 0. In other words, the reverse process (and also the forward process) is deterministic. When using fewer sampling steps, DDIM outperforms DDPM.

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 , where ranges over images, and 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 conditional on description , we imagine that the requester really had in mind an image , but the image is passed through a noisy channel and came out garbled, as . Image generation is then nothing but inferring which 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

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 can be replaced by
where is the score function, trained as previously described, and is found by using a differentiable image classifier.

With temperature

The classifier-guided diffusion model samples from , which is concentrated around the maximum a posteriori estimate . If we want to force the model to move towards the maximum likelihood estimate , we can use

where is interpretable as inverse temperature. In the context of diffusion models, it is usually called the guidance scale. A high would force the model to sample from a distribution concentrated around . This often improves quality of generated images.[20]

This can be done simply by SGLD with

Classifier-free guidance (CFG)

If we do not have a classifier , we could still extract one out of the image model itself:[21]

Such a model is usually trained by presenting it with both and , allowing it to model both and .

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" 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 ("eta value") in the DDIM paper, with denoting no noise (as in deterministic DDIM), and 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]

Flow-based diffusion model

Abstractly speaking, the idea of diffusion model is to take an unknown probability distribution (the distribution of natural-looking images), then progressively convert it to a known probability distribution (standard gaussian distribution), then learn a neural network that reverses the process.

In denoising diffusion models, the forward process adds noise, and the backward process removes noise. Both the forward and backward processes are SDEs, though the forward process is integrable in closed-form, so it can be done at no computational cost. The backward process is not integrable in closed-form, so it must be integrated step-by-step by standard SDE solvers, which can be very expensive.

In flow-based diffusion models, the forward process is a both deterministic flow along a time-dependent vector field, and the backward process is the same vector field, but going backwards. Both processes are solutions to ODEs. If the vector field is well-behaved, the ODE will also be well-behaved.

Given two distributions and , a flow-based model is a time-dependent velocity field in , such that if we start by sampling a point , and let it move according to the velocity field:

we end up with a point .

Rectified flow

Given two distributions and , there are infinitely many possible velocity fields to transport between them. Some are more well-behaved than others. The idea of rectified flow[24][25] is to learn a flow model such that the velocity is nearly constant along each flow path. This is beneficial, because we can integrate along such a vector field with very few steps. For example, if an ODE follows perfectly straight paths, it simplifies to , allowing for exact solutions in one step. In practice, we cannot reach such perfection, but when the flow field is nearly so, we can take a few large steps instead of many little steps.

The general idea is to start with two distributions and , then construct a flow field from it, then repeatedly apply a "reflow" operation to obtain successive flow fields , each straighter than the previous one. When the flow field is straight enough for the application, we stop. See the images in [26] for intuition.

Generally, for any time-differentiable process , can be estimated by solving:


In rectified flow, by injecting strong priors that intermediate trajectories are straight, it can achieve both theoretical relevance for optimal transport and computational efficiency, as ODEs with straight paths can be simulated precisely without time discretization.

Specifically, rectified flow seeks to match an ODE with the marginal distributions of the linear interpolation between points from distributions and . Given observations and , the canonical linear interpolation yields a trivial case , which cannot be causally simulated without . To address this, is "projected" into a space of causally simulatable ODEs, expressed as , by minimizing the least squares loss with respect to the direction :

The data pair can be any coupling of and , typically independent (i.e., ) obtained by randomly combining observations from and . This process ensures that the trajectories closely mirror the density map of trajectories but reroute at intersections to ensure causality. This rectifying process is also known as Flow Matching,[27] Stochastic Interpolation,[28] and Alpha-Blending.[citation needed]

A distinctive aspect of rectified flow is its capability for "reflow", which straightens the trajectory of ODE paths. Denote the rectified flow induced from as . Recursively applying this operator generates a series of rectified flows , starting with , where is the -th iteration of rectified flow induced from . This "reflow" process not only reduces transport costs but also straightens the paths of rectified flows, making paths straighter with increasing .

Rectified flow includes a nonlinear extension where linear interpolation is replaced with any time-differentiable curve that connects and , given by . This framework encompasses DDIM and probability flow ODEs as special cases, with particular choices of and . However, in the case where the path of is not straight, the reflow process no longer ensures a reduction in convex transport costs, and also no longer straighten the paths of .[24]

Choice of 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 and a noisy image , and predicts a noise from it. Since predicting the noise is the same as predicting the denoised image, then subtracting it from , denoising architectures tend to work well. For example, the U-Net, which was found to be good for denoising images, is often used for denoising diffusion models that generate images.[29]

For DDPM, the underlying architecture does not have to be a U-Net. It just has to predict the noise somehow. For example, the diffusion transformer (DiT) uses a Transformer to predict the mean and diagonal covariance of the noise, given the textual conditioning and the partially denoised image. It is the same as standard U-Net-based denoising diffusion model, with a Transformer replacing the U-Net.[30]

DDPM can be used to model general data distributions, not just natural-looking images. For example, Human Motion Diffusion[31] 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. 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.[32]

As a particularly simple example, consider image inpainting. The conditions are , the reference image, and , the inpainting mask. The conditioning is imposed at each step of the backward diffusion process, by first sampling , a noisy version of , then replacing with , where means elementwise multiplication.[33]

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,[31] 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

Transformer,[35] 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.[29]

In more detail, the diffusion upscaler is trained as follows:[29]

  • Sample , where is the high-resolution image, is the same image but scaled down to a low-resolution, and is the conditioning, which can be the caption of the image, the class of the image, etc.
  • Sample two white noises , two time-steps . Compute the noisy versions of the high-resolution and low-resolution images: .
  • Train the denoising network to predict given . That is, apply gradient descent on on the L2 loss .

Examples

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

OpenAI

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)[36] is a 3.5-billion diffusion model, and a small version was released publicly.[4] Soon after, DALL-E 2 was released (2022-04).[37] 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".

Sora (2024-02) is a diffusion Transformer model (DiT).

Stability AI

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

Stable Diffusion 3 (2024-02)[39] changed the latent diffusion model from the UNet to a Transformer model, and so it is a DiT. It uses rectified flow.

Google Imagen

Imagen (2022-05)[40][41] uses a T5 language emodel to encode the input text into embeddings. It is a cascaded diffusion model with three steps. The first step denoises a white noise to a 64×64 image, conditional on text embedding. The second step upscales the image by 64×64→256×256, conditional on text embedding. The third step is similar, upscaling by 256×256→1024×1024. The three denoising networks are all U-Nets.

Imagen 2 (2023-12) is also diffusion-based. It can generate images based on a prompt that mixes images and text. No further information available.[42]

See also

Further reading

  • Guidance: a cheat code for diffusion models. Overview of classifier guidance and classifier-free guidance, light on mathematical details.
  • Mathematical details omitted in the article.
    • "Power of Diffusion Models". AstraBlog. 2022-09-25. Retrieved 2023-09-25.
    • Weng, Lilian (2021-07-11). "What are Diffusion Models?". lilianweng.github.io. Retrieved 2023-09-25.

References

  1. ].
  2. ^ ].
  3. ].
  4. ^ a b GLIDE, OpenAI, 2023-09-22, retrieved 2023-09-24
  5. .
  6. .
  7. .
  8. .
  9. ^ a b Ho, Jonathan; Jain, Ajay; Abbeel, Pieter (2020). "Denoising Diffusion Probabilistic Models". Advances in Neural Information Processing Systems. 33. Curran Associates, Inc.: 6840–6851.
  10. S2CID 252199918
    .
  11. ^ Sohl-Dickstein, Jascha; Weiss, Eric; Maheswaranathan, Niru; Ganguli, Surya (2015-06-01). "Deep Unsupervised Learning using Nonequilibrium Thermodynamics" (PDF). Proceedings of the 32nd International Conference on Machine Learning. 37. PMLR: 2256–2265.
  12. ^ Weng, Lilian (2021-07-11). "What are Diffusion Models?". lilianweng.github.io. Retrieved 2023-09-24.
  13. ^ "Generative Modeling by Estimating Gradients of the Data Distribution | Yang Song". yang-song.net. Retrieved 2023-09-24.
  14. ].
  15. ^ "Sliced Score Matching: A Scalable Approach to Density and Score Estimation | Yang Song". yang-song.net. Retrieved 2023-09-24.
  16. ISSN 0304-4149
    .
  17. ].
  18. ].
  19. ^ ].
  20. ].
  21. ].
  22. ].
  23. ].
  24. ^ ].
  25. ].
  26. ^ "Rectified Flow — Rectified Flow". www.cs.utexas.edu. Retrieved 2024-04-04.
  27. ^ .
  28. ].
  29. ^ ].
  30. ].
  31. ].
  32. .
  33. .
  34. ].
  35. ].
  36. ^ Alammar, Jay. "The Illustrated Stable Diffusion". jalammar.github.io. Retrieved 2022-10-31.
  37. ^ "Imagen: Text-to-Image Diffusion Models". imagen.research.google. Retrieved 2024-04-04.
  38. .
  39. ^ "Imagen 2 - our most advanced text-to-image technology". Google DeepMind. Retrieved 2024-04-04.