Plug-and-play Class-aware Knowledge Injection for Prompt Learning with Visual-Language Model

Junhui Yin, Nan Pu, Xinyu Zhang, Lingfeng Yang, Lin Wu, Xiaojie Wang, Zhun Zhong

TL;DR

The paper introduces CAKI (Class-Aware Knowledge Injection), a plug-and-play framework designed to boost the performance of Vision-Language Models (VLMs) like CLIP during prompt learning. By establishing a class-level knowledge bank from few-shot samples, CAKI dynamically retrieves and injects fine-grained, class-specific prompts at test time without requiring additional training during inference. This simple addition consistently improves downstream classification, domain generalization, segmentation, and object detection across multiple state-of-the-art prompt learning methods.

What Problem Does This Paper Address?

When adapting massive Vision-Language Models (VLMs) to downstream tasks using few-shot data, full fine-tuning is computationally impractical. Instead, researchers use Few-Shot Prompt Learning (FSPL) to optimize a small set of learnable prompt tokens while keeping the VLM frozen.

Current FSPL methods generally fall into two camps, both of which suffer from distinct limitations:

  1. Class-shared prompts: These learn a single set of prompts for an entire domain. Because they are coarse-grained, they often cause under-clustering, meaning distinct classes are easily misclassified into a single category.
  2. Instance-specific prompts: These generate unique prompts for every single image on the fly. However, they ignore broader class-level commonalities, leading to over-clustering, where images from the same class are falsely splintered into multiple sub-categories.

This paper addresses the missing middle ground: class-specific knowledge. The authors argue that explicitly capturing and injecting class-level information provides the fine-grained supervision needed to prevent both under- and over-clustering errors.

Methods

The authors propose the Class-Aware Knowledge Injection (CAKI) framework, which operates in two distinct stages:

1. Class-specific Prompt Generation (CSPG)

During the training phase, CAKI uses few-shot labeled samples to learn distinct, optimized prompt vectors ($P_c$) for each individual class. These are stored in a local Key-Value Cache Memory (Knowledge Bank):

  • Key: The VLM's text embedding of the class name.
  • Value: The learned class-specific prompt vector.

2. Query-key Prompt Matching (QKPM)

At test time, the exact class of an incoming image is unknown. To circumvent this, CAKI uses a training-free, coarse-to-fine retrieval mechanism:

  • Coarse Prediction: The test image is first processed by a baseline VLM (using standard class-shared prompts) to generate an initial probability distribution.
  • Retrieval: This initial distribution acts as a matching score to query the top-$K$ most relevant class-specific prompts from the knowledge bank. This leverages the fact that visually similar classes (e.g., cats and tigers) share meaningful prompt features.
  • Fine-Grained Ensemble: The model makes multiple predictions using the retrieved top-$K$ prompts, which are then ensembled using the matching scores as weights. This refined prediction is combined with the initial coarse prediction to produce the final output.

Datasets

The CAKI framework was comprehensively evaluated across several benchmarks:

  • Base-to-Novel & Few-Shot Classification (10 Datasets): Caltech-101, Oxford-Pets, Stanford Cars, Oxford-Flowers102, Food-101, FGVC Aircraft, EuroSAT, SUN-397, Describable Textures (DTD), and UCF-101.
  • Test-Time Domain Adaptation: DomainNet.
  • Domain Generalization: ImageNet (source) evaluated on out-of-distribution variants ImageNet-V2, ImageNet-S, ImageNet-A, and ImageNet-R.
  • Downstream Vision Tasks: Semantic segmentation (Cityscapes, BDD, Mapillary, ADE, Pascal, ACDC) and object detection.

Key Insights and Ideas

  • The "Middlespace" Matters: Class-specific prompts provide a crucial regularizing effect. They are fine-grained enough to separate distinct classes, yet stable enough across instances to prevent intra-class fracturing.
  • Exploiting Visual Ambiguity: Instead of trying to guess the exact class perfectly on the first pass, QKPM embraces the visual similarities between related classes. If an image looks 70% like a dog and 20% like a wolf, blending the "dog" and "wolf" prompts yields a more robust representation than relying on a generic domain prompt.
  • True Plug-and-Play Compatibility: Because the test-time matching mechanism is entirely training-free, CAKI acts as a modular wrapper. It can be seamlessly combined with both training-time methods (like CoOp, MaPLe, and PromptSRC) and test-time adaptation methods (like TPT), consistently yielding performance gains.

Key Results

  • Base-to-Novel Generalization: When plugged into existing methods like CoOp, CoCoOp, MaPLe, and PromptSRC under a 1-shot setting, CAKI consistently improved the Harmonic Mean (HM) accuracy across almost all 10 evaluation datasets. For instance, it provided a +3.3% boost to CoOp on Flower102 and a +3.1% boost to PromptSRC on FGVC Aircraft.
  • Downstream Tasks: Applying CAKI to semantic segmentation models (using SEEM-Tiny as a base) yielded an improvement of 1.3% mIoU on the BDD dataset and 1.9% on the ADE dataset.
  • Efficiency and Scalability: The storage requirements and training/testing times scale linearly with the number of classes. For example, with $K=3$ retrieved prompts, CAKI achieves a significant accuracy improvement with a highly practical inference latency of just 0.146 seconds per image.

Limitations and Future Work

  • Dependency on the Coarse Model: CAKI's retrieval mechanism relies on the initial prediction of the baseline model. If the coarse model's predictions lack meaningful semantic proximity to the true class (i.e., it is completely wrong), the retrieved prompts will be uninformative or noisy.
  • Unseen Novel Classes: CAKI can struggle with novel classes whose semantic characteristics are entirely unrepresented by the base classes stored in the knowledge bank.

Future Directions: The authors suggest integrating Large Language Models (LLMs) for adaptive semantic expansion to provide richer contextual cues for entirely unseen classes, as well as incorporating entropy-based uncertainty modeling to make the prompt retrieval process more robust.

Summary updated

BibTeX
@article{yin2026plug,
  author = {Junhui Yin and Nan Pu and Xinyu Zhang and Lingfeng Yang and Lin Wu and Xiaojie Wang and Zhun Zhong},
  title = {{Plug-and-play Class-aware Knowledge Injection for Prompt Learning with Visual-Language Model}},
  journal = {Int. J. Comput. Vis.},
  year = {2026},
  doi = {10.1007/s11263-026-02851-x},
}
Figure