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

