Search

Search papers and lessons

Diffusion Models Beat GANs on Image Synthesis

Prafulla Dhariwal, Alex Nichol · 2021

Dhariwal and Nichol tuned a diffusion model's architecture and steered its sampling with a classifier's gradients, reaching image quality that beat the best GANs on ImageNet.

On this page

  • Colour = topic mix
  • Linear algebra
  • Probability
  • Neural network basics
  • Calculus
  • Discrete math
  • Generative models
  • ReadReady to readNot yet

TL;DR

Dhariwal and Nichol showed that a denoising diffusion model can produce images that score better than the best GAN models on the standard sample-quality measure. They did it in two steps: a careful search over the network's architecture, and classifier guidance, which nudges each denoising step toward a chosen class using the gradient of a separate classifier. On ImageNet at 256 by 256 pixels, the guided model reached an FID of 4.59, the best reported at the time for that resolution.

Builds on: Denoising Diffusion Probabilistic Models (DDPM), Group Normalization, DDIM Sampler, Fréchet Inception Distance · Leads to: Classifier Guidance, which this paper introduces for diffusion models, and classifier-free guidance, the label-free successor

Why it mattered

For several years, the best image generators were GANs. Diffusion models had good diversity, were stable to train, and scaled well, but their images looked worse. This paper closed that gap. Its guidance method became the basis for controlling text-to-image systems: later work replaced the separate classifier with a single model trained with and without the condition, which is now classifier-free guidance. The architecture changes, especially adaptive group normalization, were adopted in later diffusion image models.

The problem

Before this paper, a diffusion model could reach high quality on some datasets, but on ImageNet, the difficult dataset of many object classes, it still trailed BigGAN-deep. The authors identified two likely causes. First, GAN architectures had been refined for years, while diffusion models still used a simpler DDPM network. Second, GANs can trade diversity for fidelity: they produce sharp samples from a narrow part of the data distribution, while diffusion models cover the whole distribution but look blurrier.

Sample quality was measured with the Fréchet Inception Distance (FID), where lower is better, and with precision and recall, which separate "the images look real" from "the images cover the data."

How it works

The method runs in three stages, in the order a sample is made.

  1. Design the denoiser. Start from the DDPM U-Net and change its architecture through a series of ablations (each change is tried alone against the same baseline, then kept or dropped), each trained on ImageNet at 128 by 128 pixels. The final model uses more attention heads with 64 channels each, attention at three resolutions (32, 16 and 8), residual blocks borrowed from BigGAN for up- and downsampling, and adaptive group normalization to inject the timestep and class information.
  2. Train a classifier on noisy images. A classifier pϕ(y∣xt)p_\phi(y \mid x_t) is trained on images with the same noise levels the diffusion model sees, so it can judge a class even when the picture is still grainy.
  3. Sample with guidance. At every denoising step, compute the classifier's gradient with respect to the current noisy image, scale it by a number ss, and shift the mean of the denoiser's Gaussian step by that amount. The step still uses the denoiser's own variance.
  4. Repeat until x0x_0 is reached. The final image has the denoiser's realism and the classifier's preferred class.

A tiny trace shows the mechanism with toy numbers, which are not from the paper. Take one pixel coordinate where the denoiser's mean is μ=0\mu = 0, its variance is Σ=0.25\Sigma = 0.25, and the classifier's gradient is g=2g = 2.

  1. Without guidance the next value is drawn around 0, so it is centred at 0.
  2. With scale s=1s = 1, the shift is sΣg=1×0.25×2=0.5s \Sigma g = 1 \times 0.25 \times 2 = 0.5, so the centre moves to 0.5.
  3. With scale s=10s = 10, the shift is 10×0.25×2=510 \times 0.25 \times 2 = 5, ten times larger. The step moves far in the classifier's direction.
  4. Check: the shift grows linearly in ss, which is why the paper's scale is a tuning knob that trades diversity for fidelity.
flowchart LR
  N["noise x_T"] --> D["denoiser step: mean mu, variance Sigma"]
  C["classifier gradient of log p(y given x_t)"] --> M["shift mean by s Sigma g"]
  D --> M
  M --> X["sample x_(t-1)"]
  X -->|repeat for each t| D

The math

Let xtx_t be the noisy image at step tt. The denoiser defines a Gaussian step

