Home

Awesome

Grad-CAM with PyTorch

PyTorch implementation of Grad-CAM (Gradient-weighted Class Activation Mapping) [1] in image classification. This repository also contains implementations of vanilla backpropagation, guided backpropagation [2], deconvnet [2], and guided Grad-CAM [1], occlusion sensitivity maps [3].

Requirements

Python 2.7 / 3.+

$ pip install click opencv-python matplotlib tqdm numpy
$ pip install "torch>=0.4.1" torchvision

Basic usage

python main.py [DEMO_ID] [OPTIONS]

Demo ID:

Options:

The command above generates, for top k classes:

The guided-* do not support F.relu but only nn.ReLU in this codes. For instance, off-the-shelf inception_v3 cannot cut off negative gradients during backward operation (issue #2).

Demo 1

Generate all kinds of visualization maps given a torchvision model, a target layer, and images.

python main.py demo1 -a resnet152 -t layer4 \
                     -i samples/cat_dog.png -i samples/vegetables.jpg # You can add more images
Predicted class#1 boxer#2 bull mastiff#3 tiger cat
Grad-CAM [1]
Vanilla backpropagation
"Deconvnet" [2]
Guided backpropagation [2]
Guided Grad-CAM [1]

Grad-CAM with different models for "bull mastiff" class

Modelresnet152vgg19vgg19_bndensenet201squeezenet1_1
Layerlayer4featuresfeaturesfeaturesfeatures
Grad-CAM [1]

Demo 2

Generate Grad-CAM maps for "bull mastiff" class, at different layers of ResNet-152 (hardcoded).

python main.py demo2 -i samples/cat_dog.png
Layerrelulayer1layer2layer3layer4
Grad-CAM [1]

Demo 3

Generate the occlusion sensitivity map [1, 3] based on logit scores. The red and blue regions indicate a relative increase and decrease from non-occluded scores respectively: the blue regions are critical!

python main.py demo3 -a resnet152 -i samples/cat_dog.png
Patch size10x1015x1525x2535x3545x4590x90
"boxer" sensitivity
"bull mastiff" sensitivity
"tiger cat" sensitivity

This demo takes much time to compute per-pixel logits. You can control the resolution by changing sampling stride (--stride), or increasing batch size as to fit on your GPUs (--n-batches). The model is wrapped with torch.nn.DataParallel so that runs on multiple GPUs by default.

References

  1. R. R. Selvaraju, A. Das, R. Vedantam, M. Cogswell, D. Parikh, and D. Batra. Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization. In ICCV, 2017
  2. J. T. Springenberg, A. Dosovitskiy, T. Brox, and M. Riedmiller. Striving for Simplicity: The All Convolutional Net. arXiv, 2014
  3. M. D. Zeiler, R. Fergus. Visualizing and Understanding Convolutional Networks. In ECCV, 2013