-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
541 lines (415 loc) · 17.1 KB
/
utils.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import gzip
import heapq
import os
import pickle
import shlex
import subprocess
from timeit import default_timer as timer
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.datasets import cifar10, mnist
from tensorflow.keras import utils
from models.Cifar10VGG import Cifar10VGG
from models.ImagenetResNet import ImagenetResNet50
from models.MnistVGG import MnistVGG
def get_neuron_result(layer_result, neuron, input_id=0):
dim_1 = neuron // (layer_result.shape[2] * layer_result.shape[3])
dim_2 = (neuron % (layer_result.shape[2] * layer_result.shape[3])) // layer_result.shape[3]
dim_3 = (neuron % (layer_result.shape[2] * layer_result.shape[3])) % layer_result.shape[3]
return layer_result[input_id][dim_1][dim_2][dim_3]
def get_topk_activation_by_layer_idx_in_batch(model, cur_input, k, layer, neuron, cur_input_id):
layer_result = model.get_layer_result_by_layer_id(cur_input, layer)
heap = get_topk_activation_heap_for_batch(cur_input_id, k, layer_result, neuron)
return heap
def get_topk_activation_by_layer_name_in_batch(model, cur_input, k, layer_name, neuron, cur_input_id):
layer_result = model.get_layer_result_by_layer_name(cur_input, layer_name)
heap = get_topk_activation_heap_for_batch(cur_input_id, k, layer_result, neuron)
return heap
def get_topk_activation_heap_for_batch(cur_input_id, k, layer_result, neuron):
heap = []
for input_id, real_id in enumerate(cur_input_id):
neuron_result = get_neuron_result(layer_result, neuron, input_id)
if len(heap) < k:
heapq.heappush(heap, (neuron_result, real_id))
elif (neuron_result, real_id) > heap[0]:
heapq.heapreplace(heap, (neuron_result, real_id))
return heap
def get_group_activations_from_layer(neuron_group, layer_activations):
activations = list()
for neuron_idx in neuron_group.neuron_idx_list:
activations.append(layer_activations[neuron_idx])
return np.asarray(activations)
def update_min_distance_heap(cur_input_id, model, dataset, dist_func, layer_sample, heap, k, neuron_group,
layer_result_dataset=None):
if layer_result_dataset is None:
cur_input = []
for input_id in cur_input_id:
cur_input.append(dataset[input_id])
layer_result_batch = model.get_layer_result_by_layer_id(cur_input, neuron_group.layer_id)
else:
layer_result_batch = []
for idx in cur_input_id:
layer_result_batch.append(layer_result_dataset[idx])
# layer_result_batch = np.array(layer_result_batch)
for input_id, real_id in enumerate(cur_input_id):
group_sample = get_group_activations_from_layer(neuron_group, layer_sample)
group_input = get_group_activations_from_layer(neuron_group, layer_result_batch[input_id])
dist = dist_func(group_sample, group_input)
if len(heap) < k:
heapq.heappush(heap, (-dist, real_id))
elif (-dist, real_id) > heap[0]:
heapq.heapreplace(heap, (-dist, real_id))
def update_max_norm_heap(cur_input_id, model, dataset, norm, heap, k, neuron_group, layer_result_dataset=None):
if layer_result_dataset is None:
cur_input = []
for input_id in cur_input_id:
cur_input.append(dataset[input_id])
layer_result_batch = model.get_layer_result_by_layer_id(cur_input, neuron_group.layer_id)
else:
layer_result_batch = []
for idx in cur_input_id:
layer_result_batch.append(layer_result_dataset[idx])
for input_id, real_id in enumerate(cur_input_id):
group_input = get_group_activations_from_layer(neuron_group, layer_result_batch[input_id])
dist = norm(group_input)
if len(heap) < k:
heapq.heappush(heap, (dist, real_id))
elif (dist, real_id) > heap[0]:
heapq.heapreplace(heap, (dist, real_id))
def initialize_data_model():
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
model = Cifar10VGG(train=False)
print(model.model.summary())
return model, x_test
def plot_cifar(X, y, idx):
img = X[idx].reshape(32, 32, 3)
plt.imshow(img, interpolation='nearest')
plt.title('img: %d, true label: %d' % (idx, y[idx]))
# plt.savefig("%d.png" % idx)
plt.show()
def plot_mnist(X, y, idx, label_pred=None):
img = X[idx].reshape(28, 28)
plt.imshow(img, cmap='gray')
if label_pred is None:
label_pred = -1
plt.title('img: %d, true label: %d, predicted: %d' % (idx, y[idx], label_pred))
# plt.savefig("%d.png" % idx)
plt.show()
def l2_dist(x, y):
return np.sqrt(l2(x, y))
def l2_norm(x):
return np.sqrt(np.sum(np.square(x)))
def l2(x, y):
if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
if x.ndim != y.ndim:
assert False
return np.sum(np.square(x - y))
def cosine(x, y):
x = x.flatten()
y = y.flatten()
return l2_dist(x / np.linalg.norm(x), y / np.linalg.norm(y))
def get_centroid(x):
return np.mean(x, axis=0)
def gini(x):
x = np.nan_to_num(x, False)
x = x.flatten()
if np.amin(x) < 0:
x -= np.amin(x)
x += 0.0000000001
x = np.sort(x)
index = np.arange(1, x.shape[0] + 1)
n = x.shape[0]
return (np.sum((2 * index - n - 1) * x)) / (n * np.sum(x))
def l0_sparsity(x):
x = np.nan_to_num(x, False)
sparsity = 1.0 - (np.count_nonzero(x) / float(x.size))
return sparsity
def load_imagenet_test_resnet_dataset_model():
start = timer()
x_test = np.load("/data/ilsvrc2012/ilsvrc2012_test.npy")
end = timer()
load_time = end - start
model = ImagenetResNet50()
return x_test, model, load_time
def load_imagenet_val_resnet_dataset_model():
start = timer()
x_val = np.load("/data/ilsvrc2012/ilsvrc2012_val_10000.npy")
end = timer()
load_time = end - start
model = ImagenetResNet50()
return x_val, model, load_time
def load_cifar10_vgg_dataset_model():
start = timer()
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
end = timer()
load_time = end - start
model = Cifar10VGG(train=False)
return x_train, y_train, x_test, y_test, model, load_time
def load_mnist_vgg_dataset_model():
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)
y_train = utils.to_categorical(y_train)
y_test = utils.to_categorical(y_test)
model = MnistVGG(train=False)
return x_train, y_train, x_test, y_test, model
def equal_tuple(a, x, eps=1e-4):
for i, j in zip(a, x):
if abs(i - j) > eps:
return False
return True
def bisect_left(a, x, lo=0, hi=None):
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo + hi) // 2
if a[mid] < x:
lo = mid + 1
else:
hi = mid
return lo
def binary_search(a, x):
pos = bisect_left(a, x)
for i in range(max(0, pos - 3), min(len(a), pos + 2)):
if equal_tuple(a[i], x):
return i
return -1
def is_power_of_two(n):
if n == 0:
return False
while n != 1:
if n % 2 != 0:
return False
n = n // 2
return True
def gload(filename):
clear_cache()
file = gzip.GzipFile(filename, 'rb')
res = pickle.load(file)
file.close()
return res
def gdump(obj, filename):
file = gzip.GzipFile(filename, 'wb')
pickle.dump(obj, file, -1)
file.flush()
os.fsync(file)
file.close()
def load_pickle(filename):
clear_cache()
with open(filename, 'rb') as file:
res = pickle.load(file)
return res
def persist_pickle(filename, obj):
with open(filename, 'wb') as file:
pickle.dump(obj, file, protocol=pickle.HIGHEST_PROTOCOL)
file.flush()
os.fsync(file)
def persist_numpy(filename, obj):
with open(filename, 'wb') as file:
np.save(file, obj)
file.flush()
os.fsync(file)
def get_bits(n, n_bits):
return [n >> i & 1 for i in range(n_bits - 1, -1, -1)]
def get_layer_result_by_layer_name(model, x, layer_name, batch_size=None):
if batch_size is None:
res = model.get_layer_result_by_layer_name(x, layer_name)
else:
r = list()
n = len(x)
for i in range(n // batch_size + 1):
if (i + 1) * batch_size >= n:
layer_res = model.get_layer_result_by_layer_name(x[i * batch_size: n], layer_name)
else:
layer_res = model.get_layer_result_by_layer_name(x[i * batch_size: (i + 1) * batch_size], layer_name)
r.append(layer_res)
if (i + 1) * batch_size >= n:
break
res = np.concatenate(r, axis=0)
return res
def get_layer_result_by_layer_id(model, x, layer_id, batch_size=None):
if batch_size is None:
res = model.get_layer_result_by_layer_id(x, layer_id)
else:
r = list()
n = len(x)
for i in range(n // batch_size + 1):
if (i + 1) * batch_size >= n:
layer_res = model.get_layer_result_by_layer_id(x[i * batch_size: n], layer_id)
else:
layer_res = model.get_layer_result_by_layer_id(x[i * batch_size: (i + 1) * batch_size], layer_id)
r.append(layer_res)
if (i + 1) * batch_size >= n:
break
res = np.concatenate(r, axis=0)
return res
def get_layer_results_by_layer_names(model, x, layer_names, batch_size=None):
if batch_size is None:
res = model.get_layer_results_by_layer_names(x, layer_names)
else:
r = list()
for i in range(len(layer_names)):
r.append(list())
n = len(x)
for i in range(n // batch_size + 1):
if (i + 1) * batch_size >= n:
layer_res = model.get_layer_results_by_layer_names(x[i * batch_size: n], layer_names)
else:
layer_res = model.get_layer_results_by_layer_names(x[i * batch_size: (i + 1) * batch_size], layer_names)
for j in range(len(layer_res)):
r[j].append(layer_res[j])
if (i + 1) * batch_size >= n:
break
res = list()
for i in range(len(r)):
res.append(np.concatenate(r[i], axis=0))
return res
def get_most_similar_input_based_on_neuron_group(model, dataset, k, neuron_group, dist_func, image_sample_id,
batch_size, layer_result_dataset=None):
if batch_size is None:
batch_size = 2000
if layer_result_dataset is None:
layer_sample = model.get_layer_result_by_layer_id([dataset[image_sample_id]], neuron_group.layer_id)[0]
else:
layer_sample = layer_result_dataset[image_sample_id]
heap = []
cur_input_id = []
for i in range(dataset.shape[0]):
cur_input_id.append(i)
if (i + 1) % batch_size == 0 or i + 1 == dataset.shape[0]:
update_min_distance_heap(cur_input_id, model, dataset, dist_func, layer_sample, heap, k, neuron_group,
layer_result_dataset)
cur_input_id = []
return heap
def get_topk_images_producing_highest_activation_based_on_neuron_group(model, dataset, k, neuron_group, norm,
batch_size, layer_result_dataset=None):
if batch_size is None:
batch_size = 2000
heap = []
cur_input_id = []
for i in range(dataset.shape[0]):
cur_input_id.append(i)
if (i + 1) % batch_size == 0 or i + 1 == dataset.shape[0]:
update_max_norm_heap(cur_input_id, model, dataset, norm, heap, k, neuron_group, layer_result_dataset)
cur_input_id = []
return heap
def get_topk_activations_given_images(model, dataset, image_ids, layer_name, k):
res = list()
image_samples = list()
for image_sample_id in image_ids:
image_samples.append(dataset[image_sample_id])
layer_result_image_samples = get_layer_result_by_layer_name(model, image_samples, layer_name)
for idx, image_sample_id in enumerate(image_ids):
heap = list()
for neuron_idx, activation in np.ndenumerate(layer_result_image_samples[idx]):
if len(heap) < k:
heapq.heappush(heap, (activation, neuron_idx))
elif (activation, neuron_idx) > heap[0]:
heapq.heapreplace(heap, (activation, neuron_idx))
res.append(sorted(heap, reverse=True))
return res
def get_rev_sorted_activations_given_images(model, dataset, image_ids, layer_name, nonzero, eps=5e-2):
res = list()
image_samples = list()
for image_sample_id in image_ids:
image_samples.append(dataset[image_sample_id])
layer_result_image_samples = get_layer_result_by_layer_name(model, image_samples, layer_name)
for idx, image_sample_id in enumerate(image_ids):
act_neurons = list()
for neuron_idx, activation in np.ndenumerate(layer_result_image_samples[idx]):
if nonzero:
if abs(activation) > eps:
act_neurons.append((activation, neuron_idx))
else:
act_neurons.append((activation, neuron_idx))
res.append(sorted(act_neurons, reverse=True))
return res
def warm_up_model(model, dataset):
model.predict([dataset[0]])
def get_layer_result_for_image_batch(model, dataset, image_batch, layer_id, batch_size):
cur_input = []
for input_id in image_batch:
cur_input.append(dataset[input_id])
layer_result = get_layer_result_by_layer_id(model, cur_input, layer_id, batch_size)
return layer_result
def get_partition_id_by_image_id(bit_array, image_id, bits_per_image):
start_bit = image_id * bits_per_image
end_bit = start_bit + bits_per_image
res = 0
for bit in bit_array[start_bit:end_bit]:
res = (res << 1) | bit
return res
def get_image_ids_by_partition_id(bit_array, partition_id, bits_per_image, n_images):
images = set()
partition_bits = get_bits(partition_id, bits_per_image)
for image_id in range(n_images):
start_bit = image_id * bits_per_image
end_bit = start_bit + bits_per_image
same = True
for i, pos in enumerate(range(start_bit, end_bit)):
if int(partition_bits[i]) != int(bit_array[pos]):
same = False
break
if same:
images.add(image_id)
return images
def _get_double_pointers(x):
return (x.__array_interface__['data'][0] + np.arange(x.shape[0]) * x.strides[0]).astype(np.uintp)
def warm_up_layer(model, dataset, layer_id, batch_size):
for i in range(dataset.shape[0] // batch_size + 1):
if (i + 1) * batch_size >= dataset.shape[0]:
model.get_layer_result_by_layer_id(dataset[i * batch_size: dataset.shape[0]], layer_id)
else:
model.get_layer_result_by_layer_id(dataset[i * batch_size: (i + 1) * batch_size], layer_id)
def prod(tup):
res = 1
for ele in tup:
res *= ele
return res
def clear_cache():
subprocess.run(shlex.split(
"echo 1 > /proc/sys/vm/drop_caches"
), shell=True)
def prepare_layers_result_dataset(model, dataset, layer_names, all_layer_names, BATCH_SIZE):
print("Preparing layers_result_dataset ...")
layers_result_dataset = dict()
for i in range(len(layer_names)):
layer_id = all_layer_names.index(layer_names[i])
if layer_id not in layers_result_dataset:
layers_result_dataset[layer_id] = get_layer_result_by_layer_name(model, dataset, layer_names[i],
batch_size=BATCH_SIZE)
return layers_result_dataset
def persist_index(dataset_name, layer_name, n_partitions, ratio, par_low_bound, par_upp_bound, rev_act, rev_bit_arr,
rev_idx_act, rev_idx_idx):
clear_cache()
for i, obj in enumerate([rev_act, rev_idx_act, rev_bit_arr, par_low_bound, par_upp_bound, rev_idx_idx]):
if i <= 4:
filename = f"./index/{dataset_name}_{layer_name}_{n_partitions}_{ratio}_reverse_indices_{i}.npy"
np.save(filename, obj)
else:
filename = f"./index/{dataset_name}_{layer_name}_{n_partitions}_{ratio}_reverse_indices_{i}.pickle"
persist_pickle(filename, obj)
def evaluate(std, answer, eps=1e-4):
std = sorted(std)
answer = sorted(answer)
std_image = [x[1] for x in std]
answer_image = [x[1] for x in answer]
tp = 0
for i in range(len(answer)):
if answer_image[i] in std_image or (i < len(std) and abs((answer[i][0] - std[i][0]) / std[i][0]) <= eps):
tp += 1
if len(answer) == 0:
return 0.0, 0.0
else:
return tp / len(answer), tp / len(std)