Skip to main content
Open In Colab A trained CLIP model can classify images into categories it was never trained on, without a single labeled example. This section shows that the procedure is an ordinary linear classifier whose weights are written in English, measures it on the 10,000 test images of CIFAR-10, and compares it with a classifier trained on labeled examples. The notation follows the CLIP section: ℓ\boldsymbol\ell is an image, t\mathbf{t} a text, fℓf_\ell and ftf_t the two encoders, and zℓ=fℓ(ℓ)\mathbf{z}_\ell = f_\ell(\boldsymbol\ell) and zt=ft(t)\mathbf{z}_t = f_t(\mathbf{t}) their unit-length embeddings.

Zero-shot classification is a linear classifier

For classes y∈{1,…,K}y \in \{1, \dots, K\}, write a prompt ty\mathbf{t}^y for each class, such as “a photo of a dog.”, and embed it, wy=ft(ty)\mathbf{w}_y = f_t(\mathbf{t}^y). For an image with embedding zℓ\mathbf{z}_\ell, predict the class whose prompt is most similar: y^=arg⁡max⁡y  wy⊤zℓ.\hat{y} = \arg\max_y \; \mathbf{w}_y^\top \mathbf{z}_\ell . This is a linear classifier on the image embedding. The weight matrix W=[w1,…,wK]⊤W = [\mathbf{w}_1, \dots, \mathbf{w}_K]^\top has one row per class, and the scores WzℓW \mathbf{z}_\ell followed by a softmax give class probabilities, exactly as in logistic regression. The difference is where the weights come from. Logistic regression fits WW to labeled images. Zero-shot classification computes WW from text, so changing the classes means changing the prompts, and no image of the new classes is needed. The quality of the classifier then depends on two things: how well the image embedding separates the classes, and how well the prompts point at them.

Embedding the data

The image encoder runs once over the CIFAR-10 test set and once over the training set. Every classifier below, zero-shot or trained, works on these frozen embeddings. The images are 32×3232 \times 32 and are enlarged to 224×224224 \times 224 by the processor, which makes this a harder test than the photos CLIP was trained on.

Prompts

Radford et al. found that the bare class name is a weak prompt (Radford et al., 2021). Captions on the web are sentences, not single words, and a single word can be ambiguous: “crane” is a bird and a machine. Wrapping the name in a sentence such as a photo of a \{class\}. moves the prompt closer to the captions the text encoder was trained on. Averaging the embeddings of several such sentences, prompt ensembling, gives a weight vector that depends less on the wording of any one of them. The cell compares the three choices.
The bare class names give 87.4% on the test set, the single template 88.3% and the ensemble of eight templates 89.6%. The gains are modest here because the ten CIFAR-10 names are common, unambiguous words. On data sets with rare or ambiguous class names, Radford et al. report much larger gains from the same two steps. Output from cell 5 Most errors fall between classes that look alike at 32×3232 \times 32 pixels. Cats and dogs are confused in both directions (7% each), 13% of the deer are called horses, and frogs, the weakest class at 78%, go to cats and birds. Horses (98%), ships (96%) and trucks (95%) are almost always right.

Zero-shot against a linear probe

How good is a classifier written in English, compared with one trained on labeled images? A linear probe answers this on the same embedding. It trains a logistic regression on the frozen image embeddings of kk labeled images per class and is tested on the same 10,000 test images. Both classifiers are linear in zℓ\mathbf{z}_\ell, so the comparison measures only where the weights come from. Radford et al. found that, averaged over 27 data sets, zero-shot CLIP matched a linear probe trained with 4 examples per class.
Output from cell 7 The zero-shot classifier (89.6%) is as accurate as a linear probe trained with 16 labeled images per class (89.9%), and clearly more accurate than one trained with 4 per class (84.3%). With all 5,000 images per class the probe reaches 95.0%, so the image embedding holds more information about these classes than the text prompts manage to point at. Prompts give a good classifier when there are no labels, and a few hundred labeled images are enough to do better.

Exercises

  1. Add the eight templates one at a time, in the order given, and plot the accuracy against the number of templates.
  2. Replace “automobile” with “car” and “airplane” with “plane” in the prompts. Does the accuracy change? What does this tell you about choosing class names?
  3. Initialize the weights of the logistic regression with the zero-shot weights WW (scaled by 100) and train it with 4 examples per class. Does starting from the text help?
  4. Evaluate the zero-shot classifier on CIFAR-100, which has 100 classes. How does the gap to the linear probe change with the number of classes?

Key references

Radford et al. introduced zero-shot transfer with CLIP and compared it with linear probes on 27 data sets (Radford et al., 2021).

References

  • Radford, A., Kim, J., Hallacy, C., Ramesh, A., Goh, G., et al. (2021). Learning transferable visual models from natural language supervision. arXiv [cs.CV]. Link

Found an error on this page? Report an issue.