ML Systems Lab Open interactive version →
Advanced 65 min read CLIPmultimodalcontrastivezero-shotALIGN

CLIP and Multimodal Contrastive Alignment

Image-text contrastive pretraining, zero-shot classification, ALIGN, CLIP failure modes

An image search system trained on 400 million image-text pairs from the web can, at zero-shot, classify images into 1,000 ImageNet categories at 76% top-1 accuracy—without ever seeing an ImageNet label. The mechanism is not magic: the web contains images of golden retrievers paired with captions that say "golden retriever," images of airplanes paired with captions that say "airplane," and so on for essentially every visual concept. CLIP trains an image encoder and a text encoder to align their representations for matching pairs, using InfoNCE loss over all N²-N non-matching pairs in each batch.

Zero-shot classification works because encoding "a photo of a {class}" with the text encoder places it in the region of embedding space where the image encoder maps images of that class. The text template acts as a classifier without fine-tuning. The mechanism fails on concepts absent from the pretraining distribution—CLIP cannot classify medical imaging findings that never appeared in web image-text pairs, because there is no alignment relationship to transfer.

The training loss scales with N². For a batch of N pairs, compute an N×N similarity matrix where S_{ij} = cosine_sim(img_i, txt_j). Loss is mean cross-entropy over rows (image finds correct text) and columns (text finds correct image). Temperature τ is learned starting at 0.07, with a clamp at τ ≥ 0.01 to prevent overflow. At batch size 32,768 on 256 GPUs, each pair has 32,767 negatives in both directions—a tight mutual information estimate.

Why CLIP embeddings generalize broadly is the key insight. A supervised ImageNet classifier needs only features that distinguish 1,000 categories; texture and background are often sufficient. CLIP must align images with their natural language descriptions, and language describes everything: color, texture, style, spatial layout, action, emotion, domain. The image encoder must encode everything language can describe. This is why CLIP representations transfer to tasks that ImageNet classifiers systematically fail on.

NOT this. "CLIP understands compositional descriptions" is wrong. "A red cube on a blue sphere" and "a blue cube on a red sphere" receive similar CLIP scores. The model encodes "red," "cube," "blue," "sphere," and "on" as separately weighted features that co-occur with certain images—a bag of co-occurring features, not a compositionally bound description. The ARO benchmark shows CLIP performs near chance on relation, attribute, and ordering understanding. The failure is systematic, not incidental, because the contrastive objective does not require compositional understanding—only co-occurrence alignment.

Key points

Takeaway

CLIP's zero-shot capability works because web-scale image-text co-occurrence covers essentially all visual concepts, and aligning images with open-vocabulary descriptions forces encoding of everything language can describe. The failure mode—poor performance on compositional and relational descriptions—is systematic: CLIP encodes co-occurrence, not composition, so any task requiring binding attributes to specific objects will fail regardless of scale.

Recap

Check your understanding

Q1. Why does CLIP achieve 76% top-1 on ImageNet zero-shot despite never seeing ImageNet labels? What makes this possible?

Q2. A team uses CLIP to build a visual similarity search system for a medical image archive. After deployment, they find retrieval quality is poor and the system fails to distinguish between benign and malignant radiological findings. Diagnose the problem and propose fixes.

Q3. Select the two true statements about CLIP's learned temperature τ.

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →