What is Grad-CAM?

Grad-CAM (Gradient-weighted Class Activation Mapping) is a model-specific attribution method that uses gradients of a selected output with respect to spatial feature maps to create a coarse, target-specific localization heatmap.

Quick Facts

Full NameGradient-weighted Class Activation Mapping
SpecificationOfficial Specification

How It Works

Choose the target and spatial layer

Fix the model checkpoint and evaluation mode, then declare the exact scalar target: a class logit, detection score, caption token score, or another differentiable output. Select a layer whose activations preserve meaningful spatial structure. The original Grad-CAM paper uses gradients entering a convolutional layer to produce target-specific localization without retraining the model.

Weight feature maps with pooled gradients

Run a forward pass, retain the chosen feature maps, and backpropagate the target score. Average each channel's gradient over spatial positions to estimate its target weight, sum the weighted feature maps, apply ReLU when the goal is positive supporting evidence, and upsample. Preserve the raw map because colormap, clipping, smoothing, and min-max normalization can exaggerate weak differences.

Test localization, faithfulness, and stability

Compare layers, target classes, seeds, nearby inputs, and sign handling; include randomization checks and domain-valid insertion or deletion tests. Evaluate localization only when trustworthy masks or boxes exist. Recent CAM robustness research shows why consistency under prediction-preserving perturbations and responsiveness when predictions change should both be measured rather than trusting visual appeal.

Key Characteristics

  • Produces a target-specific heatmap from spatial feature maps and gradients
  • Uses globally averaged gradients as channel importance weights
  • Usually retains positive evidence through a ReLU after channel aggregation
  • Works without retraining but requires differentiable access to internal activations
  • Trades semantic abstraction against spatial resolution through layer selection
  • Needs quantitative faithfulness, localization, randomization, and stability checks

Common Use Cases

  1. Checking which image regions support a selected classification logit
  2. Comparing localization behavior across model versions or classes
  3. Finding dataset shortcuts such as watermarks, borders, or backgrounds
  4. Reviewing spatial evidence in detection, captioning, or visual question answering
  5. Generating hypotheses for masking, ablation, and targeted data collection

Example

loading...
Loading code...

Frequently Asked Questions

How is Grad-CAM different from the original CAM method?

CAM obtains class weights directly from a restricted architecture with global average pooling before the classifier. Grad-CAM derives channel weights from gradients of a chosen target and can therefore work with a broader class of differentiable CNN-based models without retraining or replacing the prediction head.

Why is the last convolutional layer often used for Grad-CAM?

Late convolutional layers usually retain spatial layout while encoding more class-relevant abstractions than early layers. Their maps are also lower resolution, so localization is coarse. The last layer is a convention rather than a guarantee; compare several valid layers and report the one used.

What information does the ReLU remove from a Grad-CAM map?

ReLU removes locations whose weighted sum is negative, leaving regions that positively support the selected target under the method. That is useful for positive-evidence localization but hides opposing evidence. If negative evidence matters, preserve and analyze signed pre-ReLU maps separately.

Can Grad-CAM be used with Vision Transformers?

Only with an explicit spatial representation and reshape rule. Transformer tokens, class tokens, patch embeddings, and attention blocks do not automatically satisfy the CNN feature-map assumptions. State the target layer, removed tokens, grid reconstruction, gradients, and upsampling, then validate the adapted method independently.

Does a Grad-CAM heatmap prove which pixels caused a prediction?

No. It is a gradient-weighted summary at one internal layer, affected by resolution, saturation, ReLU, and visualization choices. Test faithfulness with parameter randomization, insertion or deletion, masking that stays in distribution, alternative layers and methods, and held-out localization labels where available.

Related Terms

Related Articles