← all repositories
frgfm/torch-cam

X-ray specs for neural networks, no PhD required

A PyTorch wrapper that turns any CNN into an interpretable heatmap factory using hooks, not hacks.

2.3k stars Python Other AI
torch-cam
Velocity · 7d
+1.0
★ / day
Trend
steady
star history

What it does TorchCAM wraps your PyTorch model and uses forward/backward hooks to extract class activation maps (CAMs) automatically. You run inference normally; it intercepts the gradients and feature maps needed to show which pixels convinced your model that border collie was a wallaby. The library ships with nine CAM variants—from vanilla CAM through Layer-CAM—and utilities to overlay heatmaps or benchmark them against each other.

The interesting bit The wrapper-as-context-manager pattern (with LayerCAM(model) as cam_extractor:) means you don’t refactor your forward pass. The library also includes quantitative benchmarks (Average Drop, Increase in Confidence) on imagenette, so you can argue about interpretability with numbers instead of vibes.

Key highlights

  • Nine methods in one API: CAM, Grad-CAM, Grad-CAM++, Smooth Grad-CAM++, Score-CAM, SS-CAM, IS-CAM, XGrad-CAM, Layer-CAM
  • Automatic layer targeting (last non-reduced conv layer) with optional override
  • Built-in overlay and visualization utilities; Streamlit demo app on Hugging Face Spaces
  • Performance benchmark scripts included; tested on ResNet-18 and MobileNetV3
  • Python 3.11+, installable via pip or uv

Caveats

  • Requires Python 3.11 or higher, which may exclude older environments
  • Benchmark table in the README is incomplete (truncated mid-sentence during latency section)
  • Default layer selection is heuristic; you’ll need target_layer for non-standard architectures

Verdict Grab this if you’re debugging classification models and need to show stakeholders (or yourself) where the model is actually looking. Skip it if you’re working outside the conv-layer paradigm—transformer attention maps aren’t on the menu.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.