Saliency Maps, Grad-CAM and Integrated Gradients
Which parts of the image influenced the network's decision? The basic gradient methods, their advantages and their limits.
Vanilla gradient
$$S(x) = \left|\frac{\partial y_c}{\partial x}\right|$$
Fast but noisy. SmoothGrad Averages the gradient over noisy copies of the image to smooth the map.
Grad-CAM
- Compute the gradient of the class score \(y_c\) with respect to the feature maps \(A^k\) in a convolutional layer.
- The weight \(\alpha_k^c\) = the average gradient over positions.
- The map:
$$L^c = \mathrm{ReLU}\Big(\sum_k \alpha_k^c A^k\Big)$$
Integrated gradients
$$IG_i(x) = (x_i – x_i’) \int_0^1 \frac{\partial f\big(x’ + \alpha(x – x’)\big)}{\partial x_i}\, d\alpha$$
They satisfy the completeness property: the sum of the attributions equals \(f(x) – f(x’)\).
An implementation with Captum
import torch
from torchvision.models import resnet50, ResNet50_Weights
from captum.attr import LayerGradCam, IntegratedGradients, LayerAttribution
model = resnet50(weights=ResNet50_Weights.DEFAULT).eval()
x = torch.randn(1, 3, 224, 224) # replace with a preprocessed image
target = model(x).argmax(1).item()
gc = LayerGradCam(model, model.layer4)
heat = LayerAttribution.interpolate(gc.attribute(x, target=target), (224, 224))
ig = IntegratedGradients(model)
attr = ig.attribute(x, baselines=torch.zeros_like(x), target=target, n_steps=50)
print(heat.shape, attr.shape)Comparison
| Method | Spatial resolution | Cost | Note |
|---|---|---|---|
| Vanilla gradient | High but noisy | Low | Affected by saturation |
| SmoothGrad | Clearer | Medium | Depends on the noise level |
| Grad-CAM | Coarse | Low | Specific to convolutional networks |
| Integrated gradients | High | Higher | Depends on the baseline |
In the next lesson we will see that some saliency methods pass the “eye test” but fail faithfulness tests.
Lesson terms
- Integrated gradients
- An attribution that accumulates gradients along a straight path from a baseline input to the actual input.
- Saliency map
- A map that gives each pixel a score estimating its effect on the model's decision, often from the gradient.
- Gradient-weighted class activation map
- A method that produces a class activation map by weighting the feature maps of a convolutional layer by the average of the class gradients with respect to them.
Abstract
Saliency maps use the gradient of the class output with respect to the pixels to estimate how sensitive the decision is to each pixel, but they are noisy. Grad-CAM weights the feature maps of the last convolutional layer by the average of their gradients, producing a coarse, class-specific heatmap. Integrated gradients accumulate gradients along a path from a baseline to the image and satisfy the completeness property. All these methods need their faithfulness verified, and they do not tell us why a region matters, only where the model looked.
Review questions
-
Why are Grad-CAM maps coarse?
Because they are built at the resolution of the last convolutional layer, then upscaled to the image size.
-
What is the role of the baseline in integrated gradients?
It represents "absence of information", and the attribution measures the difference from it, so its choice changes the result.
Sources and references
Complete the lesson
You have completed all the lessons in the track! You can now request the track completion certificate.
Request the certificate