Skip to content

pytorch-grad-cam

Python library that shows which parts of an image a computer vision model looked at to make its prediction.

Stars
13k
Forks
1.7k
Licence
MIT
Language
Python
Last update
5 days ago
Type
Library

What it is

This is a Python package of explainability methods for computer vision models built with PyTorch. It produces heatmaps (CAMs) that highlight the image regions that most influenced a model's output, such as a classification label or an object detection result. People use it to check whether a model is looking at sensible features, either while developing a model or when diagnosing predictions in production.

  • Generate a Grad-CAM heatmap over a ResNet50 classification prediction
  • Compare GradCAM, AblationCAM and ScoreCAM outputs on the same image
  • Visualize which regions drive a CLIP text prompt match
  • Explain similarity between image embeddings

Great for

  • Researchers comparing explainability methods for image models
  • Engineers debugging CNN or Vision Transformer classifiers in PyTorch
  • Teams working on object detection or semantic segmentation who need visual explanations
  • Students learning how Grad-CAM-style methods work

Not ideal for

  • People who need explanations for text or tabular models, since the README focuses on computer vision
  • Users not comfortable writing Python code, because it is used through a programming API rather than a graphical interface
  • Anyone needing a guarantee that an explanation is correct; the README includes metrics for checking trust but does not claim explanations are always faithful
Step-by-step guide: how to use pytorch-grad-camInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    You'll need: Python · PyTorch

    Install from PyPI

    pip install grad-cam
  2. 2

    Run it

    • Install the package with pip install grad-cam
    • Load a PyTorch model and choose a target layer
    • Import a method such as GradCAM and an image utility such as show_cam_on_image
    • Run the CAM method on a preprocessed image and overlay the heatmap
  3. 3

    Get the most out of it

    • Try several CAM methods on the same input to compare results, as the README's examples do
    • Use the metrics section of the README to check how trustworthy an explanation is
    • Use the batched implementation for faster processing of multiple images
    • Consult the linked documentation book for advanced tutorials

Downloads

Strengths

  • Covers many methods (GradCAM, HiResCAM, ScoreCAM, EigenCAM and others) in one package
  • Supports classification, object detection, segmentation and embedding similarity, per the README
  • Released under the permissive MIT license

Trade-offs

  • The README is truncated and does not show full API details or version requirements
  • Some methods are expensive to run (the README notes a batched implementation for AblationCAM, implying the others are slower)
  • No releases are published, so users depend on the latest repository state

How it compares

ProjectStarsTypeLevelUpdatedBest for
pytorch-grad-cam (this)13kLibraryintermediate5 days agoResearchers comparing explainability methods for image models
annotated_deep_learning_paper_implementations67.6kLearning resourceintermediate4 days agoStudents learning how transformers, GANs, or optimizers work internally
Made-With-ML49.9kLearning resourceintermediate8 months agoSoftware engineers who want to learn how ML models get deployed and maintained
pytorch-tutorial32.5kLearning resourceintermediate3.2 years agoResearchers or engineers who already know Python and want to learn PyTorch
shap25.8kLibraryintermediate4 days agoData scientists who need to explain tree-based models such as XGBoost, LightGBM, or CatBoost
pytorch_geometric24.1kLibraryintermediate15 days agoResearchers who want ready-made implementations of published graph neural network methods

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.