-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
1225 lines (1122 loc) · 58.6 KB
/
main.py
File metadata and controls
1225 lines (1122 loc) · 58.6 KB
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
import copy
import filecmp
import hashlib
import json
import os
import os.path
import shutil
import subprocess
import time
import uuid
from io import StringIO
from pathlib import Path
from typing import Optional, Union
import numpy as np
import pandas as pd
import seml
import torch
import torch.nn as nn
from torch import autograd
from torchvision import transforms
from sacred import Experiment
#from torchattacks import PGD, GN, DeepFool, AutoAttack
from data.CIFAR10 import CIFAR10
from data.FashionMNIST import FashionMNIST
from data.GSC2 import GSC2_PyTorch, GSC2_TF
from data.MNIST import MNIST
from data.SVHN import SVHN
from data.TinyImageNet import TinyImageNet
from data.CINIC10 import CINIC10
from models_noisy.cnn_HE import CNN_HE
from models_noisy.lenet import LeNet
from models_noisy.mlp import MLP
from models_noisy.lenet_qat import LeNetQAT
from models_noisy.vgg_qat import VGG_QAT
from models_noisy.util import WeightClamper
from models_noisy.util import ReConfigNoise
from models_noisy.vgg import VGG
from models_noisy.resnet_new import ResNet
from models_noisy.resnet_qat import ResNetQAT
from models_noisy.densenet import DenseNet
from noise_operator import config as cfg
from util import cluster
from util.console_logging import print_status
from util.attacks import *
from util.awp import AdvWeightPerturb
from util.sam import SAM
sacred_exp = Experiment(save_git_info=False)
seml.setup_logger(sacred_exp)
@sacred_exp.post_run_hook
def collect_stats(_run):
seml.collect_exp_stats(_run)
@sacred_exp.config
def config():
overwrite = None
db_collection = None
if db_collection is not None:
sacred_exp.observers.append(seml.create_mongodb_observer(db_collection, overwrite=overwrite))
# sacred_exp.observers.append(seml.create_neptune_observer('uhdcsg/' + db_collection, api_token=None))
class ExperimentWrapper:
"""
A simple wrapper around a sacred experiment, making use of sacred's captured functions with prefixes.
This allows a modular design of the configuration, where certain sub-dictionaries (e.g., "data") are parsed by
specific method. This avoids having one large "main" function which takes all parameters as input.
"""
def __init__(self,
sacred_exp,
init_all=True,
nfs_artifact_root=None,
device=None,
):
# Setup internal variables
self._sacred_exp = sacred_exp
self._seml_return_data = dict()
self._seml_return_data['artifacts'] = dict()
if nfs_artifact_root is None:
self._nfs_artifact_root = cluster.get_artifact_root()
elif nfs_artifact_root:
self._nfs_artifact_root = nfs_artifact_root
try:
self.num_avail_cpus = int(os.environ['SLURM_CPUS_PER_TASK'])
self.num_avail_cpus = min(3, self.num_avail_cpus)
except KeyError:
self.num_avail_cpus = 1
self.hostname = os.environ.get("SLURM_NODELIST", "Unknown")
if device is None:
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
else:
self.device = device
# Generate the artifact UUID, we trust that the config-hash and UUID4 together are sufficiently unique here
config_hash = self.get_config_hash()
self._artifact_uuid = f'{config_hash}_{str(uuid.uuid4())}'
# Do main initialization
if init_all:
self.init_all()
def get_seml_return_data(self):
return self._seml_return_data
def log_scalar(self, metric_name, value, log_to_sacred_observers=False):
"""
Wrapper function, which logs to sacred and to the seml return argument at the same time.
"""
# Log to sacred observers
if log_to_sacred_observers:
self._sacred_exp.log_scalar(metric_name, value)
# Log to seml
log_time = time.time()
if metric_name not in self._seml_return_data.keys():
self._seml_return_data[metric_name] = list()
self._seml_return_data[metric_name + "_time"] = list()
self._seml_return_data[metric_name].append(value)
self._seml_return_data[metric_name + "_time"].append(log_time)
@sacred_exp.capture
def get_config_hash(self, general, data, model, optimizer, noise_settings, db_collection):
"""Calculates the sha1 hash value of the current experiment configuration
Inspired from: https://github.com/TUM-DAML/seml/blob/7d9352e51c9a83b77aa30617e8926863f0f48797/seml/utils.py#L191"""
config_hash = hashlib.sha1()
config_dict = {'general': general, 'data': data, 'model': model, 'optimizer': optimizer,
'noise_settings': noise_settings, 'db_collection': db_collection}
config_hash.update(json.dumps(config_dict, sort_keys=True).encode("utf-8"))
return config_hash.hexdigest()
def add_artifact(self, filename: Union[str, Path],
name: Optional[str] = None,
metadata: Optional[dict] = None,
content_type: Optional[str] = None,
) -> None:
"""
Copies the artifact to the network file system and stores its path in the seml result data.
"""
# Create a new artifact folder
artifact_folder = self._nfs_artifact_root / self._artifact_uuid
artifact_folder.mkdir()
# Copy over the file
src_file = Path(filename)
dst_file = artifact_folder / src_file.name
shutil.copy2(src_file, dst_file)
print(f"Copied artifact {src_file} to {dst_file}")
# Check that the file was copied correctly and overwrite otherwise
for i in range(5):
time.sleep(1.)
filecmp.clear_cache()
if not filecmp.cmp(src_file, dst_file, shallow=False):
print(f"Artifact copy mismatches the original, retrying ({i}-th try).")
shutil.copy2(src_file, dst_file)
else:
break
else: # No break executed
print("Copy operation of the artifact failed after too many retries.")
# Save the artifact name in the seml data
if name is None:
name = src_file.name
self._seml_return_data['artifacts'][name] = str(dst_file)
# With the prefix option we can "filter" the configuration for the sub-dictionary under "data".
@sacred_exp.capture()
def init_dataset(self, data):
"""
Perform dataset loading, preprocessing etc.
Since we set prefix="data", this method only gets passed the respective sub-dictionary, enabling a modular
experiment design.
"""
# Find the dataset
dataset = data['dataset']
if dataset == "MNIST":
self.data = MNIST
elif dataset == "CIFAR10":
self.data = CIFAR10
elif dataset == "GSC2_PyTorch":
self.data = GSC2_PyTorch
elif dataset == "GSC2_TF":
self.data = GSC2_TF
elif dataset == "SVHN":
self.data = SVHN
elif dataset == "FashionMNIST":
self.data = FashionMNIST
elif dataset == "TinyImageNet":
self.data = TinyImageNet
elif dataset == "CINIC10":
self.data = CINIC10
else:
raise ValueError(f"Dataset with name {dataset} is not supported.")
# Get the batch_size
if 'batch_size' in data:
batch_size = data['batch_size']
else:
batch_size = 128
#batch_size = 256
# Init the dataset
#if dataset == "TinyImageNet" and self.hostname.find("rivulet") >= 0:
# self.data = self.data(num_workers=self.num_avail_cpus,
# data_root=str(Path('/local/') / Path('datasets/')),
# batch_size=batch_size,
# )
self.data = self.data(num_workers=self.num_avail_cpus,
data_root=str(cluster.get_artifact_root() / Path('datasets/')),
batch_size=batch_size,
)
# Download the data
self.data.prepare_data()
# Setup the torch datasets
self.data.setup()
# Init train and val dataloaders
self.train_loader = self.data.train_dataloader()
self.val_loader = self.data.val_dataloader()
if self.data.has_test_dataset:
self.test_loader = self.data.test_dataloader()
@staticmethod
def assemble_layer_noise_config(experiment_noise_settings):
'''
Create the layer wise configuration for the noise factory from the experiment configuration.
'''
layer_wise_noise_config = dict()
# Check for a layer wise setting, this addresses only one layer
# Check that we have a sub dictionary available to us
# Otherwise this might just be none and we can continue.
try:
layer_wise_setting = experiment_noise_settings['layer_wise']
except KeyError:
layer_wise_setting = None
if isinstance(layer_wise_setting, dict):
# For now only configuring one single layer is supported
ex_layer_settings = experiment_noise_settings['layer_wise']
single_layer_config = cfg.resolve_config_from_name(
ex_layer_settings['noise_type'],
**ex_layer_settings
)
index = int(ex_layer_settings['layer_index'])
layer_wise_noise_config[index] = single_layer_config
return layer_wise_noise_config
# Check for a layer mapped setting, this addresses multiple layers
try:
layer_mapped_setting = experiment_noise_settings['layer_mapped']
except KeyError:
layer_mapped_setting = None
if isinstance(layer_mapped_setting, dict):
mapped_settings = copy.deepcopy(layer_mapped_setting)
std_map = mapped_settings['std_map']
# Do mapping rescaling
std_multiplication_factor = mapped_settings['std_multiplication_factor']
if mapped_settings['re_normalize_mapping']:
std_sum = sum(std_map.values())
std_multiplication_factor /= std_sum
for key in std_map.keys():
std_map[key] *= std_multiplication_factor
# Create mapping
for index in std_map.keys():
kwarg_settings = copy.deepcopy(mapped_settings['noise_op_kwargs'])
kwarg_settings[mapped_settings['std_key_name']] = std_map[index]
single_layer_config = cfg.resolve_config_from_name(
mapped_settings['noise_type'],
**kwarg_settings
)
index = int(index)
layer_wise_noise_config[index] = single_layer_config
# Done
return layer_wise_noise_config
return layer_wise_noise_config
def create_model(self, model, default_noise_config, layer_wise_noise_config):
# Setup the nn
model_class = model['model_class']
if model_class == "MLP":
# Here we can pass the "model_params" dict to the constructor directly, which can be very useful in
# practice, since we don't have to do any model-specific processing of the config dictionary.
internal_model = MLP(**model['MLP'],
num_classes=self.data.num_classes,
input_shape=self.data.dims,
default_noise_config=default_noise_config,
layer_wise_noise_config=layer_wise_noise_config)
elif model_class == 'VGG':
internal_model = VGG(**model['VGG'],
num_classes=self.data.num_classes,
input_shape=self.data.dims,
default_noise_config=default_noise_config,
layer_wise_noise_config=layer_wise_noise_config)
elif model_class == 'LeNet':
internal_model = LeNet(**model['LeNet'],
num_classes=self.data.num_classes,
input_shape=self.data.dims,
default_noise_config=default_noise_config,
layer_wise_noise_config=layer_wise_noise_config)
elif model_class == "CNN_HE":
internal_model = CNN_HE(**model['CNN_HE'],
num_classes=self.data.num_classes,
input_shape=self.data.dims,
default_noise_config=default_noise_config,
layer_wise_noise_config=layer_wise_noise_config)
elif model_class == 'LeNet_QAT':
internal_model = LeNetQAT(**model['LeNet_QAT'],
num_classes=self.data.num_classes,
input_shape=self.data.dims,
default_noise_config=default_noise_config,
layer_wise_noise_config=layer_wise_noise_config)
elif model_class == 'VGG_QAT':
internal_model = VGG_QAT(**model['VGG_QAT'],
num_classes=self.data.num_classes,
input_shape=self.data.dims,
default_noise_config=default_noise_config,
layer_wise_noise_config=layer_wise_noise_config)
elif model_class == 'ResNet':
internal_model = ResNet(**model['ResNet'],
num_classes=self.data.num_classes,
input_shape=self.data.dims,
default_noise_config=default_noise_config,
layer_wise_noise_config=layer_wise_noise_config)
elif model_class == 'ResNet_QAT':
internal_model = ResNetQAT(**model['ResNet_QAT'],
num_classes=self.data.num_classes,
input_shape=self.data.dims,
default_noise_config=default_noise_config,
layer_wise_noise_config=layer_wise_noise_config)
elif model_class == 'DenseNet':
internal_model = DenseNet(**model['DenseNet'],
num_classes=self.data.num_classes,
input_shape=self.data.dims,
default_noise_config=default_noise_config,
layer_wise_noise_config=layer_wise_noise_config)
else:
raise ValueError(f"Model with name {model_class} is not supported.")
# Move the model to the correct device and set the datatype to float32
internal_model.to(self.device, dtype=torch.float32)
# Do a dry run to initialize the lazy-init layers
init_tensor = torch.randn(2, *self.data.dims, dtype=torch.float32, device=self.device)
internal_model(init_tensor)
return internal_model
@sacred_exp.capture()
def init_model(self, general, model, noise_settings, data, _log, kl_div_metric=None):
# Setup the noise, quantized, pruned, whatever default model
# Get default and layer wise noise settings
default_noise_config = cfg.resolve_config_from_name(
noise_settings['default']['noise_type'],
**noise_settings['default']
)
layer_wise_noise_config = self.assemble_layer_noise_config(noise_settings)
self.model = self.create_model(model, default_noise_config, layer_wise_noise_config)
# load from pre-trained model
# use another parameter, enable_pretrain, and then model_dir
if "enable_pretrain" in model and model["enable_pretrain"]:
pre_train_model = torch.load(model["pretrain_dir"])
self.model.load_state_dict(pre_train_model.state_dict(), strict=False)
if "distill" in model and model["distill"]["is_enabled"]:
self.teacher = torch.load(model["distill"]["teacher"])
if "enable_awp" in general and general["enable_awp"]:
self.proxy_model = self.create_model(model, default_noise_config, layer_wise_noise_config)
if "param_sigma" in noise_settings and noise_settings["param_sigma"]:
self.pure_model = torch.load(noise_settings["param_sigma"]["clean_model"])
self.model.load_state_dict(self.pure_model.state_dict(), strict=False)
# set up noise in pure model
self.pure_model.apply(ReConfigNoise(default_noise_config)).to(self.device)
# set up noise in model
parametric_conf = {
"GaussMean": 0.0,
"GaussStd": noise_settings["param_sigma"]["init_Std"],
"enable_in_training": 1,
}
parametric_noise_config = cfg.resolve_config_from_name(noise_settings["default"]["noise_type"], **parametric_conf)
self.model.apply(ReConfigNoise(parametric_noise_config)).to(self.device)
# Print check
_log.info(f"Created model of the following configuration: {self.model}")
# Check for more models, which need setting up
# KL_div models
if kl_div_metric is not None:
if kl_div_metric['compute_against_pre_trained_no_noise']:
self.compute_kl_against_no_noise = True
# Create the reference model from a non-noisy checkpoint
# First get the checkpoint from the NFS share
pre_trained_db_entry = get_no_noise_db_entry_equivalent_to_current_exp()
found_pre_trained_model = False
if pre_trained_db_entry is None:
_log.warning('No equivalent pre-trained model was found, '
'evaluating this experiment may take longer than what would otherwise be required.')
else:
found_pre_trained_model, pre_trained_checkpoint = load_checkpoint_from_exp(pre_trained_db_entry, device=self.device)
if not found_pre_trained_model:
raise RuntimeError("Could not find a pretrained no-noise experiment, which is required to compute the KL-divergence")
# Then create the model, w/o noise
default_noise_config = cfg.resolve_config_from_name(
'NoNoise',
)
layer_wise_noise_config = self.assemble_layer_noise_config({'layer_wise': None})
self.kl_no_noise_model = self.create_model(model, default_noise_config, layer_wise_noise_config)
# Load the weights
self.kl_no_noise_model.load_state_dict(pre_trained_checkpoint['state_dict'])
else:
self.compute_kl_against_no_noise = False
else:
self.compute_kl_against_no_noise = False
# Setup the criterion
crit_name = model['criterion']
if crit_name == "CrossEntropyLoss":
loss_weight = None
# Re-weighting for GSC2_TF
if 'weight_criterion' in model:
if model['weight_criterion']:
if data['dataset'] == "GSC2_TF":
# Compute weights to balance the training dataset
bins, edges = np.histogram(self.data._ds_train._label_array, 12)
class_density = bins / bins.sum()
inverse_class_density = 1 - class_density
# Further suppress the "unknown" label, since it is severely overrepresented in training data
# The exact value is taken from here: https://github.com/mlcommons/tiny_results_v0.7/blob/691f8b26aa9dffa09b1761645d4a35ad35a4f095/open/hls4ml-finn/code/kws/KWS-W3A3/training/const_QMLP.yaml#L32
label_suppression_unknown = 3.6
inverse_class_density[-1] /= label_suppression_unknown
inverse_class_density /= inverse_class_density.sum()
inverse_class_density = inverse_class_density.astype(np.float32)
loss_weight = torch.from_numpy(inverse_class_density).to(self.device)
else:
ValueError("Weighting is currently not implemented for other datasets than GSC2_TF.")
self.criterion = nn.CrossEntropyLoss(weight=loss_weight)
else:
raise ValueError(f"Criterion with name {crit_name} is not supported.")
@sacred_exp.capture()
def init_optimizer(self, general, optimizer):
# Set the optimizer
optim_name = optimizer['optimizer_type']
weight_decay = optimizer.get("weight_decay", 0)
if optim_name == "Adam":
if "enable_sam" in general and general["enable_sam"]:
is_adaptive = general["is_adaptive"]
rho = general["sam_rho"]
self.optimizer = SAM(self.model.parameters(), torch.optim.Adam, rho=rho, adaptive=is_adaptive, lr=optimizer['lr'], weight_decay=weight_decay)
else:
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=optimizer['lr'], weight_decay=weight_decay)
if "enable_awp" in general and general["enable_awp"]:
self.proxy_optim = torch.optim.Adam(self.model.parameters(), lr=0.01, weight_decay=weight_decay)
gamma = general["awp_gamma"]
self.awp_adversary = AdvWeightPerturb(model=self.model, proxy=self.proxy_model, proxy_optim=self.proxy_optim, gamma=gamma)
else:
raise ValueError(f"Optimizer with name {optim_name} is not supported.")
# Set the scheduler
sched_name = optimizer['lr_scheduler']
if sched_name == "CosineAnnealingLR":
self.scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(self.optimizer, T_max=general['num_epochs'])
else:
raise ValueError(f"Scheduler with name {sched_name} is not supported.")
# Figure out the clamping and put in none if it wasn't specified
try:
weight_clamping_params = [general['weight_clamping']['min'], general['weight_clamping']['max']]
except KeyError:
weight_clamping_params = [None, None]
self._weightClamper = WeightClamper(*weight_clamping_params)
def init_all(self):
"""
Sequentially run the sub-initializers of the experiment.
"""
self.init_dataset()
self.init_model()
self.init_optimizer()
@sacred_exp.capture()
def reset_noiseop_per_batch(self, noise_settings, _log, batch_size=128, enable_log=False):
""" variance of variance
std for each image is sampled from distribution N(GaussStd_in_training, Std_of_GaussStd_in_training)
"""
mean_std = noise_settings["default"]["GaussStd_in_training"]
std_of_std = noise_settings["default"]["Std_of_GaussStd_in_training"]
if "is_mean_of_std" in noise_settings and noise_settings["is_mean_of_std"]:
mean_std = mean_std * noise_settings["default"]["Mean_of_GaussStd_in_training"]
std_per_batch = list(mean_std + std_of_std * np.abs(np.random.randn(batch_size)))
noise_info = noise_settings["default"]
noise_new = copy.deepcopy(noise_info)
noise_new["GaussStd"] = std_per_batch
noise_config = cfg.resolve_config_from_name(noise_new["noise_type"], **noise_new)
noise_case = ReConfigNoise(noise_config)
self.model.apply(noise_case).to(self.device)
#_log.info(f"info of std per batch, {len(std_per_batch), std_per_batch}")
#_log.info(f"model after reset noiseoperator {self.model}")
if enable_log:
self.log_scalar("train_std_per_batch", std_per_batch)
@sacred_exp.capture()
def training_step(self, optimizer, noise_settings, cur_train_std = None, ):
# Training step
self.model.train()
summed_loss = 0.
correct = 0
total = 0
if "GaussStd_in_training" in noise_settings["default"]:
train_std = noise_settings["default"]["GaussStd_in_training"]
else:
train_std = noise_settings["default"].get("GaussStd", 0)
for batch_idx, (inputs, targets) in enumerate(self.train_loader):
inputs, targets = inputs.to(self.device), targets.to(self.device)
self.optimizer.zero_grad()
# if the "enable_std_per_img" is on, we only do that after the incremeantal training part.
if cur_train_std is not None and cur_train_std >= train_std:
# apply individual std to each img
if noise_settings.get("enable_std_per_img", False) and "Std_of_GaussStd_in_training" in noise_settings["default"]:
self.reset_noiseop_per_batch(batch_size=inputs.shape[0], enable_log=batch_idx<2)
outputs = self.model(inputs)
loss = self.criterion(outputs, targets)
# add gradient penalty to loss
if "gradient_reg" in optimizer:
lambda_reg = optimizer["gradient_reg"]
grad_outputs = torch.ones(loss.shape)
gradients = autograd.grad(outputs=loss, inputs=self.model.parameters(),
create_graph=True)
grad_l2_norm = 0
for temp_grad in gradients:
grad_l2_norm += torch.norm(temp_grad, p=2)
total_loss = loss + lambda_reg * grad_l2_norm
else:
total_loss = loss
total_loss.backward()
self.optimizer.step()
# Apply weight clamping after optimization step
self.model.apply(self._weightClamper)
summed_loss += total_loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
# Logging
accuracy = 100. * correct / total
self.log_scalar("training.loss", summed_loss)
self.log_scalar("training.accuracy", accuracy)
return summed_loss, accuracy
@sacred_exp.capture()
def validation_step(self, general, run_on_test_dataset_instead=False):
# Validation step
topk = general.get("topk", 1)
self.model.eval()
summed_loss = 0.
correct = 0
total = 0
distorted_probabilities = []
reference_probabilities = []
with torch.no_grad():
curr_ds = self.val_loader
if run_on_test_dataset_instead:
curr_ds = self.test_loader
for batch_idx, (inputs, targets) in enumerate(curr_ds):
inputs, targets = inputs.to(self.device), targets.to(self.device)
outputs = self.model(inputs)
loss = self.criterion(outputs, targets)
summed_loss += loss.item()
total += targets.size(0)
if topk == 1:
_, predicted = outputs.max(1)
correct += predicted.eq(targets).sum().item()
else:
_, predicted = outputs.topk(topk, dim=1)
correct += predicted.eq(targets.view(-1, 1)).any(dim=1).sum().item()
if self.compute_kl_against_no_noise:
distorted_probabilities.append(torch.nn.functional.log_softmax(outputs, dim=1).detach())
self.kl_no_noise_model.eval()
ref_out = self.kl_no_noise_model(inputs)
reference_probabilities.append(torch.nn.functional.log_softmax(ref_out, dim=1).detach())
if self.compute_kl_against_no_noise:
distorted_probabilities = torch.cat(distorted_probabilities, dim=0)
reference_probabilities = torch.cat(reference_probabilities, dim=0)
kl_div = torch.nn.functional.kl_div(distorted_probabilities, reference_probabilities,
reduction='batchmean', log_target=True).cpu().item()
log_name = 'test.kl_div' if run_on_test_dataset_instead else 'validation.kl_div'
self.log_scalar(log_name, kl_div)
# Logging
accuracy = 100. * correct / total
if run_on_test_dataset_instead:
self.log_scalar("test.loss", summed_loss)
self.log_scalar("test.accuracy", accuracy)
else:
self.log_scalar("validation.loss", summed_loss)
self.log_scalar("validation.accuracy", accuracy)
return summed_loss, accuracy
@sacred_exp.capture()
def training_step_adv(self, general):
self.model.train()
summed_loss = 0.
correct = 0
total = 0
# Training step using the attack
adv_type = general["adv_type"]
"""
if adv_type == "FGSM":
attack = FGSM(self.model, eps=4/255) # 8/255 as default
elif adv_type == "PGD":
# eps = 8/255, alpha = 1/255, and steps = 10 as default
attack = PGD(self.model, eps=4/255, alpha=1/255, steps=3, random_start=True)
"""
for batch_idx, (inputs, targets) in enumerate(self.train_loader):
inputs, targets = inputs.to(self.device), targets.to(self.device)
if adv_type == "GN":
trans = transforms.GaussianBlur(kernel_size=(7, 7), sigma=(0.1, 0.75))
inputs_adv = trans(inputs)
elif adv_type == "PGD":
inputs_adv, _, _, _ = PGD(self.model, inputs, targets)
else:
raise ValueError(f"The given adv_type is not expected.")
#inputs_adv = attack(inputs, targets)
self.optimizer.zero_grad()
outputs = self.model(inputs_adv)
loss = self.criterion(outputs, targets)
loss.backward()
self.optimizer.step()
# Apply weight clamping after optimization step
self.model.apply(self._weightClamper)
summed_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
# Logging
accuracy = 100. * correct / total
self.log_scalar("training.loss", summed_loss)
self.log_scalar("training.accuracy", accuracy)
return summed_loss, accuracy
@sacred_exp.capture()
def training_step_awp(self, general):
self.model.train()
summed_loss = 0.
correct = 0
total = 0
# Training step using the attack
adv_type = general.get("adv_type", None)
for batch_idx, (inputs, targets) in enumerate(self.train_loader):
inputs, targets = inputs.to(self.device), targets.to(self.device)
if adv_type == "PGD":
inputs_adv, _, _, _ = PGD(self.model, inputs, targets)
#attack = PGD(self.model, eps=8./255, alpha=2./255, steps=10)
#inputs_adv = attack(inputs, targets)
else:
# none
inputs_adv = inputs
# awp
awp = self.awp_adversary.calc_awp(inputs_adv=inputs_adv,
targets=targets)
self.awp_adversary.perturb(awp) # model is perturbed
self.optimizer.zero_grad()
outputs = self.model(inputs_adv)
loss = self.criterion(outputs, targets)
loss.backward()
self.optimizer.step()
self.awp_adversary.restore(awp)
# Apply weight clamping after optimization step
self.model.apply(self._weightClamper)
summed_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
# Logging
accuracy = 100. * correct / total
#self.log_scalar("adv_type", adv_type)
#self.log_scalar("gamma", general["awp_gamma"])
self.log_scalar("training.loss", summed_loss)
self.log_scalar("training.accuracy", accuracy)
return summed_loss, accuracy
def training_step_distill(self, model):
T = model["distill"]["T"]
b = model["distill"]["b"]
self.teacher.eval()
self.model.train()
summed_loss = 0.
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(self.train_loader):
inputs, targets = inputs.to(self.device), targets.to(self.device)
self.optimizer.zero_grad()
student_logits = self.model(inputs)
#forward pass with the teacher model, do not save gradients here
with torch.no_grad():
teacher_logits = self.teacher(inputs)
soft_targets = nn.functional.softmax(teacher_logits / T, dim=-1)
#soft_prob = nn.functional.softmax(student_logits / T, dim=-1)
# Scaled by T**2 as suggested by the authors of the paper "Distilling the knowledge in a neural network"
#soft_loss = T**2 * torch.sum(soft_targets * (soft_targets.log() - soft_prob.log()))/student_logits.size()[0]
soft_loss = T**2 * self.criterion(student_logits/T, soft_targets)
label_loss = self.criterion(student_logits, targets)
# Weighted sum of the two losses
loss = (1-b) * label_loss + b * soft_loss
loss.backward()
self.optimizer.step()
# Apply weight clamping after optimization step
self.model.apply(self._weightClamper)
summed_loss += loss.item()
_, predicted = student_logits.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
# Logging
accuracy = 100. * correct / total
self.log_scalar("training.loss", summed_loss)
self.log_scalar("training.accuracy", accuracy)
return summed_loss, accuracy
def training_step_sam(self):
# Training step
self.model.train()
summed_loss = 0.
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(self.train_loader):
inputs, targets = inputs.to(self.device), targets.to(self.device)
#self.optimizer.zero_grad()
outputs = self.model(inputs)
loss = self.criterion(outputs, targets)
loss.backward()
#self.optimizer.step()
self.optimizer.first_step(zero_grad=True)
self.criterion(self.model(inputs), targets).backward()
self.optimizer.second_step(zero_grad=True)
# Apply weight clamping after optimization step
self.model.apply(self._weightClamper)
summed_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
# Logging
accuracy = 100. * correct / total
self.log_scalar("training.loss", summed_loss)
self.log_scalar("training.accuracy", accuracy)
return summed_loss, accuracy
@sacred_exp.capture()
def training_step_noisy_weight(self, noise_settings):
# Training step
self.model.train()
summed_loss = 0.
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(self.train_loader):
inputs, targets = inputs.to(self.device), targets.to(self.device)
self.optimizer.zero_grad()
# step 0: get noise information, GaussAdd as default
mean = 0
std = 0
noise_default_type = noise_settings["default"]["noise_type"]
if noise_default_type == "GaussAdd":
# global noise
mean = noise_settings["default"]["GaussMean"]
std = noise_settings["default"]["GaussStd"]
# step 1: inject noise on weight
src_params = []
for p_name, p in self.model.named_parameters():
if p_name.find('weight'):
src_params.append(p.clone())
p.data = p.data + torch.normal(mean, std, p.size(), device=self.device)
outputs = self.model(inputs)
loss = self.criterion(outputs, targets)
loss.backward()
#step 2: reset the weights and update
for p_name, p in self.model.named_parameters():
if p_name.find('weight'):
p.data = src_params.pop(0)
self.optimizer.step()
# Apply weight clamping after optimization step
self.model.apply(self._weightClamper)
summed_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
# Logging
accuracy = 100. * correct / total
self.log_scalar("training.loss", summed_loss)
self.log_scalar("training.accuracy", accuracy)
return summed_loss, accuracy
@sacred_exp.capture()
def training_param_sigmas(self, noise_settings):
param_sigma = noise_settings["param_sigma"]
optimizer = torch.optim.Adam(self.model.parameters(), lr=param_sigma["lr"], weight_decay=param_sigma["weight_decay"])
num_epochs = param_sigma["num_epochs"]
T = param_sigma["T"]
b = param_sigma["b"]
# set std learnable explicitly
for p in self.model.named_parameters():
#print(p[0], p[1].requires_grad)
if p[0].find('weight')>=0 or p[0].find('bias')>=0 :
p[1].requires_grad = False
else:
p[1].requires_grad = True
for epoch in range(num_epochs):
self.pure_model.eval()
self.model.train()
summed_loss = 0.
correct = 0
teacher_correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(self.train_loader):
inputs, targets = inputs.to(self.device), targets.to(self.device)
optimizer.zero_grad()
student_logits = self.model(inputs)
#forward pass with the teacher model, do not save gradients here
with torch.no_grad():
teacher_logits = self.pure_model(inputs)
soft_targets = nn.functional.softmax(teacher_logits / T, dim=-1)
#soft_prob = nn.functional.softmax(student_logits / T, dim=-1)
# Scaled by T**2 as suggested by the authors of the paper "Distilling the knowledge in a neural network"
#soft_loss = T**2 * torch.sum(soft_targets * (soft_targets.log() - soft_prob.log()))/student_logits.size()[0]
soft_loss = T**2 * self.criterion(student_logits/T, soft_targets)
label_loss = self.criterion(student_logits, targets)
# Weighted sum of the two losses
loss = (1-b) * label_loss + b * soft_loss
loss.backward()
optimizer.step()
summed_loss += loss.item()
_, predicted = student_logits.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
_, teacher_predicted = teacher_logits.max(1)
teacher_correct += teacher_predicted.eq(targets).sum().item()
# Logging
accuracy = 100. * correct / total
teacher_accuracy = 100. * teacher_correct / total
self.log_scalar("sigma_training.acc", accuracy)
self.log_scalar("sigma_training.teacher_acc", teacher_accuracy)
#record the learned sigma
for p in self.model.named_parameters():
if p[0].find('std')>=0:
self.log_scalar("learned_sigma", p[1].data.item())
# set back learnable params
for p in self.model.named_parameters():
#print(p[0], p[1].requires_grad)
if p[0].find('weight')>=0 or p[0].find('bias')>=0 :
p[1].requires_grad = True
else:
p[1].requires_grad = False
# We can call this command, e.g., from a Jupyter notebook with init_all=False to get an "empty" experiment wrapper,
# where we can then for instance load a pretrained model to inspect the performance.
@sacred_exp.command(unobserved=True)
def get_experiment(init_all=False):
print('get_experiment')
experiment = ExperimentWrapper(sacred_exp, init_all=init_all)
return experiment
@sacred_exp.capture
def check_if_curr_exp_has_noise_during_training(noise_settings):
# First check if no noise is applied during training
noise_during_training = False
# Check if global noise is applied during training
if noise_settings['default']['noise_type'] != 'NoNoise':
if noise_settings['default']['enable_in_training']:
noise_during_training = True
# Check if any layer wise noise is applied during training
if noise_settings['layer_wise'] is not None:
if noise_settings['layer_wise']['enable_in_training']:
noise_during_training = True
return noise_during_training
@sacred_exp.capture
def get_no_noise_db_entry_equivalent_to_current_exp(general, noise_settings, data, model, optimizer, db_collection, _log):
# Load already computed seml results, which have no noise and check if an equivalent one exists
# Select experiments, which have no default noise AND which have no layer noise
filter_dict = {
#'config.noise_settings.default.noise_type': 'NoNoise',
'config.noise_settings.layer_wise': None,
'config.noise_settings.layer_mapped': None,
}
fields_to_get = ['config', 'result']
seml_res = seml.get_results(db_collection, filter_dict=filter_dict, fields=fields_to_get, to_data_frame=False)
pre_trained_db_entry = None
for completed_exp in seml_res:
completed_exp_cfg = completed_exp['config']
# Check if the experiment is actually without noise (these two operations should already be done by
# MongoDB through the use of the filter_dict variable)
if not completed_exp_cfg['noise_settings']['default']['noise_type'] == 'NoNoise' and "GaussStd_in_training" not in noise_settings["default"]:
continue
if completed_exp_cfg['noise_settings']['layer_wise'] is not None:
continue
# Check if the experiment matches our current config, excluding the noise
# ToDo: Make this stricter again, i.e. completed_exp_cfg['model']['conf_name'] should check the whole model again, not just the conf
# This was changed to make sure it dosen't take the repetition config into account, but it should take other stuff into account.
# So it should do: completed_exp_cfg['model'] == model
# But excluting the repetition_config key.
### stricter conditions by Xiao
same_model = completed_exp_cfg['model'] == model
same_general = completed_exp_cfg['general'] == general
#if same_model is not True or same_general is not True:
if same_model is not True:
continue
gauss_std_in_training = noise_settings["default"].get("GaussStd_in_training", 0)
gotten_gauss_std_in_training = completed_exp_cfg['noise_settings']['default'].get("GaussStd_in_training", 0)
if gauss_std_in_training != gotten_gauss_std_in_training:
continue
std_of_std_in_training = noise_settings["default"].get("Std_of_GaussStd_in_training", 0)
gotten_std_of_std_in_training = completed_exp_cfg['noise_settings']['default'].get("Std_of_GaussStd_in_training", 0)
if std_of_std_in_training != gotten_std_of_std_in_training:
continue
mean_of_std_in_training = noise_settings["default"].get("Mean_of_GaussStd_in_training", 0)
gotten_mean_of_std_in_training = completed_exp_cfg['noise_settings']['default'].get("Mean_of_GaussStd_in_training", 0)
if mean_of_std_in_training != gotten_mean_of_std_in_training:
continue
### below is the loose conditions by Hendrik, could merged after check
internal_sub_model = model[list(model.keys())[0]]['conf_name']
gotten_sub_model = completed_exp_cfg['model'][list(completed_exp_cfg['model'].keys())[0]]['conf_name']
# Same for the number of epochs, or rather the general key, here we wated to cut out the 'experiment_name' key.
internal_num_epochs = general['num_epochs']
gotten_num_epochs = completed_exp_cfg['general']['num_epochs']
internal_repeat_number = general['repeat_number']
gotten_repeat_number = completed_exp_cfg['general']['repeat_number']
if completed_exp_cfg['data'] == data and gotten_sub_model == internal_sub_model and \
completed_exp_cfg['optimizer'] == optimizer and gotten_num_epochs == internal_num_epochs and gotten_repeat_number == internal_repeat_number:
_log.info('Found an equivalent pretrained model')
pre_trained_db_entry = completed_exp
break
return pre_trained_db_entry
@sacred_exp.capture
def load_checkpoint_from_exp(pre_trained_db_entry, _log, device='cpu'):
_log.info(f'Loading model, with _id: {pre_trained_db_entry["_id"]} to device: {device}')
# Load the checkpoint of the pre-trained model
checkpoint_path = Path(pre_trained_db_entry['result']['artifacts']['Trained model checkpoint'])
local_checkpoint_path = cluster.convert_artifact_path_to_local_path(checkpoint_path, logger=_log)
if local_checkpoint_path.exists():
pre_trained_checkpoint = torch.load(local_checkpoint_path, map_location=device)
found_pre_trained_model = True
else:
found_pre_trained_model = False
pre_trained_checkpoint = None
_log.warning("Could not load the model, "
"because the checkpoint file doesn't exist on the local artifact storage.")
return found_pre_trained_model, pre_trained_checkpoint
# In some cases the model being trained doesn't inject any noise during training, only during evaluation.
# In these cases the training is equivalent to noise less training, so we try to load the model from an equivalent
# training run.
@sacred_exp.capture
def get_pre_trained_checkpoint(general, data, model, optimizer, noise_settings, db_collection, _log, device='cpu'):
# Check if the model can be loaded from a pre-existing checkpoint
found_pre_trained_model = False
pre_trained_checkpoint = None
noise_during_training = check_if_curr_exp_has_noise_during_training(noise_settings)
if not noise_during_training:
_log.info('Found that this experiment contains no noise at training time.')
_log.info('Searching for a pretrained model without noise.')
pre_trained_db_entry = get_no_noise_db_entry_equivalent_to_current_exp(general, noise_settings, data, model, optimizer, db_collection, _log)
#pre_trained_db_entry = None
if pre_trained_db_entry is None: