Back to paper library
arXiv 2022CVPR 2022Model adaptationPaper notes

CoCoOp

Read the image first

Original paper

Conditional Prompt Learning for Vision-Language Models

Paper authors

Kaiyang Zhou · Jingkang Yang · Chen Change Loy · Ziwei Liu

Publication
CVPR 2022Conference
First submitted to arXiv
arXiv:2203.05557
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{modeltuningcocoop2022,
  title = {{Conditional Prompt Learning for Vision-Language Models}},
  author = {Kaiyang Zhou and Jingkang Yang and Chen Change Loy and Ziwei Liu},
  year = {2022},
  eprint = {2203.05557},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2203.05557}
}
On this page

After the authors released CoOp, they found a problem.

CoOp learns context vectors that stay fixed after training rather than changing with each input image. Classification still compares image and text features; the model does use the image.

Defining the Problem

Since vision-language models usually contain hundreds of millions to billions of parameters, full-model fine-tuning is impractical in terms of resource requirements and may disrupt the model's pre-trained representation space. Therefore, before this, the authors proposed CoOp, which uses prompt learning to guide the model's generalization to new tasks.

However, CoOp overfits to specific training categories when learning context, causing a significant drop in accuracy on unseen categories. This static design lacks generalization ability and struggles to handle a broader range of scenarios.

An intuitive idea is: we can allow the model to adjust the content of the tokens based on the input image's information during prediction.

Solving the Problem

This paper's framework is almost the same as the previous CoOp, so we'll focus on some important parts.

Model Architecture

Zhou et al., Conditional Prompt Learning for Vision-Language Models, Figure 2, arXiv:2203.05557v2

Compared with CoOp, CoCoOp adds a branch that uses image features to produce a conditional token and adds it to the learned context vectors. Image information can therefore shape the continuous prompt vectors fed to the text encoder; this does not generate readable natural-language sentences.

CoCoOp uses a lightweight Meta-Net to produce an offset from image-encoder features and add it to each learned context vector. The Linear–ReLU–Linear bottleneck has a hidden width of 1/16 of the image-feature dimension. Below, xx denotes image features and gg the text encoder.

The conditional context vector formula is as follows:

vm(x)=vm+π,π=hθ(x)v_m(x) = v_m + \pi, \quad \pi = h_\theta(x)

The prompt format is updated to ti(x)={v1(x),v2(x),...,vM(x),ci}t_i(x) = \{v_1(x), v_2(x), ..., v_M(x), c_i\}, and the prediction probability formula is updated to:

p(y∣x)=exp⁡(sim(x,g(ty(x)))/τ)∑i=1Kexp⁡(sim(x,g(ti(x)))/τ)p(y|x) = \frac{\exp(\text{sim}(x, g(t_y(x)))/\tau)}{\sum_{i=1}^{K} \exp(\text{sim}(x, g(t_i(x)))/\tau)}

During training, the context vector {vm}m=1M\{v_m\}_{m=1}^M and Meta-Net parameters θ\theta are optimized simultaneously, while the base model (CLIP) remains frozen.

Discussion

Here, we mainly look at the differences between CoCoOp and CoOp.

Generalization Ability on Unseen Categories

Zhou et al., CoCoOp, Table 1, arXiv:2203.05557v2; training on 16 shots per base class

The authors conducted experiments on 11 different datasets. In the table, Base represents the base classes, New the unseen classes, and H their harmonic mean. CoOp is the first-generation model, and CoCoOp is the model from this paper.

From the table, we can observe that although CoOp performs better than CLIP on base categories, its accuracy drops significantly on new categories. The average accuracy drops from 82.69% on base categories to 63.22% on new categories, a difference of 19.47 percentage points. This may be because CoOp over-optimized base categories, leading to poor performance on new categories, highlighting the need to improve the generalization ability of learned prompts.

CoCoOp reaches 71.69% average accuracy on new classes, above CoOp’s 63.22% but still below manually prompted CLIP’s 74.22%. Five of the eleven datasets gain over 10 percentage points relative to CoOp. ImageNet has a smaller gain: 67.88% to 70.43%, or 2.55 points, exceeding CLIP’s 68.14%. The table’s H is the harmonic mean of Base and New, not their arithmetic mean.

Base accuracy drops on nine datasets, by less than 3 percentage points on six of them. On StanfordCars it drops 7.63 points while new-class accuracy gains 13.19. These changes sum to a positive 5.56 points, but their equal-weight arithmetic mean is 2.78 points. The table’s harmonic mean rises from 68.13% to 72.01%; neither this change nor the arithmetic mean is the 5.56-point sum used in the paper’s discussion.

Who Can’t Increase Parameters?

Zhou et al., CoCoOp, Table 5, arXiv:2203.05557v2

CoCoOp introduces Meta-Net, increasing the number of parameters. As an expert, you might immediately ask:

  • Is this improvement only due to the increased number of parameters?

To answer this, the authors designed an experiment where they removed Meta-Net from CoCoOp and increased the context number of CoOp to the maximum value with a similar number of parameters to CoCoOp.

In Table 5’s ImageNet comparison, CoOp (ctx=4) has 2,048 learnable parameters and 67.88% New accuracy. Increasing context to 60 gives 30,720 parameters but lowers New accuracy to 65.34%: 2.54 points below the original ctx=4 CoOp. CoCoOp with Meta-Net has 34,816 learnable parameters and 70.43% New accuracy. This comparison between similar parameter counts supports the value of conditional context in this setting. The 2.54-point drop is not a comparison with CoCoOp.

In this ImageNet comparison, increasing CoOp’s context length alone did not reproduce CoCoOp’s gain on new classes.

Conclusion

CoCoOp, based on the design of conditional prompts, improves generalization beyond static prompts and performs excellently in various scenarios, such as cross-dataset prompt transfer and domain generalization.

The authors mention at the end that CoCoOp’s training efficiency is significantly lower than that of CoOp, and in 7 of the 11 datasets, the performance on unseen categories is still lower than CLIP, indicating that the performance gap between learned and manual prompts has not been fully bridged, and the problem is far from over.