{"notes":[{"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"}]},{"id":"multimodality/clip","category":"multimodality","legacyPath":"multimodality/2103-clip","legacySlug":"2103-clip","legacyPaths":["multi-model/2021/clip"],"title":"CLIP","subtitle":"Breaking the Dimensional Barrier","author":"Z. Yuan","paperDate":"2021-02-26","publishedOn":"2024-04-04","paperTitle":"Learning Transferable Visual Models From Natural Language Supervision","paperUrl":"https://arxiv.org/abs/2103.00020","bibliography":{"title":"Learning Transferable Visual Models From Natural Language Supervision","authors":["Alec Radford","Jong Wook Kim","Chris Hallacy","Aditya Ramesh","Gabriel Goh","Sandhini Agarwal","Girish Sastry","Amanda Askell","Pamela Mishkin","Jack Clark","Gretchen Krueger","Ilya Sutskever"],"url":"https://arxiv.org/abs/2103.00020","pdfUrl":"https://cdn.openai.com/papers/Learning_Transferable_Visual_Models_From_Natural_Language.pdf","codeUrl":"https://github.com/openai/CLIP","preprint":{"id":"2103.00020","submittedOn":"2021-02-26","url":"https://arxiv.org/abs/2103.00020"},"publication":{"venue":"ICML","year":2021,"kind":"conference","url":"https://proceedings.mlr.press/v139/radford21a.html"},"citation":"@misc{multimodalityclip2021,\n  title = {{Learning Transferable Visual Models From Natural Language Supervision}},\n  author = {Alec Radford and Jong Wook Kim and Chris Hallacy and Aditya Ramesh and Gabriel Goh and Sandhini Agarwal and Girish Sastry and Amanda Askell and Pamela Mishkin and Jack Clark and Gretchen Krueger and Ilya Sutskever},\n  year = {2021},\n  eprint = {2103.00020},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/2103.00020}\n}\n","citationPath":"/content/papers/citations/multimodality/clip.a63b118c04c7.bib"},"thumbnail":{"src":"/content/papers/assets/03a0527e167fb49905db2879fecd0e0b7b140cb837e239778ffa7f83f02e25ef.jpg","width":1726,"height":622},"locale":"en","path":"/content/papers/en/multimodality/clip.f9c727d9ae23.md","headings":[{"id":"defining-the-problem","text":"Defining the Problem","depth":2},{"id":"the-solution","text":"The Solution","depth":2},{"id":"model-architecture","text":"Model Architecture","depth":3},{"id":"large-scale-dataset","text":"Large-Scale Dataset","depth":3},{"id":"training-details","text":"Training Details","depth":3},{"id":"discussion","text":"Discussion","depth":2},{"id":"zero-shot-learning","text":"Zero-Shot Learning","depth":3},{"id":"are-the-representations-any-good","text":"Are the Representations Any Good?","depth":3},{"id":"is-clip-truly-generalizable","text":"Is CLIP Truly Generalizable?","depth":3},{"id":"human-vs-machine","text":"Human vs. Machine","depth":3},{"id":"overlap-in-data","text":"Overlap in Data","depth":3},{"id":"limitations","text":"Limitations","depth":2},{"id":"ethical-concerns","text":"Ethical Concerns","depth":3},{"id":"conclusion","text":"Conclusion","depth":2},{"id":"key-contributions","text":"Key Contributions:","depth":3},{"id":"future-directions","text":"Future Directions:","depth":3}],"anchorAliases":{"match-the-text-before-trying-to-write-it":"model-architecture","four-hundred-million-pairs-and-a-sizable-training-bill":"large-scale-dataset","experimental-results-and-discussion":"discussion","strong-zero-shot-results-depend-on-the-task-and-the-baseline":"zero-shot-learning","distribution-shift-figure-13s-strength-and-figure-14s-warning":"is-clip-truly-generalizable","humans-overlapping-data-and-use-boundaries":"human-vs-machine"},"guide":null,"relatedReading":[{"id":"model-tuning/coop","relation":"next","topic":"Adapting image-text models with prompts"},{"id":"multimodality/flip","relation":"comparison","topic":"Scaling and accelerating image-text pretraining"},{"id":"multimodality/florence","relation":"comparison","topic":"Scaling and accelerating image-text pretraining"}]},{"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"}]}],"text":"Earlier, we discussed the **Soft Prompts** paper. Now, let’s take a look at its subsequent applications.\n\n\n> [!TIP]\n> \n> If you haven’t read it yet, you can refer to our previous article:\n> \n> - [**[21.04] Soft Prompts: A soft reminder**](/blog/papers/model-tuning/soft-prompts)\n\n\nThis plugin-based method for model tuning is primarily aimed at LLMs or VLMs.\n\nIn this paper, the authors target VLMs, specifically focusing on the well-known **CLIP**.\n\n\n> [!TIP]\n> \n> Haven’t heard of CLIP? Check out this article first:\n> \n> - [**[21.03] CLIP: Breaking the Dimensional Barrier**](/blog/papers/multimodality/clip)\n\n\nCLIP’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.\n\nThe authors decided to apply the idea of Soft Prompts to CLIP, resulting in this paper.\n\n## Problem Definition\n\n\n\n![problem](/content/papers/assets/c1646569fe96c052710b4492d44640ea130ac35b56f9653d7669ec7b1d1774b2.jpg)\n\n\n\nThe 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.\n\nAs 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%!\n\nThe 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.\n\nWhat’s going on here? How can just a few seemingly insignificant words lead to such dramatic differences?\n\n\n> [!TIP]\n> \n> Just when you’ve finally grasped the characteristics of one model, a new one enters the market, forcing you to start all over again.\n\n\n## Reviewing CLIP\n\nWe’re guessing you don’t want to read the CLIP paper, so here’s a quick review of its architecture.\n\n\n\n![clip](/content/papers/assets/03a0527e167fb49905db2879fecd0e0b7b140cb837e239778ffa7f83f02e25ef.jpg)\n\n\n\nCLIP consists of two encoders:\n\n- **Image Encoder**: Embeds high-dimensional image data into a low-dimensional feature space. The architecture can use either CNNs or ViTs.\n- **Text Encoder**: Based on a Transformer architecture, it converts natural language into textual representations.\n\nText 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](https://github.com/openai/CLIP/blob/main/clip/simple_tokenizer.py) has 49,408 entries including special tokens. Follow the checkpoint configuration when implementing it.\n\nThe 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.\n\nCLIP 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**.\n\nReview complete—congratulations, you’re now a CLIP expert!\n\n## Problem Solving\n\n### Model Architecture\n\n\n\n![problem](/content/papers/assets/86ecd435ec6fa318673f5754231fe34217c79955e473b560d3ef270366b2aa5d.jpg)\n\n\n\nThe figure above illustrates the architecture of **Context Optimization (CoOp)**, which models contextual words as learnable continuous vectors, avoiding the need for manual prompt engineering.\n\nBoth 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, $f$ is the feature of image $x$, $K$ the class count and $\\tau$ the temperature. A class name can span multiple tokens.\n\nThe authors propose two context designs:\n\n- **Unified Context (UC)**\n\n  In the design of unified context prompts, all categories share the same contextual word vectors. The prompt structure is as follows:\n\n  $$\n  t = [V]_1 [V]_2 \\dots [V]_M  \\text{[CLASS]},\n  $$\n\n  where $[V]_m$ ($m \\in \\{1, \\dots, M\\}$) is a vector with the same dimensionality as the word embeddings (e.g., 512 for CLIP), and $M$ is a hyperparameter specifying the number of contextual tokens.\n\n  The inference process is as follows:\n\n  - The prompt $t$ is passed to the text encoder $g(\\cdot)$, producing the corresponding classification weight vector (taken from the [EOS] token position).\n  - The prediction probability is calculated as:\n\n  $$\n  p(y = i|x) = \\frac{\\exp(\\cos(g(t_i), f)/\\tau)}{\\sum_{j=1}^K \\exp(\\cos(g(t_j), f)/\\tau)},\n  $$\n\n  where $t_i$ represents the prompt for the $i$-th category, including the word embedding vector for that category name.\n\n\n> [!TIP]\n> \n> In addition to placing the class token `[CLASS]` at the end of the prompt, it can also be placed in the middle, such as:\n> \n> $$\n> t = [V]_1 \\dots [V]_{\\frac{M}{2}} \\text{[CLASS]} [V]_{\\frac{M}{2}+1} \\dots [V]_M.\n> $$\n> \n\n\n- **Class-Specific Context (CSC)**\n\n  For class-specific context, each category has its own independent contextual word vectors. The prompt structure is as follows:\n\n  $$\n  t_i = [V]_i^1 [V]_i^2 \\dots [V]_i^M \\text{[CLASS]},\n  $$\n\n  where $[V]_i^m$ ($m \\in \\{1, \\dots, M\\}$) is a vector with the same dimensionality as the word embeddings, and $M$ is a hyperparameter specifying the number of contextual tokens.\n\n  The inference process is as follows:\n\n  - The prompt $t_i$ is passed to the text encoder $g(\\cdot)$, producing the corresponding classification weight vector (taken from the [EOS] token position).\n  - The prediction probability is calculated as:\n\n  $$\n  p(y = i|x) = \\frac{\\exp(\\cos(g(t_i), f)/\\tau)}{\\sum_{j=1}^K \\exp(\\cos(g(t_j), f)/\\tau)},\n  $$\n\n  where $t_i$ represents the prompt for the $i$-th category, including the word embedding vector for that category name.\n\n\n> [!TIP]\n> \n> In simpler terms, either all categories share a single set of learnable contextual word vectors, or each category has its own set.\n> \n> Intuitively, having a dedicated set of contextual word vectors for each category should yield better results.\n> \n> But is this really the case? Let’s take a look at the experimental results.\n\n\n## Discussion\n\n### Performance Across Multiple Datasets\n\n\n\n![result](/content/papers/assets/597e68951301a5a0ce3eb8c033892bfdde7cc7fd22cc4d3500570af82d217078.jpg)\n\n\n\nSolid lines represent the improvements of CoOp over CLIP, stars denote zero-shot CLIP.<br/>\nDashed lines indicate the linear probe alternative.<br/>\n$M$ denotes context length.<br/>\n“end” or “mid” refers to placing the class token at the end or middle of the context.<br/>\nCSC indicates class-specific context.\n\n\n\n\n\n\n---\n\nOn 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.\n\n- For specialized tasks such as **EuroSAT** and **DTD**, performance improvements exceed **45 percentage points** and **20 percentage points**, respectively.\n- 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**.\n- Even for the challenging **ImageNet** dataset (with 1,000 categories), a **4.77 percentage points** improvement is observed.\n\n### Comparison Between UC and CSC\n\nEarlier, we introduced **Unified Context (UC)** and **Class-Specific Context (CSC)**. Which one performs better?\n\nReferring back to the figure, let’s compare the performance with and without CSC.\n\nOn 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.\n\nThere 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.\n\n### Generalization and Transferability\n\n\n\n![zero-shot](/content/papers/assets/f5437b76d121d28e608e5349cad4ee6989419972595eb8aaa5c5605471260f26.jpg)\n\n\n\nSince 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.\n\nIn the table, `source` refers to using `ImageNet` for fine-tuning, and `target` refers to datasets used to test generalization capability.\n\nCoOp beats zero-shot CLIP in most of the tested distribution-shift settings, particularly with shorter contexts ($M=4$). There are exceptions: for ResNet-50 at $M=16$, 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.\n\n### How Long Should the Prompt Be?\n\n\n\n![context-length](/content/papers/assets/b5987a69a6129022c62306a759f2653636359073643e6873a07bde14383114a5.jpg)\n\n\n\nThe authors conducted experiments on 11 datasets, setting the context length to 4, 8, and 16.\n\nAcross 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.\n\n### Initialization Method\n\n\n\n![init](/content/papers/assets/a51f1c9bfd66370c68bd22b6dec16cf8d867e9a1f009553506c38f2c2ee95db2.jpg)\n\n\n\nThe authors compared the effects of random initialization and manual initialization. The manual initialization used the embedding vector of \"a photo of a.\"\n\nThe 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.\n\n### Interpretability of Prompts\n\n\n\n![explain](/content/papers/assets/13433e45562956277f36e1bf1d93b9d7df3eac6a7c9eb5b230e32bc26761d39d.jpg)\n\n\n\nSince the context vectors are optimized in a continuous space, they are difficult to interpret directly.\n\nThe authors adopted an indirect approach by searching for the nearest words in the vocabulary based on Euclidean distance to the learned vectors.\n\nBelow are the search results for some datasets:\n\n- **Food101**: \"enjoyed\" is somewhat relevant to the task.\n- **OxfordPets**: \"fluffy\" and \"paw\" have some degree of relevance.\n- **DTD**: \"pretty\" reflects material descriptions.\n\nNearest 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.\n\n### Conclusion\n\nThe 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.\n\nAs 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.\n\n\n> [!TIP]\n> \n> The original paper already discusses noisy Food101 labels and overfitting. Later [CoCoOp](/blog/papers/model-tuning/cocoop) focuses on unseen-class generalization within a dataset. These are distinct limitations; good distribution-shift results do not resolve all of them.\n> \n> We’ll revisit in the future to explore how subsequent research addresses these existing challenges.\n","imageSizes":{"/content/papers/assets/c1646569fe96c052710b4492d44640ea130ac35b56f9653d7669ec7b1d1774b2.jpg":{"width":1224,"height":476},"/content/papers/assets/03a0527e167fb49905db2879fecd0e0b7b140cb837e239778ffa7f83f02e25ef.jpg":{"width":1726,"height":622},"/content/papers/assets/86ecd435ec6fa318673f5754231fe34217c79955e473b560d3ef270366b2aa5d.jpg":{"width":1224,"height":564},"/content/papers/assets/597e68951301a5a0ce3eb8c033892bfdde7cc7fd22cc4d3500570af82d217078.jpg":{"width":1636,"height":1738},"/content/papers/assets/f5437b76d121d28e608e5349cad4ee6989419972595eb8aaa5c5605471260f26.jpg":{"width":1040,"height":896},"/content/papers/assets/b5987a69a6129022c62306a759f2653636359073643e6873a07bde14383114a5.jpg":{"width":940,"height":788},"/content/papers/assets/a51f1c9bfd66370c68bd22b6dec16cf8d867e9a1f009553506c38f2c2ee95db2.jpg":{"width":572,"height":252},"/content/papers/assets/13433e45562956277f36e1bf1d93b9d7df3eac6a7c9eb5b230e32bc26761d39d.jpg":{"width":1666,"height":802}}}
