Search

Search papers and lessons

Hierarchical Text-Conditional Image Generation with CLIP Latents

Aditya Ramesh, Prafulla Dhariwal, Alex Nichol et al. · 2022

DALL-E 2 writes an image from a caption in two stages: a prior turns the caption into a CLIP image embedding, and a diffusion decoder turns that embedding into pixels.

On this page

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

TL;DR

DALL-E 2 (the paper calls its full system unCLIP) generates an image from a text caption in two stages. A prior turns the caption into a CLIP image embedding, a compact summary of what the picture should contain, and a diffusion decoder turns that summary into pixels. Splitting the job this way gave more varied images at almost the same photorealism as the strongest text-to-image model of the time.

Builds on: Learning Transferable Visual Models From Natural Language Supervision (CLIP), Denoising Diffusion Probabilistic Models (DDPM), Classifier-Free Guidance · Leads to: Latent Diffusion Model (a related design that also runs diffusion on a learned code, but in a different space)

Why it mattered

Within a year of the paper, text-to-image generation moved from research demos to public products. The DALL-E 2 Preview ran as a public service in 2022, and the same two-stage idea of "plan a semantic code, then render it" shaped later image systems. The paper also showed a practical fact that became standard: a diffusion model that is conditioned on a good embedding can produce many different pictures for the same meaning, and guidance can raise quality without flattening that variety.

The problem

Before this work, the best text-to-image systems faced a trade-off. Guidance, the trick of pushing a diffusion model harder toward its condition, made samples sharper and closer to the caption, but it also made them more alike. Two samples from the same caption often looked nearly identical after strong guidance, because the model's samples collapsed toward a single most-typical picture.

The CLIP model, from Learning Transferable Visual Models From Natural Language Supervision (CLIP), had already learned an image encoder and a text encoder that put matching image and caption pairs close together. Its image embeddings were robust, and they carried semantics and style. But CLIP can only read images. Nothing in it writes an image. The paper asks: can we run CLIP's image side backwards, so that a caption can be turned into the image embedding and then into pixels?

How it works

Think of the pipeline as two rooms. In the first room, a caption goes in and a list of numbers comes out. In the second room, that list goes in and a picture comes out.

  1. Start with a caption. Take the caption, for example "a corgi wearing a red scarf in the snow." A frozen CLIP text encoder reads it and produces a text embedding, a vector of 1,024 numbers in the paper's setting.
  2. The prior proposes an image embedding. The prior is a model that takes the caption and produces a plausible CLIP image embedding ziz_i for a picture matching that caption. The paper tries two prior designs: an autoregressive one and a diffusion one. The diffusion prior is the better of the two, so we follow it here.
  3. Pick the better of two candidates. The diffusion prior draws two candidate embeddings. The one with the larger dot product against the caption's text embedding (a vector in the same space, produced by the CLIP text encoder) is kept. Suppose candidate A scores 0.31 and candidate B scores 0.27. A is kept, because it agrees more with the caption.
  4. The decoder paints from the embedding. The decoder is a diffusion model. It starts from pure noise at 64 by 64 pixels and removes noise over many steps. At each step it is given the CLIP image embedding, so the denoising keeps pulling the picture toward the content the embedding describes.
  5. Upsample twice. The 64 by 64 image goes through one upsampler to 256 by 256, and a second upsampler takes it to 1024 by 1024. Each upsampler is a diffusion model that adds detail while staying faithful to its input.
  6. Output. The 1024 by 1024 image is the result.

Check: the two upsamplers each multiply the side length by 4, and 64×4×4=102464 \times 4 \times 4 = 1024. The prior has no pixels to look at; it only ever sees captions and embeddings.

The reason for the detour through an embedding is the same idea that makes the paper useful. The embedding holds the scene's meaning, such as objects, style and layout, and leaves out fine details that do not matter for the meaning. Once the meaning is fixed by the prior, the decoder can sample many different renderings of it. Varying the decoder's noise gives varied images with the same content. This is the source of the diversity gain reported in the paper.

The math

The whole model is one factorisation of the probability of an image xx given a caption yy. Let ziz_i be the CLIP image embedding of xx. Because ziz_i is a deterministic function of xx, the image and its embedding carry the same information, so

P(x∣y)=P(x,zi∣y)=P(x∣zi,y) P(zi∣y).P(x \mid y) = P(x, z_i \mid y) = P(x \mid z_i, y)\, P(z_i \mid y).

The first equality holds because ziz_i is a function of xx, so "the picture xx" and "the picture xx together with its embedding" are the same event. The second is the chain rule of probability. In words: the probability of a picture given a caption equals the probability of an embedding given the caption, times the probability of the picture given that embedding.

Step 2 in symbols: the prior

The prior models P(zi∣y)P(z_i \mid y). The paper uses a decoder-only Transformer for the diffusion prior. It is trained to predict the clean embedding ziz_i from a noisy version zi(t)z_i^{(t)} at diffusion time tt, with the caption yy as context. The loss is a mean squared error:

Lprior=Et∼[1,T],  zi(t)∼qt[∥fθ(zi(t),t,y)−zi∥2].L_{\text{prior}} = \mathbb{E}_{t \sim [1, T],\; z_i^{(t)} \sim q_t} \Big[ \big\| f_\theta(z_i^{(t)}, t, y) - z_i \big\|^2 \Big].

Here TT is the number of diffusion steps, qtq_t is the distribution of the noisy embedding at step tt, and fθf_\theta is the network with parameters θ\theta. The expectation averages over random steps and random noise. Predicting ziz_i directly, rather than the noise, was a choice the authors found worked better for this prior.

Step 3 in symbols: the selection rule

The prior draws two samples zi(a)z_i^{(a)} and zi(b)z_i^{(b)}. The one kept is

z^i=arg⁡max⁡k∈{a,b}  zi(k)⋅zt,\hat{z}_i = \arg\max_{k \in \{a, b\}} \; z_i^{(k)} \cdot z_t,

where zyz_y is the CLIP text embedding of the caption yy and ⋅\cdot is the dot product (multiply matching entries, then add). In the toy example above, 0.31>0.270.31 > 0.27, so z^i=zi(a)\hat{z}_i = z_i^{(a)}.

Step 4 in symbols: the decoder

The decoder models P(x∣zi,y)P(x \mid z_i, y) with a diffusion model. It is trained with the standard denoising objective of Denoising Diffusion Probabilistic Models (DDPM), with the CLIP embedding added to the timestep signal and passed as four extra context tokens. Classifier-free guidance is trained in by replacing the embedding with a null value on 10 percent of training examples and dropping the caption on 50 percent. At sampling time, the guidance scale ss combines the two predictions. Here xtx_t is the noisy image at diffusion step tt. Writing ϵθ(xt,c)\epsilon_\theta(x_t, c) for the noise the network predicts when conditioned on cc (the CLIP embedding and caption), and ∅\varnothing for the null condition, the guided prediction is

ϵ~=ϵθ(xt,∅)+s(ϵθ(xt,c)−ϵθ(xt,∅)),\tilde{\epsilon} = \epsilon_\theta(x_t, \varnothing) + s \big(\epsilon_\theta(x_t, c) - \epsilon_\theta(x_t, \varnothing)\big),

as described in Classifier-Free Guidance. A larger ss pushes the picture harder toward the condition.

Step 5 in symbols: the upsamplers

Write x64x_{64}, x256x_{256} and x1024x_{1024} for the images at the three resolutions. Each upsampler is a conditional diffusion model that generates the larger image from the smaller one, so the decoder's output is refined in two stages:

x64  ⟶  x256  ⟶  x1024.x_{64} \;\longrightarrow\; x_{256} \;\longrightarrow\; x_{1024}.

Each arrow multiplies the side length by 4, as in Example 2 below.

Worked examples

Example
Example 1: the chain rule with two numbers

Suppose a caption yy has two possible embeddings with prior probabilities P(z(1)∣y)=0.6P(z^{(1)} \mid y) = 0.6 and P(z(2)∣y)=0.4P(z^{(2)} \mid y) = 0.4. Each embedding leads to a decoder that yields a given image xx with probability P(x∣z(1),y)=0.5P(x \mid z^{(1)}, y) = 0.5 and P(x∣z(2),y)=0.1P(x \mid z^{(2)}, y) = 0.1.

  1. Multiply the first pair: 0.6×0.5=0.300.6 \times 0.5 = 0.30. This is the probability that the sampled embedding is z(1)z^{(1)} and the image is xx.
  2. Multiply the second pair: 0.4×0.1=0.040.4 \times 0.1 = 0.04.
  3. Add them, since the two embeddings are separate cases: 0.30+0.04=0.340.30 + 0.04 = 0.34.
  4. The result, P(x∣y)=0.34P(x \mid y) = 0.34, is the probability the full pipeline outputs xx for this caption. Interpretation: most of the mass comes from the more likely embedding, but the less likely one still contributes.
Example
Example 2: the upsampling chain

The decoder makes a 64 by 64 image. The paper then uses two upsampler diffusion models.

  1. The first upsampler takes 64 by 64 to 256 by 256. The side length is multiplied by 256/64=4256 / 64 = 4.
  2. The second upsampler takes 256 by 256 to 1024 by 1024. The side length is multiplied by 1024/256=41024 / 256 = 4.
  3. The total factor on each side is 4×4=164 \times 4 = 16, and the pixel count grows by 16×16=25616 \times 16 = 256 times, from 642=409664^2 = 4096 pixels to 10242=10485761024^2 = 1048576 pixels.
  4. Interpretation: most of the meaning is fixed at 64 by 64. The upsamplers add detail and work mainly from the low-resolution image they are given.
Example
Try it: a diversity check

Suppose a team runs the decoder 4 times on one fixed embedding, using 4 different noise seeds. Each run produces an image. In the paper's framing, the embedding is "frozen" and only the noise changes. If the 4 images share the same subject, pose and palette but differ in small textures and background details, is the system behaving as the paper predicts, and why does the guidance scale matter?

Answer: Yes. The embedding fixes the high-level meaning (subject, pose, colours), while the decoder's noise decides the small details, so the prediction holds. The guidance scale ss matters because it pushes each sample harder toward the condition. A larger ss tends to sharpen images and make them closer to the embedding, but it can also make samples alike. The paper's point is that with the prior in front, the embedding already carries the meaning, so the samples can stay varied in their details even when guidance is strong.

Results

The main claims, with the paper's numbers:

  • Zero-shot FID on MS-COCO at 256 by 256. The diffusion-prior unCLIP reached 10.39, versus 12.24 for GLIDE (lower is better). FID measures how close the sample distribution is to real images, so a lower score is better, though it says nothing about whether a single image matches its caption.
  • Human evaluations against GLIDE. Raters chose between paired images. The unCLIP diffusion prior won 48.9 percent of photorealism comparisons, a near tie with GLIDE, which was slightly preferred. It won 45.3 percent of caption-similarity comparisons, so GLIDE was slightly better there. It won 70.5 percent of diversity comparisons, a clear advantage.
  • Why the prior matters. The paper compares the full stack with a decoder fed the CLIP text embedding directly. The prior step, which turns the caption into an image embedding first, is what the authors credit for the gains in the comparison.

Limits, stated by the authors. unCLIP binds attributes to objects less well than GLIDE, so a prompt asking for two cubes with two different colours often mixes them up. It struggles to render legible text. Complex scenes lose detail because the base decoder works at 64 by 64. The paper also discusses the risks of more realistic generation, including deception, and points to a separate deployment analysis.

Reading guide

The paper is read in this order. Each section names the lessons it needs.

  1. Introduction. Needs Learning Transferable Visual Models From Natural Language Supervision (CLIP) for the idea of a shared image-text space, and Diffusion Model for the generative framework.
  2. Method: the decoder. Needs Denoising Diffusion Probabilistic Models (DDPM) for the denoising training, U-Net for the image architecture family, and Classifier-Free Guidance for the guidance trick. Skim Transposed Convolution only if the upsampling architecture is unfamiliar.
  3. Method: the prior. Needs Transformer with a causal mask for the autoregressive prior, and Autoregressive Model for that prior. The diffusion prior uses the same denoising ideas as the decoder.
  4. Image manipulations. Needs Representation Learning and Dot Product. These sections show that editing an embedding changes the picture in controlled ways.
  5. Results. Needs Mean Squared Error for the loss and Contrastive Learning for the CLIP training that produced the embeddings.
  6. Limitations. No new prerequisites. Read it with the Results section above.

Check yourself

QuestionWhy does the decoder's conditioning on the CLIP embedding help diversity, and what is held fixed when the noise changes?Show answer

The CLIP embedding fixes the high-level meaning of the image: its subjects, style and layout. Different noise seeds give different renderings of that meaning, so the non-essential details vary while the content stays the same.

QuestionA prior assigns probability 0.2 to embedding e1e_1 and 0.8 to e2e_2 for some caption. The decoder gives an image xx probability 0.5 given e1e_1 and 0.25 given e2e_2. What is P(x∣y)P(x \mid y)?Show answer

Use the chain rule and sum over the two embeddings: 0.2×0.5+0.8×0.25=0.10+0.20=0.300.2 \times 0.5 + 0.8 \times 0.25 = 0.10 + 0.20 = 0.30. So P(x∣y)=0.30P(x \mid y) = 0.30.

QuestionWhy did the authors pick the diffusion prior over the autoregressive prior?Show answer

The diffusion prior works on the continuous CLIP embedding directly, so it needs no compression or discretisation step, and the paper found it the stronger of the two priors on its evaluation.

QuestionWhy does unCLIP struggle to bind colours to objects, even though its images are diverse and realistic?Show answer

The CLIP image embedding does not explicitly record which attribute belongs to which object. The decoder then reconstructs an image from an embedding in which attributes and objects can be mixed up. The paper hypothesises that this is the cause, and it is the reason for the gap with GLIDE on that prompt type.

Sources

Read the full paper

Aditya Ramesh, Prafulla Dhariwal, Alex Nichol et al., 2022. arXiv:2204.06125