-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathhirescam.py
31 lines (27 loc) · 981 Bytes
/
hirescam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import numpy as np
from pytorch_grad_cam.base_cam import BaseCAM
from pytorch_grad_cam.utils.svd_on_activations import get_2d_projection
class HiResCAM(BaseCAM):
def __init__(self, model, target_layers,
reshape_transform=None):
super(
HiResCAM,
self).__init__(
model,
target_layers,
reshape_transform)
def get_cam_image(self,
input_tensor,
target_layer,
target_category,
activations,
grads,
eigen_smooth):
elementwise_activations = grads * activations
if eigen_smooth:
print(
"Warning: HiResCAM's faithfulness guarantees do not hold if smoothing is applied")
cam = get_2d_projection(elementwise_activations)
else:
cam = elementwise_activations.sum(axis=1)
return cam