Adapting Grad-CAM for Embedding Networks

Lei Chen, Jianhui Chen, Hossein Hajimirsadeghi, Greg Mori

Introduction

Interpretability in deep learning often relies on Grad-CAM (Gradient-weighted Class Activation Mapping) to highlight the regions of an image that drive a model's prediction. While effective for classification tasks with fixed labels, Grad-CAM fails for embedding networks used in image retrieval, face recognition, and recommendation systems. These networks map inputs into a high-dimensional vector space where similarity is defined by distance rather than discrete categories. This paper introduces a novel adaptation of Grad-CAM that leverages triplet loss and a weight-transfer mechanism to provide visual explanations for embedding-based decisions.

Methodology

The proposed framework addresses two primary challenges: the lack of class scores and the computational cost of backpropagation during inference.

Triplet-Based Grad-Weights

Instead of a class score, the system uses triplet loss as the differentiable signal. During training, the model samples multiple triplets (anchor, positive, and negative examples). Gradients of the triplet loss are calculated with respect to the feature maps to derive "grad-weights." To ensure stability, these weights are aggregated across 50 different triplets for each training image.

The "Top-M" Filter

The authors discovered that using all feature channels (e.g., 2,048 in ResNet-50) introduces significant noise. By selecting only the Top-M (e.g., 50) channels with the highest positive weights, the resulting heatmaps become sharper and more accurately localized on the object of interest.

The proposed architecture for adapting Grad-CAM to embedding networks, showing the offline training phase and the online testing phase.

Weight-Transfer for Inference

A major hurdle for real-time systems is that standard Grad-CAM requires a backward pass. To solve this, the authors build a database of embeddings and their corresponding grad-weights from the training set. For a new test image:

  1. The network performs a forward pass to generate an embedding.
  2. The system identifies the nearest neighbor in the training database.
  3. The grad-weights from that neighbor are "transferred" to the test image to generate the visual attention map.
An illustration of the weight-transfer process, where weights from a known training sample are applied to a new test image to generate a heatmap.

Key Insights and Results

  • Semantic Proximity: The core hypothesis is that images close in embedding space share semantic features. Therefore, the filters activated for a training image are likely the same ones relevant for its nearest neighbor in the test set.
  • Localization Accuracy: On the CUB200-2011 bird dataset, the "Top-50" method achieved a Bounding Box (BBox) accuracy score of 0.760, significantly higher than the standard Grad-CAM baseline of 0.643.
  • Real-World Application: In a house price estimation task (GTA-sold dataset), the model highlighted features like roads and structures. Incorporating these visual attention features into a regression model improved price prediction accuracy by 8.0%.
  • Efficiency: The weight-transfer method makes visual explanations nearly computationally "free," operating 5-10 times faster than complex variants like Grad-CAM++.

Conclusion

By adapting Grad-CAM for embedding networks, this research provides a scalable and efficient way to interpret similarity-based models. While the method is sensitive to domain shifts—where test images differ significantly from the training distribution—it offers a powerful tool for de-noising explanations and understanding the specialized neurons that drive high-dimensional embeddings.

Summary updated

Abstract

The gradient-weighted class activation mapping (Grad-CAM) method can faithfully highlight important regions in images for deep model prediction in image classification, image captioning and many other tasks. It uses the gradients in back-propagation as weights (grad-weights) to explain network decisions. However, applying Grad-CAM to embedding networks raises significant challenges because embedding networks are trained by millions of dynamically paired examples (e.g. triplets). To overcome these challenges, we propose an adaptation of the Grad-CAM method for embedding networks. First, we aggregate grad-weights from multiple training examples to improve the stability of Grad-CAM. Then, we develop an efficient weight-transfer method to explain decisions for any image without back-propagation. We extensively validate the method on the standard CUB200 dataset in which our method produces more accurate visual attention than the original Grad-CAM method. We also apply the method to a house price estimation application using images. The method produces convincing qualitative results, showcasing the practicality of our approach.
BibTeX
@inproceedings{chen2020adapting,
  author = {Lei Chen and Jianhui Chen and Hossein Hajimirsadeghi and Greg Mori},
  title = {{Adapting Grad-CAM for Embedding Networks}},
  booktitle = {IEEE/CVF Winter Conference on Applications of Computer Vision},
  year = {2020},
  url = {https://openreview.net/forum?id=yJ_dipos-ft},
}
Figure