Interpreting CLIP's Image Representation via Text-Based Decomposition

Yossi Gandelsman, Alexei A Efros, Jacob Steinhardt

OpenAI’s CLIP has become a cornerstone of modern AI, powering everything from image search to generative models like DALL-E. However, despite its widespread use, CLIP largely remains a "black box." While we know it maps images and text into a shared latent space, the exact mechanics of how its Vision Transformer (ViT) constructs its final image representation remain opaque.

Without understanding these internal mechanics, it is difficult to debug models when they fail—for instance, when they rely on "spurious correlations," such as identifying a waterbird based solely on the presence of a water background rather than the bird itself. Recent research tackles this challenge by "peeking inside" the CLIP image encoder, decomposing its complex representations into a sum of interpretable parts.

A Surgical Decomposition of CLIP

To understand how CLIP builds its representations, researchers leveraged three specific architectural features of the CLIP-ViT model:

  1. Residual Connections: Because ViTs add the output of each layer to a "residual stream," the final output can be mathematically viewed as a simple sum of contributions from every layer.
  2. Attention Mechanism: Within each layer, the output is a weighted sum of contributions from different image patches (locations).
  3. Joint Vision-Language Space: Because CLIP aligns images with text, internal visual features can be projected into the language space to discover which words "explain" them.
CLIP representation decomposition into interpretable text and spatial components

The centerpiece of this methodology is TEXTSPAN, a greedy algorithm designed to find a "basis" of text descriptions for each attention head. By feeding thousands of ChatGPT-generated descriptions into the model, TEXTSPAN identifies which text directions best explain the variance in a specific head's output.

Key Insights: Inside the Black Box

By applying this decomposition, several fascinating properties of CLIP's internal structure emerged:

  • The "Late Bloomer" Effect: Through "mean-ablation" (averaging out components to test their necessity), researchers found that the vast majority of CLIP’s final representation is constructed in just the last four attention layers. Surprisingly, the Multi-Layer Perceptron (MLP) blocks—which contain a massive portion of the model's parameters—have almost no direct effect on the final output representation.
  • Head Specialization: Attention heads are not generic processors; they act as highly specialized "experts." The decomposition revealed "counting heads" that track the number of subjects, "location heads" that look for geographic cues, and "shape heads" that specialize in geometric primitives like ovals or triangles.
  • Emergent Localization: Even though CLIP is trained on image-level text descriptions and is never explicitly trained to segment images, individual tokens naturally "write" information to the representation that corresponds directly to their physical location in the image.

Fixing Biases and Improving Performance

Understanding these internal mechanisms isn't just an academic exercise; it has immediate practical benefits for model repair and downstream tasks.

  • Eliminating Spurious Correlations: On the Waterbirds dataset—a benchmark designed to test if models rely on background cues—researchers identified which attention heads were responsible for "geolocation" (the background water/land). By simply "turning off" these specific heads without any retraining, the worst-group accuracy for the ViT-L model jumped dramatically from 48% to 73%.
  • State-of-the-Art Zero-Shot Segmentation: Leveraging the emergent localization discovered in the image tokens, the researchers built a zero-shot segmenter. This method outperformed existing explainability methods (like GradCAM), achieving a Mean Intersection over Union (mIoU) of 54.50 on ImageNet-segmentation, compared to the previous best of 47.47.
  • Representation Compression: The study showed that projecting a head's 768-dimensional output onto just 60 text-based directions found by TEXTSPAN allowed the model to maintain 72.77% accuracy (down only slightly from a base of 75.25%). This proves that these text labels genuinely capture the "essence" of the model's logic.

Limitations and Future Directions

While this text-based decomposition offers a powerful new lens into CLIP, the authors note that their method focuses primarily on direct effects—how a component contributes directly to the final sum. It does not fully account for indirect effects, where an early layer might significantly alter the input for a later layer without directly adding to the final output.

Furthermore, not every attention head yields a clear, human-readable role. Some heads may represent complex, abstract features that don't map neatly to single text concepts, or they might work in ensembles. Future work aims to apply this logic to other architectures, paving the way for more efficient, interpretable "white-box" AI models.

Summary updated

BibTeX
@inproceedings{gandelsman2024interpreting,
  author = {Yossi Gandelsman and Alexei A Efros and Jacob Steinhardt},
  title = {{Interpreting CLIP's Image Representation via Text-Based Decomposition}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=5Ca9sSzuDp},
}
Figure