arXiv(opens in a new tab)Computer ScienceSep 2021Cited by 3 papers on PaperVantage

Manuscript Offprint

arXiv:2109.01134

Learning to Prompt for Vision-Language Models

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

Abstract

Large pre-trained vision-language models like CLIP have shown great potential in learning representations that are transferable across a wide range of downstream tasks. Different from the traditional representation learning that is based mostly on discretized labels, vision-language pre-training aligns images and texts in a common feature space, which allows zero-shot transfer to a downstream task via prompting, i.e., classification weights are synthesized from natural language describing classes of interest. In this work, we show that a major challenge for deploying such models in practice is prompt engineering, which requires domain expertise and is extremely time-consuming—one needs to spend a significant amount of time on words tuning since a slight change in wording could have a huge impact on performance. Inspired by recent advances in prompt learning research in natural language processing (NLP), we propose Context Optimization (CoOp), a simple approach specifically for adapting CLIP-like vision-language models for downstream image recognition. Concretely, CoOp models a prompt’s context words with learnable vectors while the entire pre-trained parameters are kept fixed. To handle different image recognition tasks, we provide two implementations of CoOp: unified context and class-specific context. Through extensive experiments on 11 datasets, we demonstrate that CoOp requires as few as one or two shots to beat hand-crafted prompts with a decent margin and is able to gain significant improvements over prompt engineering with more shots, e.g., with 16 shots the average gain is around 15% (with the highest reaching over 45%). Despite being a learning-based approach, CoOp achieves superb domain generalization performance compared with the zero-shot model using hand-crafted prompts.

1 Introduction

Figure

Figure 1: Prompt engineering vs Context Optimization (CoOp). The former needs to use a held-out validation set for words tuning, which is inefficient; the latter automates the process and requires only a few labeled images for learning.

A common approach for building state-of-the-art visual recognition systems is to train vision models to predict for a fixed set of object categories using discrete labels (He et al., 2016; Dosovitskiy et al., 2021). From a technical point of view, this is achieved by matching image features—produced by a vision model like ResNet (He et al., 2016) or ViT (Dosovitskiy et al., 2021)—with a fixed set of weights that are seen as visual concepts and initialized randomly. Although training categories often have a textual form, such as “goldfish” or “toilet paper,” they will be converted into discrete labels just for easing the computation of the cross-entropy loss, leaving the semantics encapsulated in texts largely unexploited. Such a learning paradigm limits visual recognition systems to closed-set visual concepts, making them unable to deal with new categories since additional data are required for learning a new classifier.

Recently, vision-language pre-training such as CLIP (Radford et al., 2021) and ALIGN (Jia et al., 2021) has emerged as a promising alternative for visual representation learning. The main idea is to align images and raw texts using two separate encoders—one for each modality. For instance, both CLIP and ALIGN formulate the learning objective as a contrastive loss, which pulls together images and their textual descriptions while pushes away unmatched pairs in the feature space. By pre-training at a large scale, models can learn diverse visual concepts and can readily be transferred to any downstream task through prompting (Radford et al., 2021; Jia et al., 2021; Fürst et al., 2021; Li et al., 2021; Singh et al., 2021; Yuan et al., 2021). In particular, for any new classification task one can first synthesize the classification weights by giving sentences describing task-relevant categories to the text encoder, and then compare with image features produced by the image encoder.

We observe that for pre-trained vision-language models, the text input, known as prompt, plays a key role in downstream datasets. However, identifying the right prompt is a non-trivial task, which often takes a significant amount of time for words tuning—a slight change in wording could make a huge difference in performance. For instance, for Caltech101 (Figure 1(a), 2nd vs 3rd prompt), adding “a” before the class token brings more than 5% increase in accuracy. Moreover, prompt engineering also requires prior knowledge about the task and ideally the language model’s underlying mechanism. This is exemplified in Figure 1(b-d) where adding task-relevant context can lead to significant improvements, i.e., “flower” for Flowers102, “texture” for DTD and “satellite” for EuroSAT. Tuning the sentence structure could bring further improvements, e.g., putting “a type of flower” after the class token for Flowers102, keeping only “texture” in the context for DTD, and adding “centered” before “satellite photo” for EuroSAT. However, even with extensive tuning, the resulting prompts are by no means guaranteed to be optimal for these downstream tasks.

