Search

Search papers and lessons

Photorealistic Text-to-Image Diffusion Models with Deep Language Understanding

Chitwan Saharia, William Chan, Saurabh Saxena et al. · 2022

Imagen makes photorealistic images from text by pairing a frozen T5 language model with a cascade of diffusion models, and it found that a bigger text encoder helps more than a bigger image model.

On this page

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

TL;DR

Imagen turns a sentence into a photorealistic picture. A frozen T5-XXL language model reads the text and produces a sequence of embeddings. A 64 by 64 diffusion model draws the picture from those embeddings, and two super-resolution diffusion models enlarge it to 256 and then 1024 pixels. The paper found that a larger frozen text encoder improved image quality more than a larger image model did, and it introduced dynamic thresholding, which lets the sampler use strong guidance without producing washed-out images. Imagen reached a zero-shot FID of 7.27 on COCO.

Builds on: Classifier-Free Guidance, Cascaded Diffusion Model, Exploring the Limits of Transfer Learning (T5) · Leads to: later text-to-image systems that combine a frozen language encoder with cascaded or latent diffusion (no lesson node yet)

Why it mattered

By 2022, text-to-image diffusion models such as DALL-E 2 and Latent Diffusion were producing striking pictures, and the field was asking whether a language model's understanding of text could help more than the image-text encoders that were standard. Imagen answered that question with a clear yes, backed by a new benchmark. Its zero-shot COCO FID of 7.27 beat DALL-E 2 at 10.39 and GLIDE at 12.24 in the paper's Table 2. On COCO, human raters found Imagen's image-text alignment on par with the reference images, and the paper's DrawBench study showed raters preferred Imagen over the other systems it compared. The paper has about 9,300 citations on Semantic Scholar, and its design choices (a frozen language encoder, classifier-free guidance, cascaded super-resolution) became common ingredients of later systems.

The problem

A text-to-image model has to do three things at once: understand the sentence, including its counts, spatial relations and rare words; produce a coherent scene; and make the result look like a photograph. Earlier systems had several weak points:

  1. Text encoders trained only on paired image-caption data learn a vocabulary of visual concepts, but they see far less text than a language model does. Models such as CLIP can miss compositional details, such as which object is on which side.
  2. Strong guidance (classifier-free guidance with a large weight) makes the image match the text better, but it also makes the colours saturated and the images unnatural, and at high guidance the samples can diverge.
  3. Single-stage generation at high resolution is expensive, and each stage has to produce detail that the earlier stage did not capture.

The paper addresses each point with one design choice: a text encoder that is a large pretrained language model, a sampler that keeps high guidance stable, and a cascade of super-resolution stages.

How it works

Trace one prompt, "a corgi wearing a red bow tie, studio photo", through the pipeline.

  1. Encode the text. The prompt is tokenised and passed through the frozen T5-XXL encoder, which outputs a sequence of embeddings, one per token. The weights of T5 are not changed during training. Check: the output is a sequence with one vector per token, not a single summary vector.
  2. Start from noise. The base diffusion model begins with a 64 by 64 grid of random Gaussian noise. Check: the grid has the same shape as the final low-resolution image.
  3. Denoise step by step. At each step the U-Net predicts the clean image x from the noisy grid. The text embeddings enter through cross-attention at several resolutions, so every spatial location can look at the words. Check: the prediction is an image-shaped array, not a list of labels.
  4. Apply classifier-free guidance. The model is run twice, once with the text and once without, and the two noise predictions are combined with a guidance weight. A weight above 1 pushes the sample harder toward the text. Check: weight 1 gives the plain conditional prediction.
  5. Threshold the prediction. Each x prediction is clipped so that its values stay in the range of real images, which is between minus 1 and plus 1 after scaling. The dynamic version, described in the math, adapts the clip to each image. Check: a prediction with a value of 1.8 is pulled back into range.
  6. Produce the base image. After the last denoising step the 64 by 64 image is complete. It has the layout, colours and rough content of the scene.
  7. Super-resolve to 256. A second super-resolution diffusion model takes the 64 by 64 image, corrupts it with a random amount of Gaussian noise or blur (the augmentation level), and denoises it while conditioned on the text and on that noise level. Check: the augmentation hides small errors in the base image, so the model learns to correct them.
  8. Super-resolve to 1024. A third model takes the 256 by 256 image and adds detail at 1024 by 1024. It trains on crops and keeps the text cross-attention. Check: the output is four times wider and taller than the 256 by 256 input.
flowchart LR
  A[Prompt] --> B[Frozen T5-XXL encoder]
  B --> C[Base 64 by 64 diffusion with guidance]
  C --> D[Super-resolution to 256]
  D --> E[Super-resolution to 1024]
  B --> D
  B --> E

The diagram shows the text embeddings feeding every stage. The base image is only as good as the layout it draws, and the two super-resolution models add detail without changing that layout.

The math

