Class activation mapping

Also known as: class activation map

On this page 7

Class activation mapping (CAM) is a technique that produces a heatmap over an input image showing which spatial regions a convolutional neural network relied on when it predicted a specific class. Within Explainable AI, a class activation map is one of the most widely used visual explanations for image classifiers, because it is cheap to compute and easy to read.

The original method, introduced in 2016, works by weighting the final convolutional feature maps with the classifier weights of the target class. Its descendants, including Grad-CAM, Grad-CAM++ and Score-CAM, generalize the idea to almost any convolutional architecture without retraining.

How class activation mapping computes a heatmap

Original CAM requires a specific architecture: the last convolutional layer is followed by global average pooling (GAP) and a single fully connected layer that outputs class scores. Because GAP averages each feature map into one number, the class score is a weighted sum of feature map averages.

Moving the weights before the averaging step gives the map itself. For class c, the class activation map is the sum over channels k of the weight w_k^c multiplied by the feature map A_k. The result has the spatial resolution of the last convolutional layer, typically 7 x 7 or 14 x 14, and is upsampled to the input size with bilinear interpolation before it is overlaid on the image.

ComponentRoleTypical value
Feature mapsSpatial detectors from the last convolutional layer512 to 2,048 channels
Class weightsImportance of each channel for one classFrom the final dense layer
Weighted sumCombines channels into one coarse map7 x 7 to 14 x 14 grid
UpsamplingResizes the map to the image sizeBilinear interpolation
NormalizationScales values for display as a heatmapMin-max to the 0 to 1 range
Components of a class activation map

The method works because each channel of a deep convolutional layer tends to act as a detector for a semantic pattern, such as fur texture, a wheel or an eye, while keeping its spatial layout intact. The class weights therefore say which detectors matter, and the feature maps say where those detectors fired. Retrofitting an existing network with GAP for original CAM usually costs a small amount of accuracy, which is the main reason later variants avoid this requirement altogether.

Variants of class activation mapping

The architectural restriction of original CAM motivated a family of gradient-based and gradient-free variants. They differ mainly in how they obtain the channel weights that replace the classifier weights.

MethodHow channel weights are obtainedMain advantage
CAMFinal dense layer weights after GAPExact, no gradients needed
Grad-CAMSpatially averaged gradients of the class scoreWorks with any CNN architecture
Grad-CAM++Weighted higher-order gradient termsBetter coverage of multiple objects
Score-CAMScore change when each map masks the inputNo gradients, less noisy maps
Layer-CAMElement-wise positive gradients per locationFiner maps from earlier layers
Main members of the CAM family

Grad-CAM applies a ReLU to the weighted sum so that only regions with a positive influence on the class are kept. Combining Grad-CAM with guided backpropagation yields Guided Grad-CAM, which adds pixel-level detail to the coarse localization.

Class activation mapping compared with other attribution methods

Class activation mapping belongs to feature attribution, but it explains decisions at the level of regions rather than individual pixels. This makes it coarser than pixel-gradient methods but usually more stable and more class-discriminative.

MethodResolutionClass-discriminativeCost
Grad-CAMCoarse regionsYesOne forward and backward pass
Saliency gradientsPixel levelWeaklyOne backward pass
SmoothGradPixel level, denoisedWeaklyTens of passes
Occlusion sensitivityPatch levelYesHundreds of passes
Integrated gradientsPixel levelYesTwenty to several hundred passes
CAM-based maps versus related explanation methods

In practice, Grad-CAM is a default first check for convolutional image models, while occlusion and Shapley-based methods are used when stronger faithfulness guarantees are worth the higher computational cost.

Where class activation maps are used

Class activation maps serve both model debugging and weakly supervised localization. Because the map shows where the evidence for a class lies, it can reveal that a model has learned a spurious shortcut, such as a watermark, a ruler or a hospital-specific text marker, instead of the object itself.

  • Shortcut detection during validation of medical, industrial and remote sensing classifiers.
  • Weakly supervised object localization, where thresholded maps produce bounding boxes without box labels.
  • Dataset auditing, where maps across many images expose background bias in a class.
  • User-facing explanations, where an overlay helps an expert decide whether to trust a prediction.

Libraries such as Captum and several open-source CAM toolkits implement these methods for common frameworks. The choice of target layer matters: the last convolutional block gives the most semantic but coarsest map, while earlier layers give sharper but less class-specific maps.

Limitations and pitfalls of class activation mapping

A class activation map shows correlation with the class score, not a causal proof of reasoning. Sanity-check studies have shown that some attribution maps barely change when model weights are randomized, so explanation faithfulness must be tested rather than assumed.

PitfallConsequenceRemedy
Low map resolutionSmall objects appear as diffuse blobsUse earlier layers or Layer-CAM
Gradient saturationImportant regions receive near-zero weightTry Score-CAM or Grad-CAM++
Single-image readingAnecdotal conclusions about the modelAggregate maps over many samples
Non-CNN modelsNo natural spatial feature mapsUse attention rollout for transformers
Typical pitfalls and remedies

Color scales also mislead. Min-max normalization always makes some region look hot, even when the class score is low, so maps should be shown together with the prediction confidence.

In simple terms

Imagine asking a person why they think a photo shows a dog. They point at the ears and the nose. A class activation map is the model pointing at the picture. It colors the parts of the image that pushed the model toward its answer.

Warm colors mean strong evidence. Cool colors mean little evidence. The map is rough, not exact. It shows where the model looked, not exactly why it decided.

A practical example

A hospital research team trains a convolutional network to detect pneumonia on chest X-rays and reaches a validation AUC of 0.93. Before deployment, the team generates Grad-CAM maps from the last convolutional block for 500 positive cases and reviews them with two radiologists.

About one third of the maps highlight the corner of the image with a portable scanner marker instead of the lungs. Portable scans are taken from sicker patients, so the model had learned a shortcut tied to the acquisition device. The team crops and masks the markers, retrains the network and checks the maps again. The new maps focus on the lung fields, and the AUC on an external hospital dataset improves from 0.78 to 0.86.