Skip to content

Commit

Permalink
EigenGradCAM
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobgil committed Apr 29, 2021
1 parent bd2fc06 commit 811b1d8
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Tested on Common CNN Networks and Vision Transformers!
| AblationCAM | Zero out activations and measure how the output drops (this repository includes a fast batched implementation) |
| ScoreCAM | Perbutate the image by the scaled activations and measure how the output drops |
| EigenCAM | Takes the first principle component of the 2D Activations (no class discrimination, but seems to give great results)|
| EigenGradCAM | Like EigenCAM but with class discrimination: First principle component of Activations*Grad --> Looks like GradCAM, but cleaner|


### What makes the network think the image label is 'pug, pug-dog' and 'tabby, tabby cat':
Expand Down
6 changes: 4 additions & 2 deletions cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
GradCAMPlusPlus, \
AblationCAM, \
XGradCAM, \
EigenCAM
EigenCAM, \
EigenGradCAM

from pytorch_grad_cam import GuidedBackpropReLUModel
from pytorch_grad_cam.utils.image import show_cam_on_image, \
Expand Down Expand Up @@ -49,7 +50,8 @@ def get_args():
"gradcam++": GradCAMPlusPlus,
"ablationcam": AblationCAM,
"xgradcam": XGradCAM,
"eigencam": EigenCAM}
"eigencam": EigenCAM,
"eigengradcam": EigenGradCAM}

if args.method not in list(methods.keys()):
raise Exception(f"method should be one of {list(methods.keys())}")
Expand Down
3 changes: 2 additions & 1 deletion pytorch_grad_cam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
from pytorch_grad_cam.grad_cam_plusplus import GradCAMPlusPlus
from pytorch_grad_cam.score_cam import ScoreCAM
from pytorch_grad_cam.eigen_cam import EigenCAM
from pytorch_grad_cam.guided_backprop import GuidedBackpropReLUModel
from pytorch_grad_cam.eigen_grad_cam import EigenGradCAM
from pytorch_grad_cam.guided_backprop import GuidedBackpropReLUModel
3 changes: 1 addition & 2 deletions pytorch_grad_cam/eigen_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def get_cam_image(self,
target_category,
activations,
grads):
reshaped_activations = activations.reshape(activations.shape[0], -1).transpose()

reshaped_activations = (activations).reshape(activations.shape[0], -1).transpose()
# Centering before the SVD seems to be important here,
# Otherwise the image returned is negative
reshaped_activations = reshaped_activations - reshaped_activations.mean(axis=0)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='grad-cam',
version='1.2.3',
version='1.2.4',
author='Jacob Gildenblat',
author_email='[email protected]',
description='Many Class Activation Map methods implemented in Pytorch. Including Grad-CAM, Grad-CAM++, Score-CAM, Ablation-CAM and XGrad-CAM',
Expand Down

0 comments on commit 811b1d8

Please sign in to comment.