What Makes Good Examples for Visual In-Context Learning?

Yuanhan Zhang, Kaiyang Zhou, Ziwei Liu · DOI

Visual In-Context Learning (VICL) allows large-scale vision models to adapt to new tasks through a few-shot "prompt" of example images and annotations. Despite its potential, VICL is highly sensitive to the specific examples chosen, where poor selections can lead to task failure. This paper introduces a prompt retrieval framework designed to automate the selection of high-quality examples. By leveraging supervised contrastive learning to identify examples with high semantic and spatial similarity to the query, the proposed method improves performance by up to 8% mIoU over random selection across various vision tasks.

Introduction: The Sensitivity of Visual In-Context Learning

In-context learning, a hallmark of Large Language Models (LLMs), has recently been extended to computer vision. By presenting a model with a grid of example images and their corresponding masks (e.g., foreground segmentation), a vision model can "learn" to perform that task on a new query image without any parameter updates.

However, visual models exhibit extreme prompt sensitivity. As shown in the figure below, the choice of example images can cause performance to fluctuate wildly. While random selection has been the standard practice, it often fails to provide the optimal context for the model. This research addresses the challenge of making VICL reliable by automating the selection of "good" examples.

Visual in-context learning sensitivity (a) and the proposed prompt retrieval framework (b).

The Prompt Retrieval Framework

The authors propose two primary strategies for example selection:

  1. Unsupervised Retrieval (UnsupPR): This approach utilizes off-the-shelf models like CLIP to find images in a training database that are visually similar to the query image. It relies on general feature similarity to provide context.
  2. Supervised Retrieval (SupPR): This core innovation involves training a dedicated retriever using contrastive learning. The system ranks candidate examples based on the vision model's actual performance (e.g., mIoU). Examples that yield high performance are treated as "positives," while poor performers are "negatives." The retriever is then trained to map the query image and high-performing examples into a shared feature space.
The Supervised Retrieval (SupPR) training pipeline, where examples are ranked by performance to train a learnable feature extractor.

Experimental Evaluation

The framework was tested on three distinct tasks using an image inpainting model as the base vision engine:

  • Foreground Segmentation: Evaluated on Pascal-$5^i$ and MSCOCO.
  • Single Object Detection: Evaluated on Pascal VOC.
  • Image Colorization: Evaluated on ImageNet-2012.

Notably, the base model was never explicitly trained on these datasets, testing its true in-context learning capabilities.

Key Insights: What Defines a "Good" Example?

The study reveals several critical factors for successful VICL:

  • Beyond Category Labels: A good example isn't just an image from the same class. The best prompts share semantic, spatial, and stylistic traits with the query. For instance, if a query image features a cat at a specific angle on a specific background, the most effective prompt is one with a similar pose and environment.
  • Quality Over Quantity: While performance generally scales with the number of examples (up to 7-8), the quality of those examples is paramount. A single, well-retrieved example often outperforms a larger set of randomly chosen ones.
  • Robustness to Order: Unlike NLP, where the sequence of examples can significantly impact LLM outputs, the arrangement of images in the visual grid has a negligible effect on performance, provided the examples are high-quality.

Results and Performance Gains

The retrieval-based approach demonstrated consistent improvements across all tasks:

  • Segmentation: SupPR achieved 35.56 mIoU, a significant increase from the 27.56 mIoU of random selection.
  • Object Detection: Performance improved from 25.45 to 28.22 mIoU.
  • Model Agnostic: The framework proved effective across different retriever backbones (CLIP, EVA, ViT), suggesting that "retrieval-as-prompting" is a robust paradigm for computer vision.

Conclusion and Future Work

While prompt retrieval significantly boosts VICL, challenges remain. Performance gains diminish under significant distribution shifts (e.g., training on Pascal and testing on MSCOCO). Furthermore, the effectiveness of prompting is ultimately capped by the base model's inherent capabilities; for tasks like colorization where the base model was weak, retrieval offered only marginal gains. Future research will likely focus on developing vision models that inherently better align spatial and semantic features to further unlock the potential of visual in-context learning.

Summary updated

BibTeX
@inproceedings{zhang2023what,
  author = {Yuanhan Zhang and Kaiyang Zhou and Ziwei Liu},
  title = {{What Makes Good Examples for Visual In-Context Learning?}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=pIXTMrBe7f},
}
Figure