TL;DR
Can off-the-shelf image diffusion models perform visual in-context learning without any task-specific training? This paper introduces SD-VICL, a training-free framework that repurposes a frozen Stable Diffusion model for vision tasks simply by rewiring its internal self-attention mechanisms and introducing an implicitly-weighted prompt ensembling method. Without a single parameter update, SD-VICL outperforms prior models trained specifically for visual in-context learning across six diverse vision benchmarks.
What Problem Does This Paper Address?
In Natural Language Processing, Large Language Models (LLMs) possess an emergent superpower known as in-context learning (ICL): given a couple of input-output demonstrations in a prompt, the model solves new tasks on unseen queries without updating its weights.
Bringing this capability to computer vision—Visual In-Context Learning (V-ICL)—has proven tricky. Current visual prompting approaches typically require heavy training:
- Uncurated methods (e.g., Visual Prompting, IMProv) train masked autoencoders to "inpaint" the answer within a multi-panel image grid. However, shrinking multiple high-resolution images into a single grid destroys fine details, and models frequently fail to interpret relationships between prompt pairs.
- Curated methods (e.g., Painter, SegGPT) train vision transformers on explicit multi-task datasets. This limits generalization to task distributions seen during training, defeating the true zero-shot promise of in-context learning.
The authors ask: Can an off-the-shelf vision foundation model execute visual in-context learning out-of-the-box, without any fine-tuning or curated task data?
Methods
The authors introduce SD-VICL (Stable Diffusion based Visual In-Context Learning), an inference-only pipeline built on standard pre-trained Stable Diffusion:
Query Image (C) --------> Q_C \
Prompt Source (A) ------> K_A --> [Attention Map] x V_B --> Prediction Latent (D)
Prompt Target (B) ------> V_B /
- Latent Inversion & Parallel Denoising: The prompt image ($A$), prompt ground truth ($B$), and query image ($C$) are mapped to diffusion noise latents via DDPM inversion. The target output latent ($D$) is initialized with the query's noise latent ($z_D^{(T)} \leftarrow z_C^{(T)}$) to preserve structural alignment.
- In-Place Attention Re-computation: In the U-Net upsampling self-attention layers, the standard self-attention is rewired: $$\Delta \phi_D^{(t)} = \text{softmax}\left(\frac{Q_C^{(t)} \cdot (K_A^{(t)})^T}{\tau \sqrt{d}}\right) \cdot V_B^{(t)}$$
- Query ($Q_C$) captures the spatial semantics of the test image.
- Key ($K_A$) encodes reference context in the prompt image.
- Value ($V_B$) injects the ground truth output domain (e.g., masks, sketches, colors).
- Temperature $\tau$ modulates attention sharpness.
- Artifact Suppression & Domain Alignment:
- Attention Contrasting: Enhances salient semantic correlations and suppresses background noise.
- Swap-Guidance: Blends modified attention updates with default generative noise predictions over time, preventing artifacts.
- AdaIN: Aligns the latent color and distribution statistics of prediction $D$ to match the prompt target $B$.
- Implicitly-Weighted Prompt Ensembling (IWPE): When multiple reference prompts are available, their Key ($K_{A_i}$) and Value ($V_{B_i}$) vectors are concatenated along the spatial sequence dimension. Unlike uniform feature averaging, query patches dynamically attend to the most semantically relevant prompt patches.
Datasets
The approach is evaluated zero-shot across six distinct vision tasks:
- Foreground Segmentation & Single Object Detection: Pascal-$5^i$
- Semantic Segmentation: Cityscapes
- Keypoint Detection: DeepFashion
- Edge Detection: NYUDv2
- Colorization: ImageNet
Demonstrations are selected unsupervised via cosine similarity over frozen CLIP image embeddings. No new training datasets are introduced.
Key Insights and Ideas
- Diffusion models are latent visual correspondences engines: Generative text-to-image models implicitly learn fine-grained spatial and semantic correspondences between visual concepts. Rewiring $Q_C \times K_A^T$ unlocks these correspondences without training.
- Cross-image domain transfer via Value swapping: Using $V_B$ forces the generated tokens into the output domain (e.g., keypoint coordinates, binary silhouettes, or RGB segmentation palettes) while retaining the structural geometry dictated by $Q_C$.
- Attention concatenation beats grid-stitching: Providing multiple prompts by tiling them into a single canvas lowers resolution. Concatenating Keys and Values in the latent attention layers preserves full resolution and lets the softmax attention naturally weight the most informative prompt.
Key Results
SD-VICL consistently outperforms prior V-ICL methods (both uncurated and curated models):
| Task | Metric | Visual Prompting [3] | IMProv [33] | SD-VICL (1 Prompt) | SD-VICL (5 Prompts) |
|---|---|---|---|---|---|
| Foreground Seg. | mIoU ↑ | 35.04 | 40.69 | 43.92 | 55.49 |
| Object Detection | mIoU ↑ | 46.29 | 44.42 | 51.55 | 57.10 |
| Semantic Seg. | mIoU ↑ | 21.30 | 15.80 | 22.08 | 27.01 |
| Keypoint Det. | MSE ↓ / PCK ↑ | 35.83 / 10.87 | 44.67 / 2.81 | 5.36 / 77.19 | 4.37 / 82.24 |
| Edge Detection | MSE ↓ / LPIPS ↓ | 0.1006 / 0.3925 | 0.1125 / 0.5146 | 0.0282 / 0.1548 | 0.0213 / 0.1216 |
| Colorization | FID ↓ | 111.06 | 105.90 | 53.40 | 44.84 |
- Outperforming Supervised Baselines: SD-VICL beats task-trained models like Painter (55.09 mIoU on FG Seg, 0.0926 MSE on Edge Detection) and LVM across several tasks without ever seeing task annotations during training.
- Speed vs. Prompts: Multi-prompt ensembling reduces required denoising steps. 5 prompts at 30 steps outperform 1 prompt at 70 steps while executing faster (35.9s vs. 38.4s on an NVIDIA A100 GPU).
Limitations and Future Work
- Inference Latency: Iterative diffusion denoising across multiple parallel branches ($A, B, C, D$) takes ~35 seconds per inference on an A100, which is far slower than feed-forward vision transformers.
- Hyperparameter Sensitivity: Parameters like temperature ($\tau$), contrasting strength ($\beta$), and swap-guidance scale ($\gamma$) still require careful tuning across tasks.
- Single-Prompt Bottleneck in Complex Scenes: In multi-class semantic segmentation, a single prompt rarely contains all classes present in the query, necessitating multi-prompt ensembling to resolve class ambiguity.
Summary updated