質問編集履歴

1

プログラムを全文記載しました。

2019/10/10 06:11

投稿

koukimaru22
koukimaru22

スコア6

test CHANGED
File without changes
test CHANGED
@@ -28,13 +28,97 @@
28
28
 
29
29
  ```python3
30
30
 
31
+ def target_category_loss(x, category_index, nb_classes):
32
+
33
+ return tf.multiply(x, K.one_hot([category_index], nb_classes))
34
+
35
+
36
+
37
+ def target_category_loss_output_shape(input_shape):
38
+
39
+ return input_shape
40
+
41
+
42
+
43
+ def normalize(x):
44
+
45
+ return x / (K.sqrt(K.mean(K.square(x))) + 1e-5)
46
+
47
+
48
+
49
+ def load_image(path):
50
+
51
+ img_path = path
52
+
53
+ img = image.load_img(img_path, target_size=(224, 224))
54
+
55
+ x = image.img_to_array(img)
56
+
57
+ x = np.expand_dims(x, axis=0)
58
+
59
+ x = preprocess_input(x)
60
+
61
+ return x
62
+
63
+
64
+
65
+
66
+
67
+ def register_gradient():
68
+
69
+ if "GuidedBackProp" not in ops._gradient_registry._registry:
70
+
71
+ @ops.RegisterGradient("GuidedBackProp")
72
+
73
+ def _GuidedBackProp(op, grad):
74
+
75
+ dtype = op.inputs[0].dtype
76
+
77
+ return grad * tf.cast(grad > 0., dtype) * \
78
+
79
+ tf.cast(op.inputs[0] > 0., dtype)
80
+
81
+
82
+
83
+
84
+
85
+ def compile_saliency_function(model, activation_layer='block5_conv3'):
86
+
87
+ input_img = model.input
88
+
89
+ layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]])
90
+
91
+ layer_output = layer_dict[activation_layer].output
92
+
93
+ max_output = K.max(layer_output, axis=3)
94
+
95
+ saliency = K.gradients(K.sum(max_output), input_img)[0]
96
+
97
+ return K.function([input_img, K.learning_phase()], [saliency])
98
+
99
+
100
+
101
+
102
+
103
+ def modify_backprop(model, name):
104
+
105
+ g = tf.get_default_graph()
106
+
107
+ with g.gradient_override_map({'Relu': name}):
108
+
109
+ new_model = VGG16(weights='imagenet')
110
+
111
+ return new_model
112
+
113
+
114
+
115
+
116
+
31
- def deprocess_image(x):
117
+ def deprocess_image(x):
32
118
 
33
119
  if np.ndim(x) > 3:
34
120
 
35
- x = np.squeeze(x)
121
+ x = np.squeeze(x)
36
-
37
- # normalize tensor: center on 0., ensure std is 0.1
38
122
 
39
123
  x -= x.mean()
40
124
 
@@ -44,16 +128,12 @@
44
128
 
45
129
 
46
130
 
47
- # clip to [0, 1]
48
-
49
131
  x += 0.5
50
132
 
51
133
  x = np.clip(x, 0, 1)
52
134
 
53
135
 
54
136
 
55
- # convert to RGB array
56
-
57
137
  x *= 255
58
138
 
59
139
  if K.image_dim_ordering() == 'tf':
@@ -66,6 +146,112 @@
66
146
 
67
147
 
68
148
 
149
+
150
+
151
+ def grad_cam(input_model, image, category_index, layer_name):
152
+
153
+ nb_classes = 1000
154
+
155
+ target_layer = lambda x: target_category_loss(x, category_index, nb_classes)
156
+
157
+
158
+
159
+ x = input_model.layers[-1].output
160
+
161
+ x = Lambda(target_layer, output_shape=target_category_loss_output_shape)(x)
162
+
163
+ model = keras.models.Model(input_model.layers[0].input, x)
164
+
165
+
166
+
167
+ loss = K.sum(model.layers[-1].output)
168
+
169
+ conv_output = [l for l in model.layers if l.name is layer_name][0].output
170
+
171
+
172
+
173
+ grads = normalize(K.gradients(loss, conv_output)[0])
174
+
175
+ gradient_function = K.function([model.layers[0].input], [conv_output, grads])
176
+
177
+
178
+
179
+ output, grads_val = gradient_function([image])
180
+
181
+ output, grads_val = output[0, :], grads_val[0, :, :, :]
182
+
183
+
184
+
185
+ weights = np.mean(grads_val, axis = (0, 1))
186
+
187
+ cam = np.ones(output.shape[0 : 2], dtype = np.float32)
188
+
189
+
190
+
191
+ for i, w in enumerate(weights):
192
+
193
+ cam += w * output[:, :, i]
194
+
195
+
196
+
197
+ cam = cv2.resize(cam, (224, 224))
198
+
199
+ cam = np.maximum(cam, 0)
200
+
201
+ heatmap = cam / np.max(cam)
202
+
203
+
204
+
205
+ image = image[0, :]
206
+
207
+ image -= np.min(image)
208
+
209
+ image = np.minimum(image, 255)
210
+
211
+
212
+
213
+ cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET)
214
+
215
+ cam = np.float32(cam) + np.float32(image)
216
+
217
+ cam = 255 * cam / np.max(cam)
218
+
219
+ return np.uint8(cam), heatmap
220
+
221
+
222
+
223
+ preprocessed_input = load_image("./dog_cat.jpg")
224
+
225
+
226
+
227
+ model = VGG16(weights='imagenet')
228
+
229
+
230
+
231
+ predictions = model.predict(preprocessed_input)
232
+
233
+ top_1 = decode_predictions(predictions)[0][0]
234
+
235
+ print('Predicted class:')
236
+
237
+ print('%s (%s) with probability %.2f' % (top_1[1], top_1[0], top_1[2]))
238
+
239
+
240
+
241
+ predicted_class = np.argmax(predictions)
242
+
243
+ print(predicted_class)
244
+
245
+
246
+
247
+ cam, heatmap = grad_cam(model, preprocessed_input, predicted_class, "block5_conv3")
248
+
249
+
250
+
251
+ cv2.imwrite("gradcam.jpg", cam)
252
+
253
+
254
+
69
255
  register_gradient()
70
256
 
71
257
  guided_model = modify_backprop(model, 'GuidedBackProp')
@@ -76,11 +262,7 @@
76
262
 
77
263
  gradcam = saliency[0] * heatmap[..., np.newaxis]
78
264
 
79
-
80
-
81
- cv2.imwrite("guied_gradcam.jpg", deprocess_image(gradcam))
265
+ cv2.imwrite("guided_gradcam.jpg", deprocess_image(gradcam))
82
-
83
-
84
266
 
85
267
 
86
268