-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.py
336 lines (295 loc) · 16.6 KB
/
push.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
import os
import time
import cv2
import matplotlib.pyplot as plt
import numpy as np
import torch
from torch.utils.data import DataLoader
from find_nearest import compute_heatmap
from helpers import makedir, find_high_activation_crop
from receptive_field import compute_rf_prototype
def push_prototypes(dataloader: DataLoader, # pytorch dataloader (must be unnormalized in [0,1])
prototype_network_parallel,
# pytorch network with prototype_vectors
class_specific: bool = True,
preprocess_input_function=None, # normalize if needed
prototype_layer_stride=1,
root_dir_for_saving_prototypes=None,
# if not None, prototypes will be saved here
epoch_number=None,
# if not provided, prototypes saved previously will be overwritten
prototype_img_filename_prefix: str = None,
prototype_self_act_filename_prefix: str = None,
proto_bound_boxes_filename_prefix: str = None,
save_prototype_class_identity: bool = True,
# which class the prototype image comes from
log=print,
device='cuda',
prototype_activation_function_in_numpy=None):
"""push each prototype to the nearest patch in the training set"""
prototype_network_parallel.eval()
log('\tpush')
start = time.time()
prototype_shape = prototype_network_parallel.module.prototype_shape
n_prototypes = prototype_network_parallel.module.num_prototypes
# saves the closest distance seen so far
global_min_proto_dist = np.full(n_prototypes, np.inf)
# saves the patch representation that gives the current smallest distance
global_min_fmap_patches = np.zeros(
[n_prototypes,
prototype_shape[1],
prototype_shape[2],
prototype_shape[3]])
'''
proto_rf_boxes and proto_bound_boxes column:
0: image index in the entire dataset
1: height start index
2: height end index
3: width start index
4: width end index
5: (optional) class identity
'''
if save_prototype_class_identity:
proto_rf_boxes = np.full(shape=[n_prototypes, 6], fill_value=-1)
proto_bound_boxes = np.full(shape=[n_prototypes, 6], fill_value=-1)
else:
proto_rf_boxes = np.full(shape=[n_prototypes, 5], fill_value=-1)
proto_bound_boxes = np.full(shape=[n_prototypes, 5], fill_value=-1)
if root_dir_for_saving_prototypes is not None:
if epoch_number is not None:
proto_epoch_dir = os.path.join(root_dir_for_saving_prototypes,
'epoch-' + str(epoch_number))
makedir(proto_epoch_dir)
else:
proto_epoch_dir = root_dir_for_saving_prototypes
else:
proto_epoch_dir = None
search_batch_size = dataloader.batch_size
num_classes = prototype_network_parallel.module.num_classes
for push_iter, data in enumerate(dataloader):
search_batch_input= data[0]
search_y = data[1]
masks = data[3] if len(data) == 3 else None
'''
start_index_of_search keeps track of the index of the image
assigned to serve as prototype
'''
start_index_of_search_batch = push_iter * search_batch_size
update_prototypes_on_batch(search_batch_input,
masks,
start_index_of_search_batch,
prototype_network_parallel,
global_min_proto_dist,
global_min_fmap_patches,
proto_rf_boxes,
proto_bound_boxes,
class_specific=class_specific,
search_y=search_y,
num_classes=num_classes,
preprocess_input_function=preprocess_input_function,
prototype_layer_stride=prototype_layer_stride,
dir_for_saving_prototypes=proto_epoch_dir,
prototype_img_filename_prefix=prototype_img_filename_prefix,
prototype_self_act_filename_prefix=prototype_self_act_filename_prefix,
prototype_activation_function_in_numpy=prototype_activation_function_in_numpy,
device=device)
if proto_epoch_dir is not None and proto_bound_boxes_filename_prefix is not None:
np.save(os.path.join(proto_epoch_dir,
f'{proto_bound_boxes_filename_prefix}-receptive_field{str(epoch_number)}.npy'),
proto_rf_boxes)
np.save(os.path.join(proto_epoch_dir, proto_bound_boxes_filename_prefix + str(
epoch_number) + '.npy'),
proto_bound_boxes)
log('\tExecuting push ...')
prototype_update = np.reshape(global_min_fmap_patches,
tuple(prototype_shape))
prototype_network_parallel.module.prototype_vectors.data.copy_(
torch.tensor(prototype_update, dtype=torch.float32).to(device))
prototype_network_parallel.to(device)
end = time.time()
log('\tpush time: \t{0}'.format(end - start))
# update each prototype for current search batch
def update_prototypes_on_batch(search_batch_input,
masks,
start_index_of_search_batch,
prototype_network_parallel,
global_min_proto_dist, # this will be updated
global_min_fmap_patches, # this will be updated
proto_rf_boxes, # this will be updated
proto_bound_boxes, # this will be updated
# global_max_act,
# global_min_act,
class_specific=True,
search_y=None, # required if class_specific == True
num_classes=None, # required if class_specific == True
preprocess_input_function=None,
prototype_layer_stride=1,
dir_for_saving_prototypes=None,
prototype_img_filename_prefix=None,
prototype_self_act_filename_prefix=None,
prototype_activation_function_in_numpy=None,
device='cuda'):
prototype_network_parallel.eval()
if preprocess_input_function is not None:
# print('preprocessing input for pushing ...')
# search_batch = copy.deepcopy(search_batch_input)
search_batch = preprocess_input_function(search_batch_input)
else:
search_batch = search_batch_input
with torch.no_grad():
search_batch = search_batch.to(device)
# this computation currently is not parallelized
protoL_input_torch, proto_dist_torch = prototype_network_parallel.module.push_forward(
search_batch)
protoL_input_ = np.copy(protoL_input_torch.detach().cpu().numpy())
proto_dist_ = np.copy(proto_dist_torch.detach().cpu().numpy())
del protoL_input_torch, proto_dist_torch
if class_specific:
class_to_img_index_dict = {key: [] for key in range(num_classes)}
# img_y is the image's integer label
for img_index, img_y in enumerate(search_y):
img_label = img_y.item()
class_to_img_index_dict[img_label].append(img_index)
prototype_shape = prototype_network_parallel.module.prototype_shape
n_prototypes = prototype_shape[0]
proto_h = prototype_shape[2]
proto_w = prototype_shape[3]
max_dist = prototype_shape[1] * prototype_shape[2] * prototype_shape[3]
for j in range(n_prototypes):
# if n_prototypes_per_class != None:
if class_specific:
# target_class is the class of the class_specific prototype
target_class = torch.argmax(
prototype_network_parallel.module.prototype_class_identity[j]).item()
# if there is not images of the target_class from this batch
# we go on to the next prototype
if len(class_to_img_index_dict[target_class]) == 0:
continue
proto_dist_j = proto_dist_[class_to_img_index_dict[target_class]][:, j, :,
:]
else:
# if it is not class specific, then we will search through
# every example
proto_dist_j = proto_dist_[:, j, :, :]
batch_min_proto_dist_j = np.amin(proto_dist_j)
if batch_min_proto_dist_j < global_min_proto_dist[j]:
batch_argmin_proto_dist_j = \
list(np.unravel_index(np.argmin(proto_dist_j, axis=None),
proto_dist_j.shape))
if class_specific:
'''
change the argmin index from the index among
images of the target class to the index in the entire search
batch
'''
batch_argmin_proto_dist_j[0] = class_to_img_index_dict[target_class][
batch_argmin_proto_dist_j[0]]
# retrieve the corresponding feature map patch
img_index_in_batch = batch_argmin_proto_dist_j[0]
fmap_height_start_index = batch_argmin_proto_dist_j[
1] * prototype_layer_stride
fmap_height_end_index = fmap_height_start_index + proto_h
fmap_width_start_index = batch_argmin_proto_dist_j[
2] * prototype_layer_stride
fmap_width_end_index = fmap_width_start_index + proto_w
batch_min_fmap_patch_j = protoL_input_[img_index_in_batch,
:,
fmap_height_start_index:fmap_height_end_index,
fmap_width_start_index:fmap_width_end_index]
global_min_proto_dist[j] = batch_min_proto_dist_j
global_min_fmap_patches[j] = batch_min_fmap_patch_j
# get the receptive field boundary of the image patch
# that generates the representation
protoL_rf_info = prototype_network_parallel.module.proto_layer_rf_info
rf_prototype_j = compute_rf_prototype(search_batch.size(dim=2),
batch_argmin_proto_dist_j,
protoL_rf_info)
# get the whole image
original_img_j = search_batch_input[rf_prototype_j[0]]
original_img_j = original_img_j.numpy()
original_img_j = np.transpose(original_img_j, (1, 2, 0))
original_img_size = original_img_j.shape[0]
# crop out the receptive field
rf_img_j = original_img_j[rf_prototype_j[1]:rf_prototype_j[2],
rf_prototype_j[3]:rf_prototype_j[4], :]
# save the prototype receptive field information
proto_rf_boxes[j, 0] = rf_prototype_j[0] + start_index_of_search_batch
proto_rf_boxes[j, 1] = rf_prototype_j[1]
proto_rf_boxes[j, 2] = rf_prototype_j[2]
proto_rf_boxes[j, 3] = rf_prototype_j[3]
proto_rf_boxes[j, 4] = rf_prototype_j[4]
if proto_rf_boxes.shape[1] == 6 and search_y is not None:
proto_rf_boxes[j, 5] = search_y[rf_prototype_j[0]].item()
# find the highly activated region of the original image
proto_dist_img_j = proto_dist_[img_index_in_batch, j, :, :]
if prototype_network_parallel.module.prototype_activation_function == 'log':
proto_act_img_j = np.log((proto_dist_img_j + 1) / (
proto_dist_img_j + prototype_network_parallel.module.epsilon))
elif prototype_network_parallel.module.prototype_activation_function == 'linear':
proto_act_img_j = max_dist - proto_dist_img_j
else:
proto_act_img_j = prototype_activation_function_in_numpy(
proto_dist_img_j)
upsampled_act_img_j = cv2.resize(proto_act_img_j, dsize=(
original_img_size, original_img_size),
interpolation=cv2.INTER_CUBIC)
proto_bound_j = find_high_activation_crop(upsampled_act_img_j)
# crop out the image patch with high activation as prototype image
proto_img_j = original_img_j[proto_bound_j[0]:proto_bound_j[1],
proto_bound_j[2]:proto_bound_j[3], :]
# save the prototype boundary (rectangular boundary of highly activated region)
proto_bound_boxes[j, 0] = proto_rf_boxes[j, 0]
proto_bound_boxes[j, 1] = proto_bound_j[0]
proto_bound_boxes[j, 2] = proto_bound_j[1]
proto_bound_boxes[j, 3] = proto_bound_j[2]
proto_bound_boxes[j, 4] = proto_bound_j[3]
if proto_bound_boxes.shape[1] == 6 and search_y is not None:
proto_bound_boxes[j, 5] = search_y[rf_prototype_j[0]].item()
if dir_for_saving_prototypes is not None:
if prototype_self_act_filename_prefix is not None:
# save the numpy array of the prototype self activation
np.save(os.path.join(dir_for_saving_prototypes,
f'{prototype_self_act_filename_prefix}{j}.npy'),
proto_act_img_j)
if prototype_img_filename_prefix is not None:
# save the whole image containing the prototype as png
plt.imsave(os.path.join(dir_for_saving_prototypes,
f'{prototype_img_filename_prefix}-original{j}.png'),
original_img_j, vmin=0.0, vmax=1.0)
if masks is not None:
m = masks[rf_prototype_j[0]].numpy()[..., np.newaxis]
plt.imsave(os.path.join(dir_for_saving_prototypes,
f'{prototype_img_filename_prefix}-original-relevant{j}.png'),
original_img_j*m, vmin=0.0, vmax=1.0)
plt.imsave(os.path.join(dir_for_saving_prototypes,
f'{prototype_img_filename_prefix}-original-irrelevant{j}.png'),
original_img_j * (1 - m), vmin=0.0, vmax=1.0)
heatmap = compute_heatmap(upsampled_act_img_j)
overlayed_original_img_j = 0.5 * original_img_j + 0.3 * heatmap
plt.imsave(os.path.join(dir_for_saving_prototypes,
f'{prototype_img_filename_prefix}-original_with_self_act{j}.png'),
overlayed_original_img_j, vmin=0.0, vmax=1.0)
# if different from the original (whole) image, save the prototype receptive field as png
if rf_img_j.shape[0] != original_img_size or rf_img_j.shape[
1] != original_img_size:
plt.imsave(os.path.join(dir_for_saving_prototypes,
f'{prototype_img_filename_prefix}-receptive_field{j}.png'),
rf_img_j,
vmin=0.0,
vmax=1.0)
overlayed_rf_img_j = overlayed_original_img_j[
rf_prototype_j[1]:rf_prototype_j[2],
rf_prototype_j[3]:rf_prototype_j[4]]
plt.imsave(os.path.join(dir_for_saving_prototypes,
f'{prototype_img_filename_prefix}-receptive_field_with_self_act{j}.png'),
overlayed_rf_img_j,
vmin=0.0,
vmax=1.0)
# save the prototype image (highly activated region of the whole image)
plt.imsave(os.path.join(dir_for_saving_prototypes,
f'{prototype_img_filename_prefix}{j}.png'),
proto_img_j,
vmin=0.0,
vmax=1.0)
if class_specific:
del class_to_img_index_dict