liveBook · Manning

Class activation heatmap

As explained in Deep Learning with Python, Third Edition

A visualization technique that highlights image regions relevant to a specific predicted class by producing class-specific activation heatmaps.

A class activation heatmap highlights which image regions contributed to a specific ConvNet prediction. Each cell scores the importance of a location for one class, helping explain decisions and roughly locate objects. section 10.3

African-elephant prediction

For an image containing two African elephants, Grad-CAM produces a heatmap for the predicted class. Strong activation around the calf’s ears helps explain the African-elephant prediction and distinguishes it from an Indian-elephant prediction. section 10.3.4

A practical Grad-CAM workflow is:

  • Run the complete pretrained Xception classifier and identify the target output class. section 10.3
  • Weight each activation channel by the gradient of the selected class, average the weighted channels, discard negative values, and normalize the result between zero and one. section 10.3
Listing 10.23 Gradient pooling and channel importance weighting
pooled_grads = np.mean(grads, axis=(0, 1, 2))                       #1
last_conv_layer_output = last_conv_layer_output[0].copy()
for i in range(pooled_grads.shape[-1]):                             #2
    last_conv_layer_output[:, :, i] *= pooled_grads[i]               #2
heatmap = np.mean(last_conv_layer_output, axis=-1)                  #3

Inspect the compact NumPy operation that pools gradients, scales each convolutional channel by its class importance, and averages channels into a heatmap. section 10.3.4

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines class-specific spatial scores and works through Grad-CAM construction, normalization, and overlay without surveying alternative methods.
Examples
Uses dogs-versus-cats classification and a pretrained Xception model analyzing two African elephants.
What it assumes
Assumes familiarity with ConvNet predictions, convolutional feature maps, gradients, and pretrained model inference.
What you can do after
Diagnose/debug with Grad-CAM by generating, normalizing, and overlaying class-specific evidence on an input image.
How it connects
Connects ConvNets, convolutional layers, gradients, image classification, object localization, and model interpretability.

Also explained in

Compare how 2 books explain class activation heatmap →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.