-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAM.py
315 lines (251 loc) · 10.6 KB
/
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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
## CAM.py
## A script to carry out class activation mapping on some images of each class
## Written by Daniel Buscombe,
## Northern Arizona University
## daniel.buscombe.nau.edu
import numpy as np
import ast
import scipy
import matplotlib.pyplot as plt
from imageio import imread, imwrite
from tensorflow.python.keras.applications.resnet50 import ResNet50, preprocess_input
from tensorflow.python.keras.preprocessing import image
from tensorflow.python.keras.models import Model
import sys, glob, os
import pandas as pd
def pretrained_path_to_tensor(img_path):
# loads RGB image as PIL.Image.Image type
img = image.load_img(img_path, target_size=(224, 224))
# convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)
x = image.img_to_array(img)
# convert 3D tensor to 4D tensor with shape (1, 224, 224, 3) and return 4D tensor
x = np.expand_dims(x, axis=0)
# convert RGB -> BGR, subtract mean ImageNet pixel, and return 4D tensor
return preprocess_input(x)
def get_model():
# define ResNet50 model
model = ResNet50(weights='imagenet')
# get AMP layer weights
amp_weights = model.layers[-1].get_weights()[0]
# extract wanted output
model = Model(inputs=model.input,
outputs=(model.layers[-4].output, model.layers[-1].output))
return model, amp_weights
def get_CAM(img_path, model, amp_weights):
# get filtered images from convolutional output + model prediction vector
last_conv_output, pred_vec = model.predict(pretrained_path_to_tensor(img_path))
# change dimensions of last convolutional outpu tto 7 x 7 x 2048
last_conv_output = np.squeeze(last_conv_output)
# get model's prediction (number between 0 and 999, inclusive)
pred = np.argmax(pred_vec)
# bilinear upsampling to resize each filtered image to size of original image
mat_for_mult = scipy.ndimage.zoom(last_conv_output, (32, 32, 1), order=1) # dim: 224 x 224 x 2048
# get AMP layer weights
amp_layer_weights = amp_weights[:, pred] # dim: (2048,)
# get class activation map for object class that is predicted to be in the image
final_output = np.dot(mat_for_mult.reshape((224*224, 2048)), amp_layer_weights).reshape(224,224) # dim: 224 x 224
return final_output, pred
#==============================================================
if __name__ == '__main__':
model, amp_weights = get_model()
print ("[INFO] computing activation maps ...")
use = 5
images = glob.glob(os.getcwd()+os.sep+'test\spill\*.jpg')[:use]
C1 = []; I1 = []
for img_path in images:
CAM, pred = get_CAM(img_path, model, amp_weights)
C1.append(CAM)
I1.append(imread(img_path))
images = glob.glob(os.getcwd()+os.sep+'test\plunge\*.jpg')[:use]
C2 = []; I2 = []
for img_path in images:
CAM, pred = get_CAM(img_path, model, amp_weights)
C2.append(CAM)
I2.append(imread(img_path))
images = glob.glob(os.getcwd()+os.sep+'test\nonbreaking\*.jpg')[:use]
C3 = []; I3 = []
for img_path in images:
CAM, pred = get_CAM(img_path, model, amp_weights)
C3.append(CAM)
I3.append(imread(img_path))
levels = [0,.001,.1,.2,.3,.4,.5,.6,.7,.8,.9,1]
origin='upper'
ax=plt.subplot(251)
ax.set_aspect('equal')
plt.imshow(I1[0], cmap='gray', origin=origin)
plt.axis('off')
plt.title('A) Spilling 1', fontsize=6, loc='left')
ax=plt.subplot(252)
ax.set_aspect('equal')
plt.imshow(I1[1], cmap='gray', origin=origin)
plt.axis('off')
plt.title('B) Spilling 2', fontsize=6, loc='left')
ax=plt.subplot(253)
ax.set_aspect('equal')
plt.imshow(I1[2], cmap='gray', origin=origin)
plt.axis('off')
plt.title('C) Spilling 3', fontsize=6, loc='left')
ax=plt.subplot(254)
ax.set_aspect('equal')
plt.imshow(I1[3], cmap='gray', origin=origin)
plt.axis('off')
plt.title('D) Spilling 4', fontsize=6, loc='left')
ax=plt.subplot(255)
ax.set_aspect('equal')
plt.imshow(I1[4], cmap='gray', origin=origin)
plt.axis('off')
plt.title('E) Spilling 5', fontsize=6, loc='left')
ax=plt.subplot(256)
ax.set_aspect('equal')
plt.contourf(C1[0]/np.max(C1[0]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C1[0]/np.max(C1[0]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(257)
ax.set_aspect('equal')
plt.contourf(C1[1]/np.max(C1[1]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C1[1]/np.max(C1[1]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(258)
ax.set_aspect('equal')
plt.contourf(C1[2]/np.max(C1[2]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C1[2]/np.max(C1[2]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(259)
ax.set_aspect('equal')
plt.contourf(C1[3]/np.max(C1[3]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C1[3]/np.max(C1[3]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(2,5,10)
ax.set_aspect('equal')
plt.contourf(C1[4]/np.max(C1[4]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C1[4]/np.max(C1[4]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
#plt.tight_layout()
plt.savefig('av_actmap_spill.png', bbox_inches='tight', dpi=300)
plt.close()
print ("[INFO] spilling waves activation maps plotted ...")
plt.close('all')
ax=plt.subplot(251)
ax.set_aspect('equal')
plt.imshow(I2[0], cmap='gray', origin=origin)
plt.axis('off')
plt.title('F) Plunging 1', fontsize=6, loc='left')
ax=plt.subplot(252)
ax.set_aspect('equal')
plt.imshow(I2[1], cmap='gray', origin=origin)
plt.axis('off')
plt.title('G) Plunging 2', fontsize=6, loc='left')
ax=plt.subplot(253)
ax.set_aspect('equal')
plt.imshow(I2[2], cmap='gray', origin=origin)
plt.axis('off')
plt.title('H) Plunging 3', fontsize=6, loc='left')
ax=plt.subplot(254)
ax.set_aspect('equal')
plt.imshow(I2[3], cmap='gray', origin=origin)
plt.axis('off')
plt.title('I) Plunging 4', fontsize=6, loc='left')
ax=plt.subplot(255)
ax.set_aspect('equal')
plt.imshow(I2[4], cmap='gray', origin=origin)
plt.axis('off')
plt.title('J) Plunging 5', fontsize=6, loc='left')
ax=plt.subplot(256)
ax.set_aspect('equal')
plt.contourf(C2[0]/np.max(C2[0]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C2[0]/np.max(C2[0]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(257)
ax.set_aspect('equal')
plt.contourf(C2[1]/np.max(C2[1]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C2[1]/np.max(C2[1]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(258)
ax.set_aspect('equal')
plt.contourf(C2[2]/np.max(C2[2]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C2[2]/np.max(C2[2]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(259)
ax.set_aspect('equal')
plt.contourf(C2[3]/np.max(C2[3]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C2[3]/np.max(C2[3]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(2,5,10)
ax.set_aspect('equal')
plt.contourf(C2[4]/np.max(C2[4]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C2[4]/np.max(C2[4]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
#plt.tight_layout()
plt.savefig('av_actmap_plunge.png', bbox_inches='tight', dpi=300)
plt.close()
print ("[INFO] plunging waves activation maps plotted ...")
plt.close('all')
ax=plt.subplot(251)
ax.set_aspect('equal')
plt.imshow(I3[0], cmap='gray', origin=origin)
plt.axis('off')
plt.title('K) Unbroken 1', fontsize=6, loc='left')
ax=plt.subplot(252)
ax.set_aspect('equal')
plt.imshow(I3[1], cmap='gray', origin=origin)
plt.axis('off')
plt.title('L) Unbroken 2', fontsize=6, loc='left')
ax=plt.subplot(253)
ax.set_aspect('equal')
plt.imshow(I3[2], cmap='gray', origin=origin)
plt.axis('off')
plt.title('M) Unbroken 3', fontsize=6, loc='left')
ax=plt.subplot(254)
ax.set_aspect('equal')
plt.imshow(I3[3], cmap='gray', origin=origin)
plt.axis('off')
plt.title('N) Unbroken 4', fontsize=6, loc='left')
ax=plt.subplot(255)
ax.set_aspect('equal')
plt.imshow(I3[4], cmap='gray', origin=origin)
plt.axis('off')
plt.title('O) Unbroken 5', fontsize=6, loc='left')
ax=plt.subplot(256)
ax.set_aspect('equal')
plt.contourf(C3[0]/np.max(C3[0]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C3[0]/np.max(C3[0]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(257)
ax.set_aspect('equal')
plt.contourf(C3[1]/np.max(C3[1]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C3[1]/np.max(C3[1]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(258)
ax.set_aspect('equal')
plt.contourf(C3[2]/np.max(C3[2]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C3[2]/np.max(C3[2]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(259)
ax.set_aspect('equal')
plt.contourf(C3[3]/np.max(C3[3]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C3[3]/np.max(C3[3]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
ax=plt.subplot(2,5,10)
ax.set_aspect('equal')
plt.contourf(C3[4]/np.max(C3[4]), levels, cmap='bwr', vmin=0, vmax=1, origin=origin)
CS=plt.contour(C3[4]/np.max(C3[4]),levels, colors='k', linewidths=0.5, origin=origin)
plt.clabel(CS, inline=1, fontsize=4)
plt.axis('off')
#plt.tight_layout()
plt.savefig('av_actmap_unbroken.png', bbox_inches='tight', dpi=300)
plt.close()
print ("[INFO] unbroken waves activation maps plotted ...")