質問編集履歴
1
コードを追記しました.
test
CHANGED
File without changes
|
test
CHANGED
@@ -29,3 +29,131 @@
|
|
29
29
|
何かこの問題を解決するためのアイデア等があるかた,よろしくお願いします.
|
30
30
|
|
31
31
|
追加情報が必要な方は,返信しますのでコメントをお願いします.
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
```python
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
#準備
|
42
|
+
|
43
|
+
model = load_model('cnn_model.h5')
|
44
|
+
|
45
|
+
print(model.summary())
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
data = img_to_array(load_img('./input/cat10007.jpg', target_size=(480,640)))
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
#backend動作
|
60
|
+
|
61
|
+
layer_name = 'max_pooling3d_4'
|
62
|
+
|
63
|
+
last_conv = model.get_layer(layer_name)
|
64
|
+
|
65
|
+
data_output = model.output[:,0]
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
grads = K.gradients(data_output, last_conv.output)[0]
|
76
|
+
|
77
|
+
pooled_grads = K.mean(grads, axis=(0,1,2)
|
78
|
+
|
79
|
+
iterate = K.function([model.input],
|
80
|
+
|
81
|
+
[pooled_grads, last_conv.output[0]])
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
#Main
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
pooled_grads_val,conv_output_val = iterate([data])
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
for i in range(pooled_grads_val.shape[0]):
|
96
|
+
|
97
|
+
conv_output_val[:,:,i] *= pooled_grads_val[i]
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
heatmap = np.mean(conv_output_val,axis=-1)
|
102
|
+
|
103
|
+
print(heatmap.shape) #(46,64)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
#ヒートマップの後処理
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
heatmap = np.maximum(heatmap,0)
|
112
|
+
|
113
|
+
heatmap /= np.max(heatmap)
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
#####################
|
122
|
+
|
123
|
+
heatmap = cv2.resize(heatmap,(640,480))
|
124
|
+
|
125
|
+
heatmap = np.uint8(255 * heatmap)
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
#動画にヒートマップを適用していく.
|
130
|
+
|
131
|
+
image0 = cv2.applyColorMap(heatmap,cv2.COLORMAP_JET)
|
132
|
+
|
133
|
+
plt.imshow(image0)
|
134
|
+
|
135
|
+
plt.show()
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
frame = '元のイメージ' #入力画像
|
140
|
+
|
141
|
+
superimposed_img = frame + 0.6*image0
|
142
|
+
|
143
|
+
#superimposed_img = cv2.addWeighted(frame,0.7, image0, 0.3, 0)
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
plt.imshow(superimposed_img)
|
152
|
+
|
153
|
+
plt.imshow(image0)
|
154
|
+
|
155
|
+
plt.show()
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
```
|