-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrad-CAM.py
141 lines (124 loc) · 5.08 KB
/
Grad-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
def resize_1d(array, shape):
res = np.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 = res[::-1]
array = array[::-1]
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 = res[::-1]/(2*ratio)
array = array[::-1]
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 = res[::-1]
array = array[::-1]
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 = res[::-1]/2
array = array[::-1]
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 = []
target_layer.register_forward_hook(self.save_activation)
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]] + self.gradients
def __call__(self, x):
self.gradients = []
self.activations = []
return self.model(x)
class BaseCAM:
def __init__(self, model, target_layer, use_cuda=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, input_img):
return self.model(input_img)
def get_cam_weights(self,
input_tensor,
target_category,
activations,
grads):
raise Exception("Not Implemented")
def get_loss(self, output, target_category):
return output[target_category]
def __call__(self, input_tensor, target_category=None):
if self.cuda:
input_tensor = input_tensor.cuda()
output = self.activations_and_grads(input_tensor)
if target_category is None:
target_category = np.argmax(output.cpu().data.numpy())
self.model.zero_grad()
loss = self.get_loss(output, target_category)
loss.backward(retain_graph=True)
activations = self.activations_and_grads.activations[-1].cpu().data.numpy()[0, :]
grads = self.activations_and_grads.gradients[-1].cpu().data.numpy()[0, :]
#weights = np.mean(grads, axis=(0))
weights = self.get_cam_weights(input_tensor, target_category, activations, grads)
cam = np.zeros(activations.shape[1:], dtype=np.float32)
for i, w in enumerate(weights):
cam += w * activations[i, :]
# cam = activations.dot(weights)
# cam = activations.dot(weights)
# print(input_tensor.shape[1])
cam = resize_1d(cam, (input_tensor.shape[2]))
cam = np.maximum(cam, 0)
heatmap = (cam - np.min(cam)) / (np.max(cam) - np.min(cam) + 1e-10)#归一化处理
return heatmap
class GradCAM(BaseCAM):
def __init__(self, model, target_layer, use_cuda=False):
super(GradCAM, self).__init__(model, target_layer, use_cuda)
def get_cam_weights(self, input_tensor,
target_category,
activations, grads):
return np.mean(grads, axis=1)
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.float32)
#plt.figure(figsize=(5, 1))
output = net(input_tensor)