Skip to content

Grad-CAM for TensorFlow

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

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

Overview

The steps that are covered are:

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

Preparation

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:

    session = <CURRENT_SESSION>

    path = <URI>

    with missinglink_project.create_experiment() as experiment:
        experiment.set_properties(
            input_placeholder=<INPUT_PLACEHOLDER_NAME>,
            output_layer=<OUTPUT_LAYER_NAME>,
            last_conv_layer=<LAST_CONV_LAYER_NAME>
        )

        experiment.generate_grad_cam(path, session)

Parameter descriptions

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

    Note

    MissingLink.ai 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: http://cmeimg-a.akamaihd.net/640/photos.demandstudios.com/getty/article/103/49/516464087.jpg, MissingLink.ai 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.ai 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 TensorFlow. Now run the script and check the model on the MissingLink dashboard.