# What Is CLIP?

> What CLIP is, how it learns pictures and captions together, how it labels new photos, and where it falls short, explained for your AI interview.

**In short:** CLIP is an OpenAI model that learns to match pictures with the text written about them. It can label a new picture by comparing it with written labels, with no extra training for each task.

AI and LLM interview question Q443, Multimodal and newer topics. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-clip

## Say it in 30 seconds

CLIP is a model from OpenAI that learns pictures and words together. It is trained to match each photo with the caption that came with it, across a huge set of web pairs. To label a new photo, I write the labels as sentences and pick the closest one, with no retraining. It is strong on common things. It struggles with counting, and wording changes can move its answers, so I test it on our own photos first.

## Say it in 2 minutes

CLIP stands for Contrastive Language-Image Pre-training. OpenAI released it in January 2021. It learns what things look like from the text written about them, so it is not limited to one fixed list of labels. Older image models could only name the labels they were trained on, and that was the real change.

Training is a matching game. The model sees a photo and a set of captions, and it has to find the caption that came with that photo. OpenAI describes it as predicting which of 32,768 sampled text snippets was paired with the image. The pairs were collected from the internet, 400 million of them, so no one wrote labels for them.

After training, the model can be used with new labels and no new training. I write each possible answer as a sentence, compare the photo with each sentence, and take the closest one. That is zero-shot use. The catch is that CLIP is weaker at counting and fine detail, and its answers can move when the wording changes. So I test it on our own photos first, and I send unsure cases to a person. Each label is a short sentence, and I try several wordings of it before I trust the result.

## What is CLIP, in plain words?

CLIP is a neural network, a model built from layers of numbers, that learns to connect pictures with words. OpenAI published it in January 2021, and the paper was submitted to arXiv that February.

Older image models learned from a fixed list of labels. A model trained on the 1000 ImageNet categories can name those 1000 things, and little else. CLIP learns from captions instead, so the things it can name are not fixed in advance.

Picture a claims clerk who has read years of car damage files, each photo with the adjuster's note beside it. Shown a new photo and a few possible notes, the clerk can point to the one that fits best, even for damage they never sorted before. CLIP learned from 400 million image and caption pairs, far more than any clerk could read. A claims team could use it to sort new photos without first building a labelled set.

- **Encoder:** The part of a model that turns a photo or a sentence into a list of numbers.
- **Zero-shot:** Doing a task the model was never trained on, with no new examples and no retraining.
- **Caption:** The words written beside a picture, like a note on a claim photo.

## How does CLIP learn to match pictures and words?

Training is a matching game. The model sees a photo and a set of captions, and it has to find the caption that came with that photo. OpenAI puts it this way: given an image, the model predicts which of 32,768 sampled text snippets was actually paired with it. Every wrong snippet teaches it what does not belong.

> "given an image, predict which out of a set of 32,768 randomly sampled text snippets, was actually paired with it in our dataset." (OpenAI)
>
> "the simple pre-training task of predicting which caption goes with which image is an efficient and scalable way to learn SOTA image representations from scratch" (CLIP paper, arXiv abstract)

*Diagram, How CLIP learns from pairs: Photos and captions each pass through an encoder. The scoring step checks every pair, and training pulls the matching pairs together while pushing the mismatches apart.*
1. A photo goes into the image encoder.
2. Its caption goes into the text encoder.
3. The photo's numbers go to the scoring step.
4. The caption's numbers go to the same step.
5. Matching pairs get pulled closer together.
6. Pairs that do not match get pushed apart.

The web already holds those pairs, so no one wrote labels for them. OpenAI also says the data is unfiltered and highly noisy, so the model learns from messy pairs. OpenAI first explored an image-to-text approach. It reports that, in small to medium experiments, the contrastive objective (the matching game) was 4x to 10x more efficient than the image-to-text approach at zero-shot ImageNet classification.

1. **Encode both sides:** Each photo and each caption becomes a list of numbers, so both can be compared in one shared space.
2. **Score every pair:** Compare each photo's numbers with each caption's numbers. The matching pairs should score highest.
3. **Adjust the encoders:** Nudge both encoders so right pairs move closer and wrong pairs move apart. Repeat over the whole dataset.

## How do you use CLIP to label a new picture?

This is the part that matters at work. You don't train CLIP again for each new task. OpenAI says it can be applied to any visual classification benchmark by simply providing the names of the visual categories. You write the possible answers as sentences, encode them, encode the photo, and pick the closest sentence.

The paper reports that this approach matched the ImageNet accuracy of the original ResNet-50 without using any of its 1.28 million training examples.

*Diagram, How CLIP picks a label: A new photo and the label sentences each pass through a trained encoder. The closest match wins, with no retraining for the new task.*
1. The new photo goes into the image encoder.
2. Each label sentence goes into the text encoder.
3. The photo's numbers go to the comparison step.
4. Each label's numbers go to the same step.
5. The label with the highest score moves on.

Here is the comparison with made-up numbers. The labels are sentences, the photo is compared with each one, and the closest match wins.

```python
import math

# Made-up 3-number vectors standing in for CLIP's much longer lists of numbers.
photo = [0.9, 0.2, 0.1]  # a new claim photo, after the image encoder
labels = {
    "a photo of a dented rear bumper": [0.8, 0.3, 0.1],
    "a photo of a cracked windscreen": [0.2, 0.9, 0.2],
    "a photo of a flooded footwell": [0.1, 0.2, 0.9],
}

def cosine(a, b):
    dot = sum(x * y for x, y in zip(a, b))
    size = math.sqrt(sum(x * x for x in a)) * math.sqrt(sum(y * y for y in b))
    return dot / size

scores = {text: cosine(photo, vec) for text, vec in labels.items()}
for text, score in sorted(scores.items(), key=lambda kv: -kv[1]):
    print(f"{score:.3f}  {text}")
best = max(scores, key=scores.get)
print("Closest label:", best)
```