pθ(xt∣xt+1)≈N(μ,Σ),p_\theta(x_t \mid x_{t+1}) \approx \mathcal{N}(\mu, \Sigma),

with mean μ\mu and diagonal covariance Σ\Sigma, both computed by the network. The paper asks what happens if we multiply this step by a classifier probability pϕ(y∣xt)p_\phi(y \mid x_t), which is the same as conditioning on the label yy.

Step A, the log of the Gaussian step. Up to a constant that does not depend on xtx_t, the log-density is

−12 (xt−μ)⊤Σ−1(xt−μ),-\tfrac12\,(x_t - \mu)^\top \Sigma^{-1} (x_t - \mu),

where Σ−1\Sigma^{-1} is the inverse covariance.

Step B, the log of the classifier. Approximate log⁡pϕ(y∣xt)\log p_\phi(y \mid x_t) linearly around xt=μx_t = \mu. The linear term is g⊤(xt−μ)g^\top (x_t - \mu), where g=∇xtlog⁡pϕ(y∣xt)g = \nabla_{x_t} \log p_\phi(y \mid x_t) is the gradient: the direction in image space that raises the classifier's log-probability of yy fastest.

Step C, add the two logs. The product of the two densities has log

−12 (xt−μ)⊤Σ−1(xt−μ)+g⊤(xt−μ).-\tfrac12\,(x_t - \mu)^\top \Sigma^{-1} (x_t - \mu) + g^\top (x_t - \mu).

Step D, complete the square. Expand the quadratic around a new centre μ+Σg\mu + \Sigma g:

−12 (xt−μ−Σg)⊤Σ−1(xt−μ−Σg)=−12 (xt−μ)⊤Σ−1(xt−μ)+g⊤(xt−μ)+const.-\tfrac12\,(x_t - \mu - \Sigma g)^\top \Sigma^{-1} (x_t - \mu - \Sigma g) = -\tfrac12\,(x_t - \mu)^\top \Sigma^{-1} (x_t - \mu) + g^\top (x_t - \mu) + \text{const}.

The cross term is the check: −12⋅(−2) (xt−μ)⊤Σ−1Σg=g⊤(xt−μ)-\tfrac12 \cdot (-2)\,(x_t - \mu)^\top \Sigma^{-1} \Sigma g = g^\top (x_t - \mu), because Σ−1Σ=I\Sigma^{-1}\Sigma = I. So the sum of the two logs equals the log of a Gaussian with the same covariance and a shifted mean:

pθ,ϕ(xt∣xt+1,y)≈N(μ+Σg,Σ).p_{\theta, \phi}(x_t \mid x_{t+1}, y) \approx \mathcal{N}(\mu + \Sigma g, \Sigma).

The paper then adds a scale ss to the gradient, which is the quantity used in practice:

μ^=μ+s Σ ∇xtlog⁡pϕ(y∣xt).\hat\mu = \mu + s\,\Sigma\,\nabla_{x_t} \log p_\phi(y \mid x_t).

Check with the toy trace: μ=0\mu = 0, Σ=0.25\Sigma = 0.25, g=2g = 2, s=10s = 10 gives μ^=0+10×0.25×2=5\hat\mu = 0 + 10 \times 0.25 \times 2 = 5.

Why the scale trades diversity for fidelity

Because log⁡ps=slog⁡p\log p^s = s \log p, the gradient satisfies s∇log⁡p(y∣x)=∇log⁡p(y∣x)ss \nabla \log p(y \mid x) = \nabla \log p(y \mid x)^s exactly, so the sampler behaves as if it were drawing from a classifier distribution proportional to p(y∣x)sp(y \mid x)^s. When s>1s \gt 1, this sharpens the classifier, concentrating on the images it recognises most confidently. The paper's Figure 3 shows the effect on a set of corgi samples: at scale 1.0 the FID is 33.0, and at scale 10.0 it is 12.0. FID is computed over a whole set of samples, not one picture, so these numbers compare the two sample sets.

Architecture: the adaptive group normalization layer

The paper defines the layer as AdaGN⁡(h,y)=ys GroupNorm⁡(h)+yb\operatorname{AdaGN}(h, y) = y_s \, \operatorname{GroupNorm}(h) + y_b, where hh is the activation inside a residual block and [ys,yb][y_s, y_b] comes from a linear projection of the timestep and class embedding. Replacing it with the addition-plus-group-normalization layer from the original DDPM raises FID from 13.06 to 15.08 in the paper's ablation (Table 3; lower is better).

Results

The headline numbers, all FID on ImageNet, are 2.97 at 128 by 128 pixels, 4.59 at 256 by 256 and 7.72 at 512 by 512. With classifier guidance and an upsampling stack, the 256 result improves to 3.94 and the 512 result to 3.85. The authors also report that guided sampling matches BigGAN-deep's FID with as few as 25 denoising steps.

Table 4 isolates guidance on ImageNet at 256 by 256. The unconditional model has FID 26.21, and guidance with scale 10.0 lowers it to 12.00, while raising precision from 0.61 to 0.76 and lowering recall from 0.63 to 0.44. That is the diversity-for-fidelity trade in numbers. The conditional model has FID 10.94, and guidance at scale 1.0 gives 4.59, with precision 0.82 and recall 0.52.

Table 5 compares against other models per dataset. On LSUN Bedrooms at 256, the dropout version of the model gets FID 1.90 against 2.35 for StyleGAN. On LSUN Cats at 256 it gets 5.57 against 7.25 for StyleGAN2. On ImageNet at 64 by 64 it gets 2.61 against 4.06 for BigGAN-deep.

The limits are stated plainly. Sampling is still slower than a GAN's single forward pass, because diffusion takes many denoising steps. Classifier guidance needs labels, and the paper offers only ideas, such as synthetic labels from clustering, for unlabeled data. The ablations were run at 128 by 128 pixels, and the strongest claims use ImageNet classes and FID, which measures realism and coverage statistically rather than sample by sample.

Reading guide

  • Section 1 (Introduction) gives the motivation and the claim of state-of-the-art results. It needs GANs and the FID idea.
  • Section 2 (Background) restates the diffusion objective. Read Denoising Diffusion Probabilistic Models (DDPM) first, and DDIM Sampler for the faster sampler the paper uses.
  • Section 3 (Architecture improvements) is the ablation study. It needs Group Normalization and the notion of an ablation.
  • Section 4 (Classifier guidance) is the core idea. Read the Gaussian step in Section 2 and the completing-the-square argument in 4.1 together.
  • Section 4.3 (Scaling classifier gradients) explains the scale ss; it depends on the trade-off above.
  • Section 5 (Results) reads Tables 4 to 6. It needs Fréchet Inception Distance to interpret FID, and Classifier-Free Guidance to see what came after.
  • Appendix B has the full diffusion formulation. Appendix A reports throughput and compute.

Check yourself

QuestionIn one sentence, what does classifier guidance change in each denoising step?Show answer

It shifts the mean of the denoiser's Gaussian step by sΣ∇log⁡p(y∣xt)s \Sigma \nabla \log p(y \mid x_t), pushing the sample toward the class yy according to the classifier's gradient, while keeping the denoiser's variance.

QuestionUsing the toy values μ=0\mu = 0, Σ=0.25\Sigma = 0.25 and g=2g = 2, what is the guided mean at scale s=4s = 4?Show answer

The shift is 4×0.25×2=24 \times 0.25 \times 2 = 2, so the guided mean is 0+2=20 + 2 = 2.

QuestionTable 4 shows guidance at scale 10 lowering recall. Why would a practitioner accept that loss?Show answer

Recall measures coverage of the whole data distribution, so lower recall means fewer kinds of images are produced. The practitioner accepts it when the goal is fidelity: each sample should look like the requested class, which the precision gain from 0.61 to 0.76 reflects. If coverage matters more, a smaller scale is the right choice.

QuestionA team wants a class-conditional image generator for a dataset with no labels. Can they use this paper's classifier guidance directly, and what does the paper suggest?Show answer

No. Classifier guidance needs a classifier trained on labels. The paper suggests creating synthetic labels by clustering samples or training discriminative models to judge whether samples are real, both as future work.

QuestionReplacing AdaGN with addition plus group normalization raised FID from 13.06 to 15.08. Is that an improvement or a regression, and why?Show answer

It is a regression, because FID is a distance, so a lower value is better. Moving from 13.06 to 15.08 makes the samples less realistic in the paper's ablation, which is why the AdaGN layer was kept.

Sources

Read the full paper

Prafulla Dhariwal, Alex Nichol, 2021. arXiv:2105.05233