/cnncam

an open source python library for researchers and developers to generate GradCAM explanations for their tensorflow CNN models.

Primary LanguagePythonMIT LicenseMIT

cnncam

Maintainer PyPI version linting: pylint License: MIT Lifecycle: experimental GitHub release

Introduction

An open python library for researchers and developers to generate GradCAM explanations for the TensorFlow CNN models.

Other popular python ML frameworks will soon be supported by the cnncam library

Install

The below instructions assume you already have pip installed and exposed to the python environment where you want to run cnncam. Official instructions for installing pip can be found here

Run the below pip command in a shell of your choice.

pip install cnncam

Demo

We currently support two ways of obtaining GradCAM heatmaps:

  1. Display heatmap. If you're only interested in seeing the heatmap images for your model's prediction, you can run the below script:
from cnncam import display_heatmap
from keras.applications.vgg16 import VGG16

display_heatmap(model=VGG16(), # your keras model
                img=img, # your image
                predicted_class=pred, # your models prediction for the image 
                layer_name='block5_conv3', # the layer you would like to see GradCAM for 
                alpha=0.6 # opacity of heatmap overlayed on image
                )

Output: alt text

We can observe the differences in convolutional layer behaviours across different models, for example, the below code executes the same function on Xception instead of VGG16:

from cnncam import display_heatmap
from keras.applications.xception import Xception

display_heatmap(model=Xception(), # your keras model
                img=img, # your image
                predicted_class=pred, # your models prediction for the image 
                layer_name='block5_conv3', # the layer you would like to see GradCAM for 
                alpha=1 # opacity of heatmap overlayed on image
                )

Output: alt text

See /examples for executable examples, including the above application of our implementation of GradCAM on VGG-16 with the owner of this repo's very cute cat, meso.