-
Notifications
You must be signed in to change notification settings - Fork 129
/
local_analysis.py
354 lines (301 loc) · 17.7 KB
/
local_analysis.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
##### MODEL AND DATA LOADING
import torch
import torch.utils.data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.autograd import Variable
import numpy as np
import matplotlib.pyplot as plt
import cv2
from PIL import Image
import re
import os
import copy
from helpers import makedir, find_high_activation_crop
import model
import push
import train_and_test as tnt
import save
from log import create_logger
from preprocess import mean, std, preprocess_input_function, undo_preprocess_input_function
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-gpuid', nargs=1, type=str, default='0')
parser.add_argument('-modeldir', nargs=1, type=str)
parser.add_argument('-model', nargs=1, type=str)
parser.add_argument('-imgdir', nargs=1, type=str)
parser.add_argument('-img', nargs=1, type=str)
parser.add_argument('-imgclass', nargs=1, type=int, default=-1)
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpuid[0]
# specify the test image to be analyzed
test_image_dir = args.imgdir[0] #'./local_analysis/Painted_Bunting_Class15_0081/'
test_image_name = args.img[0] #'Painted_Bunting_0081_15230.jpg'
test_image_label = args.imgclass[0] #15
test_image_path = os.path.join(test_image_dir, test_image_name)
# load the model
check_test_accu = False
load_model_dir = args.modeldir[0] #'./saved_models/vgg19/003/'
load_model_name = args.model[0] #'10_18push0.7822.pth'
#if load_model_dir[-1] == '/':
# model_base_architecture = load_model_dir.split('/')[-3]
# experiment_run = load_model_dir.split('/')[-2]
#else:
# model_base_architecture = load_model_dir.split('/')[-2]
# experiment_run = load_model_dir.split('/')[-1]
model_base_architecture = load_model_dir.split('/')[2]
experiment_run = '/'.join(load_model_dir.split('/')[3:])
save_analysis_path = os.path.join(test_image_dir, model_base_architecture,
experiment_run, load_model_name)
makedir(save_analysis_path)
log, logclose = create_logger(log_filename=os.path.join(save_analysis_path, 'local_analysis.log'))
load_model_path = os.path.join(load_model_dir, load_model_name)
epoch_number_str = re.search(r'\d+', load_model_name).group(0)
start_epoch_number = int(epoch_number_str)
log('load model from ' + load_model_path)
log('model base architecture: ' + model_base_architecture)
log('experiment run: ' + experiment_run)
ppnet = torch.load(load_model_path)
ppnet = ppnet.cuda()
ppnet_multi = torch.nn.DataParallel(ppnet)
img_size = ppnet_multi.module.img_size
prototype_shape = ppnet.prototype_shape
max_dist = prototype_shape[1] * prototype_shape[2] * prototype_shape[3]
class_specific = True
normalize = transforms.Normalize(mean=mean,
std=std)
# load the test data and check test accuracy
from settings import test_dir
if check_test_accu:
test_batch_size = 100
test_dataset = datasets.ImageFolder(
test_dir,
transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.ToTensor(),
normalize,
]))
test_loader = torch.utils.data.DataLoader(
test_dataset, batch_size=test_batch_size, shuffle=True,
num_workers=4, pin_memory=False)
log('test set size: {0}'.format(len(test_loader.dataset)))
accu = tnt.test(model=ppnet_multi, dataloader=test_loader,
class_specific=class_specific, log=print)
##### SANITY CHECK
# confirm prototype class identity
load_img_dir = os.path.join(load_model_dir, 'img')
prototype_info = np.load(os.path.join(load_img_dir, 'epoch-'+epoch_number_str, 'bb'+epoch_number_str+'.npy'))
prototype_img_identity = prototype_info[:, -1]
log('Prototypes are chosen from ' + str(len(set(prototype_img_identity))) + ' number of classes.')
log('Their class identities are: ' + str(prototype_img_identity))
# confirm prototype connects most strongly to its own class
prototype_max_connection = torch.argmax(ppnet.last_layer.weight, dim=0)
prototype_max_connection = prototype_max_connection.cpu().numpy()
if np.sum(prototype_max_connection == prototype_img_identity) == ppnet.num_prototypes:
log('All prototypes connect most strongly to their respective classes.')
else:
log('WARNING: Not all prototypes connect most strongly to their respective classes.')
##### HELPER FUNCTIONS FOR PLOTTING
def save_preprocessed_img(fname, preprocessed_imgs, index=0):
img_copy = copy.deepcopy(preprocessed_imgs[index:index+1])
undo_preprocessed_img = undo_preprocess_input_function(img_copy)
print('image index {0} in batch'.format(index))
undo_preprocessed_img = undo_preprocessed_img[0]
undo_preprocessed_img = undo_preprocessed_img.detach().cpu().numpy()
undo_preprocessed_img = np.transpose(undo_preprocessed_img, [1,2,0])
plt.imsave(fname, undo_preprocessed_img)
return undo_preprocessed_img
def save_prototype(fname, epoch, index):
p_img = plt.imread(os.path.join(load_img_dir, 'epoch-'+str(epoch), 'prototype-img'+str(index)+'.png'))
#plt.axis('off')
plt.imsave(fname, p_img)
def save_prototype_self_activation(fname, epoch, index):
p_img = plt.imread(os.path.join(load_img_dir, 'epoch-'+str(epoch),
'prototype-img-original_with_self_act'+str(index)+'.png'))
#plt.axis('off')
plt.imsave(fname, p_img)
def save_prototype_original_img_with_bbox(fname, epoch, index,
bbox_height_start, bbox_height_end,
bbox_width_start, bbox_width_end, color=(0, 255, 255)):
p_img_bgr = cv2.imread(os.path.join(load_img_dir, 'epoch-'+str(epoch), 'prototype-img-original'+str(index)+'.png'))
cv2.rectangle(p_img_bgr, (bbox_width_start, bbox_height_start), (bbox_width_end-1, bbox_height_end-1),
color, thickness=2)
p_img_rgb = p_img_bgr[...,::-1]
p_img_rgb = np.float32(p_img_rgb) / 255
#plt.imshow(p_img_rgb)
#plt.axis('off')
plt.imsave(fname, p_img_rgb)
def imsave_with_bbox(fname, img_rgb, bbox_height_start, bbox_height_end,
bbox_width_start, bbox_width_end, color=(0, 255, 255)):
img_bgr_uint8 = cv2.cvtColor(np.uint8(255*img_rgb), cv2.COLOR_RGB2BGR)
cv2.rectangle(img_bgr_uint8, (bbox_width_start, bbox_height_start), (bbox_width_end-1, bbox_height_end-1),
color, thickness=2)
img_rgb_uint8 = img_bgr_uint8[...,::-1]
img_rgb_float = np.float32(img_rgb_uint8) / 255
#plt.imshow(img_rgb_float)
#plt.axis('off')
plt.imsave(fname, img_rgb_float)
# load the test image and forward it through the network
preprocess = transforms.Compose([
transforms.Resize((img_size,img_size)),
transforms.ToTensor(),
normalize
])
img_pil = Image.open(test_image_path)
img_tensor = preprocess(img_pil)
img_variable = Variable(img_tensor.unsqueeze(0))
images_test = img_variable.cuda()
labels_test = torch.tensor([test_image_label])
logits, min_distances = ppnet_multi(images_test)
conv_output, distances = ppnet.push_forward(images_test)
prototype_activations = ppnet.distance_2_similarity(min_distances)
prototype_activation_patterns = ppnet.distance_2_similarity(distances)
if ppnet.prototype_activation_function == 'linear':
prototype_activations = prototype_activations + max_dist
prototype_activation_patterns = prototype_activation_patterns + max_dist
tables = []
for i in range(logits.size(0)):
tables.append((torch.argmax(logits, dim=1)[i].item(), labels_test[i].item()))
log(str(i) + ' ' + str(tables[-1]))
idx = 0
predicted_cls = tables[idx][0]
correct_cls = tables[idx][1]
log('Predicted: ' + str(predicted_cls))
log('Actual: ' + str(correct_cls))
original_img = save_preprocessed_img(os.path.join(save_analysis_path, 'original_img.png'),
images_test, idx)
##### MOST ACTIVATED (NEAREST) 10 PROTOTYPES OF THIS IMAGE
makedir(os.path.join(save_analysis_path, 'most_activated_prototypes'))
log('Most activated 10 prototypes of this image:')
array_act, sorted_indices_act = torch.sort(prototype_activations[idx])
for i in range(1,11):
log('top {0} activated prototype for this image:'.format(i))
save_prototype(os.path.join(save_analysis_path, 'most_activated_prototypes',
'top-%d_activated_prototype.png' % i),
start_epoch_number, sorted_indices_act[-i].item())
save_prototype_original_img_with_bbox(fname=os.path.join(save_analysis_path, 'most_activated_prototypes',
'top-%d_activated_prototype_in_original_pimg.png' % i),
epoch=start_epoch_number,
index=sorted_indices_act[-i].item(),
bbox_height_start=prototype_info[sorted_indices_act[-i].item()][1],
bbox_height_end=prototype_info[sorted_indices_act[-i].item()][2],
bbox_width_start=prototype_info[sorted_indices_act[-i].item()][3],
bbox_width_end=prototype_info[sorted_indices_act[-i].item()][4],
color=(0, 255, 255))
save_prototype_self_activation(os.path.join(save_analysis_path, 'most_activated_prototypes',
'top-%d_activated_prototype_self_act.png' % i),
start_epoch_number, sorted_indices_act[-i].item())
log('prototype index: {0}'.format(sorted_indices_act[-i].item()))
log('prototype class identity: {0}'.format(prototype_img_identity[sorted_indices_act[-i].item()]))
if prototype_max_connection[sorted_indices_act[-i].item()] != prototype_img_identity[sorted_indices_act[-i].item()]:
log('prototype connection identity: {0}'.format(prototype_max_connection[sorted_indices_act[-i].item()]))
log('activation value (similarity score): {0}'.format(array_act[-i]))
log('last layer connection with predicted class: {0}'.format(ppnet.last_layer.weight[predicted_cls][sorted_indices_act[-i].item()]))
activation_pattern = prototype_activation_patterns[idx][sorted_indices_act[-i].item()].detach().cpu().numpy()
upsampled_activation_pattern = cv2.resize(activation_pattern, dsize=(img_size, img_size),
interpolation=cv2.INTER_CUBIC)
# show the most highly activated patch of the image by this prototype
high_act_patch_indices = find_high_activation_crop(upsampled_activation_pattern)
high_act_patch = original_img[high_act_patch_indices[0]:high_act_patch_indices[1],
high_act_patch_indices[2]:high_act_patch_indices[3], :]
log('most highly activated patch of the chosen image by this prototype:')
#plt.axis('off')
plt.imsave(os.path.join(save_analysis_path, 'most_activated_prototypes',
'most_highly_activated_patch_by_top-%d_prototype.png' % i),
high_act_patch)
log('most highly activated patch by this prototype shown in the original image:')
imsave_with_bbox(fname=os.path.join(save_analysis_path, 'most_activated_prototypes',
'most_highly_activated_patch_in_original_img_by_top-%d_prototype.png' % i),
img_rgb=original_img,
bbox_height_start=high_act_patch_indices[0],
bbox_height_end=high_act_patch_indices[1],
bbox_width_start=high_act_patch_indices[2],
bbox_width_end=high_act_patch_indices[3], color=(0, 255, 255))
# show the image overlayed with prototype activation map
rescaled_activation_pattern = upsampled_activation_pattern - np.amin(upsampled_activation_pattern)
rescaled_activation_pattern = rescaled_activation_pattern / np.amax(rescaled_activation_pattern)
heatmap = cv2.applyColorMap(np.uint8(255*rescaled_activation_pattern), cv2.COLORMAP_JET)
heatmap = np.float32(heatmap) / 255
heatmap = heatmap[...,::-1]
overlayed_img = 0.5 * original_img + 0.3 * heatmap
log('prototype activation map of the chosen image:')
#plt.axis('off')
plt.imsave(os.path.join(save_analysis_path, 'most_activated_prototypes',
'prototype_activation_map_by_top-%d_prototype.png' % i),
overlayed_img)
log('--------------------------------------------------------------')
##### PROTOTYPES FROM TOP-k CLASSES
k = 50
log('Prototypes from top-%d classes:' % k)
topk_logits, topk_classes = torch.topk(logits[idx], k=k)
for i,c in enumerate(topk_classes.detach().cpu().numpy()):
makedir(os.path.join(save_analysis_path, 'top-%d_class_prototypes' % (i+1)))
log('top %d predicted class: %d' % (i+1, c))
log('logit of the class: %f' % topk_logits[i])
class_prototype_indices = np.nonzero(ppnet.prototype_class_identity.detach().cpu().numpy()[:, c])[0]
class_prototype_activations = prototype_activations[idx][class_prototype_indices]
_, sorted_indices_cls_act = torch.sort(class_prototype_activations)
prototype_cnt = 1
for j in reversed(sorted_indices_cls_act.detach().cpu().numpy()):
prototype_index = class_prototype_indices[j]
save_prototype(os.path.join(save_analysis_path, 'top-%d_class_prototypes' % (i+1),
'top-%d_activated_prototype.png' % prototype_cnt),
start_epoch_number, prototype_index)
save_prototype_original_img_with_bbox(fname=os.path.join(save_analysis_path, 'top-%d_class_prototypes' % (i+1),
'top-%d_activated_prototype_in_original_pimg.png' % prototype_cnt),
epoch=start_epoch_number,
index=prototype_index,
bbox_height_start=prototype_info[prototype_index][1],
bbox_height_end=prototype_info[prototype_index][2],
bbox_width_start=prototype_info[prototype_index][3],
bbox_width_end=prototype_info[prototype_index][4],
color=(0, 255, 255))
save_prototype_self_activation(os.path.join(save_analysis_path, 'top-%d_class_prototypes' % (i+1),
'top-%d_activated_prototype_self_act.png' % prototype_cnt),
start_epoch_number, prototype_index)
log('prototype index: {0}'.format(prototype_index))
log('prototype class identity: {0}'.format(prototype_img_identity[prototype_index]))
if prototype_max_connection[prototype_index] != prototype_img_identity[prototype_index]:
log('prototype connection identity: {0}'.format(prototype_max_connection[prototype_index]))
log('activation value (similarity score): {0}'.format(prototype_activations[idx][prototype_index]))
log('last layer connection: {0}'.format(ppnet.last_layer.weight[c][prototype_index]))
activation_pattern = prototype_activation_patterns[idx][prototype_index].detach().cpu().numpy()
upsampled_activation_pattern = cv2.resize(activation_pattern, dsize=(img_size, img_size),
interpolation=cv2.INTER_CUBIC)
# show the most highly activated patch of the image by this prototype
high_act_patch_indices = find_high_activation_crop(upsampled_activation_pattern)
high_act_patch = original_img[high_act_patch_indices[0]:high_act_patch_indices[1],
high_act_patch_indices[2]:high_act_patch_indices[3], :]
log('most highly activated patch of the chosen image by this prototype:')
#plt.axis('off')
plt.imsave(os.path.join(save_analysis_path, 'top-%d_class_prototypes' % (i+1),
'most_highly_activated_patch_by_top-%d_prototype.png' % prototype_cnt),
high_act_patch)
log('most highly activated patch by this prototype shown in the original image:')
imsave_with_bbox(fname=os.path.join(save_analysis_path, 'top-%d_class_prototypes' % (i+1),
'most_highly_activated_patch_in_original_img_by_top-%d_prototype.png' % prototype_cnt),
img_rgb=original_img,
bbox_height_start=high_act_patch_indices[0],
bbox_height_end=high_act_patch_indices[1],
bbox_width_start=high_act_patch_indices[2],
bbox_width_end=high_act_patch_indices[3], color=(0, 255, 255))
# show the image overlayed with prototype activation map
rescaled_activation_pattern = upsampled_activation_pattern - np.amin(upsampled_activation_pattern)
rescaled_activation_pattern = rescaled_activation_pattern / np.amax(rescaled_activation_pattern)
heatmap = cv2.applyColorMap(np.uint8(255*rescaled_activation_pattern), cv2.COLORMAP_JET)
heatmap = np.float32(heatmap) / 255
heatmap = heatmap[...,::-1]
overlayed_img = 0.5 * original_img + 0.3 * heatmap
log('prototype activation map of the chosen image:')
#plt.axis('off')
plt.imsave(os.path.join(save_analysis_path, 'top-%d_class_prototypes' % (i+1),
'prototype_activation_map_by_top-%d_prototype.png' % prototype_cnt),
overlayed_img)
log('--------------------------------------------------------------')
prototype_cnt += 1
log('***************************************************************')
if predicted_cls == correct_cls:
log('Prediction is correct.')
else:
log('Prediction is wrong.')
logclose()