Context Diffusion: In-Context Aware Image Generation

Ivona Najdenkoska, Animesh Sinha, Abhimanyu Dubey, Dhruv Mahajan, Vignesh Ramanathan, Filip Radenovic

Introduction

In-context learning has revolutionized Large Language Models (LLMs), allowing them to adapt to new tasks simply by observing a few examples in the prompt. However, bringing this capability to the visual domain has proven challenging. While diffusion models have achieved remarkable success in text-to-image generation, they often struggle to learn from visual examples without heavy reliance on textual descriptions.

Existing approaches, such as Prompt Diffusion, attempt in-context learning by using source-target image pairs. However, these models frequently exhibit a "textual crutch," where performance degrades significantly if the text prompt is removed or is insufficiently descriptive. Furthermore, they often fail to generalize to "out-of-domain" tasks—scenarios not encountered during training, such as converting a rough sketch into a photorealistic image.

Methodology: The Context Diffusion Framework

The authors introduce Context Diffusion, a framework designed to treat visual examples as primary conditioning signals. Unlike previous models that treat visual context as a secondary input, Context Diffusion integrates it directly into the generation process alongside text.

Architecture

The model is built upon a Latent Diffusion Model (LDM) backbone, augmented with a dual-path conditioning strategy:

  1. Visual Control (Layout): A "query image" (e.g., an edge map or segmentation mask) defines the spatial structure of the output. This is processed through a trainable LDM encoder, similar to the ControlNet architecture.
  2. Visual Context (Style and Detail): One or more "context images" provide the desired style, texture, and color. These images are encoded using a frozen CLIP vision encoder.
The Context Diffusion architecture separates layout control from visual context, using a modified cross-attention mechanism to combine text and visual tokens.

Modified Cross-Attention

The core innovation lies in the "Modified Cross-Attention" mechanism. Instead of simply injecting visual features into the UNet, the model extracts visual tokens from the context images and concatenates them with the text prompt tokens. This unified embedding is then used in the cross-attention layers, allowing the model to dynamically weigh visual cues against textual instructions.

Few-Shot Learning

To support multiple examples (few-shot learning), the framework aggregates visual tokens from all context images via summation before the final linear projection. This allows the model to refine its understanding of the desired style as more examples are provided, effectively "averaging" the visual cues.

Experimental Results

The model was trained on a large-scale synthetic dataset of 310,000 image-caption pairs, from which various control maps (HED, segmentation, depth) were extracted to create diverse tasks.

Qualitative Performance

Context Diffusion demonstrates superior fidelity to visual context compared to baselines. Notably, it maintains high output quality even when the text prompt is entirely absent ("Prompt: None"). It also shows strong zero-shot generalization to unseen query types like Canny edges and hand-drawn sketches, which were not part of the training set.

Qualitative comparison between Context Diffusion and Prompt Diffusion across in-domain and out-of-domain tasks, showcasing the impact of few-shot learning and prompt-less generation.

Quantitative Evaluation

In human evaluations, Context Diffusion achieved an 80.2% win rate in in-domain tasks and 63.7% in out-of-domain tasks when no text prompt was provided. Quantitatively, the model consistently outperformed Prompt Diffusion in FID scores. For HED-to-image tasks without text prompts, Context Diffusion reached an FID of 17.7, a significant improvement over the baseline's 22.5.

Discussion and Key Insights

  • Modality Balance: By treating visual tokens identically to text tokens in the attention mechanism, the model avoids over-reliance on text, allowing visual context to drive the generation when text is vague.
  • Structural Separation: The explicit separation of layout (query) and appearance (context) prevents "information bleeding," ensuring the model respects the query's structure while adopting the context's style.
  • Scaling with Examples: The transition from 1-shot to 3-shot learning results in a visible improvement in style consistency and detail, indicating that the model truly learns from the provided context rather than just memorizing patterns.

Conclusion

Context Diffusion represents a significant step toward true in-context learning for image generation. By decoupling layout from style and integrating visual context directly into the attention mechanism, it achieves state-of-the-art performance in both zero-shot and few-shot scenarios. Future research may explore more advanced token aggregation methods, such as attention-based pooling, to further enhance the model's ability to capture nuanced details from large sets of example images.

Summary updated

BibTeX
@inproceedings{najdenkoska2024context,
  author = {Ivona Najdenkoska and Animesh Sinha and Abhimanyu Dubey and Dhruv Mahajan and Vignesh Ramanathan and Filip Radenovic},
  title = {{Context Diffusion: In-Context Aware Image Generation}},
  booktitle = {European Conference on Computer Vision},
  year = {2024},
  doi = {10.1007/978-3-031-72980-5\_22},
}
Figure