質問編集履歴
1
内容の加筆
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,80 +6,418 @@
|
|
6
6
|
|
7
7
|
下記コードの”heatmap”という関数をグラフ化し、x軸、z軸を1から224、y軸を計算される値にしたいです。
|
8
8
|
|
9
|
-
|
9
|
+
[参考にしているコードの説明](https://qiita.com/MuAuan/items/cbd739808c64501a1024)
|
10
|
+
|
10
|
-
|
11
|
+
[コードDLリンク](https://github.com/MuAuan/cheating_DL)
|
12
|
+
|
11
|
-
|
13
|
+
grad-cam_5category.pyというプログラムです。
|
14
|
+
|
12
|
-
|
15
|
+
3Dマップで出したいのは下記のコードのheatmap部分です
|
16
|
+
|
13
|
-
|
17
|
+
一番下に全体のコードを載せておきます。
|
18
|
+
|
14
|
-
|
19
|
+
printした際、普通にheatmapを出力した時は1列224行でしか出なかったのでfor文で224回繰り返すことで224列分出力しています。
|
20
|
+
|
21
|
+
この224行224列の行列式を3Dプロットしたいです。
|
22
|
+
|
23
|
+
![1列分の出力結果](0bc4b0140c1ecb7057b249da11b3fb62.png)
|
24
|
+
|
15
|
-
###
|
25
|
+
###該当コード
|
16
|
-
|
17
|
-
|
18
26
|
|
19
27
|
```python
|
20
28
|
|
21
|
-
fo
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
t
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
p
|
30
|
-
|
31
|
-
#
|
32
|
-
|
33
|
-
|
29
|
+
def grad_cam(input_model, image, category_index, layer_name):
|
30
|
+
|
31
|
+
nb_classes = 1000
|
32
|
+
|
33
|
+
target_layer = lambda x: target_category_loss(x, category_index, nb_classes)
|
34
|
+
|
35
|
+
x = Lambda(target_layer, output_shape = target_category_loss_output_shape)(input_model.output)
|
36
|
+
|
37
|
+
model = Model(inputs=input_model.input, outputs=x)
|
38
|
+
|
39
|
+
#model.summary()
|
40
|
+
|
41
|
+
loss = K.sum(model.output)
|
42
|
+
|
43
|
+
conv_output = [l for l in model.layers if l.name == layer_name][0].output #is
|
44
|
+
|
45
|
+
grads = normalize(_compute_gradients(loss, [conv_output])[0])
|
46
|
+
|
47
|
+
gradient_function = K.function([model.input], [conv_output, grads])
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
output, grads_val = gradient_function([image])
|
52
|
+
|
53
|
+
output, grads_val = output[0, :], grads_val[0, :, :, :]
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
weights = np.mean(grads_val, axis = (0, 1))
|
58
|
+
|
59
|
+
cam = np.ones(output.shape[0 : 2], dtype = np.float32)
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
for i, w in enumerate(weights):
|
64
|
+
|
65
|
+
cam += w * output[:, :, i]
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
cam = cv2.resize(cam, (224,224)) #299,299)) #224, 224))
|
70
|
+
|
71
|
+
cam = np.maximum(cam, 0)
|
72
|
+
|
73
|
+
heatmap = cam / np.max(cam)
|
74
|
+
|
75
|
+
for x in range(224):
|
76
|
+
|
77
|
+
print (heatmap[x]) #3マップで出力したい部分
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
#Return to BGR [0..255] from the preprocessed image
|
82
|
+
|
83
|
+
image = image[0, :]
|
84
|
+
|
85
|
+
image -= np.min(image)
|
86
|
+
|
87
|
+
image = np.minimum(image, 255)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET)
|
92
|
+
|
93
|
+
cam = np.float32(cam) + np.float32(image)
|
94
|
+
|
95
|
+
cam = 255 * cam / np.max(cam)
|
96
|
+
|
97
|
+
return np.uint8(cam), heatmap
|
98
|
+
|
99
|
+
```
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
###全体のコード
|
106
|
+
|
107
|
+
```python
|
108
|
+
|
109
|
+
from keras.applications.vgg16 import (VGG16, preprocess_input, decode_predictions)
|
110
|
+
|
111
|
+
from keras.models import Model
|
112
|
+
|
113
|
+
from keras.preprocessing import image
|
114
|
+
|
115
|
+
from keras.layers.core import Lambda
|
116
|
+
|
117
|
+
from keras.models import Sequential
|
118
|
+
|
119
|
+
from tensorflow.python.framework import ops
|
120
|
+
|
121
|
+
import keras.backend as K
|
122
|
+
|
123
|
+
import tensorflow as tf
|
124
|
+
|
125
|
+
import numpy as np
|
126
|
+
|
127
|
+
import keras
|
128
|
+
|
129
|
+
import sys
|
130
|
+
|
131
|
+
import cv2
|
132
|
+
|
133
|
+
#from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
|
134
|
+
|
135
|
+
#from keras.applications.vgg19 import VGG19, preprocess_input, decode_predictions
|
136
|
+
|
137
|
+
#from keras.applications.inception_v3 import InceptionV3, preprocess_input, decode_predictions
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
def target_category_loss(x, category_index, nb_classes):
|
142
|
+
|
143
|
+
return tf.multiply(x, K.one_hot([category_index], nb_classes))
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
def target_category_loss_output_shape(input_shape):
|
148
|
+
|
149
|
+
return input_shape
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
def normalize(x):
|
154
|
+
|
155
|
+
# utility function to normalize a tensor by its L2 norm
|
156
|
+
|
157
|
+
return x / (K.sqrt(K.mean(K.square(x))) + 1e-5)
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
def load_image(path):
|
162
|
+
|
163
|
+
img_path = sys.argv[1]
|
164
|
+
|
165
|
+
img = image.load_img(img_path, target_size=(224,224)) #299,299)) #224, 224))
|
166
|
+
|
167
|
+
x = image.img_to_array(img)
|
168
|
+
|
169
|
+
x = np.expand_dims(x, axis=0)
|
170
|
+
|
171
|
+
x = preprocess_input(x)
|
172
|
+
|
173
|
+
return x
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
def register_gradient():
|
178
|
+
|
179
|
+
if "GuidedBackProp" not in ops._gradient_registry._registry:
|
180
|
+
|
181
|
+
@ops.RegisterGradient("GuidedBackProp")
|
182
|
+
|
183
|
+
def _GuidedBackProp(op, grad):
|
184
|
+
|
185
|
+
dtype = op.inputs[0].dtype
|
186
|
+
|
187
|
+
return grad * tf.cast(grad > 0., dtype) * \
|
188
|
+
|
189
|
+
tf.cast(op.inputs[0] > 0., dtype)
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
def compile_saliency_function(model, activation_layer='block5_conv3'): #mixed10 'activation_49' add_16 add_32 activation_98
|
194
|
+
|
195
|
+
input_img = model.input
|
196
|
+
|
197
|
+
layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]])
|
198
|
+
|
199
|
+
#print(layer_dict)
|
200
|
+
|
201
|
+
layer_output = layer_dict[activation_layer].output
|
202
|
+
|
203
|
+
max_output = K.max(layer_output, axis=3)
|
204
|
+
|
205
|
+
saliency = K.gradients(K.sum(max_output), input_img)[0]
|
206
|
+
|
207
|
+
return K.function([input_img, K.learning_phase()], [saliency])
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
def modify_backprop(model, name):
|
212
|
+
|
213
|
+
g = tf.get_default_graph()
|
214
|
+
|
215
|
+
with g.gradient_override_map({'Relu': name}):
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
# get layers that have an activation
|
220
|
+
|
221
|
+
layer_dict = [layer for layer in model.layers[1:]
|
222
|
+
|
223
|
+
if hasattr(layer, 'activation')]
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
# replace relu activation
|
228
|
+
|
229
|
+
for layer in layer_dict:
|
230
|
+
|
231
|
+
if layer.activation == keras.activations.relu:
|
232
|
+
|
233
|
+
layer.activation = tf.nn.relu
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
# re-instanciate a new model
|
238
|
+
|
239
|
+
new_model = VGG16(weights='imagenet')
|
240
|
+
|
241
|
+
#new_model = ResNet50(weights='imagenet')
|
242
|
+
|
243
|
+
new_model.summary()
|
244
|
+
|
245
|
+
return new_model
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
def deprocess_image(x):
|
250
|
+
|
251
|
+
'''
|
252
|
+
|
253
|
+
Same normalization as in:
|
254
|
+
|
255
|
+
https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py
|
256
|
+
|
257
|
+
'''
|
258
|
+
|
259
|
+
if np.ndim(x) > 3:
|
260
|
+
|
261
|
+
x = np.squeeze(x)
|
262
|
+
|
263
|
+
# normalize tensor: center on 0., ensure std is 0.1
|
264
|
+
|
265
|
+
x -= x.mean()
|
266
|
+
|
267
|
+
x /= (x.std() + 1e-5)
|
268
|
+
|
269
|
+
x *= 0.1
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
# clip to [0, 1]
|
274
|
+
|
275
|
+
x += 0.5
|
276
|
+
|
277
|
+
x = np.clip(x, 0, 1)
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
# convert to RGB array
|
282
|
+
|
283
|
+
x *= 255
|
284
|
+
|
285
|
+
if K.image_dim_ordering() == 'th':
|
286
|
+
|
287
|
+
x = x.transpose((1, 2, 0))
|
288
|
+
|
289
|
+
x = np.clip(x, 0, 255).astype('uint8')
|
290
|
+
|
291
|
+
return x
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
def _compute_gradients(tensor, var_list):
|
296
|
+
|
297
|
+
grads = tf.gradients(tensor, var_list)
|
298
|
+
|
299
|
+
return [grad if grad is not None else tf.zeros_like(var) for var, grad in zip(var_list, grads)]
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
def grad_cam(input_model, image, category_index, layer_name):
|
304
|
+
|
305
|
+
nb_classes = 1000
|
306
|
+
|
307
|
+
target_layer = lambda x: target_category_loss(x, category_index, nb_classes)
|
308
|
+
|
309
|
+
x = Lambda(target_layer, output_shape = target_category_loss_output_shape)(input_model.output)
|
310
|
+
|
311
|
+
model = Model(inputs=input_model.input, outputs=x)
|
312
|
+
|
313
|
+
#model.summary()
|
314
|
+
|
315
|
+
loss = K.sum(model.output)
|
316
|
+
|
317
|
+
conv_output = [l for l in model.layers if l.name == layer_name][0].output #is
|
318
|
+
|
319
|
+
grads = normalize(_compute_gradients(loss, [conv_output])[0])
|
320
|
+
|
321
|
+
gradient_function = K.function([model.input], [conv_output, grads])
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
output, grads_val = gradient_function([image])
|
326
|
+
|
327
|
+
output, grads_val = output[0, :], grads_val[0, :, :, :]
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
weights = np.mean(grads_val, axis = (0, 1))
|
332
|
+
|
333
|
+
cam = np.ones(output.shape[0 : 2], dtype = np.float32)
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
for i, w in enumerate(weights):
|
338
|
+
|
339
|
+
cam += w * output[:, :, i]
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
cam = cv2.resize(cam, (224,224)) #299,299)) #224, 224))
|
344
|
+
|
345
|
+
cam = np.maximum(cam, 0)
|
346
|
+
|
347
|
+
heatmap = cam / np.max(cam)
|
348
|
+
|
349
|
+
for x in range(224):
|
350
|
+
|
351
|
+
print (heatmap[x])
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
#Return to BGR [0..255] from the preprocessed image
|
356
|
+
|
357
|
+
image = image[0, :]
|
358
|
+
|
359
|
+
image -= np.min(image)
|
360
|
+
|
361
|
+
image = np.minimum(image, 255)
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET)
|
366
|
+
|
367
|
+
cam = np.float32(cam) + np.float32(image)
|
368
|
+
|
369
|
+
cam = 255 * cam / np.max(cam)
|
370
|
+
|
371
|
+
return np.uint8(cam), heatmap
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
preprocessed_input = load_image(sys.argv[1])
|
376
|
+
|
377
|
+
model = VGG16(weights='imagenet')
|
378
|
+
|
379
|
+
#model = VGG19(weights='imagenet')
|
380
|
+
|
381
|
+
#model = InceptionV3(weights='imagenet')
|
382
|
+
|
383
|
+
#model = ResNet50(weights = 'imagenet')
|
384
|
+
|
385
|
+
#model.summary()
|
386
|
+
|
387
|
+
target_layer = 'block5_conv3' #'activation_49' add_16 "block5_conv3"
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
predictions = model.predict(preprocessed_input)
|
392
|
+
|
393
|
+
register_gradient()
|
394
|
+
|
395
|
+
guided_model = modify_backprop(model, 'GuidedBackProp')
|
396
|
+
|
397
|
+
guided_model.summary()
|
398
|
+
|
399
|
+
for i in range(5):
|
400
|
+
|
401
|
+
top_1 = decode_predictions(predictions)[0][i]
|
402
|
+
|
403
|
+
print(predictions.argsort()[0][::-1][i])
|
404
|
+
|
405
|
+
print('Predicted class:')
|
406
|
+
|
407
|
+
print('%s (%s) with probability %.2f' % (top_1[1], top_1[0], top_1[2]))
|
408
|
+
|
409
|
+
predicted_class = predictions.argsort()[0][::-1][i] #np.argmax(predictions)
|
34
410
|
|
35
411
|
cam, heatmap = grad_cam(model, preprocessed_input, predicted_class, target_layer)
|
36
412
|
|
37
|
-
#print(cam[0])
|
38
|
-
|
39
|
-
for x in range(224):
|
40
|
-
|
41
|
-
print (heatmap[x])
|
42
|
-
|
43
|
-
#print("---------------------")
|
44
|
-
|
45
|
-
|
413
|
+
cv2.imwrite("gradcam"+str(top_1[1])+".jpg", cam)
|
414
|
+
|
46
|
-
|
415
|
+
saliency_fn = compile_saliency_function(guided_model)
|
47
|
-
|
48
|
-
|
416
|
+
|
49
|
-
i
|
417
|
+
saliency = saliency_fn([preprocessed_input, 0])
|
50
|
-
|
51
|
-
|
418
|
+
|
52
|
-
|
53
|
-
# 3D散布図でプロットするデータを生成する為にnumpyを使用
|
54
|
-
|
55
|
-
|
419
|
+
gradcam = saliency[0] * heatmap[..., np.newaxis]
|
56
|
-
|
57
|
-
|
420
|
+
|
58
|
-
|
59
|
-
Z = np.sin(Y) # 特に意味のない正弦
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
# グラフの枠を作成
|
64
|
-
|
65
|
-
fig = plt.figure()
|
66
|
-
|
67
|
-
ax = Axes3D(fig)
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
# X,Y,Z軸にラベルを設定
|
72
|
-
|
73
|
-
ax.set_xlabel("X")
|
74
|
-
|
75
|
-
ax.set_ylabel("Y")
|
76
|
-
|
77
|
-
ax.set_zlabel("Z")
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
# .plotで描画
|
82
|
-
|
83
|
-
|
421
|
+
cv2.imwrite("guided_gradcam"+str(top_1[1])+".jpg", deprocess_image(gradcam))
|
84
422
|
|
85
423
|
```
|