forked from yiskw713/RISE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcam.py
157 lines (118 loc) · 4.57 KB
/
cam.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import torch
import torch.nn.functional as F
class SaveValues():
def __init__(self, m):
# register a hook to save values of activations and gradients
self.activations = None
self.gradients = None
self.forward_hook = m.register_forward_hook(self.hook_fn_act)
self.backward_hook = m.register_backward_hook(self.hook_fn_grad)
def hook_fn_act(self, module, input, output):
self.activations = output
def hook_fn_grad(self, module, grad_input, grad_output):
self.gradients = grad_output[0]
def remove(self):
self.forward_hook.remove()
self.backward_hook.remove()
class CAM(object):
""" Class Activation Mapping """
def __init__(self, model, target_layer):
"""
Args:
model: a base model to get CAM which have global pooling and fully connected layer.
target_layer: conv_layer before Global Average Pooling
"""
self.model = model
self.target_layer = target_layer
# save values of activations and gradients in target_layer
self.values = SaveValues(self.target_layer)
def forward(self, x, idx=None):
"""
Args:
x: input image. shape =>(1, 3, H, W)
Return:
heatmap: class activation mappings of the predicted class
"""
# object classification
score = self.model(x)
prob = F.softmax(score, dim=1)
if idx is None:
prob, idx = torch.max(prob, dim=1)
idx = idx.item()
prob = prob.item()
print("predicted class ids {}\t probability {}".format(idx, prob))
# cam can be calculated from the weights of linear layer and activations
weight_fc = list(
self.model._modules.get('fc').parameters())[0].to('cpu').data
cam = self.getCAM(self.values, weight_fc, idx)
return cam, idx
def __call__(self, x):
return self.forward(x)
def getCAM(self, values, weight_fc, idx):
'''
values: the activations and gradients of target_layer
activations: feature map before GAP. shape => (1, C, H, W)
weight_fc: the weight of fully connected layer. shape => (num_classes, C)
idx: predicted class id
cam: class activation map. shape => (1, num_classes, H, W)
'''
cam = F.conv2d(values.activations, weight=weight_fc[:, :, None, None])
_, _, h, w = cam.shape
# class activation mapping only for the predicted class
# cam is normalized with min-max.
cam = cam[:, idx, :, :]
cam -= torch.min(cam)
cam /= torch.max(cam)
cam = cam.view(1, 1, h, w)
return cam.data
class GradCAM(CAM):
""" Grad CAM """
def __init__(self, model, target_layer):
super().__init__(model, target_layer)
"""
Args:
model: a base model to get CAM, which need not have global pooling and fully connected layer.
target_layer: conv_layer you want to visualize
"""
def forward(self, x, idx=None):
"""
Args:
x: input image. shape =>(1, 3, H, W)
idx: ground truth index => (1, C)
Return:
heatmap: class activation mappings of the predicted class
"""
# anomaly detection
score = self.model(x)
prob = F.softmax(score, dim=1)
if idx is None:
prob, idx = torch.max(prob, dim=1)
idx = idx.item()
prob = prob.item()
print("predicted class ids {}\t probability {}".format(idx, prob))
# caluculate cam of the predicted class
cam = self.getGradCAM(self.values, score, idx)
return cam, idx
def __call__(self, x):
return self.forward(x)
def getGradCAM(self, values, score, idx):
'''
values: the activations and gradients of target_layer
activations: feature map before GAP. shape => (1, C, H, W)
score: the output of the model before softmax
idx: predicted class id
cam: class activation map. shape=> (1, 1, H, W)
'''
self.model.zero_grad()
score[0, idx].backward(retain_graph=True)
activations = values.activations
gradients = values.gradients
n, c, _, _ = gradients.shape
alpha = gradients.view(n, c, -1).mean(2)
alpha = alpha.view(n, c, 1, 1)
# shape => (1, 1, H', W')
cam = (alpha * activations).sum(dim=1, keepdim=True)
cam = F.relu(cam)
cam -= torch.min(cam)
cam /= torch.max(cam)
return cam.data