Awesome Open Source
Awesome Open Source

Sample code for the Class Activation Mapping

NEW: PyTorch Demo code

  • The popular networks such as ResNet, DenseNet, SqueezeNet, Inception already have global average pooling at the end, so you could generate the heatmap directly without even modifying the network architecture. Here is a sample script to generate CAM for the pretrained networks.

You also could take a look at the unified PlacesCNN scene prediction code to see how the CAM along with scene categories, scene attributes are predicted. It has been used in the PlacesCNN scene recognition demo.

We propose a simple technique to expose the implicit attention of Convolutional Neural Networks on the image. It highlights the most informative image regions relevant to the predicted class. You could get attention-based model instantly by tweaking your own CNN a little bit more. The paper is published at CVPR'16.

The framework of the Class Activation Mapping is as below: Framework

Some predicted class activation maps are: Results

Pre-trained models in Caffe:

  • GoogLeNet-CAM model on ImageNet: models/deploy_googlenetCAM.prototxt weights:[]
  • VGG16-CAM model on ImageNet: models/deploy_vgg16CAM.prototxt weights:[]
  • GoogLeNet-CAM model on Places205: models/deploy_googlenetCAM_places205.prototxt weights:[]
  • AlexNet+-CAM on ImageNet:models/deploy_alexnetplusCAM_imagenet.prototxt weights:[]
  • AlexNet+-CAM on Places205 (used in the online demo):models/deploy_alexnetplusCAM_places205.prototxt weights:[]

Usage Instructions:

  • Install caffe, compile the matcaffe (matlab wrapper for caffe), and make sure you could run the prediction example code classification.m.
  • Clone the code from Github:
git clone
cd CAM
  • Download the pretrained network
sh models/
  • Run the demo code to generate the heatmap: in matlab terminal,
  • Run the demo code to generate bounding boxes from the heatmap: in matlab terminal,

The demo video of what the CNN is looking is here. The reimplementation in tensorflow is here. The pycaffe wrapper of CAM is reimplemented at here.

ILSVRC evaluation


    author    = {Zhou, Bolei and Khosla, Aditya and Lapedriza, Agata and Oliva, Aude and Torralba, Antonio},
    title     = {Learning Deep Features for Discriminative Localization},
    booktitle = {Computer Vision and Pattern Recognition},
    year      = {2016}


The pre-trained models and the CAM technique are released for unrestricted use.

Contact Bolei Zhou if you have questions.

Related Awesome Lists
Top Programming Languages
Top Projects

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Matlab (31,139
Scene (8,263
Caffe (3,323
Imagenet (1,845
Heatmap (1,584