CLIP: Contrastive Language-Image Pre-training
Learning visual concepts from natural language supervision
Updated
Contents
CLIP (Contrastive Language-Image Pre-training) learns to connect images and text by training on 400 million image-text pairs from the internet. Instead of teaching a model a fixed list of categories, CLIP teaches it to answer one question — does this caption describe this image? — and that simple objective produces remarkably flexible visual representations.
This page assumes you know what an encoder is. If not, read Transformer and Vision Transformer first.
Why Students Should Care
- CLIP is the bridge between vision and language — the recipe behind image search, open-vocabulary detection, and the visual side of many multimodal LLMs.
- It popularized zero-shot classification: classifying images from datasets the model was never trained on, just by describing the classes in words.
- Its text embeddings guide image generators like DALL-E and Stable Diffusion (see Latent Diffusion).
A Quick Example
Show CLIP a photo and two captions: “a photo of a dog” and “a photo of a cat”. CLIP embeds the image and both captions into the same vector space and checks which caption’s vector points in the most similar direction to the image’s vector. Whichever caption matches best wins. That is the entire mechanism — everything below is about how those embeddings are learned.
Core Idea: Learn from Captions
Instead of predicting fixed categories, CLIP learns to match images with their natural language descriptions. The match score is cosine similarity between the two embeddings:
where is an image encoder (ViT or ResNet) and is a text encoder (Transformer).
In plain English: two encoders map images and captions into a shared space, where matching pairs sit close together.
Contrastive Learning
How do you train those encoders? Take a batch of image-text pairs. For each image, its own caption is the “right answer” and the other captions are wrong answers — and vice versa for each caption. CLIP maximizes similarity for correct pairs while minimizing it for incorrect ones:
where and is a learned temperature.
You do not need to memorize the loss. The important idea is: every batch is a matching game — pull true pairs together, push mismatched pairs apart.
Zero-Shot Classification
Because CLIP understands captions, you can build a classifier for a dataset it has never seen — with no extra training:
- Create text prompts: “a photo of a {class}”
- Encode all prompts with the text encoder
- Encode the image with the image encoder
- Predict the class with the highest image-text similarity
In other words: the class names themselves become the classifier.
Interactive Visualization
Explore how CLIP matches images to text descriptions:
CLIP: Image-Text Matching
Click an image or text to see similarity scores. CLIP learns to maximize diagonal (matching pairs).
Zero-shot: To classify a new image, compute similarity with text prompts like "a photo of a {class}" and pick the highest.
Prompt Engineering
Because the classifier is built from text, how you phrase the text matters. Classification accuracy depends on the prompt:
| Prompt Template | ImageNet Acc |
|---|---|
| ”{class}“ | 63.5% |
| “a photo of a {class}“ | 68.3% |
| “a good photo of a {class}“ | 69.1% |
| Ensemble of 80 templates | 76.2% |
A bare class name underperforms because CLIP’s training captions were full sentences, not single words.
Why CLIP Works
- Scale: 400M image-text pairs provide diverse supervision
- Natural language: captures nuanced visual concepts that fixed label sets cannot
- Contrastive learning: makes efficient use of every batch — each pair supplies many negative examples
- Zero-shot transfer: no task-specific training needed
Applications
CLIP powers:
- Image search: find images matching text queries
- DALL-E / Stable Diffusion: guide image generation
- Open-vocabulary detection: detect objects by name
- Multimodal models: visual understanding in LLMs
Limitations
- Struggles with fine-grained recognition
- Inherits biases from internet data
- The text encoder limits complex reasoning
- Counting and spatial relationships remain challenging
Common Confusion
- CLIP does not generate images. It scores image-text similarity. Generators like DALL-E and Stable Diffusion use CLIP-style embeddings for guidance, but the generation happens elsewhere — see Diffusion Models.
- “Zero-shot” here means no training on the target dataset — CLIP still saw 400M image-text pairs during pre-training. It is zero-shot with respect to the task, not to data in general.
- CLIP is a training method plus a pair of encoders, not one specific network. The image encoder can be a Vision Transformer or a ResNet; the recipe is the same.
Where To Go Next
- Read Vision Transformer for CLIP’s strongest image encoder
- Read Transformer for the architecture behind the text encoder
- Read Pre-training for the broader paradigm CLIP belongs to
- Read Latent Diffusion to see text embeddings steering image generation