Skip to content

Grad-CAM for Keras

This topic shows you how to use MissingLink to generate a Grad-CAM (gradient class activation map) for Keras.

Gradient class activation maps are a visualization technique for deep learning networks.

The steps that are covered are:

  • Installing OpenCV-Python
  • Generating a Grad-CAM for Keras


Ensure that you have:

Installing OpenCV-Python

  • Option 1: Installing OpenCV-Python with pip

    If you have set up the system as the documentation has detailed here, you can simply run the command below. Otherwise, go to Option 2.

    pip install opencv-python
  • Option 2: Installing OpenCV-Python based on your environment

    • Install OpenCV-Python on Windows.

    • Install OpenCV-Python on MacOS.

Write code

Generate Grad-CAM with an image, by adding the following code fragment at the end:

path = [URI]
missinglink_callback.generate_grad_cam(path, model)

Parameter descriptions

  • path: URI of an image. This is the original image.


    MissingLink understands that data can be proprietary and has an option for images to be private to only you when you provide MissingLink with a private URL.

    For URL, for example:, MissingLink will not download the image or store it on MissingLink servers. You need to ensure that the URL you have provided is private and can be accessed only from your machine or office.

    For a local file starting with "file://" or just a pure path such as "/home/user/image.jpg", MissingLink will download the file and serve it from MissingLink servers.

  • model: The model for running the prediction.

  • input_array (optional): Image preprocessed the same way as it would be used to feed to the network. If this is None MissingLink assume no preprocessing is needed and just load an image.
  • top_classes (optional): Display this number of most probable classes. 5 is the default.
  • top_images (optional): Display this number of CAMs for the most probable classes. 1 is the default.
  • dim_order (optional): Dimensions order of the image. Can be one of "nhwc" -- for channel last ordering and "nchw" for channel first ordering. "nchw" is the default.
  • class_mapping (optional): Class mappings are dictionaries you can provide in order for MissingLink to show classes by name.
  • description (optional): A string to display on the console that describes this data point. Can be the image id or any text.

You can find a full updated script here that MissingLink has provided using a pre-trained model known as VGG as an example.

You are done generating a Grad-CAM for Keras. Now run the script and check the model on the MissingLink dashboard.