Explainable AI (XAI) Advanced 15 minutes

Saliency Maps, Grad-CAM and Integrated Gradients

Which parts of the image influenced the network's decision? The basic gradient methods, their advantages and their limits.

Your progress in the track0%

Vanilla gradient

$$S(x) = \left|\frac{\partial y_c}{\partial x}\right|$$

Fast but noisy. SmoothGrad Averages the gradient over noisy copies of the image to smooth the map.

Grad-CAM

  1. Compute the gradient of the class score \(y_c\) with respect to the feature maps \(A^k\) in a convolutional layer.
  2. The weight \(\alpha_k^c\) = the average gradient over positions.
  3. The map:

$$L^c = \mathrm{ReLU}\Big(\sum_k \alpha_k^c A^k\Big)$$

Integrated gradients

$$IG_i(x) = (x_i – x_i’) \int_0^1 \frac{\partial f\big(x’ + \alpha(x – x’)\big)}{\partial x_i}\, d\alpha$$

They satisfy the completeness property: the sum of the attributions equals \(f(x) – f(x’)\).

An implementation with Captum

import torch
from torchvision.models import resnet50, ResNet50_Weights
from captum.attr import LayerGradCam, IntegratedGradients, LayerAttribution
model = resnet50(weights=ResNet50_Weights.DEFAULT).eval()
x = torch.randn(1, 3, 224, 224)              # replace with a preprocessed image
target = model(x).argmax(1).item()
gc = LayerGradCam(model, model.layer4)
heat = LayerAttribution.interpolate(gc.attribute(x, target=target), (224, 224))
ig = IntegratedGradients(model)
attr = ig.attribute(x, baselines=torch.zeros_like(x), target=target, n_steps=50)
print(heat.shape, attr.shape)

Comparison

MethodSpatial resolutionCostNote
Vanilla gradientHigh but noisyLowAffected by saturation
SmoothGradClearerMediumDepends on the noise level
Grad-CAMCoarseLowSpecific to convolutional networks
Integrated gradientsHighHigherDepends on the baseline

In the next lesson we will see that some saliency methods pass the “eye test” but fail faithfulness tests.

Lesson terms

Integrated gradients
An attribution that accumulates gradients along a straight path from a baseline input to the actual input.
Saliency map
A map that gives each pixel a score estimating its effect on the model's decision, often from the gradient.
Gradient-weighted class activation map
A method that produces a class activation map by weighting the feature maps of a convolutional layer by the average of the class gradients with respect to them.

Abstract

Saliency maps use the gradient of the class output with respect to the pixels to estimate how sensitive the decision is to each pixel, but they are noisy. Grad-CAM weights the feature maps of the last convolutional layer by the average of their gradients, producing a coarse, class-specific heatmap. Integrated gradients accumulate gradients along a path from a baseline to the image and satisfy the completeness property. All these methods need their faithfulness verified, and they do not tell us why a region matters, only where the model looked.

Review questions

  1. Why are Grad-CAM maps coarse?

    Because they are built at the resolution of the last convolutional layer, then upscaled to the image size.

  2. What is the role of the baseline in integrated gradients?

    It represents "absence of information", and the attribution measures the difference from it, so its choice changes the result.

Sources and references

  1. Simonyan, Vedaldi & Zisserman (2013) — Deep Inside Convolutional Networks
  2. Selvaraju et al. — Grad-CAM
  3. Sundararajan, Taly & Yan (2017) — Axiomatic Attribution for Deep Networks
  4. Captum Documentation

Complete the lesson