Abstract
Vision Transformer (ViT) is one of the most widely used models in the computer vision field with its great performance on various tasks. In order to fully utilize the ViT-based architecture in various applications, proper visualization methods with a decent localization performance are necessary, but these methods employed in CNN-based models are still not available in ViT due to its unique structure. In this work, we propose an attention-guided visualization method applied to ViT that provides a high-level semantic explanation for its decision. Our method selectively aggregates the gradients directly propagated from the classification output to each self-attention, collecting the contribution of image features extracted from each location of the input image. These gradients are additionally guided by the normalized self-attention scores, which are the pairwise patch correlation scores. They are used to supplement the gradients on the patch-level context information efficiently detected by the self-attention mechanism. This approach provides elaborate high-level semantic explanations with great localization performance only with the class labels. As a result, our method outperforms previous leading explainability methods of ViT in the weakly-supervised localization task and presents great capability in capturing the full instances of the target class object.
Introduction
While Vision Transformers (ViTs) have revolutionized computer vision, they remain notorious "black boxes." In the world of Convolutional Neural Networks (CNNs), tools like Grad-CAM highlight exactly which pixels led to a classification. However, applying these to ViTs is challenging due to their unique architecture—specifically the self-attention mechanism and the use of a special [class] token.
Existing ViT explainability methods often fall into two traps:
- Non-specificity: Methods like Attention Rollout show where the model is looking generally but cannot distinguish between different classes (e.g., "dog" vs. "cat").
- The Peak Intensity Problem: Methods like Layer-wise Relevance Propagation (LRP) often suffer from "peak intensity," where the heatmap focuses on a tiny, high-contrast point (like an eye) while ignoring the rest of the object. This is largely due to repeated Softmax operations amplifying high values and suppressing medium correlations.
Methods: Attention Guided CAM (AG-CAM)
The proposed Attention Guided CAM bridges the gap between gradient-based importance and the structural strengths of self-attention.
Gradient Aggregation
The method tracks gradients from the classification output ($y^c$) back through the skip connections of the transformer encoder. This captures the contribution of image features from every layer of the model, avoiding the information loss associated with only looking at the final layer.
The Sigmoid Trick
Instead of using raw attention scores influenced by Softmax, AG-CAM normalizes attention matrices using a Sigmoid function. Because Sigmoid is monotonically increasing but less aggressive than Softmax, it recovers "medium correlations" that Softmax usually suppresses, allowing the heatmap to cover the entire object rather than just a single peak.
Class Activation Mapping
By performing a Hadamard product between these gradients and the sigmoid-normalized attention maps, the method produces a final heatmap. The [class] token's contribution is discarded at the end to focus purely on the spatial image patches.
Results and Evaluation
AG-CAM was evaluated on standard benchmarks including ImageNet ILSVRC 2012, Pascal VOC 2012, and CUB 200.
Localization and Faithfulness
AG-CAM significantly outperformed existing methods across nearly all metrics:
- Localization Accuracy: On ImageNet, AG-CAM achieved an IoU of 0.5212, a substantial improvement over Attention Rollout (0.3597) and LRP-based methods (0.2029).
- Recall: A high recall of 0.6276 on ImageNet proves it captures the full extent of objects.
- Faithfulness: In pixel perturbation tests, AG-CAM achieved the highest ABPC score (0.3691), indicating the highlighted pixels are truly those the model relies on for its decision.
Multi-Instance Handling
Qualitatively, AG-CAM excels at capturing multiple instances of a target class. As shown in the comparison above, while other methods might focus on a single point or a general area, AG-CAM provides a comprehensive heatmap covering the entire object instance.
Conclusion
AG-CAM represents a major leap forward in making Vision Transformers interpretable. By leveraging gradients through skip connections and mitigating the "peak intensity" problem with sigmoid normalization, it provides class-specific, high-fidelity visualizations. Future work includes testing on diverse architectures like Swin Transformers and optimizing the computational cost associated with the required backward pass.
Summary updated