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 provides a simple way to visualize and analyze the class-specific activations of convolutional neural networks (CNNs) and Vision Transformers (ViTs) in PyTorch. It helps developers debug surprising classifier results by highlighting which parts of an image the model is focusing on to make a specific prediction.

How it works

The library uses PyTorch hooking mechanisms to retrieve internal activations without requiring the user to modify the model's architecture. It supports 12 different Class Activation Map (CAM) methods—including Grad-CAM, Layer-CAM, and LeGrad—to generate heatmaps that can be overlaid on the original input image. It also includes built-in faithfulness metrics (such as average drop and increase in confidence) to measure how accurately the CAM reflects the model's decision process.

Who it’s for

It is designed for PyTorch users working with computer vision models who need to interpret their model's predictions, perform error analysis, and verify the importance of visual cues.

Highlights

  • Extensive Method Library: Supports 12 CAM methods through a single, unified API.
  • Broad Architecture Support: Works with both CNNs and Vision Transformers (ViTs).
  • Faithfulness Metrics: Includes tools to quantitatively measure the reliability of the activation maps.
  • Agent-Ready: The explain() function creates evidence bundles that can be verified by humans or coding agents.
  • Lean Dependencies: Requires only PyTorch, NumPy, Pillow, and Matplotlib.

Related

  • Project
  • Project
  • Project
  • Project
  • Project