Skip to content

Commit 37d5aa3

Browse files
committed
feature activation method
1 parent 0d86e52 commit 37d5aa3

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ The aim is also to serve as a benchmark of algorithms and metrics for research o
4545
| LayerCAM | Spatially weight the activations by positive gradients. Works better especially in lower layers |
4646
| FullGrad | Computes the gradients of the biases from all over the network, and then sums them |
4747
| Deep Feature Factorizations | Non Negative Matrix Factorization on the 2D activations |
48-
| KPCA-CAM | Like EigenCAM but with Kernel PCA instead of PCA
48+
| KPCA-CAM | Like EigenCAM but with Kernel PCA instead of PCA |
49+
| FEM | A gradient free method that binarizes activations by an activation > mean + k * std rule. |
4950
## Visual Examples
5051

5152
| What makes the network think the image label is 'pug, pug-dog' | What makes the network think the image label is 'tabby, tabby cat' | Combining Grad-CAM with Guided Backpropagation for the 'pug, pug-dog' class |
@@ -357,3 +358,8 @@ Edo Collins, Radhakrishna Achanta, Sabine Süsstrunk`
357358
https://arxiv.org/abs/2410.00267 <br>
358359
`KPCA-CAM: Visual Explainability of Deep Computer Vision Models using Kernel PCA
359360
Sachin Karmani, Thanushon Sivakaran, Gaurav Prasad, Mehmet Ali, Wenbo Yang, Sheyang Tang`
361+
362+
https://hal.science/hal-02963298/document <br>
363+
`Features Understanding in 3D CNNs for Actions Recognition in Video
364+
Kazi Ahmed Asif Fuad, Pierre-Etienne Martin, Romain Giot, Romain
365+
Bourqui, Jenny Benois-Pineau, Akka Zemmar`

cam.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import torch
66
from torchvision import models
77
from pytorch_grad_cam import (
8-
GradCAM, HiResCAM, ScoreCAM, GradCAMPlusPlus,
8+
GradCAM, FEM, HiResCAM, ScoreCAM, GradCAMPlusPlus,
99
AblationCAM, XGradCAM, EigenCAM, EigenGradCAM,
1010
LayerCAM, FullGrad, GradCAMElementWise, KPCA_CAM
1111
)
@@ -34,7 +34,7 @@ def get_args():
3434
'of cam_weights*activations')
3535
parser.add_argument('--method', type=str, default='gradcam',
3636
choices=[
37-
'gradcam', 'hirescam', 'gradcam++',
37+
'gradcam', 'fem', 'hirescam', 'gradcam++',
3838
'scorecam', 'xgradcam', 'ablationcam',
3939
'eigencam', 'eigengradcam', 'layercam',
4040
'fullgrad', 'gradcamelementwise', 'kpcacam'
@@ -73,6 +73,7 @@ def get_args():
7373
"eigengradcam": EigenGradCAM,
7474
"layercam": LayerCAM,
7575
"fullgrad": FullGrad,
76+
"fem": FEM,
7677
"gradcamelementwise": GradCAMElementWise,
7778
'kpcacam': KPCA_CAM
7879
}

pytorch_grad_cam/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pytorch_grad_cam.grad_cam import GradCAM
2+
from pytorch_grad_cam.fem import FEM
23
from pytorch_grad_cam.hirescam import HiResCAM
34
from pytorch_grad_cam.grad_cam_elementwise import GradCAMElementWise
45
from pytorch_grad_cam.ablation_layer import AblationLayer, AblationLayerVit, AblationLayerFasterRCNN

pytorch_grad_cam/fem.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import numpy as np
2+
from pytorch_grad_cam.base_cam import BaseCAM
3+
4+
"""Feature Explanation Method.
5+
Fuad, K. A. A., Martin, P. E., Giot, R., Bourqui, R., Benois-Pineau, J., & Zemmari, A. (2020, November). Features understanding in 3D CNNS for actions recognition in video. In 2020 Tenth International Conference on Image Processing Theory, Tools and Applications (IPTA) (pp. 1-6). IEEE.
6+
https://hal.science/hal-02963298/document
7+
"""
8+
9+
class FEM(BaseCAM):
10+
def __init__(self, model, target_layers,
11+
reshape_transform=None, k=2):
12+
super(FEM, self).__init__(model,
13+
target_layers,
14+
reshape_transform,
15+
uses_gradients=False)
16+
self.k = k
17+
18+
def get_cam_image(self,
19+
input_tensor,
20+
target_layer,
21+
target_category,
22+
activations,
23+
grads,
24+
eigen_smooth):
25+
26+
27+
# 2D image
28+
if len(activations.shape) == 4:
29+
axis = (2, 3)
30+
# 3D image
31+
elif len(activations.shape) == 5:
32+
axis = (2, 3, 4)
33+
else:
34+
raise ValueError("Invalid activations shape."
35+
"Shape of activations should be 4 (2D image) or 5 (3D image).")
36+
means = np.mean(activations, axis=axis)
37+
stds = np.std(activations, axis=axis)
38+
# k sigma rule:
39+
# Add extra dimensions to match activations shape
40+
th = means + self.k * stds
41+
weights_shape = list(means.shape) + [1] * len(axis)
42+
th = th.reshape(weights_shape)
43+
binary_mask = activations > th
44+
weights = binary_mask.mean(axis=axis)
45+
return (weights.reshape(weights_shape) * activations).sum(axis=1)

0 commit comments

Comments
 (0)