---
title: Conditional Context Optimization (CoCoOp)
url: https://www.emergentmind.com/topics/conditional-context-optimization-cocoop
type: topic
---

# Conditional Context Optimization (CoCoOp)

Conditional Context Optimization (CoCoOp) is a prompt learning framework for adapting pre-trained vision-language models, such as CLIP, to downstream tasks. CoCoOp extends Context Optimization (CoOp) by introducing dynamic prompts that are conditioned on each input image, overcoming the generalization limitations of static context prompts. Through a lightweight neural prompt generator, CoCoOp improves recognition accuracy on classes unseen during training, enhances transferability to new datasets, and achieves better domain robustness at the cost of additional computation per image [2203.05557].

## 1. Foundations: Vision-Language Models and Prompt Learning

CLIP (Contrastive Language–Image Pre-training) is a prominent vision-language model that aligns images and natural language via contrastive learning. During CLIP training, an image encoder $f(\cdot)$ and a text encoder $g(\cdot)$ are jointly optimized using the InfoNCE loss, mapping paired (image, text) inputs to a shared embedding space. At inference, classification for class $i$ is performed by synthesizing a prompt $t_i$ (e.g., “a photo of a {class}”), encoding it as $w_i = g(t_i)$, and computing class probabilities for an input image $I$ using cosine similarity between $f(I)$ and $w_i$:
$$
p(y|I) = \frac{\exp(\mathrm{sim}(x, w_y)/\tau)}{\sum_k \exp(\mathrm{sim}(x, w_k)/\tau)}
$$
where $x = f(I)$ and $\tau$ is a temperature parameter.

CLIP relies on hand-crafted, fixed prompts. While robust for zero-shot transfer, these may not maximize performance for specific downstream datasets. Context Optimization (CoOp) addresses this by learning a set of context vectors to optimize prompts on the target data. However, CoOp's static learned contexts overfit base classes and degrade substantially under class shift when evaluated on unseen classes.

## 2. Model Architecture and Conditional Prompt Generation

CoCoOp introduces instance-conditional prompts by augmenting CoOp's static context tokens with a dynamic token generated per input image. Its architecture includes:

- **Static Context Vectors**: $V = \{v_1, \dots, v_M\} \subset \mathbb{R}^d$ shared across all prompts, as in CoOp.
- **Class Name Embeddings**: For class $i$, $c_i \in \mathbb{R}^d$, possibly multi-token.
- **Meta-Net**: A lightweight neural network, $h_\theta: \mathbb{R}^d \to \mathbb{R}^d$, parameterized by $\theta$. $h_\theta$ takes image feature $f(I)$ and produces an instance-conditional token $\pi(I) = h_\theta(f(I))$. This network is a two-layer MLP with a bottleneck $d \to d/16 \to d$ with ReLU activation.

Conditional prompts are synthesized in two ways:
1. **Token Appending**: Append $\pi(I)$ as an additional token:
   $$
   \text{Prompt}(I, i) = [v_1, \dots, v_M, \pi(I), c_i]
   $$
2. **Token Addition** (used in the original work): Add $\pi(I)$ to each context vector:
   $$
   v_m(I) = v_m + \pi(I), \quad \text{Prompt}(I, i) = [v_1(I), \dots, v_M(I), c_i]
   $$

Each prompt token (static, conditional, and class) is fed to the frozen CLIP transformer $g_\text{text}$. Only $V$ and $\theta$ are tuned, keeping $f(\cdot)$ and $g_\text{text}(\cdot)$ frozen.

## 3. Training Objective and Optimization

Prompt learning with CoCoOp optimizes a class-conditional cross-entropy loss equivalent to the negative InfoNCE:
$$
p(y_i|I_i) = \frac{
    \exp( \mathrm{sim}( f(I_i), g_\text{text}(\text{Prompt}(I_i, y_i)) )/\tau )
}{
    \sum_{j=1}^{K} \exp( \mathrm{sim}( f(I_i), g_\text{text}(\text{Prompt}(I_i, j)) )/\tau )
}
$$
The loss is averaged over a batch of $N$ labeled images:
$$
\mathcal{L} = -\frac{1}{N} \sum_{i=1}^N \log p(y_i | I_i)
$$
CLIP backbone parameters are frozen; only the context vectors $V$ and Meta-Net weights $\theta$ are optimized via gradient descent.

