# Class Activation Map(CAM) Visualization ## Introduction of the CAM visualization tool MMClassification provides `tools\visualizations\vis_cam.py` tool to visualize class activation map. Please use `pip install "grad-cam>=1.3.6"` command to install [pytorch-grad-cam](https://github.com/jacobgil/pytorch-grad-cam). The supported methods are as follows: | Method | What it does | | ------------ | ---------------------------------------------------------------------------------------------------------------------------- | | GradCAM | Weight the 2D activations by the average gradient | | GradCAM++ | Like GradCAM but uses second order gradients | | XGradCAM | Like GradCAM but scale the gradients by the normalized activations | | EigenCAM | Takes the first principle component of the 2D Activations (no class discrimination, but seems to give great results) | | EigenGradCAM | Like EigenCAM but with class discrimination: First principle component of Activations\*Grad. Looks like GradCAM, but cleaner | | LayerCAM | Spatially weight the activations by positive gradients. Works better especially in lower layers | **Command**: ```bash python tools/visualizations/vis_cam.py \ ${IMG} \ ${CONFIG_FILE} \ ${CHECKPOINT} \ [--target-layers ${TARGET-LAYERS}] \ [--preview-model] \ [--method ${METHOD}] \ [--target-category ${TARGET-CATEGORY}] \ [--save-path ${SAVE_PATH}] \ [--vit-like] \ [--num-extra-tokens ${NUM-EXTRA-TOKENS}] [--aug_smooth] \ [--eigen_smooth] \ [--device ${DEVICE}] \ [--cfg-options ${CFG-OPTIONS}] ``` **Description of all arguments**: - `img` : The target picture path. - `config` : The path of the model config file. - `checkpoint` : The path of the checkpoint. - `--target-layers` : The target layers to get activation maps, one or more network layers can be specified. If not set, use the norm layer of the last block. - `--preview-model` : Whether to print all network layer names in the model. - `--method` : Visualization method, supports `GradCAM`, `GradCAM++`, `XGradCAM`, `EigenCAM`, `EigenGradCAM`, `LayerCAM`, which is case insensitive. Defaults to `GradCAM`. - `--target-category` : Target category, if not set, use the category detected by the given model. - `--save-path` : The path to save the CAM visualization image. If not set, the CAM image will not be saved. - `--vit-like` : Whether the network is ViT-like network. - `--num-extra-tokens` : The number of extra tokens in ViT-like backbones. If not set, use num_extra_tokens the backbone. - `--aug_smooth` : Whether to use TTA(Test Time Augment) to get CAM. - `--eigen_smooth` : Whether to use the principal component to reduce noise. - `--device` : The computing device used. Default to 'cpu'. - `--cfg-options` : Modifications to the configuration file, refer to [Learn about Configs](../user_guides/config.md). ```{note} The argument `--preview-model` can view all network layers names in the given model. It will be helpful if you know nothing about the model layers when setting `--target-layers`. ``` ## How to visualize the CAM of CNN(ResNet-50) Here are some examples of `target-layers` in ResNet-50, which can be any module or layer: - `'backbone.layer4'` means the output of the forth ResLayer. - `'backbone.layer4.2'` means the output of the third BottleNeck block in the forth ResLayer. - `'backbone.layer4.2.conv1'` means the output of the `conv1` layer in above BottleNeck block. ```{note} For `ModuleList` or `Sequential`, you can also use the index to specify which sub-module is the target layer. For example, the `backbone.layer4[-1]` is the same as `backbone.layer4.2` since `layer4` is a `Sequential` with three sub-modules. ``` 1. Use different methods to visualize CAM for `ResNet50`, the `target-category` is the predicted result by the given checkpoint, using the default `target-layers`. ```shell python tools/visualizations/vis_cam.py \ demo/bird.JPEG \ configs/resnet/resnet50_8xb32_in1k.py \ https://download.openmmlab.com/mmclassification/v0/resnet/resnet50_batch256_imagenet_20200708-cfb998bf.pth \ --method GradCAM # GradCAM++, XGradCAM, EigenCAM, EigenGradCAM, LayerCAM ``` | Image | GradCAM | GradCAM++ | EigenGradCAM | LayerCAM | | ------------------------------------ | --------------------------------------- | ----------------------------------------- | -------------------------------------------- | ---------------------------------------- | |