CLIP — zero-shot classification
Background
CLIP learns a shared embedding space for images and text. To classify without retraining, embed the image and several candidate captions, then pick the caption whose embedding is most cosine-similar to the image. Scores come from a softmax over the (temperature-scaled) cosine similarities:
where are unit-normalized embeddings and is the logit scale.
Problem statement
Implement zero_shot_classify(image_emb, text_embs, temperature=100.0) returning (label, probs).
Input
image_emb— array-like of lengthD, the image embedding.text_embs— array-like of shape(K, D), one embedding per candidate caption.temperature—float, the logit scale (default100.0).
Output
Returns (label, probs): label is the int index of the best caption; probs is an np.ndarray of K softmax probabilities summing to 1.
Examples
Example 1
Input: image_emb = [1, 0], text_embs = [[1, 0], [0, 1]]
Output: label = 0, probs ≈ [1.0, 0.0]
Explanation: the image matches caption 0 exactly (cosine 1), caption 1 not at all (cosine 0); with τ=100 the softmax is essentially one-hot.
Constraints
- Unit-normalize the image and each text embedding before the dot product (cosine similarity).
- Scale similarities by
temperature, softmax, then argmax. - Return
(int label, np.ndarray probs)withprobssumming to 1.
Notes
- This is why CLIP does "open-vocabulary" classification: swap the candidate captions and you've changed the label set with zero training.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Exact match example picks caption 0
- •Cosine reference ignores magnitude
- •Sample probs at temperature 10