explaingit

jacobgil/pytorch-grad-cam

12,802PythonAudience · researcherComplexity · 3/5LicenseSetup · easy

TLDR

pytorch-grad-cam is a Python library that generates visual heatmaps showing which parts of an image caused an AI model to make a specific prediction, helping you understand and debug image classifiers.

Mindmap

mindmap
  root((Grad-CAM))
    What it does
      Visual heatmaps
      Model explanations
      Pixel attribution
    Methods
      GradCAM
      ScoreCAM
      EigenCAM
      AblationCAM
    Use cases
      Classification
      Object detection
      Segmentation
      CLIP models
    Setup
      pip install
      PyTorch required
      Batch processing
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Visualize which pixels in a medical image made an AI classifier flag it as abnormal, to check whether the model is focusing on the right areas.

USE CASE 2

Debug a misclassified image by generating a heatmap showing what the model paid attention to instead of the correct feature.

USE CASE 3

Compare multiple explainability methods side by side to choose the most reliable one for your computer vision model.

USE CASE 4

Validate that a trained object detector is highlighting the actual object and not background artifacts.

Tech stack

PythonPyTorch

Getting it running

Difficulty · easy Time to first run · 30min

Install via pip install grad-cam, requires an existing PyTorch model and image, no GPU required but recommended for speed.

MIT, use freely for any purpose including commercial, with no restrictions beyond keeping the copyright notice.

In plain English

This is a Python library that helps you understand why an image-recognition AI reaches the decisions it does. When a neural network classifies a photo as a dog or a cat, it processes thousands of internal signals, and by default there is no easy way to ask the network to show its work. This library generates visual heatmaps, called class activation maps, that highlight the specific pixels or regions in an image that pushed the network toward a particular answer. The library supports many different methods for producing these heatmaps. GradCAM is the most common: it weights internal activations by their gradients to show where the model focused. Other methods like ScoreCAM, AblationCAM, and EigenCAM approach the same problem from different angles, each with trade-offs in speed, accuracy, and how faithfully they represent the model's actual reasoning. More than a dozen methods are included, and the README compares them in a table so you can choose the one that fits your situation. Beyond plain image classification, the library works with object detection, semantic segmentation, image similarity comparisons, and multimodal models like CLIP. A collection of built-in metrics checks how reliable a given explanation is, which helps researchers and developers spot when a heatmap may be misleading rather than informative. Install is straightforward: pip install grad-cam. The library is tested against standard convolutional network architectures and newer Vision Transformer designs. It supports processing images in batches for efficiency, and smoothing options are available to make the output heatmaps easier to read visually. The project is intended both for people actively building or diagnosing computer vision models and for researchers comparing explainability techniques. If you want to understand why your image classifier made a wrong prediction, or to check that it is paying attention to the right parts of an image, this library gives you visual tools to investigate that.

Copy-paste prompts

Prompt 1
Using pytorch-grad-cam, write Python code that loads a pretrained ResNet50, runs GradCAM on a test image, and overlays the resulting heatmap on the original image to show what the model focused on.
Prompt 2
I have a Vision Transformer model classifying medical images. Show me how to use pytorch-grad-cam to generate a GradCAM++ heatmap and overlay it on the image, and explain what the heatmap means.
Prompt 3
My image classifier is making wrong predictions on certain images. Walk me through using pytorch-grad-cam to generate explanations for misclassified examples to understand what features are confusing the model.
Prompt 4
Explain the difference between GradCAM, ScoreCAM, and EigenCAM in plain English and when I should choose each one for debugging my image recognition model.
Open on GitHub → Explain another repo

← jacobgil on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.