Ultralytics YOLOv5, a PyTorch-based computer vision toolkit for object detection, instance segmentation and image classification with training and export.
jacobgil/pytorch-grad-cam
A PyTorch library implementing class activation map (CAM) methods to visualize which image regions drive a computer vision model's predictions.
- Stars
- 13,000
- Forks
- 1,715
- Open issues
- 166
- Last commit
- 2 months ago
What is it?
pytorch-grad-cam is a Python package of explainability methods for computer vision models built on PyTorch. It produces heatmaps showing which pixels contributed most to a model's output, which helps when diagnosing predictions during development or in production.
The package bundles a broad set of CAM variants, including Grad-CAM, HiResCAM, Grad-CAM++, Score-CAM, Ablation-CAM, Eigen-CAM and LayerCAM, along with smoothing options and metrics such as ROAD for scoring how faithful an explanation is. It works with CNNs and Vision Transformers, and it can be applied beyond classification to object detection, semantic segmentation, embedding similarity and CLIP. Batched inputs are supported, and users can adapt the methods to new architectures through reshape transforms and custom model targets.
Installation is via pip under the name grad-cam. The documentation site includes notebook tutorials covering HuggingFace classifiers, Faster R-CNN, YOLO and segmentation models.
Key features
- 01Implements many CAM methods, including Grad-CAM, Grad-CAM++, Score-CAM, Ablation-CAM and Eigen-CAM.
- 02Works with CNNs and Vision Transformers such as ViT and Swin.
- 03Supports object detection, semantic segmentation, embedding similarity and CLIP.
- 04Provides smoothing options (aug_smooth and eigen_smooth) to reduce noise in heatmaps.
- 05Includes evaluation metrics such as ROAD for measuring explanation quality.
- 06Handles batched images and runs on CPU, CUDA or MPS devices.
Use cases
- Debugging why an image classifier makes a particular prediction.
- Checking whether a detection or segmentation model focuses on the right objects.
- Comparing explanation methods and metrics in XAI research.
- Inspecting Vision Transformer attention-like behavior on custom datasets.
Quick start
- 1
Install the package from PyPI
pip install grad-cam - 2
Import a CAM method and a model, then build the CAM object and run it on an input tensor (see usage example in README)
Things to know
- Requires PyTorch and a model you supply; you must choose the correct target layer for each architecture.
Topics
FAQ
- What is pytorch-grad-cam?
- A PyTorch library implementing class activation map (CAM) methods to visualize which image regions drive a computer vision model's predictions. pytorch-grad-cam is a Python package of explainability methods for computer vision models built on PyTorch. It produces heatmaps showing which pixels contributed most to a model's output, which helps when diagnosing predictions during development or in production.
- Is pytorch-grad-cam free to use? What license does it have?
- pytorch-grad-cam is open source under the MIT license. Check the license terms before using it in commercial products.
- Is pytorch-grad-cam still maintained?
- Updated within 6 months. The last commit to pytorch-grad-cam was on Aug 13, 2026.
- What can I use pytorch-grad-cam for?
- Debugging why an image classifier makes a particular prediction. Checking whether a detection or segmentation model focuses on the right objects. Comparing explanation methods and metrics in XAI research. Inspecting Vision Transformer attention-like behavior on custom datasets.
- How do I install pytorch-grad-cam?
- Install the package from PyPI: pip install grad-cam Import a CAM method and a model, then build the CAM object and run it on an input tensor (see usage example in README)
- Where can I get pytorch-grad-cam?
- The source code and installation instructions are on GitHub: https://github.com/jacobgil/pytorch-grad-cam
Summary generated by AI from the project's README. GitHub stats refresh daily.
Related projects
Python tool for blind face restoration, enhancing blurry, low-quality or damaged faces in photos and videos using a codebook lookup transformer.
Open-source Python OCR library that detects and recognizes text in PDFs and images using deep learning models, built on PyTorch.
Official PyTorch implementation of BiRefNet, a deep learning model for high-resolution dichotomous image segmentation, commonly used for background removal.
A video LLM trained with streaming speech transcription that produces real-time commentary on live video, from the CVPR 2025 LiveCC paper.
A deep learning optimization library for making distributed training and inference of large models easier and more efficient.