frgfm / Torch Cam
Programming Languages
Projects that are alternatives of or similar to Torch Cam
Torchcam: class activation explorer
Simple way to leverage the class-specific activation of convolutional layers in PyTorch.
Table of Contents
Getting started
Prerequisites
- Python 3.6 (or more recent)
- pip
Installation
You can install the package using pypi as follows:
pip install torchcam
or using conda:
conda install -c frgfm torchcam
Usage
Torchcam was built both for users wishing to get a better understanding of their CNN models, and for researchers to enjoy a strong implementation base with popular methods. Here is a short snippet illustrating its usage:
import torch
from torchcam.cams import SmoothGradCAMpp
from torchvision.models import resnet18
img_tensor = torch.rand((1, 3, 224, 224))
model = resnet18(pretrained=True).eval()
# Hook your model before the forward pass
cam_extractor = SmoothGradCAMpp(model)
# By default the last conv layer will be selected
out = model(img_tensor)
# Retrieve the CAM
activation_map = cam_extractor(out.squeeze(0).argmax().item(), out)
Alternatively, you can use the example script like below to retrieve the CAM of a specific class on a resnet architecture.
python scripts/cam_example.py --model resnet18 --class-idx 232
Documentation
The full package documentation is available here for detailed specifications. The documentation was built with Sphinx using a theme provided by Read the Docs.
Contributing
Please refer to CONTRIBUTING
if you wish to contribute to this project.
Credits
This project is developed and maintained by the repo owner, but the implementation was based on the following precious papers:
- Learning Deep Features for Discriminative Localization: the original CAM paper
- Grad-CAM: GradCAM paper, generalizing CAM to models without global average pooling.
- Grad-CAM++: improvement of GradCAM++ for more accurate pixel-level contribution to the activation.
- Smooth Grad-CAM++: SmoothGrad mechanism coupled with GradCAM.
- Score-CAM: score-weighting of class activation for better interpretability.
- SS-CAM: SmoothGrad mechanism coupled with Score-CAM.
- IS-CAM: integration-based variant of Score-CAM.
- XGrad-CAM: improved version of Grad-CAM in terms of sensitivity and conservation.
License
Distributed under the MIT License. See LICENSE
for more information.