question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Grad-CAM implementation

See original GitHub issue

šŸš€ Feature

Implementation of Grad-CAM, probably as a vision callback.

Motivation

Grad-CAM is widely used localization method that uses the gradient information flowing into the last convolutional layer of a CNN to visualize the model’s prediction by highlighting the ā€œimportantā€ pixels in the image.

The technique does not require any modifications to the existing model architecture, so can be applied to any CNN-based architecture, including those for image captioning and visual question answering.

Pitch

Right now, for research we’re doing in the lab I’m working in, I’ve been using a modified version of this PyTorch implementation of Grad-CAM, which only works on batch_size = 1 (but it looks like there are many other PyTorch implementations, with many stars, on GitHub that we could work off of).

For the above research, I’ve added an if statement to the test_step function in our LightningModule so that if we want the cams to be saved during inference, it calls a separate util function localize that does the forward and backward pass to create the feature maps. I’m not sure this is ideal, though, because we later do another forward and backward pass on the same image to get the prediction, so there is duplicated work.

I was thinking that it would be nice to have some sort of callback that can just generate (and save?) the cams for you, without having to mess with the training pipeline. I guess we would have to figure out where the the cams would be saved, and whether just the heatmap would be saved, or the heatmap overlaid on the original image (which is probably the most helpful?).

Alternatives

Other localization methods include Integrated Gradients, WILDCAT, and Grad-CAM++, but Grad-CAM seems to be the most widely-used.

Additional context

I’m not sure if this is helpful, but to clarify further how I’m currently doing this in Lightning: I’ve created inference_step and inference_epoch_end functions that both my valid functions and my test functions in the LightningModule call (that way, we can make sure that both valid and test are doing inference in the same way). Only my test_step has a separate if statement that’s called only if the user wants to also generate cams.

Anyway, I’d love to help out on this in any way I can! I’ve never written a callback before, though, so would need some guidance on how to approach that.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

2reactions
Kshitij09commented, Oct 2, 2020

How about integrating captum for the same? They’ve wide range of interpretability alogorithms implemented with rigorous testing. We could simply write a Callback for captum and add ā€˜captum’ as explicit dependency, throwing exception at runtime. Captum also provides utility functions for visualization so it’ll save a lot of efforts if we just use the library.

cc: @ASaporta

1reaction
edgarribacommented, Sep 19, 2020

@ASaporta in case this goes through, after a quick look at the code I see at least a couple of potential places where some utilities from kornia could be used here, resize and normalize_min_max

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing Grad-CAM in PyTorch - Medium
In this post I am going to re-implement the Grad-CAM algorithm, ... not to worry, I will add this layer in the forward()...
Read more >
How to implement Grad-CAM on a trained network
I want to apply gradcam using my own model and not VGG16 or ResNet etc. apply_gradcam.py # import the necessary packages from Grad_CAM.gradcam...
Read more >
Grad-CAM class activation visualization - Keras
Grad-CAM class activation visualization Ā· Adapted from Deep Learning with Python (2017). Ā· Setup Ā· Configurable parameters Ā· The Grad-CAM algorithm.
Read more >
jacobgil/pytorch-grad-cam: Advanced AI Explainability for ...
Some methods like ScoreCAM and AblationCAM require a large number of forward passes, and have a batched implementation. You can control the batch...
Read more >
Grad-CAM: Visualize class activation maps with Keras ...
We'll then implement Grad-CAM using Keras and TensorFlow. While deep learning has facilitated unprecedented accuracy in image classificationĀ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found