The diffusion model is trained on a weighted denoising loss. Its inputs are a clean image x, a condition c (here the text embeddings), a noise sample epsilon from a standard Gaussian, and a time t between 0 and 1.

Definition: Denoising objective

The noisy image is z_t = alpha_t x + sigma_t epsilon, where alpha_t and sigma_t are functions of t that set how much signal and how much noise remain. The model x_hat_theta(z_t, c) is trained to minimise

Ex,c,ϵ,t[wt∥x^θ(zt,c)−x∥22],\mathbb{E}_{x, c, \epsilon, t}\left[ w_t \left\lVert \hat{x}_\theta(z_t, c) - x \right\rVert_2^2 \right],

where w_t is a weight that emphasises some noise levels over others.

Step 3 in symbols

At each sampling step the model predicts x_hat from z_t, and the sampler moves z_t to a slightly less noisy z_s. Because the model's output is fed back as the next input, any error in one step is carried into the next, which is why the thresholding in Step 5 matters.

Step 4 in symbols

Classifier-free guidance trains one model with the condition dropped 10% of the time. At sampling it forms the mixed noise prediction

ϵ~θ(zt,c)=w ϵθ(zt,c)+(1−w) ϵθ(zt).\tilde{\epsilon}_\theta(z_t, c) = w\, \epsilon_\theta(z_t, c) + (1 - w)\, \epsilon_\theta(z_t).

Here epsilon_theta(z_t, c) is the conditional prediction, epsilon_theta(z_t) is the unconditional one, and w is the guidance weight. Setting w = 1 gives the conditional prediction unchanged, and larger w pushes further from the unconditional one. The network itself is trained to predict the clean image x; its noise prediction is the same output in another form, epsilon = (z_t - alpha_t x_hat) / sigma_t. This section writes guidance with the noise form because that is how the guidance formula is usually stated.

Step 5 in symbols

The sampler converts epsilon into an x prediction, x_hat = (z_t - sigma_t tilde_epsilon) / alpha_t. Imagen's dynamic thresholding then works as follows. Let s be a high percentile of the absolute pixel values in x_hat. If s is greater than 1, clip x_hat to the range from minus s to plus s, and divide by s. If s is at most 1, leave x_hat alone. The effect is that the largest pixels are brought back into range while the rest of the image is kept.

The cascade in symbols

The overall sample is produced by chaining conditional models: the base model draws x_64 from the text, and each super-resolution model draws a higher-resolution image conditioned on the previous one, x_256 from x_64 and the text, and x_1024 from x_256 and the text. The noise augmentation on the low-resolution input is a forward diffusion step with a level aug_level between 0 and 1, which is sampled randomly in training and swept at inference.

Worked checks

Example
Example 1: thresholding four pixels (tiny)

Take a predicted x_hat with four pixels: (0.5, -1.8, 1.2, -0.3). Compare two rules.

  1. Static thresholding clips every pixel to the range from minus 1 to plus 1. The result is (0.5, -1, 1, -0.3). Check: the 1.8 and 1.2 are clipped to the boundary values.
  2. Dynamic thresholding takes s to be the largest absolute value here, which is 1.8. This is the 100th percentile, used only to keep the example short. Check: s = 1.8 is greater than 1.
  3. Clip to [-1.8, 1.8]. Nothing changes, because every pixel is already inside that range.
  4. Divide by s: (0.5/1.8, -1.8/1.8, 1.2/1.8, -0.3/1.8) = (0.278, -1, 0.667, -0.167).
  5. Compare the results. Static thresholding flattened the 1.2 into 1, which is the boundary value and makes a saturated pixel. Dynamic thresholding kept the relative brightness of all four pixels, with the largest one at the boundary.
  6. Interpretation: dynamic thresholding keeps the ratios between pixels, while static thresholding destroys them whenever a pixel exceeds the range.
Example
Example 2: combining two predictions (standard)

For one pixel and one step, the conditional prediction is epsilon_theta(z_t, c) = 0.8, and the unconditional prediction is epsilon_theta(z_t) = 0.2.

  1. With guidance weight w = 1: tilde_epsilon = 1 x 0.8 + 0 x 0.2 = 0.8. This is the plain conditional prediction.
  2. With guidance weight w = 5: tilde_epsilon = 5 x 0.8 + (1 - 5) x 0.2 = 4.0 - 0.8 = 3.2.
  3. The difference from the unconditional prediction grew from 0.8 - 0.2 = 0.6 to 3.2 - 0.2 = 3.0, which is five times larger. Check: the direction of the push is the same, and only its size changed.
  4. Interpretation: a large guidance weight exaggerates whatever the text adds. The same exaggeration can also push the x prediction out of the image range, which is why the thresholding of Example 1 is needed.
Example
Example 3: a super-resolution step on a realistic size (realistic)