## 4. Empirical Results and Generalization Performance

Experiments demonstrate that CoCoOp substantially improves generalization over CoOp, particularly under class shift and in cross-dataset transfer. Key findings include:

### Base-to-New Class Generalization (11 datasets, 16-shot setting)

| Method                | Base (%) | New (%) | Harmonic Mean (%) |
|-----------------------|----------|---------|-------------------|
| CLIP (manual prompt)  | 69.3     | 74.2    | 71.7              |
| CoOp (static)         | 82.7     | 63.2    | 71.7              |
| CoCoOp (conditional)  | 80.5     | 71.7    | 75.8              |

CoOp achieves high accuracy on base classes but suffers a ∼19% drop on new classes. CoCoOp reduces this gap to ∼8.8% and improves the harmonic mean by ∼4.2 points. On ImageNet's new split, CoCoOp increases new-class accuracy from 67.9% (CoOp) to 70.4%.

### Cross-Dataset Transfer (ImageNet-trained prompt evaluated zero-shot on 10 datasets)

Average accuracy increases from 63.9% (CoOp) to 65.7% (CoCoOp), with especially notable gains on Flowers (+3.2%), Aircraft (+4.5%), SUN (+3.2%), DTD (+3.8%), and UCF (+1.7%).

### Domain Generalization (ImageNet-trained prompt evaluated on variants)

| Dataset    | CoOp (%) | CoCoOp (%) |
|------------|----------|------------|
| Sketch     | 47.99    | 48.75      |
| A          | 49.71    | 50.63      |
| R          | 75.21    | 76.18      |

CoCoOp consistently matches or outperforms CoOp on out-of-distribution sets.

## 5. Ablation Analyses and Component Contributions

A range of ablations clarify the role of design choices:

- **Context Length ($M$)**: Increasing $M$ improves new-class accuracy for both CoOp and CoCoOp, though CoCoOp with $M=4$ plus the conditional token achieves comparable or better performance than CoOp with larger $M$.
- **Token Initialization**: Using CLIP’s embedding of "a photo of a" to initialize $v_m$ outperforms random Gaussian initialization on both base and new classes.
- **Meta-Net vs Parameter Count**: Increasing $M$ in static CoOp to match CoCoOp’s parameter count does not close the performance gap, confirming that instance conditioning, not just model size, is critical for generalization.
- **Class-Incremental Evaluation**: On test sets containing both base and new classes (no retraining), accuracy is 65.2% (CLIP), 65.6% (CoOp), and 69.1% (CoCoOp), supporting increased robustness to class expansion.

## 6. Limitations and Directions for Further Research

CoCoOp entails additional computational overhead compared to static prompt methods. Each image instance necessitates a unique prompt and thus an individual forward pass through the transformer $g_\text{text}$, precluding batch processing of prompts. In practice, training uses batch size 1 and more epochs, increasing memory and time requirements.

On 7 out of 11 datasets, CoCoOp’s accuracy on new classes still falls short of CLIP's manual prompts, indicating room for closing the gap under strong class shift. Potential future research includes:

- Developing more efficient conditional prompt architectures (e.g., parallel static and conditional tokens, shared adapter modules).
- Scaling Meta-Net capacity or pre-training it across diverse datasets for better transferability.
- Exploring richer conditional signals, such as multi-scale features or textual image descriptions.

In summary, CoCoOp’s innovation lies in dynamic, image-conditioned prompts, effectively addressing class-shift and out-of-distribution robustness by integrating a small, learnable neural network into the prompt generation process. This yields superior generalization to unseen classes and new domains, at the cost of greater training complexity and computational requirements [2203.05557].

Source: https://www.emergentmind.com/topics/conditional-context-optimization-cocoop