ElemedexA pocket encyclopedia of AI architectures
No. 036

CLIP

Contrastive Language–Image Pre-training

Matchmaker

It was trained by playing a matching game: 32,768 photos, 32,768 captions, which goes with which? After enough rounds it recognizes almost anything that has a name on the internet without having seen a single ImageNet label. Handwritten digits, on the other hand, give it trouble.

Height
image ViT-L/14: 24 · text: 12
Weight
≈ 0.9 GB (ViT-L/14, fp16)
Parameters
≈ 150 M (ViT-B/32) · ≈ 430 M (ViT-L/14)
Context
≈ 76 text tokens · 1 image
Training data
WIT: 400 M image–text pairs
Year
2021
Generation
5 · Large language models
Creators
Radford, Kim, Hallacy, Ramesh, Goh, Agarwal, Sastry, Askell, Mishkin, Clark, Krueger & Sutskever (OpenAI)
Kind
Model

Base stats

Total
252

Approximate, ordinal scales meant for comparing species with each other; the hard numbers are in the data sheet.

Weaknesses

  • It struggles to count objects and to tell apart car models, flower species or aircraft variants.
  • It fails out of distribution: 88% on handwritten digits, below logistic regression on raw pixels.
  • It treats captions almost as bags of words: it confuses attributes, spatial relations and negation.
  • With certain label sets it reproduces offensive associations by racial group, documented by its own authors.

Habitat

It grew up on WIT, 400 million image–text pairs collected from the internet with 500,000 queries, and never saw ImageNet's 1.28 million labeled photos. Today it is the hinge between words and images: its text encoder guides Stable Diffusion and its shared space powers image search.

Moves

Abilities

Shared space
Images and texts become vectors in the same space, so a photo and a sentence can be compared with cosine similarity.
Contrastive objective
In each batch of N pairs, it pulls the N true pairs together and pushes the N² − N false ones apart; it never has to predict the caption's exact words.
Written classifier
Classes are written as sentences (“A photo of a {label}.”) and the closest one to the image wins: switching tasks means switching lists.

Anatomy

Two encoders that meet in a common space. In training, every image in a batch is compared with every caption: true pairs (the diagonal) should score high and the rest low. In use, classes are written as sentences and the one closest to the image is chosen.

Evolution chain

036 CLIP

CLIP is a pair of encoders, one for images and one for text, trained so that an image and its caption land close together in a single vector space (Radford et al., 2021, sec. 2.3). It generates nothing: it is a multimodal embedding model that lets you compare a photo directly with a sentence (Alammar & Grootendorst, 2024, pp. 264–265).

How it works

The starting point is data. OpenAI assembled WIT, 400 million image–text pairs collected from the internet with 500,000 queries (up to 20,000 pairs per query), with a total word count similar to that of WebText, the corpus of GPT-2 (Radford et al., 2021, sec. 2.2).

The key decision was the objective. Predicting the exact words of each caption turned out to be slow; predicting only which caption, as a whole, goes with which image was several times more efficient (2021, sec. 2.3). In each batch of N pairs, CLIP computes the cosine similarity of all N × N possible combinations and learns to maximize the N true ones and minimize the N² − N false ones. This is contrastive learning: the model learns what makes two things alike by also being shown which ones do not belong together (Alammar & Grootendorst, 2024, pp. 266–268). Batches held 32,768 pairs, and the largest version with a Vision Transformer (ViT) image encoder trained for 12 days on 256 V100 GPUs (Radford et al., 2021, sec. 2.5).

Classifying without examples

Once trained, CLIP becomes a zero-shot classifier with no fine-tuning: classes are written as sentences ("A photo of a dog.", "A photo of a cat.") and the one closest to the image is chosen. That way it reached 76.2% on ImageNet, matching a supervised ResNet-50 without using any of its 1.28 million labeled images (Radford et al., 2021, secs. 3.1.2–3.1.3). The authors note a problem any linguist will recognize: polysemy. "Crane" is both a machine and a bird, and ImageNet contains both; "boxer" may be a dog or an athlete. Giving context in the prompt helps disambiguate, as with GPT-3 (2021, sec. 3.1.4).

What changed

CLIP replaced closed label lists, like ImageNet's thousand classes, with supervision in natural language, which can name anything. Its shared space became infrastructure: CLIP's text encoder conditions generation in diffusion systems such as Stable Diffusion, and CLIP was the stepping stone to models that take images in and produce text (Millière & Buckner, 2024b, sec. 3.1.1).

Limits

The authors list several (Radford et al., 2021, sec. 6): it does poorly on fine-grained classification (car models, flower species), on counting objects and on tasks rare in its data, and it generalizes poorly to what is truly out of distribution, such as MNIST's handwritten digits (88%). They also document biases: with certain label sets, it classified photos of people into crime-related or "non-human" categories at very different rates depending on racial group (2021, sec. 7.1). And since its objective does not require preserving word order, its text encoder treats captions almost as bags of words: it confuses which attribute goes with which object, spatial relations, counts and negation (Millière & Buckner, 2024b, sec. 3.1.1).

Sources

  • Alammar, J., & Grootendorst, M. (2024). Hands-on large language models: Language understanding and generation. O’Reilly Media. §
  • Harnad, S. (1990). The symbol grounding problem. Physica D: Nonlinear Phenomena, 42(1–3), 335–346. https://doi.org/10.1016/0167-2789(90)90087-6 §
  • Millière, R., & Buckner, C. (2024a). A philosophical introduction to language models—Part I: Continuity with classic debates (arXiv:2401.03910). arXiv. https://doi.org/10.48550/arXiv.2401.03910 §
  • Millière, R., & Buckner, C. (2024b). A philosophical introduction to language models—Part II: The way forward (arXiv:2405.03207). arXiv. https://doi.org/10.48550/arXiv.2405.03207 §
  • Radford, A., Kim, J. W., Hallacy, C., Ramesh, A., Goh, G., Agarwal, S., Sastry, G., Askell, A., Mishkin, P., Clark, J., Krueger, G., & Sutskever, I. (2021). Learning transferable visual models from natural language supervision. In Proceedings of the 38th International Conference on Machine Learning (pp. 8748–8763). PMLR. https://proceedings.mlr.press/v139/radford21a.html §