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.
| Component | Role | Typical value |
|---|---|---|
| Feature maps | Spatial detectors from the last convolutional layer | 512 to 2,048 channels |
| Class weights | Importance of each channel for one class | From the final dense layer |
| Weighted sum | Combines channels into one coarse map | 7 x 7 to 14 x 14 grid |
| Upsampling | Resizes the map to the image size | Bilinear interpolation |
| Normalization | Scales values for display as a heatmap | Min-max to the 0 to 1 range |
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.
| Method | How channel weights are obtained | Main advantage |
|---|---|---|
| CAM | Final dense layer weights after GAP | Exact, no gradients needed |
| Grad-CAM | Spatially averaged gradients of the class score | Works with any CNN architecture |
| Grad-CAM++ | Weighted higher-order gradient terms | Better coverage of multiple objects |
| Score-CAM | Score change when each map masks the input | No gradients, less noisy maps |
| Layer-CAM | Element-wise positive gradients per location | Finer maps from earlier layers |
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.
| Method | Resolution | Class-discriminative | Cost |
|---|---|---|---|
| Grad-CAM | Coarse regions | Yes | One forward and backward pass |
| Saliency gradients | Pixel level | Weakly | One backward pass |
| SmoothGrad | Pixel level, denoised | Weakly | Tens of passes |
| Occlusion sensitivity | Patch level | Yes | Hundreds of passes |
| Integrated gradients | Pixel level | Yes | Twenty to several hundred passes |
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.
| Pitfall | Consequence | Remedy |
|---|---|---|
| Low map resolution | Small objects appear as diffuse blobs | Use earlier layers or Layer-CAM |
| Gradient saturation | Important regions receive near-zero weight | Try Score-CAM or Grad-CAM++ |
| Single-image reading | Anecdotal conclusions about the model | Aggregate maps over many samples |
| Non-CNN models | No natural spatial feature maps | Use attention rollout for transformers |
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.