-
Notifications
You must be signed in to change notification settings - Fork 14
/
cam.py
325 lines (244 loc) · 9.47 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import torch
import torch.nn.functional as F
from statistics import mode, mean
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
class GradCAMpp(CAM):
""" Grad CAM plus plus """
def __init__(self, model, target_layer):
super().__init__(model, target_layer)
"""
Args:
model: a base model
target_layer: conv_layer you want to visualize
"""
def forward(self, x, idx=None):
"""
Args:
x: input image. shape =>(1, 3, H, W)
Return:
heatmap: class activation mappings of predicted classes
"""
# 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))
# caluculate cam of the predicted class
cam = self.getGradCAMpp(self.values, score, idx)
return cam, idx
def __call__(self, x):
return self.forward(x)
def getGradCAMpp(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. shape => (1, n_classes)
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
# calculate alpha
numerator = gradients.pow(2)
denominator = 2 * gradients.pow(2)
ag = activations * gradients.pow(3)
denominator += ag.view(n, c, -1).sum(-1, keepdim=True).view(n, c, 1, 1)
denominator = torch.where(
denominator != 0.0, denominator, torch.ones_like(denominator))
alpha = numerator / (denominator + 1e-7)
relu_grad = F.relu(score[0, idx].exp() * gradients)
weights = (alpha * relu_grad).view(n, c, -1).sum(-1).view(n, c, 1, 1)
# shape => (1, 1, H', W')
cam = (weights * activations).sum(1, keepdim=True)
cam = F.relu(cam)
cam -= torch.min(cam)
cam /= torch.max(cam)
return cam.data
class SmoothGradCAMpp(CAM):
""" Smooth Grad CAM plus plus """
def __init__(self, model, target_layer, n_samples=25, stdev_spread=0.15):
super().__init__(model, target_layer)
"""
Args:
model: a base model
target_layer: conv_layer you want to visualize
n_sample: the number of samples
stdev_spread: standard deviationß
"""
self.n_samples = n_samples
self.stdev_spread = stdev_spread
def forward(self, x, idx=None):
"""
Args:
x: input image. shape =>(1, 3, H, W)
Return:
heatmap: class activation mappings of predicted classes
"""
stdev = self.stdev_spread / (x.max() - x.min())
std_tensor = torch.ones_like(x) * stdev
indices = []
probs = []
for i in range(self.n_samples):
self.model.zero_grad()
x_with_noise = torch.normal(mean=x, std=std_tensor)
x_with_noise.requires_grad_()
score = self.model(x_with_noise)
prob = F.softmax(score, dim=1)
if idx is None:
prob, idx = torch.max(prob, dim=1)
idx = idx.item()
probs.append(prob.item())
indices.append(idx)
score[0, idx].backward(retain_graph=True)
activations = self.values.activations
gradients = self.values.gradients
n, c, _, _ = gradients.shape
# calculate alpha
numerator = gradients.pow(2)
denominator = 2 * gradients.pow(2)
ag = activations * gradients.pow(3)
denominator += \
ag.view(n, c, -1).sum(-1, keepdim=True).view(n, c, 1, 1)
denominator = torch.where(
denominator != 0.0, denominator, torch.ones_like(denominator))
alpha = numerator / (denominator + 1e-7)
relu_grad = F.relu(score[0, idx].exp() * gradients)
weights = \
(alpha * relu_grad).view(n, c, -1).sum(-1).view(n, c, 1, 1)
# shape => (1, 1, H', W')
cam = (weights * activations).sum(1, keepdim=True)
cam = F.relu(cam)
cam -= torch.min(cam)
cam /= torch.max(cam)
if i == 0:
total_cams = cam.clone()
else:
total_cams += cam
total_cams /= self.n_samples
idx = mode(indices)
prob = mean(probs)
print("predicted class ids {}\t probability {}".format(idx, prob))
return total_cams.data, idx
def __call__(self, x):
return self.forward(x)