-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
1199 lines (1080 loc) · 53.9 KB
/
helper.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
from copy import deepcopy
import shutil
from typing import List
import numpy as np
import torch
from torch.optim.lr_scheduler import MultiStepLR, OneCycleLR
from torchvision import transforms
from torchvision.datasets import CIFAR100
# Avalanche
from avalanche.benchmarks.classic import SplitCIFAR100 #, SplitCUB200
#from avalanche.benchmarks.classic.cmnist import _get_mnist_dataset
from src.benchmarks.miniimagenet_benchmark import get_miniimgnet_dataset
from avalanche.evaluation.metrics import ExperienceForgetting, StreamForgetting, accuracy_metrics, loss_metrics, \
StreamConfusionMatrix, timing_metrics
from avalanche.training.plugins import ReplayPlugin
from avalanche.training.strategies import Naive
from src.methods.eval_stored_weights import EvalStoredWeightsPlugin
from avalanche.training.strategies.base_strategy import BaseStrategy
# CUSTOM
from src.benchmarks.miniimagenet_benchmark import SplitMiniImageNet
from src.eval.continual_eval import ContinualEvaluationPhasePlugin
from src.eval.continual_eval_metrics import TaskTrackingLossPluginMetric, \
TaskTrackingGradnormPluginMetric, TaskTrackingFeatureDriftPluginMetric, TaskTrackingAccuracyPluginMetric, \
TaskAveragingPluginMetric, WindowedForgettingPluginMetric, \
TaskTrackingMINAccuracyPluginMetric, TrackingLCAPluginMetric, WCACCPluginMetric, WindowedPlasticityPluginMetric
from src.eval.initial_eval_stage import InitialEvalStage
from src.eval.downstream_finetune import DownstreamFinetuneAccuracyMetric
from src.eval.linear_mh_probing_metric import LinearProbingAccuracyMetric
from src.eval.linear_probing_metric_last_n import LinearProbingLastNAccuracyMetricLastN
from src.eval.knn_probing_metric import KNNProbingAccuracyMetric
from src.eval.knn_pca_probing_metric import KNNPCAProbingAccuracyMetric
from src.eval.ncm_probing_metric import NCMProbingAccuracyMetric
from src.eval.downstream_linear import DownstreamLinearProbeAccuracyMetric
from src.eval.concatenated_linear_mh_probing_metric import ConcatenatedLinearProbingAccuracyMetric
from src.eval.concatenated_knn_probing_metric import ConcatenatedKNNProbingAccuracyMetric
from src.eval.concatenated_ncm_probing_metric import ConcatenatedNCMProbingAccuracyMetric
from src.eval.downstream_concatenated_probing_metric import ConcatDownStreamProbingAccuracyMetric
from src.eval.downstream_knn import DownstreamKKNNAccuracyMetric
from src.eval.pca_probing import PCAProbingMetric
from src.eval.concatenated_pca_probing import ConcatenatedPCAProbingMetric
from src.utils import ExpLRSchedulerPlugin, IterationsInsteadOfEpochs, EpochLRSchedulerPlugin, OneCycleSchedulerPlugin
from src.methods.lwf_standard import LwFStandardPlugin
from src.methods.packnet import PackNetPlugin
from src.methods.mas import MASPlugin
from src.methods.replay import ERPlugin
from src.methods.der import DERPlugin
from src.methods.freeze import FreezeModelPlugin, FreezeAllButBNPlugin
from src.methods.reinit_backbone import ReInitBackbonePlugin
from src.methods.exclude_experience import ExcludeExperiencePlugin
from src.methods.epoch_adapter import EpochLengthAdapterPlugin
from src.methods.store_models import StoreModelsPlugin
from src.methods.separate_networks import SeparateNetworks
from src.methods.separate_networks import ConcatFeatClassifierAdapterPlugin
from src.methods.joint import JointTrainingPlugin, ConcatJointTrainingPlugin
from src.methods.supcon import SupCon
from src.methods.barlow_twins import BarlowTwins
from libs.supcon.utils import TwoCropTransform
def args_to_tensorboard(writer, args):
"""
Copyright: OCDVAE
Takes command line parser arguments and formats them to
display them in TensorBoard text.
Parameters:
writer (tensorboard.SummaryWriter): TensorBoard SummaryWriter instance.
args (dict): dictionary of command line arguments
"""
txt = ""
for arg in sorted(vars(args)):
txt += arg + ": " + str(getattr(args, arg)) + "<br/>"
writer.add_text('command_line_parameters', txt, 0)
return
def get_dataset(scenario_name,
dset_rootpath,
train_transform=None,
eval_transform=None,
subsample_classes=None):
print("\nGetDataset:eval_transform: {}".format(eval_transform))
train_set = None
test_set = None
n_classes = None
dset_rootpath = deepcopy(dset_rootpath)
eval_transform = deepcopy(eval_transform)
# Add or remove 'to_pil' transform if needed
if not scenario_name in ["miniimgnet"] \
and isinstance(eval_transform.transforms[0], transforms.ToPILImage): #.transforms[0]
eval_transform.transforms = eval_transform.transforms[1:]
elif scenario_name in ["miniimgnet"] \
and not isinstance(eval_transform.transforms[0], transforms.ToPILImage):
eval_transform.transforms.insert(0, transforms.ToPILImage())
if scenario_name == 'cifar100':
train_set = CIFAR100(root=dset_rootpath, train=True, download=True, transform=eval_transform)
test_set = CIFAR100(root=dset_rootpath, train=False, download=True, transform=eval_transform)
n_classes = 100
elif scenario_name == 'miniimgnet':
train_set, test_set = get_miniimgnet_dataset(rootpath=dset_rootpath, transform=eval_transform)
n_classes = 100
else:
raise ValueError("Unknown dataset name: {}".format(scenario_name))
if subsample_classes:
print("Subsampling classes: {}".format(subsample_classes))
# Get masks for all classes
train_mask = (torch.tensor(train_set.targets) == subsample_classes[0])
test_mask = (torch.tensor(test_set.targets) == subsample_classes[0])
for c in subsample_classes[1:]:
train_mask = train_mask | (torch.tensor(train_set.targets) == c)
test_mask = test_mask | (torch.tensor(test_set.targets) == c)
# Make tensor that is True for all indices that are in subsample_classes
train_set.data = train_set.data[train_mask]
train_set.targets = torch.tensor(train_set.targets)[train_mask].tolist()
test_set.data = test_set.data[test_mask]
test_set.targets = torch.tensor(test_set.targets)[test_mask].tolist()
# Convert the targets to proper task-labes
subsample_map = {k: v for v, k in enumerate(subsample_classes)}
train_set.targets = [subsample_map[y] for y in train_set.targets]
test_set.targets = [subsample_map[y] for y in test_set.targets]
# Adjust n_classes
n_classes = len(subsample_classes)
return train_set, test_set, n_classes
def get_transforms(data_aug,
input_size,
norm_mean=(0.0, 0.0, 0.0),
norm_std=(1.0, 1.0, 1.0),
use_to_pil=False):
"""
Single place in codebase where data transforms are defined.
Return_ List of transforms for train and test
"""
to_pil = transforms.ToPILImage()
resize = transforms.Resize(size=(input_size[1], input_size[2]))
crop_flip = transforms.Compose([
transforms.RandomResizedCrop(size=(input_size[1], input_size[2]),
scale=(0.1 if input_size[0]>=64 else 0.2, 1.),
),
transforms.RandomHorizontalFlip(p=0.5),
])
sim_clr = transforms.Compose([
transforms.RandomResizedCrop(size=(input_size[1], input_size[2]),
scale=(0.1 if input_size[0]>=64 else 0.2, 1.),
),
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomApply(
[transforms.ColorJitter(brightness=0.4,
contrast=0.4,
saturation=0.2,
hue=0.1)],
p=0.8
),
transforms.RandomGrayscale(p=0.2),
transforms.RandomApply(
[transforms.GaussianBlur(
kernel_size=input_size[0]//20*2+1,
sigma=(0.1, 2.0)
)],
p=0.5 if input_size[0]>32 else 0.0),
])
rand_crop_aug = transforms.Compose([
transforms.RandomCrop(size=(input_size[1], input_size[2]), padding=4, padding_mode='reflect'),
transforms.RandomHorizontalFlip(),
])
autoaug_cifar10 = transforms.Compose([
transforms.RandomResizedCrop(size=(input_size[1], input_size[2])),
transforms.RandomHorizontalFlip(),
transforms.AutoAugment(transforms.autoaugment.AutoAugmentPolicy.CIFAR10)
])
autoaug_imgnet = transforms.Compose([
transforms.RandomResizedCrop(size=(input_size[1], input_size[2])),
transforms.RandomHorizontalFlip(),
transforms.AutoAugment(transforms.autoaugment.AutoAugmentPolicy.IMAGENET)
])
rand_grayscale = transforms.Compose([transforms.RandomGrayscale(p=0.2)])
rand_gauss_blur = transforms.Compose([
transforms.RandomApply([
transforms.GaussianBlur(kernel_size=input_size[0]//20*2+1, sigma=(0.1, 2.0))
], p=0.5 if input_size[0]>32 else 0.0
)
])
normalize = transforms.Normalize(norm_mean, norm_std)
to_tensor = transforms.ToTensor()
train_transforms = []
test_transforms = []
# Optional addition of ToPILImage transform
if use_to_pil:
train_transforms.append(to_pil)
test_transforms.append(to_pil)
# Default addtion of Resize transform
train_transforms.append(resize)
test_transforms.append(resize)
if "simclr" in data_aug:
train_transforms.append(sim_clr)
elif "crop_flip" in data_aug:
train_transforms.append(crop_flip)
elif "rand_crop" in data_aug:
train_transforms.append(rand_crop_aug)
elif "auto_cifar10" in data_aug:
train_transforms.append(autoaug_cifar10)
elif "auto_imgnet" in data_aug:
train_transforms.append(autoaug_imgnet)
# Default addition of Normalize and ToTensor transforms
train_transforms.extend([to_tensor, normalize])
test_transforms.extend([to_tensor, normalize])
# Finally, compose everything into a single transform
return transforms.Compose(train_transforms), transforms.Compose(test_transforms)
def get_scenario(args, scenario_name, dset_rootpath,
num_experiences=None, use_data_aug=None, seed=42):
print(f"\n[SCENARIO] {scenario_name}, Task Incr = {args.task_incr}")
# Check for 'none' string #NOTE: this will happen when using default roots for downstream sets
if not dset_rootpath is None:
if dset_rootpath.lower() == "none":
dset_rootpath = None
# Prepare general transforms
train_transform = None
test_transform = None
data_transforms = dict()
if scenario_name in ["cifar100"]:
input_size = (3, 32, 32)
elif scenario_name in ["miniimgnet"]:
input_size = (3, 84, 84)
else:
raise ValueError(f"Unknown scenario name: {scenario_name}")
if not args.overwrite_input_size is None:
input_size = (input_size[0], args.overwrite_input_size[0], args.overwrite_input_size[1])
norm_mean = None
norm_stddev = None
if args.overwrite_mean and args.overwrite_stddev:
norm_mean = tuple(args.overwrite_mean)
norm_stddev = tuple(args.overwrite_stddev)
print("Overwriting mean and stddev with", norm_mean, norm_stddev)
n_classes = None
fixed_class_order = args.fixed_class_order
# Prepare datasets/scenarios
# CIFAR100
if scenario_name == 'cifar100':
n_classes = 100
n_experiences = 10
if not fixed_class_order:
fixed_class_order = [i for i in range(n_classes)]
if args.use_rand_class_ordering:
assert not args.fixed_class_order, "Cannot use random class ordering with fixed class ordering!"
fixed_class_order = np.random.permutation(n_classes).tolist()
print("the order is ", fixed_class_order)
fixed_class_order = [int(x) for x in fixed_class_order]
if not num_experiences is None:
n_experiences = num_experiences
train_transform, test_transform = get_transforms(
use_data_aug,
input_size=input_size,
use_to_pil=False,
norm_mean=norm_mean if norm_mean else (0.485, 0.456, 0.406), # (0.5071, 0.4867, 0.4408)
norm_std=norm_stddev if norm_stddev else (0.229, 0.224, 0.225) # (0.2675, 0.2565, 0.2761)
)
print("train transform:")
print(train_transform)
altered_train_transform = train_transform #NOTE: need this potentially redundant line for 'supcon' strategy
if 'supcon' in args.strategy or 'barlow_twins' in args.strategy:
altered_train_transform = TwoCropTransform(train_transform)
print("altered transform")
print(altered_train_transform)
scenario = SplitCIFAR100(
n_experiences=n_experiences,
return_task_id=args.task_incr,
seed=seed,
fixed_class_order=fixed_class_order,
train_transform=altered_train_transform,
eval_transform=test_transform,
dataset_root=dset_rootpath
)
scenario.n_classes = n_classes
scenario.fixed_class_order = fixed_class_order
initial_out_features = n_classes // n_experiences
# MiniImageNet
elif scenario_name == 'miniimgnet':
n_classes = 100
n_experiences = 20
fixed_class_order = [i for i in range(n_classes)]
if args.use_rand_class_ordering:
print("Using random class ordering for MiniImageNet...")
fixed_class_order = np.random.permutation(n_classes).tolist()
print("the order is ", fixed_class_order)
if not num_experiences is None:
n_experiences = num_experiences
train_transform, test_transform = get_transforms(
use_data_aug,
input_size=input_size,
use_to_pil=True,
norm_mean=norm_mean if norm_mean else (0.4914, 0.4822, 0.4465),
norm_std=norm_stddev if norm_stddev else (0.2023, 0.1994, 0.2010)
)
altered_train_transform = train_transform #NOTE: need this potentially redundant line for 'supcon' strategy
if 'supcon' in args.strategy or 'barlow_twins' in args.strategy:
altered_train_transform = TwoCropTransform(train_transform)
scenario = SplitMiniImageNet(
dset_rootpath,
n_experiences=n_experiences,
return_task_id=args.task_incr, # NOTE: args.dset_rootpath as first argument (original code)
seed=seed, per_exp_classes=args.per_exp_classes_dict,
fixed_class_order=fixed_class_order,
preprocessed=True,
train_transform=altered_train_transform,
test_transform=test_transform
)
scenario.n_classes = n_classes
scenario.fixed_class_order = fixed_class_order
initial_out_features = n_classes // n_experiences # For Multi-Head
else:
raise ValueError("Unknown scenario name.")
# Cutoff if applicable
scenario.train_stream = scenario.train_stream[: args.partial_num_tasks]
scenario.test_stream = scenario.test_stream[: args.partial_num_tasks]
# Pack transforms #NOTE: this is necessary because I am unable to retrieve the transforms from the scenario object (or avalanche dataset)
data_transforms["train"] = train_transform
data_transforms["eval"] = test_transform
print(f"Scenario = {scenario_name}")
return scenario, data_transforms, input_size, initial_out_features
def get_continual_evaluation_plugins(args, scenario):
"""Plugins for per-iteration evaluation in Avalanche."""
assert args.eval_periodicity >= 1, "Need positive "
if args.eval_with_test_data:
args.evalstream_during_training = scenario.test_stream # Entire test stream
else:
args.evalstream_during_training = scenario.train_stream # Entire train stream
print(f"Evaluating on stream (eval={args.eval_with_test_data}): {args.evalstream_during_training}")
# Metrics
loss_tracking = TaskTrackingLossPluginMetric()
# Expensive metrics
gradnorm_tracking = None
if args.track_gradnorm:
gradnorm_tracking = TaskTrackingGradnormPluginMetric() # if args.track_gradnorm else None # Memory+compute expensive
# Acc derived plugins
acc_tracking = TaskTrackingAccuracyPluginMetric()
#
acc_min = TaskTrackingMINAccuracyPluginMetric()
#acc_min_avg = TaskAveragingPluginMetric(acc_min)
#wc_acc_avg = WCACCPluginMetric(acc_min)
tracking_plugins = [
loss_tracking, gradnorm_tracking, acc_tracking,
#acc_min, acc_min_avg, #wc_acc_avg
]
tracking_plugins = [p for p in tracking_plugins if p is not None]
trackerphase_plugin = ContinualEvaluationPhasePlugin(tracking_plugins=tracking_plugins,
max_task_subset_size=args.eval_task_subset_size,
eval_stream=args.evalstream_during_training,
eval_max_iterations=args.eval_max_iterations,
mb_update_freq=args.eval_periodicity,
num_workers=args.num_workers,
)
return [trackerphase_plugin, *tracking_plugins]
def get_metrics(scenario, args, data_transforms):
"""Metrics are calculated efficiently as running avgs."""
# Pass plugins, but stat_collector must be called first
minibatch_tracker_plugins = []
# Stats on external tracking stream
if args.enable_continual_eval:
tracking_plugins = get_continual_evaluation_plugins(args, scenario)
minibatch_tracker_plugins.extend(tracking_plugins)
metrics = [
#accuracy_metrics(experience=True, stream=True),
#loss_metrics(minibatch=True, experience=True, stream=True),
#ExperienceForgetting(), # Test only
#StreamForgetting(), # Test only
#StreamConfusionMatrix(num_classes=scenario.n_classes, save_image=True),
# CONTINUAL EVAL
*minibatch_tracker_plugins,
# LOG OTHER STATS
# timing_metrics(epoch=True, experience=False),
# cpu_usage_metrics(experience=True),
# DiskUsageMonitor(),
# MinibatchMaxRAM(),
# GpuUsageMonitor(0),
]
if not "supcon" in args.strategy\
and not "barlow" in args.strategy\
and not "concat" in args.strategy:
metrics.append(accuracy_metrics(experience=True, stream=True))
# Linear Probing Evaluation
if args.use_lp_eval:
print("\nAdding a probing eval plugin")
if "linear_ER" in args.use_lp_eval:
print("Using linear probe on replay buffer!")
metrics.append(LinearProbingAccuracyMetric(
criterion=torch.nn.CrossEntropyLoss(),
train_stream=scenario.train_stream,
test_stream=scenario.test_stream,
eval_all=args.lp_eval_all,
train_mb_size=128,
force_task_eval=args.lp_force_task_eval,
num_finetune_epochs=args.lp_finetune_epochs,
num_head_copies=args.lp_probe_repetitions,
skip_initial_eval=args.skip_initial_eval,
num_workers=args.num_workers,
buffer_lp_dataset=args.lp_buffer_dataset,
normalize_features=args.lp_normalize_features,
train_stream_from_ER_buffer=True
)
)
elif "concat_linear" in args.use_lp_eval:
print("Using concat linear probe")
metrics.append(ConcatenatedLinearProbingAccuracyMetric(
criterion=torch.nn.CrossEntropyLoss(),
train_stream=scenario.train_stream,
train_mb_size=args.bs,
test_stream=scenario.test_stream,
eval_all=args.lp_eval_all,
force_task_eval=args.lp_force_task_eval,
num_finetune_epochs=args.lp_finetune_epochs,
num_head_copies=args.lp_probe_repetitions,
skip_initial_eval=args.skip_initial_eval,
num_workers=args.num_workers,
buffer_lp_dataset=args.lp_buffer_dataset,
normalize_features=args.lp_normalize_features)
)
elif "concat_linear_reduce" in args.use_lp_eval:
print("Using concat linear probe with PCA reduction")
metrics.append(ConcatenatedLinearProbingAccuracyMetric(
criterion=torch.nn.CrossEntropyLoss(),
train_stream=scenario.train_stream,
train_mb_size=args.bs,
test_stream=scenario.test_stream,
eval_all=args.lp_eval_all,
force_task_eval=args.lp_force_task_eval,
num_finetune_epochs=args.lp_finetune_epochs,
num_head_copies=args.lp_probe_repetitions,
skip_initial_eval=args.skip_initial_eval,
num_workers=args.num_workers,
buffer_lp_dataset=args.lp_buffer_dataset,
normalize_features=args.lp_normalize_features,
reduce_dim_in_head=args.lp_reduce_dim,
pca_on_subset=args.lp_pca_on_subset)
)
elif "linear" in args.use_lp_eval and args.lp_eval_last_n is None:
print("Using linear probe")
metrics.append(LinearProbingAccuracyMetric(
criterion=torch.nn.CrossEntropyLoss(),
train_stream=scenario.train_stream,
test_stream=scenario.test_stream,
eval_all=args.lp_eval_all,
train_mb_size=128,
force_task_eval=args.lp_force_task_eval,
num_finetune_epochs=args.lp_finetune_epochs,
num_head_copies=args.lp_probe_repetitions,
skip_initial_eval=args.skip_initial_eval,
num_workers=args.num_workers,
buffer_lp_dataset=args.lp_buffer_dataset,
normalize_features=args.lp_normalize_features,
)
)
elif "linear" in args.use_lp_eval:
print("Using linear probe and finetune after layer", args.lp_eval_last_n)
metrics.append(LinearProbingLastNAccuracyMetricLastN(
layer_N=args.lp_eval_last_n,
train_stream=scenario.train_stream,
test_stream=scenario.test_stream,
eval_all=args.lp_eval_all,
force_task_eval=args.lp_force_task_eval,
num_finetune_epochs=args.lp_finetune_epochs,
num_head_copies=args.lp_probe_repetitions,
skip_initial_eval=args.skip_initial_eval
)
)
if 'knn_ER' in args.use_lp_eval:
print("\n\nUsing KNN probe on replay buffer!\n\n")
metrics.append(KNNProbingAccuracyMetric(
train_stream=scenario.train_stream,
test_stream=scenario.test_stream,
k=args.knn_k,
train_mb_size=args.bs,
eval_all=args.lp_eval_all,
force_task_eval=args.lp_force_task_eval,
num_finetune_epochs=args.lp_finetune_epochs,
skip_initial_eval=args.skip_initial_eval,
train_stream_from_ER_buffer=True
)
)
elif "concat_knn" in args.use_lp_eval:
print("Using KNN probe")
metrics.append(ConcatenatedKNNProbingAccuracyMetric(
train_stream=scenario.train_stream,
test_stream=scenario.test_stream,
k=args.knn_k,
train_mb_size=args.bs,
eval_all=args.lp_eval_all,
force_task_eval=args.lp_force_task_eval,
num_finetune_epochs=args.lp_finetune_epochs,
skip_initial_eval=args.skip_initial_eval,
reduce_dim_in_head=args.lp_reduce_dim,
pca_on_subset=args.lp_pca_on_subset
)
)
elif "knn_pca" in args.use_lp_eval:
print("Using KNN probe with PCA step")
metrics.append(KNNPCAProbingAccuracyMetric(
train_stream=scenario.train_stream,
test_stream=scenario.test_stream,
k=args.knn_k,
train_mb_size=args.bs,
eval_all=args.lp_eval_all,
force_task_eval=args.lp_force_task_eval,
num_finetune_epochs=args.lp_finetune_epochs,
skip_initial_eval=args.skip_initial_eval,
reduce_dim_in_head=args.lp_reduce_dim,
pca_on_subset=args.lp_pca_on_subset
)
)
elif "knn" in args.use_lp_eval:
print("Using KNN probe")
metrics.append(KNNProbingAccuracyMetric(
train_stream=scenario.train_stream,
test_stream=scenario.test_stream,
k=args.knn_k,
train_mb_size=args.bs,
eval_all=args.lp_eval_all,
force_task_eval=args.lp_force_task_eval,
num_finetune_epochs=args.lp_finetune_epochs,
skip_initial_eval=args.skip_initial_eval
)
)
if "ncm" in args.use_lp_eval:
print("Using NCM probe")
metrics.append(NCMProbingAccuracyMetric(
train_stream=scenario.train_stream,
test_stream=scenario.test_stream,
k=[1],
train_mb_size=args.bs,
eval_all=args.lp_eval_all,
force_task_eval=args.lp_force_task_eval,
num_finetune_epochs=args.lp_finetune_epochs,
skip_initial_eval=args.skip_initial_eval
)
)
elif "concat_ncm" in args.use_lp_eval:
print("Using NCM probe")
metrics.append(ConcatenatedNCMProbingAccuracyMetric(
train_stream=scenario.train_stream,
test_stream=scenario.test_stream,
k=[1],
train_mb_size=args.bs,
eval_all=args.lp_eval_all,
force_task_eval=args.lp_force_task_eval,
num_finetune_epochs=args.lp_finetune_epochs,
skip_initial_eval=args.skip_initial_eval,
reduce_dim_in_head=args.lp_reduce_dim,
pca_on_subset=args.lp_pca_on_subset
)
)
# Down Stream Evaluation (Full Sets)
if args.downstream_sets and args.downstream_method:
assert len(args.downstream_sets) == len(args.downstream_rootpaths), "Must specify a rootpath for each downstream set"
for task_id in range(len(args.downstream_sets)):
# Get the respective dataset
print("\nPrepare dataset for downstream task")
print("args.downstream_sets[task_id]", args.downstream_sets[task_id])
print("args.downstream_rootpaths[task_id]", args.downstream_rootpaths[task_id])
train_set, test_set, n_classes = get_dataset(
scenario_name=args.downstream_sets[task_id],
dset_rootpath=args.downstream_rootpaths[task_id],
train_transform=data_transforms["train"],
eval_transform=data_transforms["eval"],
#subsample_classes= scenario.fixed_class_order[
# args.downstream_subsample_tasks[0]*(scenario.n_classes // len(scenario.train_stream)):
# (args.downstream_subsample_tasks[1]+1)*(scenario.n_classes // len(scenario.train_stream))]
)
# Initialize finetune metric
if "finetune" in args.downstream_method:
metrics.append(DownstreamFinetuneAccuracyMetric(
args=args,
criterion=torch.nn.CrossEntropyLoss(),
downstream_task=args.downstream_sets[task_id],
train_set=train_set,
eval_set=test_set,
train_mb_size=args.bs,
eval_mb_size=args.bs,
n_classes = n_classes,
freeze_from=args.ft_freeze_from,
freeze_to=args.ft_freeze_to,
))
print("Added finetune donwstream evaluation for task", args.downstream_sets[task_id])
# Initialize linear metric
if "linear" in args.downstream_method:
if args.use_concatenated_eval:
metrics.append(ConcatDownStreamProbingAccuracyMetric(
args=args,
downstream_task=args.downstream_sets[task_id],
train_set=train_set, eval_set=test_set,
n_classes=n_classes
))
print("Added concatenated downstream evaluation for task", args.downstream_sets[task_id])
continue # NOTE: continue to not append the downstream metric as well because its redundant
metrics.append(DownstreamLinearProbeAccuracyMetric(
args=args,
criterion=torch.nn.CrossEntropyLoss(),
downstream_task=args.downstream_sets[task_id],
train_set=train_set,
eval_set=test_set,
train_mb_size=args.bs,
eval_mb_size=args.bs,
n_classes = n_classes,
buffer_lp_dataset=args.lp_buffer_dataset,
)
)
print("Added linear downstream evaluation for", args.downstream_sets[task_id])
# Initialize downstream metrics
if "knn" in args.downstream_method:
metrics.append(DownstreamKKNNAccuracyMetric(
args=args,
downstream_task=args.downstream_sets[task_id],
train_set=train_set,
eval_set=test_set,
k=args.knn_k
)
)
print("Added KNN downstream evaluation for", args.downstream_sets[task_id])
# Down Stream Evaluation (Subsampled Sets)
if not args.downstream_subsample_sets is None:
assert len(args.downstream_subsample_sets) == len(args.downstream_subsample_rootpaths), "Must specify a rootpath for each downstream set"
assert args.downstream_method, "Must specify a downstream method"
assert len(args.downstream_subsample_tasks) == 2*len(args.downstream_subsample_sets), "Must specify a start and end task for EACH subsampling set"
for task_id in range(len(args.downstream_subsample_sets)):
# Get the respective dataset
train_set, test_set, n_classes = get_dataset(
scenario_name=args.downstream_subsample_sets[task_id],
dset_rootpath=args.downstream_subsample_rootpaths[task_id],
train_transform=data_transforms["eval"], #train_transform=data_transforms["train"],
eval_transform=data_transforms["eval"],
subsample_classes = scenario.fixed_class_order[
args.downstream_subsample_tasks[(2*task_id)]*(scenario.n_classes // len(scenario.train_stream)):
(args.downstream_subsample_tasks[(2*task_id)+1]+1)*(scenario.n_classes // len(scenario.train_stream))]
)
if "linear_ER" in args.downstream_method:
suffix = "_" + str(args.downstream_subsample_tasks[(2*task_id)]) + "-" + str(args.downstream_subsample_tasks[(2*task_id)+1])
metrics.append(DownstreamLinearProbeAccuracyMetric(
args=args,
criterion=torch.nn.CrossEntropyLoss(),
downstream_task=args.downstream_subsample_sets[task_id] + suffix,
train_set=train_set,
eval_set=test_set,
train_mb_size=args.bs,
eval_mb_size=args.bs,
n_classes = n_classes,
buffer_lp_dataset=args.lp_buffer_dataset,
train_stream_from_ER_buffer=True
)
)
elif "linear" in args.downstream_method:
suffix = "_" + str(args.downstream_subsample_tasks[(2*task_id)]) + "-" + str(args.downstream_subsample_tasks[(2*task_id)+1])
metrics.append(DownstreamLinearProbeAccuracyMetric(
args=args,
criterion=torch.nn.CrossEntropyLoss(),
downstream_task=args.downstream_subsample_sets[task_id] + suffix,
train_set=train_set,
eval_set=test_set,
train_mb_size=args.bs,
eval_mb_size=args.bs,
n_classes = n_classes,
buffer_lp_dataset=args.lp_buffer_dataset,
)
)
if "knn_ER" in args.downstream_method:
suffix = "_" + str(args.downstream_subsample_tasks[(2*task_id)]) + "-" + str(args.downstream_subsample_tasks[(2*task_id)+1])
metrics.append(DownstreamKKNNAccuracyMetric(
args=args,
downstream_task=args.downstream_subsample_sets[task_id] + suffix,
train_set=train_set,
eval_set=test_set,
k=args.knn_k,
train_stream_from_ER_buffer=True
)
)
# PCA Evaluation
if args.pca_eval and args.downstream_sets: # NOTE: works because float 0.0 evaluates to False
print("Using PCA evaluation")
for task_id in range(len(args.downstream_sets)):
train_set, test_set, n_classes = get_dataset(
scenario_name=args.downstream_sets[task_id],
dset_rootpath=args.downstream_rootpaths[task_id],
train_transform=data_transforms["train"],
eval_transform=data_transforms["eval"]
)
if args.use_concatenated_eval:
for threshold in args.pca_eval:
metrics.append(ConcatenatedPCAProbingMetric(args=args,
downstream_task=args.scenario,
train_set=train_set, eval_set=test_set,
n_classes=n_classes,
pca_threshold=threshold)
)
else:
metrics.append(PCAProbingMetric(args=args,
downstream_task=args.downstream_sets[task_id],
eval_set=test_set,
pca_threshold=args.pca_eval)
)
print("Added PCA downstream evaluation for", args.scenario)
print("Plugins added...\n")
return metrics
def get_optimizer(optim_name,
model,
lr,
weight_decay=0.0,
betas=(0.9,0.999),
momentum=0.9,
lr_classifier=None):
params = [{"params": model.parameters(), "lr": lr}]
if lr_classifier is not None:
params = [{"params": model.feature_extractor.parameters(), "lr": lr},
{"params": model.classifier.parameters(), "lr": lr_classifier}]
if optim_name == 'sgd':
optimizer = torch.optim.SGD(params,
lr=lr, weight_decay=weight_decay, momentum=momentum)
elif optim_name == 'adam':
optimizer = torch.optim.Adam(params,
lr=lr, weight_decay=weight_decay, betas=betas)
elif optim_name == 'adamW':
optimizer = torch.optim.AdamW(params,
lr=lr, weight_decay=weight_decay, betas=betas)
else:
print("No optimizer found for name", optim_name)
raise ValueError()
return optimizer
def get_strategy(args, model, eval_plugin, scenario, device,
plugins=None, data_transforms=None):
plugins = [] if plugins is None else plugins
# CRIT/OPTIM
criterion = torch.nn.CrossEntropyLoss()
optimizer = get_optimizer(args.optim,
model,
args.lr,
weight_decay=args.weight_decay,
momentum=args.momentum,
lr_classifier=args.lr_classifier)
initial_epochs = args.epochs[0]
# lr scheduler per experience
if args.one_cycle_lr_schedule:
print("\nUsing OneCycleLR")
# NOTE: requires num_batches, e.g. iterations per epoch
plugins.append(
OneCycleSchedulerPlugin(
optimizer = optimizer,
max_lr = args.lr,
start_lr=args.one_cycle_start_lr,
final_lr=args.one_cycle_final_lr,
epochs=args.epochs,
warmup_epochs=args.one_cycle_warmup_epochs,
three_phase=True
)
)
else:
# lr-schedule over experiences
if args.lr_decay > 0:
if len(args.epochs) > 1:
raise NotImplementedError("lr_decay not implemented for dynamic epoch length training")
print("\nAdding LRScheduler...")
milestones = list(range(1, len(scenario.train_stream))) #NOTE: this is applying the lr_decay after each experience!
print("milestines:", milestones)
sched = ExpLRSchedulerPlugin(MultiStepLR(optimizer, milestones=milestones, gamma=args.lr_decay))
plugins.append(sched)
print("Added ExperienceLRScheduler, with decay", args.lr_decay, "at milestones", milestones)
elif args.lr_decay_epoch > 0:
plugins.append(EpochLRSchedulerPlugin(
MultiStepLR(optimizer, milestones=args.lr_decay_steps_epoch, gamma=args.lr_decay_epoch)
))
print("Added EpochLRSchedulerPlugin, with decay", args.lr_decay_epoch, "at milestones", args.lr_decay_steps_epoch)
# Use Iterations if defined
if args.iterations_per_task is not None:
args.epochs = [int(1e9)] # NOTE: something absurdly high to make sure we don't stop early
initial_epochs = args.epochs[0]
it_stopper = IterationsInsteadOfEpochs(max_iterations=args.iterations_per_task)
plugins.append(it_stopper)
print("\nUsing iterations instead of epochs, with", args.iterations_per_task, "iterations per task")
# STRATEGY
if args.strategy == 'finetune':
strategy = Naive(model, optimizer, criterion,
train_epochs=initial_epochs, device=device,
train_mb_size=args.bs, evaluator=eval_plugin,
plugins=plugins
)
elif args.strategy == 'concat_finetune':
strategy = Naive(model, optimizer, criterion,
train_epochs=initial_epochs, device=device,
train_mb_size=args.bs, evaluator=eval_plugin,
plugins=plugins
)
strategy.plugins.append(
ConcatFeatClassifierAdapterPlugin()
)
elif args.strategy == 'supcon':
strategy = SupCon(
model,
optimizer,
train_epochs=initial_epochs,
train_mb_size=args.bs,
eval_mb_size=args.bs,
train_transforms=TwoCropTransform(data_transforms['train']),
eval_transforms=data_transforms['eval'],
evaluator=eval_plugin,
plugins=plugins,
supcon_temperature=args.supcon_temperature,
device=device
)
elif args.strategy == 'supcon_spread':
strategy = SupCon(
model,
optimizer,
train_epochs=initial_epochs,
train_mb_size=args.bs,
eval_mb_size=args.bs,
train_transforms=TwoCropTransform(data_transforms['train']),
eval_transforms=data_transforms['eval'],
evaluator=eval_plugin,
plugins=plugins,
device=device,
use_spread_loss=True,
alpha=args.supcon_spread_alpha
)
elif args.strategy == 'supcon_joint':
strategy = SupCon(
model,
optimizer,
train_epochs=initial_epochs,
train_mb_size=args.bs,
eval_mb_size=args.bs,
train_transforms=TwoCropTransform(data_transforms['train']),
eval_transforms=data_transforms['eval'],
evaluator=eval_plugin,
plugins=plugins,
device=device
)
strategy.plugins.append(
JointTrainingPlugin()
)
elif args.strategy == 'barlow_twins':
strategy = BarlowTwins(
model,
optimizer,
train_epochs=initial_epochs,
train_mb_size=args.bs,
eval_mb_size=args.bs,
train_transforms=TwoCropTransform(data_transforms['train']),
eval_transforms=data_transforms['eval'],
evaluator=eval_plugin,
plugins=plugins,
device=device,
projection_dim=args.projector_dim
)
elif args.strategy == 'barlow_twins_joint':
strategy = BarlowTwins(
model,
optimizer,
train_epochs=initial_epochs,
train_mb_size=args.bs,
eval_mb_size=args.bs,
train_transforms=TwoCropTransform(data_transforms['train']),
eval_transforms=data_transforms['eval'],
evaluator=eval_plugin,
plugins=plugins,
device=device,
projection_dim=1024
)
strategy.plugins.append(
JointTrainingPlugin()
)
elif args.strategy == 'joint':
strategy = BaseStrategy(
model,
optimizer,
criterion,
train_mb_size=args.bs,
train_epochs=initial_epochs, eval_mb_size=256, eval_every=-1,
evaluator=eval_plugin, device=device,
plugins=plugins
)
strategy.plugins.append(
JointTrainingPlugin()
)
elif args.strategy == 'concat_joint':
strategy = BaseStrategy(
model,
optimizer,
criterion,
train_mb_size=args.bs,
train_epochs=initial_epochs, eval_mb_size=256, eval_every=-1,
evaluator=eval_plugin, device=device,
plugins=plugins
)
strategy.plugins.append(
ConcatFeatClassifierAdapterPlugin(reinit_all_backbones=True)
)
strategy.plugins.append(
ConcatJointTrainingPlugin()
)
elif args.strategy == 'separate_networks':
strategy = SeparateNetworks(
model,
optimizer,
criterion,
train_mb_size=args.bs,
train_epochs=initial_epochs,
eval_mb_size=256,
eval_every=-1,
evaluator=eval_plugin,
device=device,