-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_new.py
977 lines (781 loc) · 48.1 KB
/
train_new.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
"""train_new.py
Usage:
train_new.py <f_model_config> <f_opt_config> <dataset> [--dont-save] [--db] [--cuda] [--test] [--mc_dropout_passes=<passes>] [--apd_gan=<gan_config>] [--apd=<apd_config>] [--prefix=<p>]
train_new.py <f_model_config> <f_opt_config> <dataset> [--dont-save] [--db] [--cuda] [--test] [--mc_dropout_passes=<passes>] [--prefix=<p>]
train_new.py -r <exp_name> <idx> [--test]
Options:
--mc_dropout_passes=<passes> The number of MC dropout passes to perform at test time. If 0, don't use MC dropout. [default: 0].
--prefix=<p> Prefix for experiment name [default:]
--apd=<gan_config> if using online apd (e.g. opt/gan-config/babymnist-dcgan.yaml) [default:]
Arguments:
Example:
python train_new.py model/config/fc1-mnist-100.yaml opt/config/sgld-mnist-1-1.yaml mnist-50000 --cuda
"""
import matplotlib as mtl
mtl.use('Agg')
import os
import pdb
import copy
import yaml
import shutil
import datetime
import pickle as pkl
# import tabulate
from docopt import docopt
from tensorboard_monitor.configuration import *
from tensorboard_monitor.monitor import *
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
# from tqdm import tqdm
import pickle as pickle
# Local imports
import utils
from opt.loss import *
from model.fc import *
from model.cnn import *
from opt.nsgd import NoisedSGD
torch.backends.cudnn.enabled = False
## GAN related
import gan_utils
from gan_pytorch import *
def load_configuration(arguments, name_dataset):
if arguments['-r']:
exp_name = arguments['<exp_name>']
f_model_config = 'model/config/' + exp_name[exp_name.find(':') + 1:].split('-X-')[0] + '.yaml'
f_opt_config = 'opt/config/' + exp_name[exp_name.find(':') + 1:].split('-X-')[1] + '.yaml'
old_exp_name = exp_name
exp_name += '_resumed'
else:
f_model_config = arguments['<f_model_config>']
f_opt_config = arguments['<f_opt_config>']
model_name = os.path.basename(f_model_config).split('.')[0]
opt_name = os.path.basename(f_opt_config).split('.')[0]
timestamp = '{:%Y-%m-%d}'.format(datetime.datetime.now())
data_name = name_dataset
if arguments['--prefix'] :
exp_name = '%s:%s-X-%s-X-%s@%s' % (arguments['--prefix'], model_name, opt_name, data_name, timestamp)
else:
exp_name = '%s-X-%s-X-%s@%s' % (model_name, opt_name, data_name, timestamp)
model_config = yaml.load(open(f_model_config, 'rb'))
opt_config = yaml.load(open(f_opt_config, 'rb'))
print('\n\n\n\n>>>>>>>>> [Experiment Name]')
print(exp_name)
print('<<<<<<<<<\n\n\n\n')
## Experiment stuff
# create exp dir and copy the configs over
exp_dir_name = './saves/{}'.format(exp_name)
if not os.path.exists(exp_dir_name):
os.makedirs(exp_dir_name)
model_config_fname = os.path.basename(f_model_config)
opt_config_fname = os.path.basename(f_opt_config)
shutil.copy(f_model_config, os.path.join(exp_dir_name, model_config_fname))
shutil.copy(f_opt_config, os.path.join(exp_dir_name, opt_config_fname))
## Model
model = eval(model_config['name'])(**model_config['kwargs'])
model = utils.cuda(model, arguments)
## Optimizer
opt = eval(opt_config['name'])(model.parameters(), **opt_config['kwargs'])
Loss = CE()
if arguments['-r']:
model.load('./saves/%s/model_%s.t7' % (old_exp_name, arguments['<idx>']))
opt.load_state_dict(torch.load('./saves/%s/opt_%s.t7' % (old_exp_name, arguments['<idx>'])))
if arguments['--test']:
raise NotImplementedError()
# exp_name = 'cifar-cnn-globe-X-sgld-cifar5-X-cifar5-20000@2018-02-06'
return model, opt, Loss, exp_name, model_config, opt_config
def update_LearningRate(optimizer, iteration, opt_config):
sd = optimizer.state_dict()
learning_rate = sd['param_groups'][0]['lr']
if 'lrsche' in opt_config and opt_config['lrsche'] != [] and opt_config['lrsche'][0][0] == iteration:
raise ("I didn't not support LR schedule after multi-chain MCMC")
_, tmp_fac = opt_config['lrsche'].pop(0)
sd = optimizer.state_dict()
assert len(sd['param_groups']) == 1
sd['param_groups'][0]['lr'] *= tmp_fac
optimizer.load_state_dict(sd)
if iteration > 0 and 'lrpoly' in opt_config:
raise ("I didn't not support LR schedule after multi-chain MCMC")
a, b, g = opt_config['lrpoly']
sd = optimizer.state_dict()
step_size = a * ((b + iteration) ** (-g))
sd['param_groups'][0]['lr'] = step_size
optimizer.load_state_dict(sd)
return learning_rate
def check_point(model, optimizer, iteration, exp_name):
if iteration > 0 and iteration % 1000 == 0:
name = './saves/%s/model_%i.t7' % (exp_name, iteration)
print("[Saving to] {}".format(name))
torch.save(model.state_dict(), name)
torch.save(optimizer.state_dict(), './saves/%s/opt_%i.t7' % (exp_name, iteration))
def posterior_sampling(sample_size, model, learning_rate):
model.cpu()
model.posterior_samples.append(copy.deepcopy(model.state_dict()))
model.cuda()
model.posterior_weights.append(learning_rate)
if len(model.posterior_samples) > sample_size:
del model.posterior_samples[0]
del model.posterior_weights[0]
#########################
### Anomaly Detection ###
#########################
def run_test_mc_dropout(model, dataloader, arguments):
# To be used on val and test sets
was_training = model.training
model.train() # Important: keep train mode on for MC dropout
mc_posterior_accuracy_list = []
mc_posterior_loss_list = []
for data in dataloader:
inputs, labels = data
inputs = utils.cuda(inputs, arguments)
inputs, labels = Variable(inputs, volatile=True), Variable(labels, volatile=True)
mean_probs = utils.mc_dropout_expectation(model, inputs, keep_samples=False, passes=int(arguments['--mc_dropout_passes']))
mean_predictions = mean_probs.data.cpu().numpy().argmax(1)
mc_posterior_accuracy_batch = utils.inference_accuracy(mean_predictions, labels)
mc_posterior_loss_batch = F.nll_loss(torch.log(mean_probs.cpu()), labels)
mc_posterior_accuracy_list.append(mc_posterior_accuracy_batch)
mc_posterior_loss_list.append(mc_posterior_loss_batch.data.cpu().numpy())
mc_posterior_accuracy = np.mean(mc_posterior_accuracy_list)
mc_posterior_loss = np.mean(mc_posterior_loss_list)
if not was_training:
model.eval()
return mc_posterior_accuracy, mc_posterior_loss.data[0]
def get_dataloader(task, batch_size):
size_trainset = int(task.split('-')[1])
name_dataset = task.split('-')[0]
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
if name_dataset == 'babymnist':
# Train set
trainset = BabyMnist(train=True, transform=transform)
trainset.train_data = trainset.train_data[:size_trainset, :, :, :]
trainset.train_labels = trainset.train_labels[:size_trainset]
# Validation set
valset = BabyMnist(train=True, transform=transform)
valset.train_data = valset.train_data[size_trainset:, :, :, :]
valset.train_labels = valset.train_labels[size_trainset:]
# Test set
testset = BabyMnist(train=False, transform=transform)
elif name_dataset == 'mnist':
# Train set
trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
trainset.train_data = trainset.train_data[:size_trainset, :, :]
trainset.train_labels = trainset.train_labels[:size_trainset]
# Validation set
valset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform)
valset.train_data = valset.train_data[size_trainset:, :, :]
valset.train_labels = valset.train_labels[size_trainset:]
# Test set
testset = torchvision.datasets.MNIST(root='./data', train=False, download=True, transform=transform)
elif name_dataset == 'fashion':
# Train set
trainset = torchvision.datasets.FashionMNIST(root='./data', train=True, download=True, transform=transform)
trainset.train_data = trainset.train_data[:size_trainset, :, :]
trainset.train_labels = trainset.train_labels[:size_trainset]
# Validation set
valset = torchvision.datasets.FashionMNIST(root='./data', train=True, download=True, transform=transform)
valset.train_data = valset.train_data[size_trainset:, :, :]
valset.train_labels = valset.train_labels[size_trainset:]
# Test set
testset = torchvision.datasets.FashionMNIST(root='./data', train=False, download=True, transform=transform)
elif name_dataset == 'cifar10':
# Train set
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainset.train_data = trainset.train_data[:size_trainset, :, :, :]
trainset.train_labels = trainset.train_labels[:size_trainset]
# Validation set
valset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
valset.train_data = valset.train_data[size_trainset:, :, :]
valset.train_labels = valset.train_labels[size_trainset:]
# Test set
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=2)
valloader = torch.utils.data.DataLoader(valset, batch_size=batch_size, shuffle=False, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=2)
return trainloader, valloader, testloader, name_dataset
def evaluate(model, testloader, posterior_flag, Loss, opt_config):
model.eval()
posterior_weights = model.posterior_weights
posterior_samples = model.posterior_samples
point_accuracy = []
point_loss = []
posterior_accuracy = []
posterior_loss = []
for i, data in enumerate(testloader, 0):
# Load data
# data for inference
test_inputs, test_labels = data
test_inputs, test_labels = utils.cuda((test_inputs, test_labels), arguments)
test_inputs, test_labels = Variable(test_inputs, volatile=True), Variable(test_labels, volatile=True)
# Prediction
# Point Prediction
point_outputs = model.forward(test_inputs)
point_loss_batch = F.cross_entropy(point_outputs, test_labels)
point_loss.append(point_loss_batch.data[0])
point_predictions = Loss.inference_prediction(point_outputs)
point_accuracy_batch = utils.inference_accuracy(point_predictions, test_labels)
point_accuracy.append(point_accuracy_batch)
# Bayesian Prediction
if posterior_flag:
# posterior_outputs = utils.posterior_expectation(model, test_inputs)
posterior_outputs = utils.posterior_expectation(model, test_inputs, keep_samples=False, use_mini_batch=opt_config['batch_size'])
# posterior_loss_batch = Loss.nll(torch.log(posterior_outputs), test_labels)
posterior_loss_batch = F.nll_loss(torch.log(posterior_outputs), test_labels.cpu())
posterior_loss.append(posterior_loss_batch.data[0])
posterior_predictions = Loss.inference_prediction(posterior_outputs)
posterior_accuracy_batch = utils.inference_accuracy(posterior_predictions, test_labels)
posterior_accuracy.append(posterior_accuracy_batch)
model.train()
# record prediction result
point_accuracy = np.mean(point_accuracy)
point_loss = np.mean(point_loss)
if posterior_flag == 1:
posterior_accuracy = np.mean(posterior_accuracy)
posterior_loss = np.mean(posterior_loss)
return point_accuracy, point_loss, posterior_accuracy, posterior_loss
def _flatten_npyfy(posterior_samples):
ret = []
for sample in posterior_samples:
item = []
for seq in sample:
for p in seq.values():
item.append(p.cpu().numpy().ravel())
ret.append(item)
return np.array([np.concatenate(item) for item in ret])
def main(arguments):
default_config = {
'batcher_kwargs':{'batch_size': 100},
'burnin_iters': 500,
'sample_size': 10,
'sample_interval': 20,
'validation_interval': 2000,
'ood_scale': 5,
'variance_monitor_interval': 50,
'ood_datasets': ['notMNIST']
}
name_task = arguments['<dataset>']
# Load argumentts: Module, Optimizer, Loss_function
model, optimizer, Loss, exp_name, model_config, opt_config = load_configuration(arguments, name_task)
models = [model]
#####
#apd#
#####
if arguments['--apd']:
if not arguments['--apd_gan']:
raise Exception("specify a gan config")
####
#### GAN CONFIG
####
gan_config = gan_utils.opt_load_configuration(arguments['--apd_gan'], None)
### compute OUTPUT_DIM
posterior_sampling(1, model, 1)
gan_config['output_dim'] = _flatten_npyfy(model.posterior_samples).shape[1]
model.posterior_samples = []
model.posterior_weights = []
###
obj_traingan = TrainGAN(None, gan_config, model, [])
obj_traingan.init_gan()
###
gan_bs = gan_config['batch_size'] # Batch size
gan_inp_buffer = []
gan_iter = 0
####
#### apd CONFIG
####
apd_config = yaml.load(open(arguments['--apd'], 'rb'))
## if any of the following is 0, make it =gan_bs
apd_config['apd_buffer_size'] = apd_config['apd_buffer_size'] or gan_bs
apd_config['T_sgld'] = apd_config['T_sgld'] or gan_bs
apd_config['N_chains'] = apd_config['N_chains'] or gan_bs
if apd_config['N_chains'] > 1:
opts = [optimizer]
for _ in range(apd_config['N_chains'] - 1):
### TODO: play with model init...
model = eval(model_config['name'])(**model_config['kwargs'])
model = utils.cuda(model, arguments)
models.append(model)
###
opts.append(eval(opt_config['name'])(model.parameters(), **opt_config['kwargs']))
#####
#apd-
#####
if arguments['--test']:
num_max_iteration = 0 # Don't do any training iterations, just jump to the test code
else:
num_max_iteration = opt_config['max_train_iters']
# Fill in default configuration for keys that are not overwritten by the config file
for key in default_config:
if key not in opt_config:
opt_config[key] = default_config[key]
# Fill in default n_anom value, if not overwritten by the config file
if 'n_anom' not in opt_config:
if name_task.split('-')[0] in ['mnist', 'fashion']:
opt_config['n_anom'] = 2000
elif name_task.split('-')[0] == 'babymnist':
opt_config['n_anom'] = 300
batch_size = opt_config['batcher_kwargs']['batch_size']
burnin_iters = opt_config['burnin_iters']
sample_size = opt_config['sample_size']
sample_interval = opt_config['sample_interval']
validation_interval = opt_config['validation_interval']
ood_scale = opt_config['ood_scale']
variance_monitor_interval = opt_config['variance_monitor_interval']
iteration = 0
is_collecting = False
total_num_samples_collected = 0
log_folder = os.path.join('./logs', exp_name)
if not os.path.exists(log_folder):
os.makedirs(log_folder)
# To keep logs of the best results found during training
# output_log_file = open(os.path.join('output', exp_name + ".txt"), 'w')
best_test_point_acc = 0
best_test_mc_acc = 0
####################
### Load dataset ###
####################
# trainLoader automatically generate training_batch
trainloader, valloader, testloader, name_dataset = utils.get_dataloader(name_task, batch_size)
ood_data = utils.load_ood_data(name_dataset, opt_config)
# Monitors
monitor = Monitor()
for k in ['train_loss', 'point_loss', 'bayesian_loss', 'apd_loss' ,'mcdrop_loss',
'train_acc', 'point_acc', 'bayesian_acc', 'apd_acc' , 'mcdrop_acc',
'point_auroc', 'bayesian_auroc', 'apd_auroc','mcdrop_auroc',
'point_aupr+', 'bayesian_aupr+', 'apd_aupr+','mcdrop_aupr+',
'point_aupr-', 'bayesian_aupr-', 'apd_aupr-','mcdrop_aupr-',
'bbald_auroc','bbald_aupr+','bbald_aupr-',
'abald_auroc','abald_aupr+','abald_aupr-']: monitor.dict_of_traces[k] = []
################
### Training ###
################
# # At any point you can hit Ctrl+C to break out of training early, and proceed to run on test data.
try:
# for iteration in range(num_max_iteration):
while iteration < num_max_iteration:
for i, data in enumerate(trainloader, 0):
inputs, labels = utils.cuda(data, arguments)
inputs, labels = Variable(inputs), Variable(labels)
# Update learning rate
learning_rate = update_LearningRate(optimizer, iteration, opt_config)
# Inference
optimizer.zero_grad()
outputs = models[0].forward(inputs)
training_loss = F.cross_entropy(outputs, labels)
training_loss.backward() # Computes gradients of the model parameters wrt the loss
monitor.record_matplot(training_loss.data[0], iteration, 'train_loss')
prob_outputs = F.softmax(outputs)
training_predictions = prob_outputs.data.cpu().numpy().argmax(1)
accuracy = utils.inference_accuracy(training_predictions, labels)
monitor.record_matplot(accuracy, iteration, 'train_acc')
optimizer.step()
## run rest of the chains
if len(models) > 1:
for idx in range(1,len(models)):
opts[idx].zero_grad()
loss = F.cross_entropy(models[idx].forward(inputs), labels)
loss.backward()
opts[idx].step()
# Monitor variance
if (opt_config['name'] == 'NoisedSGD') and (is_collecting == False) and (iteration % variance_monitor_interval == 0):
is_collecting = iteration >= burnin_iters
# Sample MCMC
if (is_collecting == True) and (iteration % sample_interval == 0):
for model in models:
posterior_sampling(sample_size, model, learning_rate)
if len(model.posterior_samples) > 0 and opt_config['name'] == 'NoisedSGD':
posterior_flag = 1
else:
posterior_flag = 0
# Val-Set Evaluation
if (iteration % validation_interval == 0):
######################
### Classification ###
######################
print("Num posterior samples/Model: {}\nNum Chains: {}, Total: {}".\
format(len(model.posterior_samples), len(models), len(models)*len(model.posterior_samples) ))
# point_accuracy, point_loss, posterior_accuracy, posterior_loss = evaluate(models[1], valloader, posterior_flag, Loss)
point_accuracy, point_loss, posterior_accuracy, posterior_loss = evaluate(models[0], valloader, posterior_flag, Loss, opt_config)
monitor.record_matplot(point_accuracy, iteration, 'point_acc')
monitor.record_matplot(point_loss, iteration, 'point_loss')
if posterior_accuracy:
monitor.record_matplot(posterior_accuracy, iteration, 'bayesian_acc')
monitor.record_matplot(posterior_loss, iteration, 'bayesian_loss')
if posterior_accuracy:
print("It: {:5d} | Acc: {:.4f} | Point Acc: {:.4f} | Posterior Acc: {:.4f}".format(iteration, accuracy, point_accuracy, posterior_accuracy))
print("It: {:5d} | Loss: {:.4f} | Point Loss: {:.4f} | Posterior Loss: {:.4f}".format(iteration, training_loss.data[0], point_loss, posterior_loss))
sys.stdout.flush()
else:
print("It: {:5d} | Acc: {:.4f} | Point Acc: {:.4f}".format(iteration, accuracy, point_accuracy))
sys.stdout.flush()
##################
### MC-Dropout ###
##################
if arguments['--mc_dropout_passes'] and int(arguments['--mc_dropout_passes']) > 0:
test_mc_accuracy, test_mc_loss = run_test_mc_dropout(model, valloader, arguments)
monitor.record_matplot(test_mc_accuracy, iteration, 'mcdrop_acc')
monitor.record_matplot(test_mc_loss, iteration, 'mcdrop_loss')
print("MC-Dropout Val Accuracy: {:.4f}".format(test_mc_accuracy))
print("MC-Dropout Val Loss: {:.4f}".format(test_mc_loss))
sys.stdout.flush()
###########################
### Save Best Val Model ###
###########################
# Store the model with the best validation accuracy (either point-accuracy or MC-Dropout accuracy)
if arguments['--mc_dropout_passes'] and int(arguments['--mc_dropout_passes']) > 0 and test_mc_accuracy > best_test_mc_acc:
best_test_mc_acc = test_mc_accuracy
print('Saving model with best validation accuracy!')
torch.save(model.state_dict(), os.path.join('saves', exp_name, 'best_mc_model.th'))
elif point_accuracy > best_test_point_acc:
best_test_point_acc = point_accuracy
print('Saving model with best validation accuracy!')
torch.save(model.state_dict(), os.path.join('saves', exp_name, 'best_point_model.th'))
####################
### apd Evaluate ###
####################
if arguments['--apd']:
## create a new model instance to hold GAN samples
## Model
gmodel = eval(model_config['name'])(**model_config['kwargs'])
gmodel = utils.cuda(gmodel, arguments)
## get the samples
gmodel.posterior_samples = obj_traingan.get_samples(sample_size)
gmodel.posterior_weights = [1 for _ in range(len(gmodel.posterior_samples))]
## copied from above, TODO: loop
point_accuracy, point_loss, posterior_accuracy, posterior_loss = evaluate(gmodel, valloader, posterior_flag, Loss, opt_config)
# monitor.record_matplot(point_accuracy, iteration, 'point_acc')
# monitor.record_matplot(point_loss, iteration, 'point_loss')
if posterior_accuracy:
monitor.record_matplot(posterior_accuracy, gan_iter, 'apd_acc')
monitor.record_matplot(posterior_loss, gan_iter, 'apd_loss')
if posterior_accuracy:
print("GAN It: {:5d} | Acc: {:.4f} | Point Acc: {:.4f} | Posterior Acc: {:.4f}".format(gan_iter, accuracy, point_accuracy, posterior_accuracy))
else:
print("GAN It: {:5d} | Acc: {:.4f} | Point Acc: {:.4f}".format(gan_iter, accuracy, point_accuracy))
#########################
### Anomaly detection ###
#########################
test_inputs_anomaly_detection = utils.get_anomaly_detection_test_inputs(valloader, opt_config, arguments)
test_inputs_anomaly_detection = utils.cuda(test_inputs_anomaly_detection, arguments)
ood_data = utils.cuda(ood_data, arguments)
# # Bayesian
for ood_dataset_name in opt_config['ood_datasets']:
print("OOD Dataset: {}".format(ood_dataset_name))
cur_ood_data = ood_data[ood_dataset_name]
for func_name in opt_config['ood_acq_funcs']:
if func_name != 'f_bald':
# Non-bayesian
normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(
test_inputs_anomaly_detection, cur_ood_data, utils.sm_given_np_data, {'model': model}, f_acq=func_name)
print(
"(Non-Bayesian {}) Anomaly Detection Results: \nBase Rate: {:.2f}, AUROC: {:.2f}, AUPR+: {:.2f}, AUPR-: {:.2f}".format(
func_name, normality_base_rate, auroc, n_aupr, ab_aupr))
# Bayesian
if posterior_flag == 1:
print
for ood_dataset_name in opt_config['ood_datasets']:
print("OOD Dataset: {}".format(ood_dataset_name))
cur_ood_data = ood_data[ood_dataset_name]
for func_name in opt_config['ood_acq_funcs']:
print("Func name = {}".format(func_name))
if func_name == 'f_bald':
normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(test_inputs_anomaly_detection,
cur_ood_data,
utils.posterior_expectation,
{'model': model, 'keep_samples': True, 'use_mini_batch': opt_config['batch_size']},
f_acq='f_bald')
else:
normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(test_inputs_anomaly_detection,
cur_ood_data,
utils.posterior_expectation,
{'model': model, 'keep_samples': False, 'use_mini_batch': opt_config['batch_size']},
f_acq=func_name)
print("({}) Anomaly Detection Results: \nBase Rate: {:.2f}, AUROC: {:.2f}, AUPR+: {:.2f}, AUPR-: {:.2f}".format(func_name.upper(), normality_base_rate, auroc, n_aupr, ab_aupr))
# MC-Dropout
if arguments['--mc_dropout_passes'] and int(arguments['--mc_dropout_passes']) > 0:
print
for ood_dataset_name in opt_config['ood_datasets']:
print("OOD Dataset: {}".format(ood_dataset_name))
cur_ood_data = ood_data[ood_dataset_name]
for func_name in opt_config['ood_acq_funcs']:
if func_name == 'f_bald':
keep_samples = True
else:
keep_samples = False
normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(test_inputs_anomaly_detection,
cur_ood_data,
utils.mc_dropout_expectation,
{'model': model, 'passes': arguments['--mc_dropout_passes'], 'keep_samples': keep_samples},
f_acq=func_name)
print("({}) Anomaly Detection Results: \nBase Rate: {:.2f}, AUROC: {:.2f}, AUPR+: {:.2f}, AUPR-: {:.2f}".format(func_name.upper(), normality_base_rate, auroc, n_aupr, ab_aupr))
################
### apd Anom ###
################
if arguments['--apd']:
## create a new model instance to hold GAN samples
## Model
gmodel = eval(model_config['name'])(**model_config['kwargs'])
gmodel = utils.cuda(gmodel, arguments)
## get the samples
gmodel.posterior_samples = obj_traingan.get_samples(sample_size)
gmodel.posterior_weights = [1 for _ in range(len(gmodel.posterior_samples))]
## copied from above, TODO: loop
for ood_dataset_name in opt_config['ood_datasets']:
print("OOD Dataset: {}".format(ood_dataset_name))
cur_ood_data = ood_data[ood_dataset_name]
normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(test_inputs_anomaly_detection, cur_ood_data, utils.posterior_expectation,
{'model': gmodel, 'use_mini_batch': opt_config['batch_size']})
print(
"(apd) Anomaly Detection Results: \nBase Rate: {:.2f}, AUROC: {:.2f}, AUPR+: {:.2f}, AUPR-: {:.2f}".format(
normality_base_rate, auroc, n_aupr, ab_aupr))
monitor.record_matplot(auroc, gan_iter, 'apd_auroc')
monitor.record_matplot(n_aupr, gan_iter, 'apd_aupr+')
monitor.record_matplot(ab_aupr, gan_iter, 'apd_aupr-')
normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(test_inputs_anomaly_detection, cur_ood_data, utils.posterior_expectation,
{'model': gmodel,'keep_samples': True, 'use_mini_batch': opt_config['batch_size']}, f_acq='f_bald')
print(
"(apd BALD) Anomaly Detection Results: \nBase Rate: {:.2f}, AUROC: {:.2f}, AUPR+: {:.2f}, AUPR-: {:.2f}".format(
normality_base_rate, auroc, n_aupr, ab_aupr))
monitor.record_matplot(auroc, gan_iter, 'abald_auroc')
monitor.record_matplot(n_aupr, gan_iter, 'abald_aupr+')
monitor.record_matplot(ab_aupr, gan_iter, 'abald_aupr-')
print
if iteration > 0 and iteration % (sample_size*sample_interval) == 0:
if not arguments['--dont-save']:
# print("Saving {} samples...".format(sample_size))
print ("Saving samples to disc")
posterior_samples = []
for m in models: posterior_samples += m.posterior_samples
np_samples = _flatten_npyfy(posterior_samples)
np.save('./saves/{}/params_{:04d}'.format(exp_name, iteration//(sample_size*sample_interval)), np_samples)
total_num_samples_collected += np_samples.shape[0]
print("Total num samples collected: {}\n".format(total_num_samples_collected))
###
check_point(model, optimizer, iteration, exp_name)
monitor.save_result_numpy(log_folder)
## apd
if arguments['--apd'] and iteration > 0 and iteration % (apd_config['T_sgld']*sample_interval) == 0:
posterior_samples = []
for m in models: posterior_samples += m.posterior_samples[-apd_config['T_sgld']:]
np_samples = _flatten_npyfy(posterior_samples)
gan_inp_buffer += list(np_samples)
if len(gan_inp_buffer) > gan_bs:
for _ in range(apd_config['T_gan']):
idxs = np.arange(len(gan_inp_buffer))
np.random.shuffle(idxs)
obj_traingan.update_iter(gan_iter, np.array(gan_inp_buffer)[idxs[:gan_bs]])
gan_iter+=1
## remove old MCMC samples
while len(gan_inp_buffer)>apd_config['apd_buffer_size']: del gan_inp_buffer[0]
### infinite chains
if 'is_infinite_chains' in apd_config and apd_config['is_infinite_chains']:
sds = obj_traingan.get_samples(len(models))
for (idx, m) in enumerate(models):
m.load_state_dict(sds[idx])
iteration = iteration + 1
except (KeyboardInterrupt, ValueError): # The ValueError should catch NaNs
print('-' * 89)
print('Exiting from training early!')
##################################
### Termination -- Run on test ###
##################################
print('=' * 80)
print('Test')
print('=' * 80)
num_test_runs = opt_config['num_test_runs']
# # Prepare to store table results
# cls_table = []
# ad_headers = ['Dataset'] + ['ROC', 'PR(+)', 'PR(-)'] * 3
# ad_table = []
# table = [['Uniform', '96.99', '97.99', '94.71', '98.9', '99.15', '98.63', '98.97', '99.27', '98.52']]
# print(tabulate.tabulate(table, headers, tablefmt='latex_booktabs'))
###########################
### Test Classification ###
###########################
num_samples = opt_config['num_test_samples']
if opt_config['name'] == 'NoisedSGD':
posterior_flag = 1
else:
posterior_flag = 0
# If using NSGD or SGLD, load several samples from the saves directory and evaluate the expectation of their predictions
if opt_config['name'] == 'NoisedSGD':
# posterior_flag = 1
accuracy_list = []
for i in range(num_test_runs):
print("Test run {}".format(i))
posterior_samples = utils.load_posterior_state_dicts(src_dir=exp_name, example_model=model, num_samples=num_samples)
posterior_weights = [1 for _ in range(len(posterior_samples))]
model.posterior_samples = posterior_samples # Should change this structure
model.posterior_weights = posterior_weights
point_accuracy, point_loss, posterior_accuracy, posterior_loss = evaluate(model, testloader,posterior_flag, Loss, opt_config)
print("Posterior acc: {}".format(posterior_accuracy))
accuracy_list.append(posterior_accuracy)
print("Sampling Test Results")
print("---------------------")
print("Test Accuracy: Mean {}, Std {}\n".format(np.mean(accuracy_list), np.std(accuracy_list)))
#######################
### Test MC-Dropout ###
#######################
if arguments['--mc_dropout_passes'] and int(arguments['--mc_dropout_passes']) > 0:
# Re-load model with best val accuracy
best_model_state_dict = torch.load(os.path.join('saves', exp_name, 'best_mc_model.th'))
model.load_state_dict(best_model_state_dict)
mc_accuracy_list = []
for i in range(num_test_runs):
test_mc_accuracy, test_mc_loss = run_test_mc_dropout(model, testloader, arguments)
mc_accuracy_list.append(test_mc_accuracy)
print("MC-Dropout Test Results")
print("-----------------------")
print("Test Accuracy: Mean {}, Std {}\n".format(np.mean(mc_accuracy_list), np.std(mc_accuracy_list)))
best_model_state_dict = torch.load(os.path.join('saves', exp_name, 'best_point_model.th'))
model.load_state_dict(best_model_state_dict)
point_accuracy, point_loss, posterior_accuracy, posterior_loss = evaluate(model, testloader, posterior_flag, Loss, opt_config)
if posterior_accuracy:
print("Point Acc: {:.4f} | Posterior Acc: {:.4f}".format(point_accuracy, posterior_accuracy))
else:
print("Point Acc: {:.4f}".format(point_accuracy))
##############################
### Test Anomaly detection ###
##############################
anom_result_dict = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
for scale in opt_config['test_ood_scales']:
scale = int(scale)
opt_config['ood_scale'] = scale
ood_data = utils.load_ood_data(name_dataset, opt_config)
print("OOD SCALE {}".format(scale))
print("--------------------------")
test_inputs_anomaly_detection = utils.get_anomaly_detection_test_inputs(testloader, opt_config, arguments)
if arguments['--cuda']:
test_inputs_anomaly_detection = test_inputs_anomaly_detection.cuda()
for key in ood_data:
ood_data[key] = ood_data[key].cuda()
############################################
### Test Deterministic Anomaly Detection ###
############################################
# Load model with best val accuracy
best_model_state_dict = torch.load(os.path.join('saves', exp_name, 'best_point_model.th'))
model.load_state_dict(best_model_state_dict)
for ood_dataset_name in opt_config['ood_datasets']:
print("OOD Dataset: {}".format(ood_dataset_name))
cur_ood_data = ood_data[ood_dataset_name]
for func_name in opt_config['ood_acq_funcs']:
if func_name != 'f_bald':
# Non-bayesian
normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(
test_inputs_anomaly_detection, cur_ood_data, utils.sm_given_np_data, {'model': model}, f_acq=func_name)
print(
"({} Non-Bayesian) Anomaly Detection Results: \nBase Rate: {:.2f}, AUROC: {:.2f}, AUPR+: {:.2f}, AUPR-: {:.2f}".format(
func_name, normality_base_rate, auroc, n_aupr, ab_aupr))
anom_result_dict[scale][ood_dataset_name][func_name] = [auroc, n_aupr, ab_aupr]
# anomaly_detection_monitor.record_matplot([auroc, n_aupr, ab_aupr], iteration, 'point_estimation')
#################################################
### Test Bayesian (Sampled) Anomaly Detection ###
#################################################
if posterior_flag == 1:
print
for ood_dataset_name in opt_config['ood_datasets']:
print("OOD Dataset: {}".format(ood_dataset_name))
cur_ood_data = ood_data[ood_dataset_name]
for func_name in opt_config['ood_acq_funcs']:
normality_base_rate_list = []
auroc_list = []
n_aupr_list = []
ab_aupr_list = []
for i in range(num_test_runs):
posterior_samples = utils.load_posterior_state_dicts(src_dir=exp_name, example_model=model, num_samples=num_samples)
posterior_weights = [1 for _ in range(len(posterior_samples))]
model.posterior_samples = posterior_samples # Should change this structure
model.posterior_weights = posterior_weights
# normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(test_inputs_anomaly_detection, cur_ood_data, utils.posterior_expectation,
# # {'model': model})
# {'model': model, 'use_mini_batch': opt_config['batch_size']})
if func_name == 'f_bald':
normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(test_inputs_anomaly_detection,
cur_ood_data,
utils.posterior_expectation,
{'model': model, 'keep_samples': True, 'use_mini_batch': opt_config['batch_size']},
f_acq='f_bald')
else:
normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(test_inputs_anomaly_detection,
cur_ood_data,
utils.posterior_expectation,
{'model': model, 'keep_samples': False, 'use_mini_batch': opt_config['batch_size']},
f_acq=func_name)
normality_base_rate_list.append(normality_base_rate)
auroc_list.append(auroc)
n_aupr_list.append(n_aupr)
ab_aupr_list.append(ab_aupr)
print("({} Bayesian) Anomaly Detection Results: \nBase Rate: {:.2f}/{:.3f}, AUROC: {:.2f}/{:.3f}, AUPR+: {:.2f}/{:.3f}, AUPR-: {:.2f}/{:.3f}".format(
func_name,
np.mean(normality_base_rate_list), np.std(normality_base_rate_list),
np.mean(auroc_list), np.std(auroc_list),
np.mean(n_aupr_list), np.std(n_aupr_list),
np.mean(ab_aupr_list), np.std(ab_aupr_list)))
anom_result_dict[scale][ood_dataset_name][func_name] = [(np.mean(auroc_list), np.std(auroc_list)),
(np.mean(n_aupr_list), np.std(n_aupr_list)),
(np.mean(ab_aupr_list), np.std(ab_aupr_list))]
# anomaly_detection_monitor.record_matplot([auroc, n_aupr, ab_aupr], iteration, 'bayesian')
#########################################
### Test MC-Dropout Anomaly Detection ###
#########################################
if arguments['--mc_dropout_passes'] and int(arguments['--mc_dropout_passes']) > 0:
print
# Re-load model with best val accuracy
best_model_state_dict = torch.load(os.path.join('saves', exp_name, 'best_mc_model.th'))
model.load_state_dict(best_model_state_dict)
for ood_dataset_name in opt_config['ood_datasets']:
print("OOD Dataset: {}".format(ood_dataset_name))
cur_ood_data = ood_data[ood_dataset_name]
for func_name in opt_config['ood_acq_funcs']:
normality_base_rate_list = []
auroc_list = []
n_aupr_list = []
ab_aupr_list = []
for i in range(num_test_runs):
if func_name != 'f_bald':
normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(test_inputs_anomaly_detection,
cur_ood_data,
utils.mc_dropout_expectation,
{'model': model, 'passes': arguments['--mc_dropout_passes'], 'keep_samples': False},
f_acq=func_name)
# normality_base_rate, auroc, n_aupr, ab_aupr = utils.show_ood_detection_results_softmax(test_inputs_anomaly_detection, cur_ood_data, utils.mc_dropout_expectation,
# {'model': model, 'passes': arguments['--mc_dropout_passes']})
normality_base_rate_list.append(normality_base_rate)
auroc_list.append(auroc)
n_aupr_list.append(n_aupr)
ab_aupr_list.append(ab_aupr)
# print("({}) Anomaly Detection Results: \nBase Rate: {:.2f}, AUROC: {:.2f}, AUPR+: {:.2f}, AUPR-: {:.2f}".format(func_name.upper(), normality_base_rate, auroc, n_aupr, ab_aupr))
print("({} MC-Dropout) Anomaly Detection Results: \nBase Rate: {:.2f}/{:.3f}, AUROC: {:.2f}/{:.3f}, AUPR+: {:.2f}/{:.3f}, AUPR-: {:.2f}/{:.3f}".format(
func_name,
np.mean(normality_base_rate_list), np.std(normality_base_rate_list),
np.mean(auroc_list), np.std(auroc_list),
np.mean(n_aupr_list), np.std(n_aupr_list),
np.mean(ab_aupr_list), np.std(ab_aupr_list)))
anom_result_dict[scale][ood_dataset_name][func_name] = [(np.mean(auroc_list), np.std(auroc_list)),
(np.mean(n_aupr_list), np.std(n_aupr_list)),
(np.mean(ab_aupr_list), np.std(ab_aupr_list))]
for scale in opt_config['test_ood_scales']:
for ood_dataset_name in opt_config['ood_datasets']:
anom_result_dict[scale][ood_dataset_name] = dict(anom_result_dict[scale][ood_dataset_name])
anom_result_dict[scale] = dict(anom_result_dict[scale])
anom_result_dict = dict(anom_result_dict)
with open(os.path.join('saves', exp_name, 'anom_res_s:{}'.format(opt_config['num_test_samples'])), 'wb') as f:
pkl.dump(anom_result_dict, f)
print('-' * 89)
print('Experiment directory: {}'.format(os.path.join('saves', exp_name)))
print('-' * 89)
# loss_monitor.save_result_numpy(log_folder)
# mean, std = anomaly_detection_monitor.statistics_result('anomaly_detection', 100)
# print("Mean: {}".format(mean))
# print("Std: {}".format(std))
if __name__ == '__main__':
arguments = docopt(__doc__)
print("...Docopt...")
print(arguments)
print("............\n")
main(arguments)