Interpretability, attribution, and explanation audits · Interpretability, explanations, and model-mechanism analysis · EXP006 · Algorithm
Grad-CAM
Produce a coarse class-targeted spatial map by weighting convolutional feature maps with averaged gradients of a selected score.
In CodeSplash
A CodeSplash method covers part of this entry; some options or variants aren't built yet. The methods that run it are Experimental.
- Convolutional neural network (CNN) Experimental
- Image classifier (pretrained ResNet-50 or DINOv2) Experimental
Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.
- Availability
- Partly runs in CodeSplash (Experimental)
- Code example
- Complete calculation
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- Verified in Python and R through learn.cnn. The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Produce a coarse class-targeted spatial map by weighting convolutional feature maps with averaged gradients of a selected score.
Question / hypotheses
Which spatial locations in the chosen feature layer support this score under the Grad-CAM construction?
Architecture / computational structure
The predictor supplies a selected convolutional activation tensor and score gradients with respect to it. Spatially averaged gradients weight each channel; a ReLU applied to the weighted sum retains positive support. Upsampling is a display operation that does not increase original spatial resolution.
Assumptions
The chosen layer retains spatial organization and is connected differentiably to the target score. The score, layer, channel aggregation and preprocessing must be fixed. Negative evidence is discarded by the standard final ReLU.
Formula / computational definition
Parameters and notation
Predictor weights are fixed; A is an activation state and α is a derived weight. Choices include layer, target logit or contrast, interpolation, normalization, and image overlay. Heatmap normalization can hide absolute sensitivity differences across examples. Here c indexes the chosen class, y^c is its pre-softmax score when using the original formulation, k indexes channels, (i,j) indexes spatial positions, Z is the number of spatial positions, and A^k is the selected feature map.
Fitting, fine-tuning, and adaptation
Recompute maps after fine-tuning and compare at the same architecture layer when possible. Layer changes alter the localization scale. For non-convolutional architectures, define the token-to-spatial transformation explicitly rather than treating a reshaped tensor as automatically equivalent.
How to use and calibrate
Inspect raw maps before color overlays, test model randomization, and compare deletion/occlusion behavior with plausible perturbations. Report failures and background shortcuts. Human visual agreement with an object outline is not sufficient evidence of model faithfulness.
Example application
Compare a classifier’s score support on object and background regions using a layer with a known spatial grid.
Interpretation / pitfalls
Watch out
A coarse map can look persuasive after smoothing. It can omit negative contributions and cannot establish object localization accuracy without a separate target and metric. Target-class changes and saturation affect gradients.
Python and R code examples
Scope: Exact aggregation of supplied activation maps and gradients; no CNN training or image-model execution. Python: NumPy; R: base R.
Validation: Python executed successfully on the supplied fixture; R reviewed but unexecuted. Full implementations and supported operation details are provided in the linked primary/official sources.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Complete calculation — Exact aggregation of supplied activation maps and gradients; no CNN training or image-model execution. Python: NumPy; R: base R. · needs numpy
import numpy as np
A=np.array([[[1.,2.],[0.,1.]],[[2.,0.],[1.,3.]]])
G=np.array([[[.2,.2],[.2,.2]],[[-.1,-.1],[-.1,-.1]]])
alpha=G.mean(axis=(1,2));cam=np.maximum((alpha[:,None,None]*A).sum(0),0)
assert cam.shape==(2,2);print(alpha,cam)
Complete calculation — Exact aggregation of supplied activation maps and gradients; no CNN training or image-model execution. Python: NumPy; R: base R.
A<-array(0,c(2,2,2));A[,,1]<-rbind(c(1,2),c(0,1));A[,,2]<-rbind(c(2,0),c(1,3))
alpha<-c(.2,-.1);cam<-pmax(alpha[1]*A[,,1]+alpha[2]*A[,,2],0)
print(alpha);print(cam)
Sources
- Source 1arxiv.org
- Source 2github.com
From the CodeSplash reference, entry EXP006.