TL;DR
Selvaraju and co-authors show how to draw a heatmap over an image that marks the regions a convolutional network used for one class, such as "tiger cat". The method needs no change to the network and no retraining. It takes the gradient of the class score, averages it over each feature map to get one importance number per map, and adds the weighted maps together. On VGG-16 classifiers, its top-1 localisation error beat CAM's, and its heatmaps agreed better with occlusion tests than earlier methods. The same idea also works for image captioning and visual question answering.
Builds on: Convolutional Neural Network, Backpropagation, Global Average Pooling · Leads to: later gradient-based explanation methods for vision models.
Why it mattered
By 2016 convolutional networks were the working tool for image recognition, and they were hard to inspect. Grad-CAM gave a way to ask "where was the model looking?" for any trained network that has convolutional layers and a score per class, including large off-the-shelf models such as VGG-16 and ResNet. The paper also applied the same idea to image captioning and visual question answering, which CAM could not handle, since CAM is limited to classifiers without fully connected layers.
The problem
A classifier that is right most of the time can still be right for the wrong reason. Before this paper there were two families of visual explanations, and each failed one of two needs the authors name: the explanation should be class-discriminative (it should show the object of the chosen class) and high-resolution (it should show fine detail).
The first family uses pixel-space gradients. Guided Backpropagation and Deconvolution give fine, detailed visualizations, but they are not class-discriminative, so they say which pixels matter, not which class they support.
The second family is class-discriminative. The earlier Class Activation Mapping method, CAM, gives a heatmap that lights up the object of the chosen class. But CAM needs a special architecture: the last convolutional feature maps must feed global average pooling and then a softmax. That architecture can cost accuracy, and it rules out captioning and question answering. The authors want an explanation that is class-discriminative, reasonably high-resolution, and computable on any network.
How it works
Take one tiny instance. A network has two feature maps in its last convolutional layer, each 2 by 2. It gives "cat" a raw score y of 3.
- Get the raw class score. Run the image forward and read y for the chosen class, before the softmax turns scores into probabilities. Check: the softmax squashes differences, so the raw score keeps the signal.
- Find how each position affects the score. Run backpropagation from y back to the last convolutional layer. Each of the 4 positions in each map gets a gradient: how much y would change if that activation rose a little. Check: a positive gradient means that position pushes the cat score up.
- Average the gradients per map. For map k, add its 4 gradients and divide by 4. The result alpha_k is the importance of map k for this class. Check: a map whose activations push the score up gets a positive weight, and one that pushes it down gets a negative weight.
- Add the maps, weighted. Multiply each map by its alpha and sum over the maps. This gives one 2 by 2 grid. Check: a cell with a large positive value is where the maps that matter for "cat" are active.
- Keep only the positive part. Apply ReLU, which sets negative cells to zero. A negative cell is evidence for some other class, and the paper keeps only evidence for the target. Check: every cell is now zero or positive.
- Stretch the coarse map to image size. Upsample the grid to the input resolution with bilinear interpolation and overlay it on the photo. For the paper's VGG and AlexNet cases the last convolutional layer is 14 by 14, so the heatmap is coarse but still lines up with the object.
flowchart LR A[Image] --> B[CNN forward pass] B --> C[Raw score y for class c] C --> D[Backpropagate to last conv layer] D --> E[Average gradients per map: alpha] E --> F[Weighted sum of feature maps] F --> G[ReLU keeps positive evidence] G --> H[Upsample and overlay on image]
The paper's Figure 1 uses a photo with a cat and a dog and shows the Grad-CAM support for the cat category, which localises the cat.
Worked examples
Two feature maps, each 2 by 2. Map 1 is A^1 = [[1, 0], [0, 1]] and map 2 is A^2 = [[0, 2], [0, 0]]. The gradients of the score are G^1 = [[1, 1], [1, 1]] and G^2 = [[-1, 0], [0, 0]].
- Z = 2 times 2 = 4.
- The entries of G^1 sum to 4, so alpha_1 = 4 / 4 = 1.
- The entries of G^2 sum to -1, so alpha_2 = -1 / 4 = -0.25.
- Weighted sum: 1 times A^1 plus (-0.25) times A^2. That is [[1, 0], [0, 1]] plus [[0, -0.5], [0, 0]], which equals [[1, -0.5], [0, 1]].
- Apply ReLU: the -0.5 becomes 0. The result is [[1, 0], [0, 1]].
Interpretation: the heatmap is strong on the diagonal, where map 1 is active and supports the class, and zero at the top right, where map 2 is active but works against the class.
Two maps, each 2 by 2. A^1 = [[2, 1], [0, 3]] and A^2 = [[1, -1], [2, 0]]. The gradients are G^1 = [[1, 1], [1, 1]] and G^2 = [[-2, 0], [0, 0]].
- Z = 4.
- alpha_1 = (1 + 1 + 1 + 1) / 4 = 1.
- alpha_2 = (-2 + 0 + 0 + 0) / 4 = -0.5.
- Weighted sum: 1 times [[2, 1], [0, 3]] plus (-0.5) times [[1, -1], [2, 0]]. The second term is [[-0.5, 0.5], [-1, 0]]. Adding gives [[1.5, 1.5], [-1, 3]].
- ReLU sets the -1 to 0, giving [[1.5, 1.5], [0, 3]].
- For display, divide by the largest value, 3, to get [[0.5, 0.5], [0, 1]]. The bottom-right cell is the strongest region.
Interpretation: map 2 pushes against the class, and ReLU removes the place where it does. The strongest evidence sits in the bottom-right corner.
Take a VGG-16 classifier on a common 224 by 224 ImageNet input. The paper analyses the last convolutional layer, conv5_3, whose feature maps are 14 by 14. So Z = 14 times 14 = 196, and the layer has 512 maps, which gives 512 weights alpha_k^c for one class.
- Compute one gradient per position: 512 maps times 196 positions is 100352 gradient values for one class.
- Average each map's 196 gradients to get 512 weights.
- Form the weighted sum of the 512 maps. Each map is 14 by 14, so the result is 14 by 14.
- Apply ReLU.
- Upsample the 14 by 14 result to 224 by 224 with bilinear interpolation and overlay it on the image.
Try it: for one class, how many gradient values does step 1 produce, and how many weights does step 2 leave? Answer: step 1 produces 512 times 196 = 100352 gradient values. Step 2 averages each block of 196 into one number, leaving 512 weights, one per map.
The math
Let y^c be the score for class c before the softmax. Let A^k be the k-th feature map of the chosen convolutional layer, with u rows and v columns, and let Z = u times v be its number of positions. The Grad-CAM map for class c is the ReLU of a weighted sum of the maps, where each weight is the average gradient of the score over that map.
Step 3 in symbols: the weights
For each map k:
Here A^k_{ij} is the activation at row i, column j of map k. The sum-and-divide is global average pooling of the gradients. The gradients come from backpropagation (see Partial Derivative and Chain Rule): each is a product of the weight matrices and activation slopes between that layer and the output. That is why the paper calls alpha a partial linearisation of the network below A.
Step 4 in symbols: the weighted sum
The sum runs over all maps. The ReLU keeps only positions where the weighted sum is positive. Each term scales one map by one number, so the expression is a linear combination followed by a pointwise nonlinearity (see Rectified Linear Unit (ReLU)).
Step 6 in symbols: putting it on the picture
The map L has size u by v. To overlay it on an image of height H and width W, the paper upsamples it with bilinear interpolation to H by W. It then multiplies it pointwise by the Guided Backpropagation map to get Guided Grad-CAM, which is high-resolution as well as class-specific.
Why this generalises CAM
For a network whose last convolutional maps go through global average pooling into a linear layer, CAM uses the class weights w_k^c. The paper shows that the Grad-CAM weight equals w_k^c divided by Z. After that normalisation the two maps agree, so Grad-CAM reproduces CAM where CAM applies and keeps working where it does not.
Counterfactual variant
Negate the gradient, so alpha uses minus the partial derivative. The same weighted sum then highlights regions that, if removed, would raise the class score. The paper uses this to show regions whose removal would make the model more confident about its prediction.
Results
The paper's own numbers, each copied from its text or tables.
- Weakly supervised localisation (ILSVRC-15 val, VGG-16). Grad-CAM's top-1 localisation error is 56.51 percent, against 57.20 for CAM, 61.12 for Backprop and 70.92 for c-MWP. Its top-5 localisation error is 46.41, which is worse than CAM's 45.14. Grad-CAM keeps VGG-16's classification error (top-1 30.38 percent), while the paper notes CAM is 2.98 percent worse on top-1 classification because it needs retraining with a changed architecture.
- Segmentation. Using Grad-CAM as the seed for the SEC weakly supervised segmentation method raises the PASCAL VOC 2012 IoU to 49.6, against 44.6 with CAM seeds.
- Pointing game. With recall added to the protocol, On VGG-16, Grad-CAM scores 70.58 percent against 60.30 for c-MWP.
- Class discrimination (43 workers, 90 image-category pairs, 9 ratings each). Humans identify the correct class in 61.23 percent of cases with Guided Grad-CAM, against 44.44 for Guided Backpropagation. Grad-CAM also lifts Deconvolution from 53.33 to 60.37.
- Trust (54 workers). Asked which of two models (VGG-16 or AlexNet, both correct) is more reliable, raters give Guided Grad-CAM a relative reliability of 1.27 against 1.00 for Guided Backpropagation.
- Faithfulness to occlusion (2510 PASCAL images). Rank correlation with occlusion maps: Grad-CAM 0.254, Guided Grad-CAM 0.261, Guided Backpropagation 0.168, c-MWP 0.220, CAM 0.208.
- Captioning. On DenseCap regions, the ratio of mean activation inside to outside the box is 3.27 plus or minus 0.18 for Grad-CAM, against a baseline of 1.0, and 6.38 plus or minus 0.99 for Guided Grad-CAM.
- VQA. Rank correlation with occlusion maps is 0.60 for Grad-CAM and 0.42 for Guided Backpropagation.
- Dataset bias. A doctor-versus-nurse classifier reached 82 percent test accuracy; after the training set was rebalanced, it reached 90 percent. The source images were gender-skewed (78 percent of doctor images were men, 93 percent of nurse images were women).
- Adversarial images. Even when the network reports above 0.9999 confidence in a category that is absent, Grad-CAM still localises the original categories.
Limits. The heatmap is coarse: at the resolution of the last convolutional layer, and the paper finds it worse on earlier layers, which have smaller receptive fields. Several common misreadings follow from this. Read the heatmap as "this region supports the class", not as "these exact pixels caused the decision"; the fine detail comes only from the separate Guided Backpropagation fusion. A close match with occlusion is a correlation (0.254 and 0.261 against 0.168 for Guided Backpropagation), not proof that the heatmap is the model's logic. The human studies are small and judge the explanation, not the decision. The pointing game was modified by the authors.
Reading guide
- Sections 1 and 2 (introduction, related work): needs Convolutional Neural Network and the idea of an Activation Function. CAM and the pixel-gradient methods are introduced here.
- Section 3 (the method): needs Backpropagation, Partial Derivative, Chain Rule and Global Average Pooling. Read 3.1 with the CAM formula in mind; it is the proof that Grad-CAM generalises CAM.
- Section 3.2 (Guided Grad-CAM): needs Bilinear Interpolation for the upsampling.
- Section 4 (localisation and segmentation): needs Image Segmentation and Intersection over Union.
- Section 5 (human studies and faithfulness): needs the notion of a rank correlation from statistics. The human studies are self-contained.
- Section 6 (failure modes, adversarial noise, bias): needs Adversarial Examples and Max Pooling for context on VGG-style networks.
- Section 7 (textual explanations): builds on the neuron-naming idea in Network Dissection and the broader goal in Mechanistic Interpretability.
- Section 8 (captioning and VQA): needs Image Captioning and Long Short-Term Memory.
Check yourself
QuestionRecall: how is a Grad-CAM heatmap different from the feature maps it is built from?Show answer
The feature maps are the activations the network computes for the image, one per learned filter. The Grad-CAM heatmap is their weighted sum, where each weight is the average gradient of the class score over that map, followed by ReLU. The heatmap is class-specific; the feature maps are not.
QuestionComputation: a layer has 3 maps, each 2 by 2. For one class, the gradients over map 1 sum to 8, over map 2 sum to -4, and over map 3 sum to 0. What are the three alpha values?Show answer
Z = 2 times 2 = 4. alpha_1 = 8 / 4 = 2. alpha_2 = -4 / 4 = -1. alpha_3 = 0 / 4 = 0. Map 3 contributes nothing to the heatmap, and map 2 contributes with a negative sign.
QuestionWhy: why does Grad-CAM apply ReLU to the weighted sum instead of keeping the negative values?Show answer
The paper keeps only features with a positive influence on the class of interest. Negative values mark places where the evidence points to other categories in the image. The paper's ablation supports this: removing the ReLU raises the top-1 localisation error from 59.65 to 74.98 on ILSVRC-15 val.
QuestionApplied: a student runs Grad-CAM on a skin-photo classifier and the heatmap for one class lights up a ruler placed next to the lesion, not the lesion. What should they conclude, and what should they check next?Show answer
The model's score for that class depends on the ruler, which suggests a spurious cue in the training data, the same kind of problem as the paper's doctor and nurse experiment. They should check whether rulers appear more often in one class of the training set, and test the model on images without rulers. This is a debugging observation about a model, not a medical conclusion.
Sources
- Selvaraju et al., "Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization", arXiv 1610.02391 (2016): https://arxiv.org/abs/1610.02391
- Selvaraju et al., the journal version, International Journal of Computer Vision (2019), DOI 10.1007/s11263-019-01228-7: https://doi.org/10.1007/s11263-019-01228-7