Language models generate one token at a time. Images are generated differently: a diffusion model starts from pure noise and removes it step by step. And a model that reads images needs a way to turn pixels into tokens. This page covers both: the denoising diffusion model behind image generation, and the vision transformer and contrastive training behind vision-language models.
Diffusion: learning to remove noise
The idea of a diffusion model (Sohl-Dickstein et al., 2015; Ho, Jain and Abbeel, 2020) is easy to say: take a picture and add a little random noise, again and again, until only noise is left. Then train a network to undo one step of that. If it can undo every step, it can start from pure noise and end with a picture.
The forward process: adding noise (fixed)
Starting from a data point (an image), add a little Gaussian noise (random numbers from a bell-shaped distribution) at each of steps, with variances . DDPM (Denoising Diffusion Probabilistic Models) uses and increasing linearly from to . With and ( is "alpha bar": the product of all the up to step ), any step can be sampled directly from :
where means that is standard Gaussian noise. In words: is a mix of the clean image and noise; as grows the clean part shrinks and the noise part grows.
Example. A tiny schedule with and .
- and : each is the product of the so far.
- A single number with noise , at step 2: . At step 4 the clean part would be and the noise part : more noise than signal.
With the real DDPM schedule, , , and (our computation): the signal part is , so is almost pure noise.
The reverse process: removing noise (learned)
A network is trained to predict the noise that was added. The loss is a plain mean squared error, the "simple" objective of Ho et al.:
where is the average over random choices of the step (drawn uniformly), the data point and the noise , and the bars are the squared length of the difference between the true and the predicted noise. A training step is therefore: take an image, pick a random , noise it with the formula above, ask the network what noise was added, and compare.
Sampling starts from (pure noise) and applies, for :
with (and at the last step), and . The first line removes the predicted noise from ; the second adds a small amount of fresh noise, which keeps the samples varied.
From a noise prediction one can also estimate the clean image directly: , where is the predicted noise.
Example. The tiny schedule above, with made from and noise .
- If the network predicts the noise exactly, : : the clean value is recovered exactly.
- One reverse step from with a prediction and a fresh noise : the first line gives ; the second adds . So . At the last step () the second line is dropped.
Later work changed the details: a cosine schedule that adds noise more gradually (Nichol and Dhariwal, 2021), and latent diffusion (Rombach et al., 2022), which runs the whole process in the compressed latent space of an autoencoder (a smaller representation of the image learned by a network) instead of on pixels, making high-resolution generation much cheaper.
Conditioning and classifier-free guidance
To generate "a photo of a cat", the network also receives the condition (a class or a text embedding): . Classifier-free guidance (Ho and Salimans, 2022) trains the same network to work with and without the condition, by replacing with a null token in a fraction of the training examples. At sampling time it extrapolates away from the unconditional prediction:
where is the guided prediction and the guidance weight. is the plain conditional model; larger follows the condition more closely and gives less varied images.
Example. For one number, the conditional prediction is and the unconditional one is . The difference, , is "what the condition adds". The guided prediction is :
guided prediction 0 (the conditional one) 1 2 5 The larger , the further the prediction goes in the direction of the condition, past the conditional prediction itself.
Images as tokens: the vision transformer
The ViT (Vision Transformer; Dosovitskiy et al., 2021) applies the transformer of section 7 to images by cutting them into patches. An image of pixels with channels (3 for colour: red, green, blue) becomes patches of pixels ("an image is worth 16×16 words"), each flattened into a vector of numbers and mapped to the model dimension by one linear layer. A learnable [class] embedding is prepended, as in BERT (Bidirectional Encoder Representations from Transformers), and learnable 1-D position embeddings are added. From there on, the image is a sequence of tokens like any other.
Example. An image of pixels with 3 channels, : patches, each of numbers. A sequence of 197 tokens with the class token.
A toy image with one channel, numbered row by row, and : there are 4 patches, in row-major order (left to right, top to bottom): , , and . The first patch is the square in the top-left corner, flattened row by row.
Connecting images and text: CLIP
CLIP (Contrastive Language-Image Pre-Training; Radford et al., 2021) trains an image encoder and a text encoder together on image–caption pairs from the web. In a batch of pairs, each image embedding should match its own caption and none of the other . With both embeddings normalized to unit length, the table of cosine similarities (the dot products of unit vectors: 1 if they point the same way, 0 if they are unrelated), divided by a learned temperature, is treated as logits, and the loss is the symmetric cross-entropy: once over rows (each image picks its caption) and once over columns (each caption picks its image), with the correct pairs on the diagonal. The temperature is initialized to the equivalent of 0.07.
Example. Two images, embeddings and ; two captions, embeddings and .
- Cosine similarities: . The diagonal (the right pairs) is the largest in every row and column.
- With a temperature of , the logits are the similarities times 2. Each row's cross-entropy is small, because the right caption wins by a wide margin: the loss is . With a temperature of it is . With the learned temperature, , the logits are 14 times larger and the loss is almost 0.
A model that cannot tell the captions apart would give the loss (two equally likely choices).
The result is a shared space where text and images can be compared, used for zero-shot classification (no training for the task: compare an image with the captions "a photo of a {class}", and pick the best) and as the vision part of multimodal models.
Vision-language models
The simplest way to give a language model eyes is to project the image tokens of a pre-trained vision encoder into its embedding space. LLaVA (Large Language and Vision Assistant; Liu et al., 2023) takes the visual features of CLIP's large vision transformer with 14-pixel patches (ViT-L/14), multiplies them by a trainable projection matrix , and feeds the result to the language model as if they were word embeddings. A first stage trains only on image–caption pairs; a second fine-tunes on visual instructions (section 12). The language model never sees pixels: it sees a few hundred extra tokens.
References
- J. Sohl-Dickstein, E. A. Weiss, N. Maheswaranathan, S. Ganguli, Deep Unsupervised Learning using Nonequilibrium Thermodynamics, arXiv, 2015.
- J. Ho, A. Jain, P. Abbeel, Denoising Diffusion Probabilistic Models, arXiv, 2020. The schedule, and Algorithm 2 (sampling).
- A. Nichol, P. Dhariwal, Improved Denoising Diffusion Probabilistic Models, arXiv, 2021. The cosine schedule.
- J. Ho, T. Salimans, Classifier-Free Diffusion Guidance, arXiv, 2022.
- R. Rombach et al., High-Resolution Image Synthesis with Latent Diffusion Models, CVPR, 2022.
- A. Dosovitskiy et al., An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale, ICLR, 2021.
- A. Radford et al., Learning Transferable Visual Models From Natural Language Supervision, arXiv, 2021. CLIP; Figure 3 gives the symmetric loss in pseudocode.
- H. Liu, C. Li, Q. Wu, Y. J. Lee, Visual Instruction Tuning, NeurIPS, 2023. LLaVA.