Grad-CAM

Weight each feature map by how much the score depends on it, add them up, and keep the positive part. That is the heatmap.

Overview

The question

A network says "cat". Which part of the image made it say that?

Grad-CAM answers with a heatmap, and it does so using quantities the network makes anyway during backpropagation. This Is why it works on any convolutional architecture without retraining or modification.

Grad-CAM

This module needs JavaScript: the images are computed in the page rather than downloaded.

Worth knowing

The weights are the gradient of the class score with respect to each feature map, averaged over its spatial positions.
A weight says how much raising that map's activations would raise the score — how much the class depends on it.
The final ReLU is deliberate: it keeps evidence for the class and discards evidence against it.
The heatmap is the size of the feature map, usually 7×7, and is upsampled to the image. It is coarse by construction.

Grad-CAM

A heatmap of which regions a network used, built from grads it already finds.

The recipe

Take the feature maps from the last convolutional layer — often 512 or 2048 of them, each perhaps 7×7. Then:

  1. Find the grad of the class score for each feature map.
  2. Mean each grad over its spatial spots. That number is the map's weight.
  3. Sum the feature maps, weighted by those numbers.
  4. Apply a ReLU.
  5. Upsample the result to the image size.

Steps 3 and 4 are what the visualisation shows. Three synthetic feature maps sit on the left, each with its own weight slider. The mixed heatmap is on the right. The maps are made up. The weighting math is just Grad-CAM's.

What a weight means

The grad of the score for a feature map answers: if this map's activations rose slightly, how much would the class score rise?

A large plus weight means the class depends heavily on that map, so wherever it's active is evidence for the class.

A minus weight means the opposite — that feature argues *against* this class. Set one of the sliders minus and watch what happens to the heatmap: the region belonging to that map doesn't go dark, it simply stops adding, because of the ReLU.

Why the ReLU is there

This is the step people skip and then misread the output.

Without the ReLU the heatmap would hold minus regions, meaning "this area argued against the class". Grad-CAM discards them on purpose, because the question being asked is *what supported this guess* — and a visualisation mixing support and opposition in one colour scale is very easy to misread.

The consequence is that Grad-CAM never shows you evidence against a class. Drag all three sliders minus: the heatmap is empty, not inverted. If you want to know what argued against a guess, this is the wrong tool, and running it on the competing class is often the hands-on answer.

Its limits

detail. The heatmap has the spatial size of the last convolutional layer, often 7×7. Upsampling to 224×224 makes a smooth blob, and that smoothness is interpolation rather than evidence. Grad-CAM can't tell you which pixel mattered, only which seventh of the image.

One layer. It shows the last convolutional layer. Earlier layers see different things, and the choice of layer changes the answer.

Plausibility isn't correctness. A heatmap over the animal's face looks lifelike whether or not the model used the face. Grad-CAM shows what the grads say, and there's published work on saliency methods that give sensible-looking maps for at random was reset networks — so a reasonable-looking heatmap isn't evidence the model is thinking well.

It doesn't live each architecture unchanged. Transformers have no final convolutional layer, so focus rollout or a variant is used instead.

Relatives

CAM, the predecessor, needed the architecture to end in global mean pooling followed by one dense layer, and read the weights straight from that layer. Grad-CAM's part was getting the same weights from grads instead, which removed the architectural need.

Grad-CAM++ handles a few instances of the same class better. Score-CAM drops grads fully, measuring each map's importance by occluding with it and seeing what the score does. Slower, and immune to grad saturation.

The weighting, one cell at a time

Grad-CAM's heatmap is a weighted sum of feature maps followed by a ReLU, and doing one cell by hand drops the mystery. Three maps, with weights that are the spatial-mean grads of the class score, mixed at a single location:

contributionvalue
map 1 activation 0.8 × weight +2.0+1.60
map 2 activation 0.5 × weight −1.0−0.50
map 3 activation 0.2 × weight +0.5+0.10
sum → ReLU1.20

At a cell where the negatively-weighted map dominates the sum comes out minus — say −0.65 — and the ReLU clips it to 0. That clip is the step people skip and then misread: Grad-CAM on purpose discards evidence *against* the class, because the question is what supported this guess, and a map mixing support and opposition on one colour scale is easy to misread. The consequence is that a Grad-CAM heatmap can never show you what argued against a class — drag each weight minus and it goes empty, not inverted. And to see the opposing evidence you run it on the competing class instead. The map is also only 7×7 before upsampling, so the smooth blob over the animal's face is interpolation, not pixel-level proof.

Grad-CAM's real caution is that plausibility isn't correctness. A heatmap over an animal's face looks lifelike whether or not the model used the face, and published work has made sensible-looking saliency maps for *at random was reset* networks — so a reasonable heatmap is a hypothesis about the model, not a verification of it. It also shows just one layer, often the last convolutional one, and the choice of layer changes the answer. So A Grad-CAM result is really "what the grads at this layer say", not "what the model used". Its ancestor CAM needed the network to end in global mean pooling and read the weights straight from that layer. Grad-CAM's part was getting the same weights from grads, which removed the architectural need and is why it works on almost any CNN.

Where it goes wrong with Grad-CAM

Reading the smooth blob as pixel-level evidence. It's 7×7, interpolated.

Showing the wrong class. Run it on the predicted class *and* on the class you expected. The difference is often the helpful part.

Treating it as proof. A plausible heatmap is a hypothesis about the model, not a verification of it.

Choosing a layer without saying so. The layer is a param, and the answer depends on it.

Questions people ask about Grad-CAM

What does Grad-CAM show? A heat map of the image regions that most increased the score for a chosen class, built from the last convolutional layer's feature maps.

How is it calculated? Take the gradient of the class score with respect to each feature map, average it spatially to get a weight per channel, and sum the weighted maps. A ReLU keeps only positive influence.

Why use the last convolutional layer? It balances high-level meaning with some remaining spatial layout. Earlier layers localise better but say less about the class, and later dense layers have lost the layout.

Is a Grad-CAM heat map an explanation of the model? It shows where the model looked, not why. A good-looking map can still hide shortcut features, and the map is low resolution.

How does Grad-CAM differ from the original CAM? CAM needs a global average pooling architecture. Grad-CAM uses gradients, so it works on any CNN without changing it.

Can I use it to debug a model? Yes. If maps highlight the background, watermarks or borders instead of the object, the model has likely learned a dataset artefact.

Asking the gradient which pixels mattered

Grad-CAM makes a heatmap over the image for a chosen class. Each step of it's math you can follow. This builds one end to end on a small network, then checks the two things people forget to check -- whether the map changes with the class. Whether it can be trusted when the guess is wrong.

example_01.pyNumPy
Output

The next block runs Grad-CAM step by step for both classes, showing how the weighted sum of feature maps produces a different heatmap for each class.

example_02.pyNumPy
Output

The next block shows how coarse the map really is, and warns that Grad-CAM explains the class you ask for even when the model rejected it.

example_03.pyNumPy
Output

Check yourself

0 of 3

Answer without scrolling back up.

  1. Where does a feature map's weight come from?

  2. What does the final ReLU do?

  3. Why is a Grad-CAM heatmap coarse?

Cheat sheet

Grad-CAM

Compute a Grad-CAM heatmap from gradients flowing into the last convolutional layer, then overlay it to see which regions drove the prediction.

COMPUTER VISION · vizlearn.in/computer_vision/grad_cam.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.