One space for images and text
- : the embedding of image from the image encoder.
- : the embedding of caption from the text encoder.
- : 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.
A batch of pairs gives an matrix of similarities, and the task is to pick the correct partner in each row and in each column.
It is cross-entropy over the batch, with the diagonal as the labels, applied in both directions.
Why the batch size is the architecture
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
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
- Embed the images once and store the vectors.
- Write the candidate labels as short natural sentences.
- Embed those sentences with the text encoder.
- Normalise both sides and take the dot products.
- 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.