CLIP & Image–Text Alignment

A model that learned to match pictures and words from 400 million captions.

🔴 advanced5 min readvisionmultimodal
CLIP, from OpenAI, learned to connect pictures and words by studying 400 million images paired with captions from the internet. It has two parts, one that reads images and one that reads text, trained so a matching image and caption land close together. That lets it classify images with no task-specific training: compare the image against prompts like a photo of a dog or a photo of a cat, and pick the closest. It also powers image search and guides many image generators.

The Dual-Encoder Contrastive Training Architecture

  BATCH OF N IMAGE-TEXT PAIRS: (Image_1, Text_1), (Image_2, Text_2), ..., (Image_N, Text_N)

  [ Image_1, Image_2, ..., Image_N ] ──► [ IMAGE ENCODER (ViT) ] ──► Normalized Image Embeddings I [N x D]
  [ Text_1,  Text_2,  ..., Text_N  ] ──► [ TEXT ENCODER (Trsf) ] ──► Normalized Text Embeddings  T [N x D]

                                          SIMILARITY MATRIX S = I · T^T [N x N]
                                          Text_1     Text_2     Text_3   ...  Text_N
                              Image_1   [ POSITIVE  Negative   Negative       Negative ]
                              Image_2   [ Negative  POSITIVE   Negative       Negative ]
                              Image_3   [ Negative  Negative   POSITIVE       Negative ]
                              ...
                              Image_N   [ Negative  Negative   Negative       POSITIVE ]

Mathematical Symmetric Loss Function

Given normalized image embeddings Ii\mathbf{I}_i (∥Ii∥=1\|\mathbf{I}_i\| = 1) and text embeddings Tj\mathbf{T}_j (∥Tj∥=1\|\mathbf{T}_j\| = 1):

Logit similarity matrix Si,j=(Ii⋅Tj)⋅eτS_{i,j} = (\mathbf{I}_i \cdot \mathbf{T}_j) \cdot e^\tau, where τ\tau is a learned temperature parameter.

Symmetric Cross-Entropy Loss:

LImage=−1N∑i=1Nlog⁡exp⁡(Si,i)∑j=1Nexp⁡(Si,j)\mathcal{L}_{\text{Image}} = -\frac{1}{N} \sum_{i=1}^N \log \frac{\exp(S_{i,i})}{\sum_{j=1}^N \exp(S_{i,j})} LText=−1N∑j=1Nlog⁡exp⁡(Sj,j)∑i=1Nexp⁡(Si,j)\mathcal{L}_{\text{Text}} = -\frac{1}{N} \sum_{j=1}^N \log \frac{\exp(S_{j,j})}{\sum_{i=1}^N \exp(S_{i,j})} LCLIP=12(LImage+LText)\mathbf{\mathcal{L}_{\text{CLIP}} = \frac{1}{2} \left( \mathcal{L}_{\text{Image}} + \mathcal{L}_{\text{Text}} \right)}

Zero-Shot Classification via Text Prompting

To classify an image without training task-specific classification heads:

  1. Construct Class Prompts:  ["a photo of a dog", "a photo of a cat", "a photo of a car"]
  2. Encode Text Prompts:       T = TextEncoder(Prompts) ──► T [C x D]
  3. Encode Input Image:        i = ImageEncoder(Image)  ──► i [1 x D]
  4. Compute Cosine Scores:     Scores = Softmax( i · T^T / τ )
  5. Select Top Prediction:    "a photo of a dog" (Score = 0.94!)

Prompt Engineering in Zero-Shot CLIP

Passing raw class names like "dog" yields lower accuracy than contextualized prompts.

Using prompt templates like "a photo of a {class}, a type of animal." boosts zero-shot accuracy by +5% to +10% on ImageNet because CLIP pre-training text included full captions!

Say this out loud

CLIP unifies vision and natural language by training dual Image and Text Encoders on 400 million image-text pairs using symmetric in-batch contrastive loss. CLIP enables zero-shot classification by computing cosine similarity between an input image embedding and candidate text prompt embeddings ("a photo of a {class}"). CLIP embeddings power text-to-image models like Stable Diffusion and DALL-E 2.

Follow-ups to expect

Check yourself

Question 1 of 3

How does CLIP perform Zero-Shot Image Classification on an unseen target dataset without fine-tuning weights?

More in Computer Vision

See all →
Image Augmentation Strategies5 minResizing, Normalization & Colour Spaces4 minResNet, EfficientNet & Friends5 min