concept class activation in category deep learning

This is an excerpt from Manning's book Deep Learning with R.
Visualizing heatmaps of class activation in an image—Useful for understanding which parts of an image were identified as belonging to a given class, thus allowing you to localize objects in images.
This general category of techniques is called class activation map (CAM) visualization, and it consists of producing heatmaps of class activation over input images. A class activation heatmap is a 2D grid of scores associated with a specific output class, computed for every location in any input image, indicating how important each location is with respect to the class under consideration. For instance, given an image fed into a dogs-versus-cats convnet, CAM visualization allows you to generate a heatmap for the class “cat,” indicating how cat-like different parts of the image are, and also a heatmap for the class “dog,” indicating how dog-like parts of the image are.

This is an excerpt from Manning's book Deep Learning with Python.
Visualizing heatmaps of class activation in an image— Useful for understanding which parts of an image were identified as belonging to a given class, thus allowing you to localize objects in images.
This general category of techniques is called class activation map (CAM) visualization, and it consists of producing heatmaps of class activation over input images. A class activation heatmap is a 2D grid of scores associated with a specific output class, computed for every location in any input image, indicating how important each location is with respect to the class under consideration. For instance, given an image fed into a dogs-versus-cats convnet, CAM visualization allows you to generate a heatmap for the class “cat,” indicating how cat-like different parts of the image are, and also a heatmap for the class “dog,” indicating how dog-like parts of the image are.
Listing 5.42. Setting up the Grad-CAM algorithm
african_e66lephant_output = model.output[:, 386] #1 last_conv_layer = model.get_layer('block5_conv3') #2 grads = K.gradients(african_elephant_output, last_conv_layer.output)[0] #3 pooled_grads = K.mean(grads, axis=(0, 1, 2)) #4 iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]]) #5 pooled_grads_value, conv_layer_output_value = iterate([x]) #6 for i in range(512): #7 conv_layer_output_value[:, :, i] *= pooled_grads_value[i] #7 heatmap = np.mean(conv_layer_output_value, axis=-1) #8 #1 - “African elephant” entry in the prediction vector #2 - Output feature map of the block5_conv3 layer, the last convolutional layer in VGG16 #3 - Gradient of the “African elephant” class with regard to the output feature map of block5_conv3 #4 - Vector of shape (512,), where each entry is the mean intensity of the gradient over a specific feature-map channel #5 - Lets you access the values of the quantities you just defined: pooled_grads and the output feature map of block5_conv3, given a sample image #6 - Values of these two quantities, as Numpy arrays, given the sample image of two elephants #7 - Multiplies each channel in the feature-map array by “how important this channel is” with regard to the “elephant” class #8 - The channel-wise mean of the resulting feature map is the heatmap of the class activation.