Take the step from a 64 by 64 image to a 256 by 256 image.

  1. The low-resolution image has 64 x 64 = 4,096 pixels per channel.
  2. The output has 256 x 256 = 65,536 pixels per channel. This is 65,536 / 4,096 = 16 times as many.
  3. The augmentation level is chosen at random during training, between 0 and 1. A level of 0.3 corrupts the low-resolution image with Gaussian noise of about that strength before the model sees it.
  4. The model is conditioned on the augmentation level, so it knows how much of the input to trust. At inference the paper sweeps over this level instead of drawing it at random.
  5. The paper's model for this step has about 600 million parameters, and it trains with batch size 2048 for 2.5 million steps on 128 TPU-v4 chips.

Try it: guidance weight w = 2 is used, with epsilon_theta(z_t, c) = 0.8 and epsilon_theta(z_t) = 0.2. What is the guided prediction? Answer: 2 x 0.8 + (1 - 2) x 0.2 = 1.6 - 0.2 = 1.4.

Misreadings and tradeoffs

It is tempting to think a larger image model is the main way to get better pictures. In fact the paper found that scaling the frozen text encoder improved sample quality more than scaling the image diffusion model, which is the most surprising result of the paper. Language understanding is the bottleneck for prompts with several objects and relations.

It is tempting to think that guidance only makes images match the text. In fact high guidance also pushes predictions out of the valid image range, and the sampler then produces saturated or unstable images. Thresholding is part of making guidance usable at all.

It is tempting to think that FID and CLIP scores settle which model is better. The paper itself notes that FID is not fully aligned with perceptual quality, and CLIP is weak at counting. That is why it added human evaluation and DrawBench, a set of prompts that test counts, spatial relations, rare words and imaginative scenes.

When to use this design: when you have a large budget for pretraining a language model, want strong compositional text understanding, and can afford a cascade of models at inference. When not to use it: when you need fast generation on a small device, since three diffusion stages with many denoising steps are expensive, or when your text is short and simple, where a smaller encoder may do as well. Alternatives include latent diffusion, which does the diffusion in a compressed space to save compute, and models that use CLIP as the text encoder, which are simpler but weaker on composition.

Results

The paper reports three kinds of evidence.

  • COCO, zero-shot FID-30K (Table 2): Imagen 7.27, DALL-E 2 10.39, GLIDE 12.24. Imagen is also better than Make-A-Scene at 7.55, which was trained on COCO.
  • Human ratings on COCO: raters judged how often Imagen's samples were chosen over real reference photos for photorealism (the preference rate), and found image-text alignment on par with the reference images.
  • DrawBench: 200 prompts in 11 categories, testing colours, numbers of objects, spatial relations, text in the scene, unusual interactions, rare words and misspelled prompts. Human raters compared pairs of models on fidelity and alignment, choosing either model or "indifferent", and Imagen was preferred over the other systems tested.

The ablations support the design choices. Replacing the T5-XXL encoder with CLIP made images worse on DrawBench, and raters preferred T5-XXL on both alignment and fidelity, even though the two encoders scored similarly on COCO. Dynamic thresholding allowed guidance weights that static thresholding could not use without saturation.

Limits. The model is not open to the public, which makes independent replication hard. The paper discusses the risks of photorealistic generation, including social bias in generated people and misuse, and it does not claim that the problem is solved. The human evaluation uses a fixed set of prompts and raters, so the preference numbers depend on that protocol. Finally, the system is expensive: the base model has about 2 billion parameters and was trained on 256 TPU-v4 chips.

Reading guide

Check yourself

QuestionWhat does Imagen's text encoder produce, and is it trained during Imagen's training?Show answer

The T5-XXL encoder produces a sequence of embeddings, one per token of the prompt. It is frozen, meaning its weights are not updated while the image models are trained.

QuestionWith conditional prediction 0.6 and unconditional prediction 0.4, what is the guided prediction for guidance weight w = 3?Show answer

3 x 0.6 + (1 - 3) x 0.4 = 1.8 - 0.8 = 1.0.

QuestionWhy does the paper prefer dynamic thresholding to static thresholding when guidance is high?Show answer

Static thresholding clips every pixel above 1 to 1, which flattens bright detail into saturated values and loses the relative brightness of the pixels. Dynamic thresholding scales the whole prediction down by the high percentile when it exceeds the range, which keeps the ratios between pixels, so high guidance does not saturate the image.

QuestionA team wants to improve compositional prompts such as 'three red cubes to the left of a blue sphere'. Based on the paper, which change is the most likely to help, and why?Show answer

Scaling the frozen text encoder, because the paper found that the text encoder's size improved sample quality more than the size of the image model, and that a language model encoder handled compositional prompts better than CLIP. Making the image model larger is the less effective choice in the paper's findings.

QuestionIn the super-resolution step, why does the model get the augmentation level as an input?Show answer

The low-resolution image has been corrupted by Gaussian noise at that level, so the model needs to know how much noise was added to decide how much to trust the input and how much detail to invent. This makes it robust to artifacts from the lower-resolution model.

Sources

Read the full paper

Chitwan Saharia, William Chan, Saurabh Saxena et al., 2022. arXiv:2205.11487