Hot AI News

Contrastive image-text learning: How CLIP-style models match pictures to words

CLIP-style contrastive learning teaches two encoders to put a picture and its caption close together, which makes searching images with words a nearest-neighbor lookup.

Contrastive image-text learning: How CLIP-style models match pictures to words
Photo illustration: Aiholics

Contrastive learning is a way to train a model so that things that belong together end up close to each other, and things that do not end up far apart. In CLIP-style systems, the pairs are images and their captions. The result is one shared “map” where a photo of a dog and the sentence “a dog on a beach” land near each other.

This idea sits behind most modern image, video and multimodal search.

Searching photos with a sentence

Imagine a photo library with no tags at all. You type “red bicycle leaning on a wall” and the right pictures appear. Nobody wrote that label. The system never saw it as a category.

That works because every photo and every sentence has been turned into a list of numbers, called an embedding. Similar meanings give similar lists. Your query becomes a list too, and the system returns the photos whose lists are closest. This is a nearest-neighbor lookup, and it is the same mechanism described in our guides to text embeddings and approximate nearest neighbor search.

Advertisements

Two encoders pulled together by a simple game

Two adjacent monitors show photographs paired with abstract caption cards, with a fox photo and its matching card highlighted in violet.
Photo illustration: Aiholics

A CLIP-style model has two parts, which is why it is called a dual encoder. One encoder reads images. The other reads text. Each outputs an embedding of the same length.

Training uses a large set of image-text pairs. Take a batch of them. Every image should match its own caption and none of the other captions in the batch. The model is rewarded when the right pair scores high and penalized when a wrong pair scores high.

Think of a matching game on a table. Each photo has one correct caption among many. Over millions of rounds, the two encoders learn to place matching pairs close together. They also learn to push mismatched pairs apart. That push is the “contrast” in the name.

From CLIP in 2021 to sigmoid losses and video

OpenAI published CLIP in 2021, in the paper “Learning Transferable Visual Models From Natural Language Supervision” by Alec Radford and colleagues (ICML 2021). The authors trained an image encoder and a text encoder to predict which caption goes with which image in a batch. They then tested the model on more than 30 existing datasets without extra training for each one. This is called zero-shot transfer: you describe the classes in words, and the model picks the closest.

The idea has since been refined. In 2023, Xiaohua Zhai, Basil Mustafa, Alexander Kolesnikov and Lucas Beyer at Google DeepMind introduced SigLIP. It replaces the batch-wide comparison with a sigmoid loss that scores each image-text pair on its own. The authors say this decouples the loss from batch size, which helps training at small and very large batches. They report that gains flatten out at a batch of about 32k.

Later work extended the recipe to more languages and to video. Models now also read images at different resolutions and longer text.

Advertisements

Where it shows up today

Dual encoders power photo search, content deduplication, image classification by text label, and the retrieval step in many multimodal systems. They are also a common building block for other models, which use the vision tower as an “eye”.

A recent example is Mistral’s open-weight Clipstral, covered in our story on Mistral’s 1.2B Clipstral searching images, video and 36 languages. According to Mistral, it is a dual encoder trained contrastively, with a vision transformer, a video encoder and a text encoder. All three produce 1024-dimension embeddings in one space. Mistral says training ran in two stages, first on image-text pairs and then with video. It also says the video stage improved image results. A query can be a sentence, an image or a video clip.

Mistral also reports that its text side has no fixed context limit. It notes that many retrieval models cut text at 32 to 77 tokens. That limit was typical of early CLIP-style text towers, which handled short captions best.

What the shared space cannot do

Contrastive models are good at “does this match that”, but they do not generate text or answer questions. They compress an image into a single vector, so fine detail can be lost. Counting objects, reading small text and understanding “the cup left of the book” are known weak spots of this family of models.

The training data also matters. Captions scraped from the web carry the biases and gaps of the web. Languages with little paired data tend to score lower. Mistral says its model scores about the same in 35 non-English languages as in English on the XM3600 benchmark (59.7 against 57.8 R@1, how often the first result is correct). It also says several English-focused baselines fall below 15 there. These numbers come from Mistral’s own evaluation, so independent tests will show how well they hold.

Retrieval quality also depends on the lookup. Real systems often search millions of vectors, so they trade a little accuracy for speed.

Advertisements

What to watch next

Three directions are worth following. The first is video: clips are heavier than images, and the choice of how many frames to encode affects both cost and quality. The second is language coverage, where a single model must work across dozens of languages. The third is size: smaller encoders that run on one GPU make private, on-premise search realistic for companies that cannot send media to a cloud service.

When reading any new release, look at which benchmarks are used, whether the comparison models were tested at their best settings, and whether independent groups reproduce the scores.

Sign Up for the Daily AI Pulse

One email a day. All the stories that matter.

Daniel ReedAI Research, Safety & Ethics Analyst

Daniel Reed currently works as an AI Research, Safety & Ethics Analyst at Aiholics, writing about how changes in artificial intelligence are affecting and will affect scholarship,…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like