Output:

```text
0.990  a photo of a dented rear bumper
0.434  a photo of a cracked windscreen
0.256  a photo of a flooded footwell
Closest label: a photo of a dented rear bumper
```

The answer is always the closest of the labels you gave. If none of them fit, CLIP still picks one, so set a minimum score or a minimum gap between the top two labels, tune it on checked photos from your own work, and send anything below it to a person.

## Where does CLIP fall short?

CLIP is strong on common objects and weak on some tasks. OpenAI lists its own limits, and these are the ones to know. One OpenAI test, on handwritten digits (MNIST), had zero-shot CLIP at 88%, against 99.75% for humans.

> "CLIP's zero-shot classifiers can be sensitive to wording or phrasing" (OpenAI)
>
> "when evaluated on handwritten digits from the MNIST dataset, zero-shot CLIP only achieves 88% accuracy, well below the 99.75% of humans on the dataset" (OpenAI, handwritten digits)

- **Counting and distance:** OpenAI says CLIP struggles to count objects and to judge how near the nearest car is. On those two tests it is only slightly better than random guessing.
- **Fine detail:** It trails models built for one task on car models, aircraft variants and flower species.
- **Images unlike its training data:** OpenAI says CLIP still generalises poorly to images not covered by its pre-training data.
- **Wording:** Its answers can change with how a label is phrased, so the label list needs testing.
- **Label design and bias:** How the classes are designed can shift both performance and bias, as OpenAI warns.

In claims or inspection work, a count or a model number can be the whole answer, so those are the first tests to run.

> **Tip:** Asked about limits, name counting, fine detail and wording, and say you test on your own data before trusting the result.

### How would you use CLIP to sort claims photos?

A claims team wants to sort new car damage photos into five damage types, and it adds a new type every quarter.

- **Plan A: Labels as sentences, tested.** Write each damage type as a sentence, test on past photos the team has already checked, and send unsure cases to an adjuster.
- **Plan B: Trust the top label.** Send the top label straight to payment. Counting and fine detail will slip through, and a wording change can flip the answer.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: a model that learns pictures and words together from captions.
- [ ] How it learns: match each photo to its caption, pull right pairs together, push wrong pairs apart.
- [ ] Zero-shot use: label with sentences you write, with no retraining for each task.
- [ ] Its limits: counting, fine detail, wording changes and bias from label design.
- [ ] How you would test it on your own photos before anyone relies on it.
- [ ] Its age: a 2021 model, so check newer options before you choose.

**Common traps**

- Trap: "CLIP writes captions for pictures." Better: No. It scores how well a caption fits a picture. Writing text is a different kind of model.
- Trap: "You need labelled data for every new class." Better: Not for zero-shot use. You write the labels as sentences, though you still need some checked photos to test with.
- Trap: "The top score is the true label." Better: It is the closest of the labels you gave. Set a minimum score and send weak matches to a person.

## Can you spot the problem?

**Spot the problem:** A claims team sends CLIP these label sets. Which one asks it for something it is weak at?

A. labels = ['a dented rear bumper', 'a cracked windscreen']
B. labels = ['one dent', 'two dents', 'three dents or more']
C. labels = ['front damage', 'rear damage']

**Answer:** B. Counting is one of CLIP's known weak spots, and OpenAI reports it is only slightly better than random guessing on counting tests. Use a model built for counting for that job, and use CLIP for the sentence labels it handles well.

**Quick check:** The team's results change when they add 'a photo of' in front of each label. What should they do?

A. Pick whichever wording scores best on a few photos and ship it
B. Test several wordings on checked photos from their own claims before choosing
C. Drop CLIP, since wording never matters
D. Add more labels to the list

**Answer:** B. OpenAI notes that CLIP's zero-shot classifiers can be sensitive to wording. Test several phrasings on checked photos from your own work, not on a few picked to flatter one wording.

## Can you answer these from memory?

**Flash cards**

- **What is CLIP?** An OpenAI model that learns to match pictures with the text written about them.
- **How is it trained?** It finds which caption goes with each image, across pairs collected from the internet.
- **What is zero-shot use?** Labelling with sentences you write, with no retraining for the new task.
- **Does CLIP write captions?** No. It scores how well a caption fits a picture.
- **Where does it fall short?** Counting, fine detail, wording changes, and bias from how labels are designed.
- **When was it published?** OpenAI in January 2021. The paper was submitted to arXiv on 26 February 2021.

## Likely follow-up questions

- Q441 What is a vision-language model? https://designervenkat.online/interview/ai/what-is-a-vision-language-model
- Q442 How do images become tokens? https://designervenkat.online/interview/ai/how-do-images-become-tokens
- Q444 How do diffusion models work, basically? https://designervenkat.online/interview/ai/how-do-diffusion-models-work-basically
- Q448 How do you extract structured data from documents and images? https://designervenkat.online/interview/ai/extract-structured-data-from-documents-and-images
- Q450 Video understanding basics? https://designervenkat.online/interview/ai/video-understanding-basics

## Learn it in depth

- [AI that sees, hears and reads](https://designervenkat.online/learn/ai-engineering/multimodal-ai)
- [Embeddings for builders](https://designervenkat.online/learn/ai-engineering/embeddings)
