Intermediate level · Module 7: Feature Visualization · Lesson 2 of 3
Grad-CAM and Saliency
Learners implement Grad-CAM with hooks to show which regions influenced a prediction, and compare it with simple gradient saliency.
In this lesson you will
- Implement Grad-CAM
- Interpret heatmaps over images
Key concepts
saliency (input gradients)Grad-CAMtarget layerheatmap overlayclass-specific explanation
The lesson
A saliency map shows how much each input pixel affects the class score, using gradients. It is often noisy.
Grad-CAM takes the feature maps of a late convolution layer, weights each map by the average gradient of the class score with respect to it, sums them and applies ReLU. The result, resized and overlaid on the image, highlights the regions that supported the prediction.
Grad-CAM is class-specific: compute it for the predicted class and for a competing class to see what distinguished them.
Step by step
- Register forward and backward hooks on the last convolution layer.
- Compute Grad-CAM for one prediction.
- Overlay the heatmap on the image with Matplotlib.
- Compare heatmaps for the top two classes.
Practice activity
Produce Grad-CAM explanations for ten predictions, including errors.
Expected result: Correct heatmaps with interpretations, including what the model focused on when wrong.
Check your understanding
-
Which layer does Grad-CAM usually use?
Show answer
A late convolution layer.
-
Why compute Grad-CAM for two classes?
Show answer
To see what distinguished the predicted class from a competitor.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.