What is a Class Activation Map?
Class activation maps or grad-CAM is another way of visualizing attention over input. Instead of using gradients with
respect to output (see saliency), grad-CAM uses penultimate (pre
Conv layer output. The
intuition is to use the nearest
Conv layer to utilize spatial information that gets completely lost in
There are two APIs exposed to visualize grad-CAM and are almost identical to saliency usage.
- visualize_cam: This is the general purpose API for visualizing grad-CAM.
- visualize_cam_with_losses: This is intended for research use-cases where some custom weighted loss can be used.
The only notable addition is the
penultimate_layer_idx parameter. This can be used to specify the pre-layer
whose output gradients are used. By default, keras-vis will search for the nearest layer with filters.
See saliency scenarios. Everything is identical expect the added
grad-CAM only works well if the penultimate layer is close to the layer being visualized. This also applies to
filter visualizations. You are better off using saliency of this is not the case with your model.