{"notes":[{"id":"model-tuning/cocoop","category":"model-tuning","legacyPath":"model-tuning/2203-cocoop","legacySlug":"2203-cocoop","legacyPaths":[],"title":"CoCoOp","subtitle":"Read the image first","author":"Z. Yuan","paperDate":"2022-03-10","publishedOn":"2024-11-30","paperTitle":"Conditional Prompt Learning for Vision-Language Models","paperUrl":"https://arxiv.org/abs/2203.05557","bibliography":{"title":"Conditional Prompt Learning for Vision-Language Models","authors":["Kaiyang Zhou","Jingkang Yang","Chen Change Loy","Ziwei Liu"],"url":"https://arxiv.org/abs/2203.05557","pdfUrl":"https://arxiv.org/pdf/2203.05557","codeUrl":null,"preprint":{"id":"2203.05557","submittedOn":"2022-03-10","url":"https://arxiv.org/abs/2203.05557"},"publication":{"venue":"CVPR","year":2022,"kind":"conference","url":"https://arxiv.org/abs/2203.05557"},"citation":"@misc{modeltuningcocoop2022,\n  title = {{Conditional Prompt Learning for Vision-Language Models}},\n  author = {Kaiyang Zhou and Jingkang Yang and Chen Change Loy and Ziwei Liu},\n  year = {2022},\n  eprint = {2203.05557},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/2203.05557}\n}\n","citationPath":"/content/papers/citations/model-tuning/cocoop.be720ff7f0f6.bib"},"thumbnail":{"src":"/content/papers/assets/919586e6d93113289ff42da4bc65226ef2ba90f9b74c61fd9047d1aa7d8fb65e.jpg","width":1224,"height":580},"locale":"en","path":"/content/papers/en/model-tuning/cocoop.91c469f61e2d.md","headings":[{"id":"defining-the-problem","text":"Defining the Problem","depth":2},{"id":"solving-the-problem","text":"Solving the Problem","depth":2},{"id":"model-architecture","text":"Model Architecture","depth":3},{"id":"discussion","text":"Discussion","depth":2},{"id":"generalization-ability-on-unseen-categories","text":"Generalization Ability on Unseen Categories","depth":3},{"id":"who-cant-increase-parameters","text":"Who Can’t Increase Parameters?","depth":3},{"id":"conclusion","text":"Conclusion","depth":2}],"anchorAliases":{},"guide":null,"relatedReading":[{"id":"model-tuning/coop","relation":"background","topic":"Adapting image-text models with prompts"}]},{"id":"model-tuning/coop","category":"model-tuning","legacyPath":"model-tuning/2109-coop","legacySlug":"2109-coop","legacyPaths":[],"title":"CoOp","subtitle":"Whispering in the Wind","author":"Z. Yuan","paperDate":"2021-09-02","publishedOn":"2024-11-20","paperTitle":"Learning to Prompt for Vision-Language Models","paperUrl":"https://arxiv.org/abs/2109.01134","bibliography":{"title":"Learning to Prompt for Vision-Language Models","authors":["Kaiyang Zhou","Jingkang Yang","Chen Change Loy","Ziwei Liu"],"url":"https://arxiv.org/abs/2109.01134","pdfUrl":"https://arxiv.org/pdf/2109.01134","codeUrl":null,"preprint":{"id":"2109.01134","submittedOn":"2021-09-02","url":"https://arxiv.org/abs/2109.01134"},"publication":{"venue":"International Journal of Computer Vision","shortVenue":"IJCV","year":2022,"kind":"journal","url":"https://doi.org/10.1007/s11263-022-01653-1"},"citation":"@misc{modeltuningcoop2021,\n  title = {{Learning to Prompt for Vision-Language Models}},\n  author = {Kaiyang Zhou and Jingkang Yang and Chen Change Loy and Ziwei Liu},\n  year = {2021},\n  eprint = {2109.01134},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/2109.01134}\n}\n","citationPath":"/content/papers/citations/model-tuning/coop.27fdb87fd425.bib"},"thumbnail":{"src":"/content/papers/assets/86ecd435ec6fa318673f5754231fe34217c79955e473b560d3ef270366b2aa5d.jpg","width":1224,"height":564},"locale":"en","path":"/content/papers/en/model-tuning/coop.d8071220b5a0.md","headings":[{"id":"problem-definition","text":"Problem Definition","depth":2},{"id":"reviewing-clip","text":"Reviewing CLIP","depth":2},{"id":"problem-solving","text":"Problem Solving","depth":2},{"id":"model-architecture","text":"Model Architecture","depth":3},{"id":"discussion","text":"Discussion","depth":2},{"id":"performance-across-multiple-datasets","text":"Performance Across Multiple Datasets","depth":3},{"id":"comparison-between-uc-and-csc","text":"Comparison Between UC and CSC","depth":3},{"id":"generalization-and-transferability","text":"Generalization and Transferability","depth":3},{"id":"how-long-should-the-prompt-be","text":"How Long Should the Prompt Be?","depth":3},{"id":"initialization-method","text":"Initialization Method","depth":3},{"id":"interpretability-of-prompts","text":"Interpretability of Prompts","depth":3},{"id":"conclusion","text":"Conclusion","depth":3}],"anchorAliases":{},"guide":null,"relatedReading":[{"id":"multimodality/clip","relation":"background","topic":"Adapting image-text models with prompts"},{"id":"model-tuning/cocoop","relation":"next","topic":"Adapting image-text models with prompts"}]}],"text":"After the authors released CoOp, they found a problem.\n\nCoOp 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.\n\n\n> [!TIP]\n> \n> If you haven’t read CoOp yet, you can refer to our previous article:\n> \n> - [**[21.09] CoOp: Swaying Whispers**](/blog/papers/model-tuning/coop)\n\n\n## Defining the Problem\n\nSince 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.\n\nHowever, 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.\n\nAn intuitive idea is: we can allow the model to adjust the content of the tokens based on the input image's information during prediction.\n\n## Solving the Problem\n\nThis paper's framework is almost the same as the previous CoOp, so we'll focus on some important parts.\n\n### Model Architecture\n\n\n\n![CoCoOp conditions learned context on image features through Meta-Net; paper Figure 2](/content/papers/assets/919586e6d93113289ff42da4bc65226ef2ba90f9b74c61fd9047d1aa7d8fb65e.jpg \"Zhou et al., Conditional Prompt Learning for Vision-Language Models, Figure 2, arXiv:2203.05557v2\")\n\n\n\nCompared 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.\n\n\n> [!TIP]\n> \n> The architecture of the first-generation CoOp looks like this:\n> \n> \n> \n> ![Static learned context in CoOp; Figure 2 of the earlier CoOp paper](/content/papers/assets/86ecd435ec6fa318673f5754231fe34217c79955e473b560d3ef270366b2aa5d.jpg \"Zhou et al., Learning to Prompt for Vision-Language Models, Figure 2 schematic, arXiv:2109.01134v6; this is not a CoCoOp figure\")\n> \n> \n> \n\n\nCoCoOp 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, $x$ denotes image features and $g$ the text encoder.\n\nThe conditional context vector formula is as follows:\n\n$$\nv_m(x) = v_m + \\pi, \\quad \\pi = h_\\theta(x)\n$$\n\nThe prompt format is updated to $t_i(x) = \\{v_1(x), v_2(x), ..., v_M(x), c_i\\}$, and the prediction probability formula is updated to:\n\n$$\np(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)}\n$$\n\nDuring training, the context vector $\\{v_m\\}_{m=1}^M$ and Meta-Net parameters $\\theta$ are optimized simultaneously, while the base model (CLIP) remains frozen.\n\n\n> [!TIP]\n> \n> We just breezed through another paper!\n\n\n## Discussion\n\nHere, we mainly look at the differences between CoCoOp and CoOp.\n\n### Generalization Ability on Unseen Categories\n\n![CLIP, CoOp and CoCoOp Base, New and harmonic mean H across eleven datasets; paper Table 1](/content/papers/assets/b5cf52d266905ccab20629d2a39c2032803bec823c998e9bf8ee893b2a741bee.jpg \"Zhou et al., CoCoOp, Table 1, arXiv:2203.05557v2; training on 16 shots per base class\")\n\nThe 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.\n\nFrom 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.\n\nCoCoOp 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.\n\nBase 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.\n\n### Who Can’t Increase Parameters?\n\n\n\n![Learnable parameter counts and ImageNet results for longer CoOp context and CoCoOp Meta-Net; paper Table 5](/content/papers/assets/a1b74219eda2bb6859808b7e97b4aed25fe5b2fff07bd2ed5cabeb8448c277e8.jpg \"Zhou et al., CoCoOp, Table 5, arXiv:2203.05557v2\")\n\n\n\nCoCoOp introduces Meta-Net, increasing the number of parameters. As an expert, you might immediately ask:\n\n- **Is this improvement only due to the increased number of parameters?**\n\nTo 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.\n\nIn 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.\n\nIn this ImageNet comparison, increasing CoOp’s context length alone did not reproduce CoCoOp’s gain on new classes.\n\n## Conclusion\n\nCoCoOp, 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.\n\nThe 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.\n\n\n> [!TIP]\n> \n> One of the popular research directions in recent years is adding more fancy Prompt learning engineering between the image and text branches of CLIP. We’ll compile more on this when we have time.\n","imageSizes":{"/content/papers/assets/919586e6d93113289ff42da4bc65226ef2ba90f9b74c61fd9047d1aa7d8fb65e.jpg":{"width":1224,"height":580},"/content/papers/assets/86ecd435ec6fa318673f5754231fe34217c79955e473b560d3ef270366b2aa5d.jpg":{"width":1224,"height":564},"/content/papers/assets/b5cf52d266905ccab20629d2a39c2032803bec823c998e9bf8ee893b2a741bee.jpg":{"width":1224,"height":822},"/content/papers/assets/a1b74219eda2bb6859808b7e97b4aed25fe5b2fff07bd2ed5cabeb8448c277e8.jpg":{"width":784,"height":224}}}
