What is Grad-CAM (pytorch-grad-cam)?
Grad-CAM (pytorch-grad-cam) is Pixel-attribution methods that show what a vision model looked at. It sits in the Vision & Understanding category of the Multimodal Generation tools and is open-source, written in Python and licensed MIT. To get started: install.
Overview
pytorch-grad-cam is a package of explainability methods for computer vision in PyTorch. All of them answer the same question in different ways: which pixels did this model actually use to produce this output? The result is a heatmap over the input image, which you can overlay on the original to see whether a prediction rests on the object or on the background.
The library collects a large family of class-activation-mapping methods rather than just the original Grad-CAM — HiResCAM, GradCAM++, XGradCAM, AblationCAM, ScoreCAM, EigenCAM, EigenGradCAM, LayerCAM, FullGrad, KPCA-CAM, FEM, ShapleyCAM, FinerCAM, SegEigenCAM, Deep Feature Factorization, and meta-methods such as RefineCAM and SESS that combine CAMs across layers or scales. They differ in how gradients and activations are weighted, and which ones behave well depends on the model and the layer.
It is tested against both convolutional networks and vision transformers, and it is not limited to classification: the documentation covers object detection, semantic segmentation, image-similarity and embedding explanations, and explaining a CLIP text prompt. It also ships smoothing options and, importantly, metrics for judging whether an explanation can be trusted — so the CAM you pick can be evaluated rather than eyeballed. Every method supports batches of images.
What it does
- A large collection of pixel-attribution methods behind one consistent API, from Grad-CAM to ShapleyCAM, FinerCAM and FullGrad
- Works with CNNs and vision transformers, including ViT and Swin architectures
- Beyond classification: object detection, semantic segmentation, image similarity and embedding explanations, and CLIP text prompts
- Metrics for evaluating whether an explanation is faithful, so methods can be compared and tuned
- Smoothing options (aug_smooth, eigen_smooth) to produce cleaner heatmaps
- Batched execution — every method accepts a batch tensor rather than one image at a time
Getting started
Install the package, pick the layer whose activations you want to explain, and run a CAM object over an input tensor. The example below is the README's usage snippet, explaining a torchvision ResNet-50.
Install
The PyPI package is named grad-cam.
pip install grad-camGenerate a heatmap for a prediction
Choose target_layers (usually the last convolutional block) and a target class, construct the CAM once, and reuse it across images. show_cam_on_image overlays the grayscale map on the original picture.
from pytorch_grad_cam import GradCAM, HiResCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM, FullGrad
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
from pytorch_grad_cam.utils.image import show_cam_on_image
from torchvision.models import resnet50, ResNet50_Weights
model = resnet50(weights=ResNet50_Weights.DEFAULT)
target_layers = [model.layer4[-1]]
input_tensor = # Create an input tensor image for your model..
targets = [ClassifierOutputTarget(281)]
with GradCAM(model=model, target_layers=target_layers) as cam:
grayscale_cam = cam(input_tensor=input_tensor, targets=targets)
grayscale_cam = grayscale_cam[0, :]
visualization = show_cam_on_image(rgb_img, grayscale_cam, use_rgb=True)
model_outputs = cam.outputsCompare methods and check them with metrics
Swap GradCAM for any other method imported above and compare the maps. The repository's cam.py has a fuller usage example, and the documentation site covers layer choice, detection and segmentation targets, and the explanation-quality metrics.
Commands and code are distilled from the project's own documentation — always check the official repo for the latest.
When to use it
- Diagnose a misclassification by checking whether the model attended to the object or to background cues
- Sanity-check a model before deployment, so a good validation score is not hiding a spurious shortcut
- Explain detection or segmentation outputs to reviewers who need to see the evidence, not just the label
- Benchmark explainability methods against each other using the built-in faithfulness metrics
How Grad-CAM (pytorch-grad-cam) compares
Grad-CAM (pytorch-grad-cam) alongside other open-source vision & understanding tools AI/TLDR tracks, ranked by GitHub stars.
| Tool | Stars | What it does |
|---|---|---|
| PaddleOCR | ★ 93.5k | A toolkit for detecting and recognizing text in images across many languages, plus document parsing. |
| OpenCV | ★ 91.8k | The long-standing open-source computer-vision library for C++, Python and Java; version 5 rewrote the DNN engine and added ONNX-based LLM and VLM inference in-library. |
| Ultralytics YOLO | ★ 63.9k | A framework for training and running YOLO models for real-time object detection, segmentation, and tracking. |
| YOLOv5 | ★ 58.3k | Ultralytics' PyTorch repo for training, running and exporting YOLOv5 object-detection, instance-segmentation and classification models with plain scripts and PyTorch Hub. |
| Supervision | ★ 51.8k | A Python toolkit for processing, annotating, and visualizing detections and segmentations from many vision models. |
| MediaPipe | ★ 38k | Google's on-device ML framework: ready-to-run vision, text and audio tasks with one cross-platform API for Android, iOS, web, desktop and edge. |
| MMDetection | ★ 33.1k | An OpenMMLab toolbox with many object detection and instance segmentation algorithms for research and production. |
| Grad-CAM (pytorch-grad-cam) | ★ 13k | Pixel-attribution methods that show what a vision model looked at |
