-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
1201 lines (1005 loc) · 42.2 KB
/
test.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Routines for testing a model.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import dask.array as da
import tensorflow as tf
import tensorflow.compat.v1.keras.backend as K
from tensorflow.compat.v1.keras.models import load_model
from tensorflow.compat.v1.keras.models import Model
import h5py
import yaml
import sys
import os
import argparse
from tqdm import tqdm
import re
from time import time
from data_input import train_val_split, subsample_data
from data_input import get_generator, batch_generator
from data_input import generate_batches
# NOTE: Disabled this import because cleverhans fails with TF2
# from adv_utils import init_attack
from surgery import ablate_activations, del_mse_nodes, del_extra_nodes
from surgery import network2dict, restore_nodes
from utils import print_flags
from utils import pairwise_loss, invariance_loss, mean_loss
from utils import handle_metrics
from utils import prepare_test_config, numpy_to_python
from utils import print_test_results, write_test_results
# Import the whole compat version of keras to set the losses
from tensorflow.compat.v1 import keras
keras.losses.pairwise_loss = pairwise_loss
keras.losses.invariance_loss = invariance_loss
keras.losses.mean_loss = mean_loss
# Initialize the Flags container
FLAGS = None
def main(argv=None):
K.set_floatx('float32')
print_flags(FLAGS)
# Read or/and prepare test config dictionary
if FLAGS.test_config_file:
with open(FLAGS.test_config_file, 'r') as yml_file:
test_config = yaml.load(yml_file, Loader=yaml.FullLoader)
else:
test_config = {}
test_config = prepare_test_config(test_config, FLAGS)
# Load model
model = load_model(os.path.join(FLAGS.model))
# Open HDF5 file containing the data set and get images and labels
hdf5_file = h5py.File(FLAGS.data_file, 'r')
images_tr, images_tt, labels_tr, labels_tt, _ = train_val_split(
hdf5_file, FLAGS.group_tr, FLAGS.group_tt, FLAGS.chunk_size)
# Test
results_dict = test(images_tt, labels_tt, images_tr, labels_tr, model,
test_config, FLAGS.batch_size, FLAGS.chunk_size)
# Print and write results
if FLAGS.output_dir:
if FLAGS.output_dir == '-1':
FLAGS.output_dir = os.path.dirname(FLAGS.model)
if FLAGS.append:
write_mode = 'a'
else:
write_mode = 'w'
if not os.path.exists(FLAGS.output_dir):
os.makedirs(FLAGS.output_dir)
output_file = os.path.join(FLAGS.output_dir,
'{}.txt'.format(FLAGS.output_basename))
write_test_results(results_dict, output_file, write_mode)
output_file = os.path.join(FLAGS.output_dir,
'{}.yml'.format(FLAGS.output_basename))
with open(output_file, write_mode) as f:
results_dict = numpy_to_python(results_dict)
yaml.dump(results_dict, f, default_flow_style=False)
print_test_results(results_dict)
# Close HDF5 File
hdf5_file.close()
def test(images_tt, labels_tt, images_tr, labels_tr, model, test_config,
batch_size, chunk_size):
"""
Performs a set of test operations, as specified in test_config.
Parameters
----------
images_tt : h5py Dataset
The set of test images
labels_tt : h5py Dataset
The ground truth labels of the test set
images_tr : h5py Dataset
The set of train images
labels_tr : h5py Dataset
The ground truth labels of the train set
model : Keras Model
The model
batch_size : int
Batch size
test_config : str
YAML file specifying the aspects to test and their parameters
Returns
-------
results_dict : dict
Dictionary containing some performance metrics
"""
# Ensure the model has no MSE nodes and outputs
model = del_mse_nodes(model)
results_dict = {}
daug_params_dicts = {}
# Test performance
if 'test' in test_config:
results_dict.update({'test': {}})
test_config_test = test_config['test']
# Original images (no data augmentation)
if 'orig' in test_config_test:
print('\nComputing test performance with the original images')
results_dict['test'].update({'orig': {}})
results_dict['test']['orig'] = test_rep(
images_tt, labels_tt, batch_size, model,
test_config_test['orig']['daug_params'], 1,
test_config_test['orig']['metrics'])
# Augmented images
if 'daug' in test_config_test:
results_dict['test'].update({'daug': {}})
for scheme in test_config_test['daug']:
print('\nComputing test performance with {} '
'augmentation'.format(scheme))
results_dict['test']['daug'].update({scheme: {}})
results_dict['test']['daug'][scheme] = test_rep(
images_tt, labels_tt, batch_size, model,
test_config_test['daug'][scheme]['daug_params'],
test_config_test['daug'][scheme]['repetitions'],
test_config_test['daug'][scheme]['metrics'])
# Train performance
if 'train' in test_config:
results_dict.update({'train': {}})
test_config_train = test_config['train']
# Original images (no data augmentation)
if 'orig' in test_config_train:
print('\nComputing train performance with the original images')
results_dict['train'].update({'orig': {}})
results_dict['train']['orig'] = test_rep(
images_tr, labels_tr, batch_size, model,
test_config_train['orig']['daug_params'], 1,
test_config_train['orig']['metrics'])
# Augmented images
if 'daug' in test_config_train:
results_dict['train'].update({'daug': {}})
for scheme in test_config_train['daug']:
print('\nComputing train performance with {} '
'augmentation'.format(scheme))
results_dict['train']['daug'].update({scheme: {}})
results_dict['train']['daug'][scheme] = test_rep(
images_tr, labels_tr, batch_size, model,
test_config_train['daug'][scheme]['daug_params'],
test_config_train['daug'][scheme]['repetitions'],
test_config_train['daug'][scheme]['metrics'])
# Test robustness to ablation of units
if 'ablation' in test_config:
results_dict.update({'ablation': {}})
# Test set
if 'test' in test_config['ablation']:
results_dict['ablation'].update({'test': {}})
for pct in test_config['ablation']['pct']:
print('\nComputing test robustness to ablation of {} % of the '
'units'.format(100 * pct))
results_dict['ablation']['test'].update({pct: {}})
results_dict['ablation']['test'][pct] = test_ablation(
images_tt, labels_tt, batch_size, model,
test_config['ablation']['daug_params'],
test_config['ablation']['repetitions'],
test_config['ablation']['layer_regex'],
pct,
test_config['ablation']['seed'],
test_config['ablation']['metrics'])
# Train set
if 'train' in test_config['ablation']:
results_dict['ablation'].update({'train': {}})
for pct in test_config['ablation']['pct']:
print('\nComputing train robustness to ablation of {} % of '
'the units'.format(100 * pct))
results_dict['ablation']['train'].update({pct: {}})
results_dict['ablation']['train'][pct] = test_ablation(
images_tr, labels_tr, batch_size, model,
test_config['ablation']['daug_params'],
test_config['ablation']['repetitions'],
test_config['ablation']['layer_regex'],
pct,
test_config['ablation']['seed'],
test_config['ablation']['metrics'])
# Test adversarial robustness
if 'adv' in test_config:
results_dict.update({'adv': {}})
# Subsample data
images_adv, labels_adv, aux_hdf5 = subsample_data(
images_tt, labels_tt, test_config['adv']['pct_data'],
chunk_size, test_config['adv']['shuffle_data'],
test_config['adv']['shuffle_seed'])
# White box attack
results_dict['adv'].update({'white_box': {}})
adv_model = model
for attack, attack_dict in test_config['adv']['attacks'].items():
print('\nComputing white box adversarial robustness '
'towards {}'.format(attack))
results_dict['adv']['white_box'].update({attack: {}})
results_dict_attack = results_dict['adv']['white_box'][attack]
if 'eps' in attack_dict and \
isinstance(attack_dict['eps'], list):
epsilons = attack_dict['eps']
if 'eps_iter' in attack_dict:
epsilons_iter = attack_dict['eps_iter']
else:
epsilons_iter = [None] * len(epsilons)
for eps, eps_iter in zip(epsilons, epsilons_iter):
results_dict_attack.update({eps: {}})
attack_dict['eps'] = eps
if eps_iter:
attack_dict['eps_iter'] = eps_iter
results_dict_attack[eps] = test_adv(
images_adv, labels_adv, batch_size, model,
adv_model, test_config['adv']['daug_params'],
attack_dict)
attack_dict['eps'] = epsilons
if 'eps_iter' in attack_dict:
attack_dict['eps_iter'] = epsilons_iter
else:
results_dict_attack = test_adv(
images_adv, labels_adv, batch_size, model, adv_model,
test_config['adv']['daug_params'],
attack_dict)
# Black box attack
if test_config['adv']['black_box_model']:
adv_model = load_model(test_config['adv']['black_box_model'])
results_dict['adv'].update({'black_box': {}})
for attack, attack_dict in test_config['adv']['attacks'].items():
print('\nComputing black box adversarial robustness '
'towards {}'.format(attack))
results_dict['adv']['black_box'].update({attack: {}})
results_dict_attack = results_dict['adv']['black_box'][attack]
if 'eps' in attack_dict and \
isinstance(attack_dict['eps'], list):
epsilons = attack_dict['eps']
if 'eps_iter' in attack_dict:
epsilons_iter = attack_dict['eps_iter']
else:
epsilons_iter = [None] * len(epsilons)
for eps, eps_iter in zip(epsilons, epsilons_iter):
results_dict_attack.update({eps: {}})
attack_dict['eps'] = eps
if eps_iter:
attack_dict['eps_iter'] = eps_iter
results_dict_attack[eps] = test_adv(
images_adv, labels_adv, batch_size, model,
adv_model, test_config['adv']['daug_params'],
attack_dict)
attack_dict['eps'] = epsilons
if 'eps_iter' in attack_dict:
attack_dict['eps_iter'] = epsilons_iter
else:
results_dict_attack = test_adv(
images_adv, labels_adv, batch_size, model,
adv_model, test_config['adv']['daug_params'],
attack_dict)
else:
aux_hdf5 = []
# Compute norms and metrics from the activations
if 'activations' in test_config:
print('\nComputing metrics related to the activations')
results_dict.update({'activations': {}})
results_dict['activations'] = activations(
images_tt, labels_tt, batch_size, model,
test_config['activations']['layer_regex'],
test_config['activations']['nodaug_params'],
test_config['activations']['daug_params'],
test_config['activations']['include_input'],
test_config['activations']['class_invariance'],
test_config['activations']['n_daug_rep'],
test_config['activations']['norms'])
for f in aux_hdf5:
filename = f.filename
f.close()
os.remove(filename)
return results_dict
def test_rep(images, labels, batch_size, model, daug_params, repetitions,
metrics=['accuracy']):
"""
Tests the performance of a model on a set of images, transformed according
to the specified augmentation parameters, and computes statistics over a
number of repetitions.
Parameters
----------
images : h5py Dataset
The set of images
labels : h5py Dataset
The ground truth labels
batch_size : int
Batch size
model : Keras Model
The model
daug_params : dict
Dictionary of data augmentation parameters
repetitions : int
Number of data augmentation repetitions
Returns
-------
results_dict : dict
Dictionary containing some performance metrics
"""
n_images = images.shape[0]
n_classes = labels.shape[1]
n_batches_per_epoch = int(np.ceil(float(n_images) / batch_size))
# Create batch generator
image_gen = get_generator(images, **daug_params)
batch_gen = batch_generator(image_gen, images, labels, batch_size,
aug_per_im=1, shuffle=False)
# Initialize matrix to store the predictions.
predictions = np.zeros([n_images, n_classes, repetitions])
# Iterate over the random repetitions
for r in range(repetitions):
print('Run %d/%d' % (r+1, repetitions))
init = 0
batch_gen.image_gen.reset()
# Iterate over the whole data set batch by batch
for _ in tqdm(range(n_batches_per_epoch)):
batch_images, _ = next(batch_gen())
batch_size = batch_images.shape[0]
end = init + batch_size
predictions[init:end, :, r] = \
model.predict_on_batch(batch_images)
init = end
results_dict = _stats_from_pred(predictions, labels, metrics)
return results_dict
def test_ablation(images, labels, batch_size, model, daug_params, repetitions,
layer_regex, ablation_pct, seed=None, metrics=None):
"""
Tests the performance, as in test_rep(), of an ablated model.
Parameters
----------
images : h5py Dataset
The set of images
labels : h5py Dataset
The ground truth labels
batch_size : int
Batch size
model : Keras Model
The model
daug_params : dict
Dictionary of data augmentation parameters
repetitions : int
Number of data augmentation repetitions
Returns
-------
results_dict : dict
Dictionary containing some performance metrics
See
---
test_rep()
ablate_activations()
"""
network_dict = network2dict(model)
# Perform ablation (drop a set of the units)
model_ablation = ablate_activations(model, layer_regex, ablation_pct, seed)
results_dict = {}
for r in range(repetitions):
rep_dict = test_rep(images, labels, batch_size, model_ablation,
daug_params, 1, metrics)
results_dict.update({r: rep_dict})
results_dict = _stats_from_ablation_rep(results_dict)
model = restore_nodes(model, network_dict)
del model_ablation
return results_dict
def test_adv(images, labels, batch_size, model, adv_model, daug_params,
attack_params):
"""
Tests the performance of a model on adversarial images. The adversarial
images are computed according to the attack specified in the arguments.
Parameters
----------
images : dask array
The set of images
labels : dask array
The ground truth labels
batch_size : int
Batch size
model : Keras Model
The model
adv_model : Keras Model
The model used to generate adversarial examples
daug_params : dict
Dictionary of data augmentation parameters
attack_params : dict
Dictionary of the attack parameters
Returns
-------
results_dict : dict
Dictionary containing some performance metrics
"""
# Get session
sess = K.get_session()
# Initialize adversarial attack
attack, attack_params_cleverhans, bs = init_attack(
adv_model, attack_params, sess)
if bs:
batch_size = bs
n_images = images.shape[0]
n_classes = labels.shape[1]
n_batches_per_epoch = int(np.ceil(float(n_images) / batch_size))
# Create batch generator
image_gen = get_generator(images, **daug_params)
batch_gen = batch_generator(image_gen, images, labels, batch_size,
aug_per_im=1, shuffle=False)
# Define input TF placeholder
if daug_params['crop_size']:
image_shape = daug_params['crop_size']
else:
image_shape = images.shape[1:]
x = tf.placeholder(K.floatx(), shape=(bs,) + tuple(image_shape))
y = tf.placeholder(K.floatx(), shape=(bs,) + (n_classes,))
# Define adversarial predictions symbolically
x_adv = attack.generate(x, **attack_params_cleverhans)
x_adv = tf.stop_gradient(x_adv)
predictions_adv = model(x_adv)
# Define accuracy and mean squared error symbolically
correct_preds = tf.equal(tf.argmax(y, axis=-1),
tf.argmax(predictions_adv, axis=-1))
acc_value = tf.reduce_mean(tf.to_float(correct_preds))
mse_value = tf.reduce_mean(tf.square(tf.subtract(x, x_adv)))
# Init results variables
accuracy = 0.0
mse = 0.0
with sess.as_default():
init = 0
for _ in tqdm(range(n_batches_per_epoch)):
batch = next(batch_gen())
this_batch_size = batch[0].shape[0]
# Evaluate accuracy
if isinstance(batch[1], (list, )):
yy = batch[1][0]
else:
yy = batch[1]
# Evaluate accuracy and MSE
batch_acc = acc_value.eval(feed_dict={x: batch[0], y: yy,
K.learning_phase(): 0})
accuracy += (this_batch_size * batch_acc)
batch_mse = mse_value.eval(feed_dict={x: batch[0],
K.learning_phase(): 0})
mse += (this_batch_size * batch_mse)
init += this_batch_size
accuracy /= n_images
mse /= n_images
results_dict = {'mean_acc': accuracy,
'mean_mse': mse}
return results_dict
def activations_norm(images, labels, batch_size, model, layer_regex,
daug_params, norms=['fro']):
"""
Computes the norm of the activation of all feature maps
Parameters
----------
images : h5py Dataset
The set of images
labels : h5py Dataset
The ground truth labels
batch_size : int
Batch size
model : Keras Model
The model
daug_params : dict
Dictionary of data augmentation parameters
Returns
-------
results_dict : dict
Dictionary containing some performance metrics
"""
def _update_stats(mean_norm, std_norm, norm):
mean_norm_batch = np.mean(norm, axis=0)
std_norm_batch = np.std(norm, axis=0)
mean_norm = init / float(end) * mean_norm + \
batch_size / float(end) * mean_norm_batch
std_norm = init / float(end) * std_norm ** 2 + \
batch_size / float(end) * std_norm_batch ** 2 + \
(init * batch_size) / float(end ** 2) * \
(mean_norm - mean_norm_batch) ** 2
std_norm = np.sqrt(std_norm)
return mean_norm, std_norm
def _frobenius_norm(activations):
norm = np.linalg.norm(
activations, ord='fro',
axis=tuple(range(1, len(activations.shape) - 1)))
return norm
def _inf_norm(activations):
norm = np.max(np.abs(activations),
axis=tuple(range(1, len(activations.shape) - 1)))
return norm
n_images = images.shape[0]
n_batches_per_epoch = int(np.ceil(float(n_images) / batch_size))
# Create batch generator
image_gen = get_generator(images, **daug_params)
batch_gen = batch_generator(image_gen, images, labels, batch_size,
aug_per_im=1, shuffle=False)
# Initialize list to store the mean norm of the activations
results_dict = {'activations_norm': {}, 'summary': {}}
# Iterate over the layers
model = del_extra_nodes(model)
for layer in model.layers:
if re.match(layer_regex, layer.name):
layer_name = layer.name.encode('utf-8')
print('\nLayer {}'.format(layer_name))
output = model.get_layer(layer_name)\
.outbound_nodes[0].input_tensors[0]
get_output = K.function([model.input, K.learning_phase()],
[output])
n_channels = K.int_shape(output)[-1]
results_dict['activations_norm'].update({layer_name:
{n: {'mean': np.zeros(n_channels),
'std': np.zeros(n_channels)} for n in norms}})
layer_dict = results_dict['activations_norm'][layer_name]
init = 0
batch_gen.image_gen.reset()
for _ in tqdm(range(n_batches_per_epoch)):
batch_images, _ = next(batch_gen())
batch_size = batch_images.shape[0]
end = init + batch_size
activations = get_output([batch_images, 0])[0]
for norm_key in norms:
mean_norm = layer_dict[norm_key]['mean']
std_norm = layer_dict[norm_key]['std']
if norm_key == 'fro':
norm = _frobenius_norm(activations)
elif norm_key == 'inf':
norm = _inf_norm(activations)
else:
raise NotImplementedError('Implemented norms are fro '
'and inf')
mean_norm, std_norm = _update_stats(mean_norm, std_norm,
norm)
layer_dict[norm_key]['mean'] = mean_norm
layer_dict[norm_key]['std'] = std_norm
init = end
# Compute summary statistics across the channels
for layer, layer_dict in results_dict['activations_norm'].items():
results_dict['summary'].update({layer: {}})
for norm_key, norm_dict in layer_dict.items():
results_dict['summary'][layer].update({norm_key: {
'mean': np.mean(norm_dict['mean']),
'std': np.mean(norm_dict['std'])}})
return results_dict
def activations(images, labels, batch_size, model, layer_regex, nodaug_params,
daug_params, include_input=False, class_invariance=False,
n_daug_rep=0, norms=['fro']):
"""
Computes metrics from the activations, such as the norm of the feature
maps, data augmentation invariance, class invariance, etc.
Parameters
----------
images : h5py Dataset
The set of images
labels : h5py Dataset
The ground truth labels
batch_size : int
Batch size
model : Keras Model
The model
nodaug_params : dict
Dictionary of data augmentation parameters for the baseline
daug_params : dict
Dictionary of data augmentation parameters
include_input : bool
If True, the input layer is considered for the analysis
class_invariance : bool
If True, the class invariance score is computed
n_daug_rep : int
If larger than 0, the data augentation invariance score is computed,
performing n_daug_rep repetitions of random augmentations
norms : list
List of keywords to specify the types of norms to compute on the
activations
Returns
-------
results_dict : dict
Dictionary containing some performance metrics
"""
def _update_stats(mean_norm, std_norm, norm):
mean_norm_batch = np.mean(norm, axis=0)
std_norm_batch = np.std(norm, axis=0)
mean_norm = init / float(end) * mean_norm + \
batch_size / float(end) * mean_norm_batch
std_norm = init / float(end) * std_norm ** 2 + \
batch_size / float(end) * std_norm_batch ** 2 + \
(init * batch_size) / float(end ** 2) * \
(mean_norm - mean_norm_batch) ** 2
std_norm = np.sqrt(std_norm)
return mean_norm, std_norm
def _frobenius_norm(activations):
norm = np.linalg.norm(
activations, ord='fro',
axis=tuple(range(1, len(activations.shape) - 1)))
return norm
def _inf_norm(activations):
norm = np.max(np.abs(activations),
axis=tuple(range(1, len(activations.shape) - 1)))
return norm
model = del_extra_nodes(model)
n_images = images.shape[0]
n_batches_per_epoch = int(np.ceil(float(n_images) / batch_size))
# Get relevant layers
if include_input:
layer_regex = '({}|.*input.*)'.format(layer_regex)
else:
layer_regex = layer_regex
layers = [layer.name for layer in model.layers
if re.compile(layer_regex).match(layer.name)]
# Initialize HDF5 to store the activations
# filename = 'hdf5_aux_{}'.format(time.time())
# activations_hdf5_aux = h5py.File(filename, 'w')
# hdf5_aux = [filename]
#
# grp_activations = activations_hdf5_aux.create_group('activations')
if class_invariance:
# grp_labels = activations_hdf5_aux.create_group('labels')
labels_true_da = []
labels_pred_da = []
predictions_da = []
# labels_true = grp_labels.create_dataset(
# 'labels_true', shape=(n_images, ), dtype=np.uint8)
# labels_pred = grp_labels.create_dataset(
# 'labels_pred', shape=(n_images, ), dtype=np.uint8)
# predictions = grp_labels.create_dataset(
# 'predictions', shape=labels.shape, dtype=K.floatx())
idx_softmax = model.output_names.index('softmax')
store_labels = True
else:
store_labels = False
# Initialize results dictionary
results_dict = {'activations_norm': {}, 'summary': {},
'class_invariance': {}, 'daug_invariance': {}}
# Iterate over the layers
for layer_name in layers:
# Create batch generator
image_gen = get_generator(images, **nodaug_params)
batch_gen = generate_batches(image_gen, images, labels, batch_size,
aug_per_im=1, shuffle=False)
layer = model.get_layer(layer_name)
layer_shape = layer.output_shape[1:]
n_channels = layer_shape[-1]
if re.compile('.*input.*').match(layer_name):
layer_name = 'input'
print('\nLayer {}\n'.format(layer_name))
# Create a Dataset for the activations of the layer
# activations_layer = grp_activations.create_dataset(
# layer_name, shape=(n_images, ) + layer_shape,
# dtype=K.floatx())
# Create dask array for the activations of the layer
activations_layer_da = []
# Initialize placeholders in the results dict for the layer
results_dict['activations_norm'].update({layer_name:
{n: {'mean': np.zeros(n_channels),
'std': np.zeros(n_channels)} for n in norms}})
layer_dict = results_dict['activations_norm'][layer_name]
activation_function = K.function([model.input,
K.learning_phase()],
[layer.output])
# Iterate over the data set in batches
init = 0
for batch_images, batch_labels in tqdm(
batch_gen, total=n_batches_per_epoch):
batch_size = batch_images.shape[0]
end = init + batch_size
# Store labels
if store_labels:
preds = model.predict_on_batch(batch_images)
if isinstance(preds, list):
preds = preds[idx_softmax]
labels_pred_da.append(da.from_array(
np.argmax(preds, axis=1)))
labels_true_da.append(da.from_array(
np.argmax(batch_labels, axis=1)))
predictions_da.append(da.from_array(preds))
# labels_pred[init:end] = np.argmax(preds, axis=1)
# labels_true[init:end] = np.argmax(batch_labels, axis=1)
# predictions[init:end, :] = preds
# Get and store activations
activations = activation_function([batch_images, 0])[0]
activations_layer_da.append(da.from_array(
activations, chunks=activations.shape))
# activations_layer[init:end] = activations
# Compute norms
for norm_key in norms:
mean_norm = layer_dict[norm_key]['mean']
std_norm = layer_dict[norm_key]['std']
if norm_key == 'fro':
norm = _frobenius_norm(activations)
elif norm_key == 'inf':
norm = _inf_norm(activations)
else:
raise NotImplementedError('Implemented norms are fro '
'and inf')
mean_norm, std_norm = _update_stats(mean_norm, std_norm,
norm)
layer_dict[norm_key]['mean'] = mean_norm
layer_dict[norm_key]['std'] = std_norm
init = end
if init == n_images:
store_labels = False
break
# Concatenate dask arrays
activations_layer_da = da.concatenate(activations_layer_da, axis=0)
activations_layer_da = activations_layer_da.reshape((n_images, -1))
d_activations = activations_layer_da.shape[-1]
if class_invariance:
print('\nComputing class invariance\n')
labels_pred_da = da.concatenate(labels_pred_da)
labels_true_da = da.concatenate(labels_true_da)
predictions_da = da.concatenate(predictions_da)
n_classes = len(np.unique(labels_true_da))
# Compute MSE matrix of the activations
r = da.reshape(da.sum(da.square(activations_layer_da),
axis=1), (-1, 1))
mse_matrix_da = (r - 2 * da.dot(activations_layer_da,
da.transpose(activations_layer_da)) \
+ da.transpose(r)) / d_activations
mse_matrix_da = mse_matrix_da.rechunk((mse_matrix_da.chunksize[0],
mse_matrix_da.shape[-1]))
# Compute class invariance
time0 = time()
results_dict['class_invariance'].update({layer_name: {}})
class_invariance_scores_da = []
if class_invariance:
# mse_matrix_mean = da.mean(mse_matrix_da).compute()
for cl in tqdm(range(n_classes)):
labels_cl = labels_pred_da == cl
labels_cl = labels_cl.compute()
mse_class = mse_matrix_da[labels_cl, :][:, labels_cl]
mse_class = mse_class.rechunk((-1, -1))
# mse_class_mean = da.mean(mse_class).compute()
# class_invariance_score = 1. - np.divide(
# mse_class_mean, mse_matrix_mean)
# results_dict['class_invariance'][layer_name].update(
# {cl: class_invariance_score})
class_invariance_scores_da.append(
1. - da.divide(da.mean(mse_class),
da.mean(mse_matrix_da)))
# Compute data augmentation invariance
print('\nComputing data augmentation invariance\n')
mse_daug_da = []
results_dict['daug_invariance'].update({layer_name: {}})
for r in range(n_daug_rep):
print('Repetition {}'.format(r))
image_gen_daug = get_generator(images, **daug_params)
batch_gen_daug = generate_batches(image_gen_daug, images, labels,
batch_size, aug_per_im=1,
shuffle=False)
activations_layer_daug_da = []
# Iterate over the data set in batches to compute activations
init = 0
for batch_images, batch_labels in tqdm(
batch_gen, total=n_batches_per_epoch):
batch_size = batch_images.shape[0]
end = init + batch_size
# Get and store activations
activations = activation_function([batch_images, 0])[0]
activations_layer_daug_da.append(da.from_array(
activations, chunks=activations.shape))
init = end
if init == n_images:
break
activations_layer_daug_da = da.concatenate(
activations_layer_daug_da, axis=0)
activations_layer_daug_da = activations_layer_daug_da.reshape(
(n_images, -1))
activations_layer_daug_da = activations_layer_daug_da.rechunk(
(activations_layer_daug_da.chunksize[0],
activations_layer_daug_da.shape[-1]))
# Compute MSE daug
mse_daug_da.append(da.mean(da.square(activations_layer_da - \
activations_layer_daug_da),
axis=1))
mse_daug_da = da.stack(mse_daug_da, axis=1)
mse_sum = da.repeat(da.reshape(da.sum(mse_matrix_da, axis=1),
(n_images, 1)), n_daug_rep, axis=1)
daug_invariance_score_da = 1 - n_images * da.divide(mse_daug_da, mse_sum)
time1 = time()
# Compute dask results and update results dict
results_dask = da.compute(class_invariance_scores_da,
daug_invariance_score_da)
time2 = time()
results_dict['class_invariance'][layer_name].update(
{cl: cl_inv_score
for cl, cl_inv_score in enumerate(results_dask[0])})
results_dict['daug_invariance'].update({layer_name:
{r: daug_inv_score
for r, daug_inv_score in enumerate(results_dask[1].T)}})
# Compute summary statistics of the norms across the channels
for layer, layer_dict in results_dict['activations_norm'].items():
results_dict['summary'].update({layer: {}})
for norm_key, norm_dict in layer_dict.items():
results_dict['summary'][layer].update({norm_key: {
'mean': np.mean(norm_dict['mean']),
'std': np.mean(norm_dict['std'])}})
return results_dict
def _stats_from_pred(predictions, labels, metrics):
"""
Computes the accuracy of the mean and the median of a set of predictions,
obtained from performing random data augmentation. Besides the accuracy,
additional metrics can be specified as an argument.
Parameters
----------
predictions : ndarray
The predictions over the data set, with shape [n_data, n_classes, rep]
labels : h5py Dataset
The ground truth labels
metrics : str list
List of metrics to compute, besides the accuracy
Returns
-------
results_dict : dict
Dictionary containing the performance metrics
"""
mean_predictions = np.mean(predictions, axis=2)
median_predictions = np.median(predictions, axis=2)
mean_std_predictions = np.mean(np.std(predictions, axis=2))