frgfm/torch-cam

Class activation maps for your PyTorch models (CAM, Grad-CAM, Grad-CAM++, Smooth Grad-CAM++, Score-CAM, SS-CAM, IS-CAM, XGrad-CAM, Layer-CAM, Finer-CAM, LeGrad, RefineCAM)

What it solves

TorchCAM helps you see why a machine learning model made a particular decision. For image classifiers, it produces heatmaps (class activation maps) that highlight the pixels that most influenced the prediction. For text classifiers, it measures how much each token (word) contributed to the score of a class. It also works for object detectors, segmentation models, and vision-language models (VLMs) that generate captions. This makes model behavior more transparent and helps debug surprising results.

How it works

TorchCAM wraps your existing PyTorch model. It uses PyTorch's hooking mechanism to automatically capture the intermediate activations and gradients needed to compute the activation map. You just run your model as usual, and then ask the extractor for the map for a specific class. It supports 13 different CAM methods (like Grad-CAM, LayerCAM, Score-CAM) plus token-based methods for text (TokenAblation and TokenIG). For text, it replaces tokens with a mask token to see how the class score changes, or uses integrated gradients for additive contributions. It also provides faithfulness metrics (average drop, increase in confidence) and can save an evidence bundle with a manifest for verification.

Who it’s for

Researchers, ML engineers, and data scientists who use PyTorch and want to interpret or debug model predictions. It's useful for anyone working with image classifiers, object detectors, segmentation, text classifiers, or vision-language models. The library is lightweight (only 4 runtime dependencies) and works with both CNNs and Vision Transformers. It also offers a Streamlit playground app for interactive exploration and an agent-ready workflow for coding agents.

Highlights

  • 13 CAM methods plus token-based methods for text (TokenAblation, TokenIG)
  • Works with CNNs, Vision Transformers, detectors, segmenters, and VLMs
  • Automatic target-layer resolution for CNNs and ViTs
  • Built-in faithfulness metrics (average drop, increase in confidence)
  • explain() saves a manifest-backed evidence bundle for verification
  • Agent-ready: installable skill for coding agents, llms.txt support
  • Lightweight: only 4 runtime dependencies (PyTorch, NumPy, Pillow, Matplotlib)
  • Fully typed and tested
  • Streamlit playground app with image classifiers, VLMs, and decision models
  • Example scripts and performance benchmarks

Related

  • Project
  • Project
  • Project
  • Project
  • Project