Inspired by recent prompt learning research in natural language processing (NLP) (Shin et al., 2020; Jiang et al., 2020; Zhong et al., 2021), we propose a simple approach called Context Optimization (CoOp) to automate prompt engineering, specifically for pre-trained vision-language models. Concretely, CoOp models a prompt’s context words with learnable vectors, which could be initialized with either random values or pre-trained word embeddings (see Figure 2). Two implementations are provided to handle tasks of different natures: one is based on unified context, which shares the same context with all classes and works well on most categories; while the other is based on class-specific context, which learns a specific set of context tokens for each class and is found to be more suitable for some fine-grained categories. During training, we simply minimize prediction errors using the cross-entropy loss with respect to the learnable context vectors while keeping the entire pre-trained parameters fixed. The gradients can be back-propagated all the way through the text encoder, distilling the rich knowledge encoded in the parameters for learning task-relevant context.

To demonstrate the effectiveness of CoOp, we benchmark on 11 datasets, which cover a diverse set of visual recognition tasks including classification on generic objects, scenes, actions and fine-grained categories, as well as specialized tasks like recognizing textures and satellite imagery. The results show that CoOp effectively turns pre-trained vision-language models into data-efficient visual learners, requiring as few as one or two shots to beat hand-crafted prompts with a decent margin. The performance can be further boosted by using more shots, e.g., with 16 shots the margin over hand-crafted prompts averages at around 15% and reaches over 45% for the highest. CoOp also outperforms the linear probe model, which is known as a strong few-shot learning baseline (Tian et al., 2020). Furthermore, CoOp demonstrates much stronger robustness than the zero-shot model (which uses manual prompts) to domain shifts, despite being a learning-based approach.

In summary, we make the following contributions:

  1. We present a timely study on the adaptation of recently proposed vision-language models in downstream applications and identify a critical problem associated with the deployment efficiency, i.e., prompt engineering.
  2. To automate prompt engineering specifically for pre-trained vision-language models, we propose a simple approach based on continuous prompt learning and provide two implementations that can handle different recognition tasks.
  3. We for the first time show that the proposed prompt learning-based approach outperforms both hand-crafted prompts and the linear probe model in terms of downstream transfer learning performance and robustness under domain shifts for large vision-language models.
  4. We open-source our project at https://github.com/KaiyangZhou/CoOp.

We hope the findings together with the open-source code can inspire and facilitate future research on efficient adaptation methods for large vision-language models—an emerging topic related to democratization of foundation models (Bommasani et al., 2021) i.e., making them easier and cheaper to adapt for the wider community.

References (25)

  • Modifying pixel space to adapt pre-trained models
  • A large-scale hierarchical image database
  • Learning visual representations from textual annotations
  • Transformers for image recognition at scale
  • Zero-shot learning using purely textual descriptions
  • An incremental bayesian approach tested on 101 object categories
  • A deep visual-semantic embedding model
  • Modern hopfield networks with infoloob outperform clip
  • Better vision-language models with feature adapters
  • A novel dataset and deep learning benchmark for land use and land cover classification
  • A critical analysis of out-of-distribution generalization
  • In: ICCV-W
  • Optimizing continuous prompts for generation
  • A data efficient contrastive language-image pre-training paradigm
  • A systematic survey of prompting methods in natural language processing
  • In: ICVGIP
  • Eliciting knowledge from language models with automatically generated prompts
  • A foundational language and vision alignment model
  • In: NeurIPS
  • a good embedding is all you need? In: ECCV
  • Fully test-time adaptation by entropy minimization
  • Large-scale scene recognition from abbey to zoo
  • A new foundation model for computer vision
  • Learning vs. learning to recall
  • A 10 million image database for scene recognition