Zero-Shot Prediction from Class Logits
Problem Statement
A zero-shot CLIP classifier scores an image against one text embedding per class. Given the per-class similarity scores for a single image, return the predicted class name.
Background
Zero-shot classification turns each class into a text prompt ("a photo of a dog"), embeds it, and scores the image against every class embedding. The prediction is simply the class with the highest score:
Ties go to the class that appears first in the list.
Your Task
Implement:
def zero_shot_predict(scores, class_names):
Return the predicted class name (a string).
Input Format
- scores: list of floats, one per class.
- class_names: list of strings, same length as scores.
Output Format
- A single string.
Sample
print(zero_shot_predict([0.2, 0.9, 0.5], ["cat", "dog", "bird"]))
Output:
dog
Example
print(zero_shot_predict([0.2, 0.9, 0.5], ["cat", "dog", "bird"]))
dog
- Initialize the predicted index to the first class, setting the current maximum score to (associated with "cat").
- Compare the second score, (associated with "dog"), against the current maximum of . Since , update the predicted index to the second class.
- Compare the third score, (associated with "bird"), against the new maximum of . Since , the predicted index remains at the second class.
- The final output is dog
Constraints
1 <= len(scores) == len(class_names) <= 10000.- Ties go to the earliest class index.
- Return the class name string.