Start studying
Module 12 · Generative AI Module contents
Module 12  ·  Card 16 / 18intermediate

CLIP

CLIP trains an image encoder and a text encoder side by side.

It learns to place a picture and its caption close together in one shared space.

Mismatched pairs are pushed apart, using millions of image and caption pairs from the web.

That lets you classify an image against any list of words with no task-specific training.

It is the component that connects text prompts to pictures in image generation models.

Learn in depth ↓
Module 12 · Generative AI · 18 concepts
In depth  /  15 lines

CLIP, in more detail

CLIP trains an image encoder and a text encoder side by side.

Each turns its input into a vector, and both land in one shared space.

Training uses hundreds of millions of image and caption pairs scraped from the web.

For each batch, matching pairs are pulled together and mismatched ones pushed apart.

That is contrastive learning, and the negatives come free from the rest of the batch.

No labels were needed, since the caption alongside the image is the supervision.

The result is a space where a photograph of a dog sits near the words a photo of a dog.

That enables zero-shot classification against any list of words.

Embed the image, embed each candidate label, and take the closest.

No task-specific training is required, which was the striking result in 2021.

It also enables image search by text and text search by image.

Its most consequential use is inside image generation, connecting prompts to pictures.

The text encoder from CLIP is what conditions the denoising in many diffusion models.

It inherits the biases of web captions, which is well documented.

It is also weak at counting, spatial relations and reading text in images.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath CLIP

One space for images and text

sij=ii⊤tj∥ii∥∥tj∥⋅exp⁡(τ)s_{ij} = \frac{\mathbf{i}_i^{\top} \mathbf{t}_j}{\|\mathbf{i}_i\| \|\mathbf{t}_j\|} \cdot \exp(\tau)
  • ii\mathbf{i}_i: the embedding of image ii from the image encoder.
  • tj\mathbf{t}_j: the embedding of caption jj from the text encoder.
  • τ\tau: a learned temperature, exponentiated so it stays positive.

Two separate encoders produce vectors in the same space, and the only thing tying them together is the training objective.

L=12(Limg→txt+Ltxt→img)\mathcal{L} = \frac{1}{2}\big( \mathcal{L}_{\mathrm{img} \to \mathrm{txt}} + \mathcal{L}_{\mathrm{txt} \to \mathrm{img}} \big)

A batch of NN pairs gives an N×NN \times N matrix of similarities, and the task is to pick the correct partner in each row and in each column.

Limg→txt=−1N∑i=1Nlog⁡exp⁡(sii)∑j=1Nexp⁡(sij)\mathcal{L}_{\mathrm{img} \to \mathrm{txt}} = -\frac{1}{N} \sum_{i=1}^{N} \log \frac{\exp(s_{ii})}{\sum_{j=1}^{N} \exp(s_{ij})}

It is cross-entropy over the batch, with the diagonal as the labels, applied in both directions.

Why the batch size is the architecture

N−1 negatives per exampleN - 1 \text{ negatives per example}

Every other caption in the batch is a negative, so a batch of 32,768 gives tens of thousands of negatives for free.

CLIP was trained at that scale, and the result degrades sharply at small batch sizes because the task becomes too easy to teach anything fine-grained.

The training set was 400 million image and text pairs.

The data came from the web rather than from labelled datasets, which is the other half of why it works: the supervision is the caption somebody already wrote.

Zero-shot classification falls out of it

y^=arg⁡max⁡c  i⊤tc\hat{y} = \arg\max_{c} \; \mathbf{i}^{\top} \mathbf{t}_c

Write one sentence per class, embed them, and classify an image by which sentence it is closest to.

The usual template is "a photo of a {class}".

The prompt template matters by several points, because the text encoder saw captions rather than bare nouns during training.

No training is needed for a new label set, which is the property that made the model interesting: the classifier is defined by words at inference time.

One use, in order

  1. Embed the images once and store the vectors.
  2. Write the candidate labels as short natural sentences.
  3. Embed those sentences with the text encoder.
  4. Normalise both sides and take the dot products.
  5. Softmax over the classes if a probability is wanted.

What it is actually used for now

Searching a photo library by description is the direct application, and it needs no model at query time beyond one text embedding.

Text-to-image models use the CLIP text encoder to turn a prompt into the vectors their cross-attention reads, which is why prompt phrasing behaves the way it does in those tools.

It also serves as an automatic metric for how well a generated image matches its prompt, with the known circularity that models conditioned on CLIP score well by construction.

Its weaknesses are specific: counting, spatial relations, and text inside images are all poor, because contrastive captions rarely depend on them.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
What does CLIP learn that makes zero-shot image classification possible?
Generative AI

18 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close