GradCAM for Convolutional Networks
What you'll learn: How to use gradient-weighted class activation mapping to visualize which spatial regions in an image most influence a CNN's predictions.
The Core Idea
While vanilla gradients and SmoothGrad tell you pixel-level importance, GradCAM (Gradient-weighted Class Activation Mapping) produces coarse, class-discriminative localization maps for CNNs. It answers: "Which regions of the image did the network focus on when predicting this class?"
GradCAM works by:
- Taking the gradients of a target class score with respect to feature maps in a convolutional layer (usually the last conv layer)
- Computing importance weights by global-average-pooling these gradients across spatial dimensions
- Creating a weighted sum of the feature maps using these weights
- Applying ReLU to highlight only positive contributions
Why GradCAM Works
Think of a CNN's later convolutional layers as containing high-level visual concepts (edges, textures, object parts). GradCAM asks: "How much does each spatial location in these feature maps contribute to my prediction?" The gradient flow from the class score back to these feature maps tells you exactly that. By averaging gradients across each channel, you get a weight representing that feature map's importance, then you combine weighted feature maps to create a heatmap.
The result is a coarse localization map the same size as the convolutional layer's spatial dimensions, showing which image regions drove the prediction—often overlaying beautifully on the original image to highlight, say, a cat's face when predicting "cat."
Key Takeaway: GradCAM uses gradient-weighted combinations of convolutional feature maps to produce interpretable, class-specific heatmaps showing which spatial regions matter most for CNN predictions.