-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grad-CAM 1d(sloved).py
226 lines (192 loc) · 8.2 KB
/
Grad-CAM 1d(sloved).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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import scipy
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
class Net1(nn.Module):
def __init__(self):
super(Net1, self).__init__()
# Your model
def forward(self, x):
return p4
def target_category_loss(x, category_index, nb_classes):
return torch.mul(x, F.one_hot(category_index, nb_classes))
def target_category_loss_output_shape(input_shape):
return input_shape
def normalize(x):
# utility function to normalize a tensor by its L2 norm
return x / (torch.sqrt(torch.mean(torch.square(x))) + 1e-5)
def resize_1d(array, shape):
res = torch.zeros(shape)
if array.shape[0] >= shape:
ratio = array.shape[0]/shape
for i in range(array.shape[0]):
res[int(i/ratio)] += array[i]*(1-(i/ratio-int(i/ratio)))
if int(i/ratio) != shape-1:
res[int(i/ratio)+1] += array[i]*(i/ratio-int(i/ratio))
else:
res[int(i/ratio)] += array[i]*(i/ratio-int(i/ratio))
res = torch.flip(res, dims=[0])
array = torch.flip(array, dims=[0])
for i in range(array.shape[0]):
res[int(i/ratio)] += array[i]*(1-(i/ratio-int(i/ratio)))
if int(i/ratio) != shape-1:
res[int(i/ratio)+1] += array[i]*(i/ratio-int(i/ratio))
else:
res[int(i/ratio)] += array[i]*(i/ratio-int(i/ratio))
res = torch.flip(res, dims=[0])/(2*ratio)
array = torch.flip(array, dims=[0])
else:
ratio = shape/array.shape[0]
left = 0
right = 1
for i in range(shape):
if left < int(i/ratio):
left += 1
right += 1
if right > array.shape[0]-1:
res[i] += array[left]
else:
res[i] += array[right] * \
(i - left * ratio)/ratio+array[left]*(right*ratio-i)/ratio
res = torch.flip(res, dims=[0])
array = torch.flip(array, dims=[0])
left = 0
right = 1
for i in range(shape):
if left < int(i/ratio):
left += 1
right += 1
if right > array.shape[0]-1:
res[i] += array[left]
else:
res[i] += array[right] * \
(i - left * ratio)/ratio+array[left]*(right*ratio-i)/ratio
res = torch.flip(res, dims=[0])/2
array = torch.flip(array, dims=[0])
return res
class ActivationsAndGradients:
""" Class for extracting activations and
registering gradients from targetted intermediate layers """
def __init__(self, model, target_layer):
self.model = model
self.gradients = []
self.activations = []
self.handles = []
self.handles.append(
target_layer.register_forward_hook(self.save_activation)
)
self.handles.append(
target_layer.register_backward_hook(self.save_gradient)
)
def save_activation(self, module, input, output):
self.activations.append(output)
def save_gradient(self, module, grad_input, grad_output):
# Gradients are computed in reverse order
self.gradients = [grad_output[0].cpu().detach()] + self.gradients
def __call__(self, x):
self.gradients = []
self.activations = []
return self.model(x)
def release(self):
for handle in self.handles:
handle.remove()
class BaseCAM:
def __init__(self, model, target_layer, use_cuda=False):
"""Base class for Class activation mapping.
Args:
model (torch.nn.Module): model to inspect
target_layer (torch.nn.Module): target layer to inspect
use_cuda (bool, optional): If True, use GPU. Defaults to False.
"""
self.model = model.eval()
self.target_layer = target_layer
self.cuda = use_cuda
if self.cuda:
self.model = model.cuda()
self.activations_and_grads = ActivationsAndGradients(self.model, target_layer)
def forward(self, batched_input):
"""Forward pass of the model
Args:
batched_input (torch.Tensor): signal with shape (batch_size, channels, length)
Returns:
torch.Tensor: batched model output with shape (batch_size, num_classes)
"""
return self.model(batched_input)
def get_cam_weights(self,
input_tensor,
target_category,
activations,
grads):
raise NotImplementedError("This method should be implemented in subclasses")
def __call__(self, input_tensor, target_category=None):
"""Generates class activation map for a specific category
Args:
input_tensor (torch.Tensor): batched signal with shape (batch_size, channels, length)
target_category (torch.Tensor, optional): batched target with shape (batch_size, ). Defaults to None.
Returns:
np.ndarray: CAM for the specified category
"""
if self.cuda:
input_tensor = input_tensor.cuda()
outputs = self.activations_and_grads(input_tensor)
if target_category is None:
target_category = torch.argmax(outputs.cpu().data, dim=0)
targets = [ClassifierOutputTarget(category) for category in target_category]
self.model.zero_grad()
# loss = self.get_loss(output, targets)
# loss = sum([output[i, target_category[i]] for i in range(output.shape[0])])
loss = sum([target(output) for target, output in zip(targets, outputs)])
loss.backward(retain_graph=True)
activations = self.activations_and_grads.activations[-1].cpu().data
grads = self.activations_and_grads.gradients[-1].cpu().data
weights = self.get_cam_weights(input_tensor, target_category, activations, grads)
cam = torch.zeros(activations.shape[1:], dtype=torch.float64)
weights_reshaped = weights[:, None, None, :]
activations_reshaped = activations[:, None, :, :]
cam = torch.matmul(weights_reshaped, activations_reshaped)
cam = cam.squeeze()
cam = scipy.signal.resample(cam, input_tensor.shape[1], axis=1)
cam = np.maximum(cam, 0)
heatmap = (cam - np.min(cam)) / (np.max(cam) - np.min(cam) + 1e-10)
return heatmap
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_tb):
self.activations_and_grads.release()
if isinstance(exc_value, IndexError):
print(f"IndexError: {exc_value} in backward pass. Traceback: {exc_tb}")
return True
class GradCAM(BaseCAM):
def __init__(self, model, target_layer, use_cuda=False):
"""GradCAM class for visualizing Convolutional Neural Networks
Args:
model (torch.nn.Module): model to inspect
target_layer (torch.nn.Module): target layer to inspect
use_cuda (bool, optional): If True, use GPU. Defaults to False.
"""
super(GradCAM, self).__init__(model, target_layer, use_cuda)
def get_cam_weights(self, input_tensor,
target_category,
activations, grads):
if len(grads.shape) == 2:
return torch.mean(grads, axis=1)
else:
return torch.mean(grads, axis=2)
def main():
model = Net1()
# model.load_state_dict(torch.load('./data7/parameternn.pt'))
target_layer = model.p2_6
net = GradCAM(model, target_layer)
from settest import Test
input_tensor = Test.Data[100:101, :]
input_tensor = torch.tensor(input_tensor, dtype=torch.float64)
#plt.figure(figsize=(5, 1))
output = net(input_tensor)
import scipy.io as scio
input_tensor = input_tensor.numpy().squeeze()
dataNew = "G:\\datanew.mat"
scio.savemat(dataNew, mdict={'cam': output, 'data': input_tensor})
if __name__ == '__main__':
main()