Learning to Prompt for Vision-Language Models
- Publication
- International Journal of Computer Vision 2022Journal
- First submitted to arXiv
- arXiv:2109.01134
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{modeltuningcoop2021,
title = {{Learning to Prompt for Vision-Language Models}},
author = {Kaiyang Zhou and Jingkang Yang and Chen Change Loy and Ziwei Liu},
year = {2021},
eprint = {2109.01134},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2109.01134}
}
Earlier, we discussed the Soft Prompts paper. Now, let’s take a look at its subsequent applications.
This plugin-based method for model tuning is primarily aimed at LLMs or VLMs.
In this paper, the authors target VLMs, specifically focusing on the well-known CLIP.
CLIP’s paper appeared on arXiv in March 2021, followed by the Prompt Tuning paper in April. Continuous prompting also includes earlier work such as Prefix-Tuning; the April paper was not its sole starting point.
The authors decided to apply the idea of Soft Prompts to CLIP, resulting in this paper.
Problem Definition
The problem defined here is similar to that of Soft Prompts: manually crafted prompts have many limitations. They can produce drastically different results depending on the model and task.
As shown above, take CLIP as an example. If we use the prompt a photo of [CLASS], the accuracy is around 80%. But by adding just one extra word, changing it to a photo of a [CLASS], the accuracy surprisingly increases to 86%!
The four panels show different datasets, not four individual images that all improve after adding the same article. Caltech101 illustrates adding “a”; the other panels show task-related wording such as flower, texture and satellite.
What’s going on here? How can just a few seemingly insignificant words lead to such dramatic differences?
Reviewing CLIP
We’re guessing you don’t want to read the CLIP paper, so here’s a quick review of its architecture.
CLIP consists of two encoders:
- Image Encoder: Embeds high-dimensional image data into a low-dimensional feature space. The architecture can use either CNNs or ViTs.
- Text Encoder: Based on a Transformer architecture, it converts natural language into textual representations.
Text is tokenized with byte-level BPE and mapped to token embeddings. The commonly used text width here is 512, but CLIP variants can differ. Inputs include start/end tokens within the 77-token limit; the end-token representation is normalized and projected. The paper states a vocabulary of 49,152, whereas the released tokenizer has 49,408 entries including special tokens. Follow the checkpoint configuration when implementing it.
The goal of CLIP is to align image and text embeddings. Specifically, given a batch of image-text pairs, the model maximizes the cosine similarity for matched pairs while minimizing it for unmatched pairs.
CLIP is trained on an enormous dataset. To help the model learn diverse visual concepts, the CLIP team collected a massive training dataset comprising 400 million image-text pairs.
Review complete—congratulations, you’re now a CLIP expert!
Problem Solving
Model Architecture
The figure above illustrates the architecture of Context Optimization (CoOp), which models contextual words as learnable continuous vectors, avoiding the need for manual prompt engineering.
Both CLIP encoders stay frozen. Classification cross-entropy on labeled images updates only the context vectors; gradients still pass through the text encoder to reach them. Below, is the feature of image , the class count and the temperature. A class name can span multiple tokens.
The authors propose two context designs:
-
Unified Context (UC)
In the design of unified context prompts, all categories share the same contextual word vectors. The prompt structure is as follows:
where () is a vector with the same dimensionality as the word embeddings (e.g., 512 for CLIP), and is a hyperparameter specifying the number of contextual tokens.
The inference process is as follows:
- The prompt is passed to the text encoder , producing the corresponding classification weight vector (taken from the [EOS] token position).
- The prediction probability is calculated as:
where represents the prompt for the -th category, including the word embedding vector for that category name.
-
Class-Specific Context (CSC)
For class-specific context, each category has its own independent contextual word vectors. The prompt structure is as follows:
where () is a vector with the same dimensionality as the word embeddings, and is a hyperparameter specifying the number of contextual tokens.
The inference process is as follows:
- The prompt is passed to the text encoder , producing the corresponding classification weight vector (taken from the [EOS] token position).
- The prediction probability is calculated as:
where represents the prompt for the -th category, including the word embedding vector for that category name.
Discussion
Performance Across Multiple Datasets
Solid lines represent the improvements of CoOp over CLIP, stars denote zero-shot CLIP.
Dashed lines indicate the linear probe alternative.
denotes context length.
“end” or “mid” refers to placing the class token at the end or middle of the context.
CSC indicates class-specific context.
On average, CoOp exceeds zero-shot CLIP with two labeled examples per class, not two images for the entire dataset. At 16 examples per class, the average accuracy gain is about 15 percentage points.
- For specialized tasks such as EuroSAT and DTD, performance improvements exceed 45 percentage points and 20 percentage points, respectively.
- On fine-grained datasets (e.g., Flowers102, StanfordCars, FGVCAircraft) and scene/action recognition datasets (e.g., SUN397, UCF101), performance gains generally surpass 10 percentage points.
- Even for the challenging ImageNet dataset (with 1,000 categories), a 4.77 percentage points improvement is observed.
Comparison Between UC and CSC
Earlier, we introduced Unified Context (UC) and Class-Specific Context (CSC). Which one performs better?
Referring back to the figure, let’s compare the performance with and without CSC.
On average, UC performs better in most scenarios. At 16 shots, the plotted CSC curves are higher for some fine-grained datasets, such as Flowers102 and FGVCAircraft, and for EuroSAT. But the Figure 3 curves for StanfordCars and DTD do not show that advantage, even though the paper’s prose groups them among CSC-favored tasks. With fewer than eight shots, CSC also often trails UC.
There isn’t a universally better method; rather, the choice between UC and CSC depends on the task requirements. Selecting the appropriate version based on the dataset can further enhance performance.
Generalization and Transferability
Since CLIP already has strong zero-shot capabilities, the authors were concerned whether CoOp, fine-tuned on a specific dataset, might affect CLIP's generalization ability.
In the table, source refers to using ImageNet for fine-tuning, and target refers to datasets used to test generalization capability.
CoOp beats zero-shot CLIP in most of the tested distribution-shift settings, particularly with shorter contexts (). There are exceptions: for ResNet-50 at , ImageNet-Sketch scores 32.74 versus CLIP’s 33.32 and ImageNet-R scores 54.96 versus 56.00; ViT-B/32 on ImageNet-R also falls below CLIP at both tested context lengths. The table supports improvement in many configurations, not every backbone and target. Transfer to ImageNet variants with known classes is distinct from generalization to unseen classes.
How Long Should the Prompt Be?
The authors conducted experiments on 11 datasets, setting the context length to 4, 8, and 16.
Across the 11 datasets, more context tokens generally improve average accuracy. The mid placement gains more from 4 to 8 tokens, but at 16 tokens the plotted end result remains slightly higher than mid. There is no fixed best context length: source-dataset accuracy must be balanced against robustness to distribution shifts.
Initialization Method
The authors compared the effects of random initialization and manual initialization. The manual initialization used the embedding vector of "a photo of a."
The results show that good initialization has little impact on performance. While further tuning of the initialization tokens might help, random initialization is sufficiently efficient in practice.
Interpretability of Prompts
Since the context vectors are optimized in a continuous space, they are difficult to interpret directly.
The authors adopted an indirect approach by searching for the nearest words in the vocabulary based on Euclidean distance to the learned vectors.
Below are the search results for some datasets:
- Food101: "enjoyed" is somewhat relevant to the task.
- OxfordPets: "fluffy" and "paw" have some degree of relevance.
- DTD: "pretty" reflects material descriptions.
Nearest words offer clues, not a complete translation of the learned vectors. The vectors remain in the same embedding space; they are not higher-dimensional, but are free to move beyond the vocabulary’s discrete entries.
Conclusion
The experiments show that CoOp performs well across multiple datasets and surpasses manually designed prompts in many of the tested domain-shift settings. This highlights its potential for data-efficient learning.
As the first comprehensive study on adapting large-scale vision models through prompt learning, this work has significant pioneering value. It not only provides empirical support for prompt learning but also lays a solid foundation for future research.