Overview
Thezero_shot_metadata module provides pre-defined text templates and class names for zero-shot image classification. These templates are used with build_zero_shot_classifier to create robust classifiers without training.
Source: src/open_clip/zero_shot_metadata.py
Template Collections
OPENAI_IMAGENET_TEMPLATES
lambda c: f'a photo of a {c}.'lambda c: f'a bad photo of a {c}.'lambda c: f'a photo of many {c}.'lambda c: f'a sculpture of a {c}.'lambda c: f'a low resolution photo of the {c}.'lambda c: f'a rendering of a {c}.'lambda c: f'graffiti of a {c}.'lambda c: f'a cropped photo of the {c}.'lambda c: f'a bright photo of a {c}.'lambda c: f'a dark photo of the {c}.'lambda c: f'a black and white photo of the {c}.'lambda c: f'a painting of the {c}.'lambda c: f'a {c} in a video game.'lambda c: f'itap of a {c}.'(“I took a picture of”)
src/open_clip/zero_shot_metadata.py:2
SIMPLE_IMAGENET_TEMPLATES
src/open_clip/zero_shot_metadata.py:88
Reference: OpenAI CLIP Prompt Engineering Notebook
Class Names
IMAGENET_CLASSNAMES
"tench","goldfish","great white shark""tabby cat","tiger cat","Persian cat""golden retriever","labrador retriever""laptop computer","desktop computer""pizza","cheeseburger","ice cream"
src/open_clip/zero_shot_metadata.py:99
Usage Examples
Using SIMPLE_IMAGENET_TEMPLATES
Full ImageNet Classification
Comparing Template Sets
Custom Classes with Pre-defined Templates
Creating Custom Template Variants
Inspecting Template Output
Template Design Notes
Why Multiple Templates?
Using multiple templates improves classification robustness by:- Handling ambiguity - Different phrasings capture different aspects of a concept
- Averaging out noise - Multiple templates reduce sensitivity to specific wording
- Covering variations - Templates account for different visual presentations (size, quality, style)
Template Selection
- SIMPLE_IMAGENET_TEMPLATES: Use for faster inference with good accuracy (7 templates)
- OPENAI_IMAGENET_TEMPLATES: Use for best accuracy when computation time allows (80 templates)
- Custom templates: Create domain-specific templates for specialized applications
Performance Trade-offs
SIMPLE_IMAGENET_TEMPLATES provides the best balance of accuracy and speed.
Related Functions
build_zero_shot_classifier- Use these templates to build classifiers- Zero-Shot Classification Guide - Complete guide to zero-shot classification
