-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
457 lines (378 loc) · 14.8 KB
/
main.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
import os
import torch
import torch.optim.lr_scheduler
from types import MethodType
from torch import nn
from torch.nn import CrossEntropyLoss
from torchvision.models import resnet18
from avalanche.models import SimpleMLP, FeatureExtractorModel
from avalanche.models.resnet32 import ResNet, BasicBlock
from avalanche.training.determinism.rng_manager import RNGManager
from avalanche.checkpointing import maybe_load_checkpoint, save_checkpoint
from avalanche.evaluation.metrics import (
forgetting_metrics,
accuracy_metrics,
loss_metrics,
)
from avalanche.logging import InteractiveLogger
from avalanche.training.plugins import (
EvaluationPlugin, LwFPlugin, EWCPlugin, SynapticIntelligencePlugin, FeatureDistillationPlugin)
from avalanche.training.supervised import Naive, Cumulative
from avalanche.benchmarks import SplitMNIST, SplitFMNIST, SplitCIFAR10, SplitCIFAR100
from avalanche.training.self_supervised import Naive as SelfSupervisedNaive
from src.args import parse_args
from src.benchmarks import *
from src.optimizers import *
from src.transforms import *
from src.criterions import *
from src.loggers import *
from src.models import *
from src.plugins import *
def get_model(model_name, num_classes, device, no_head=False):
if model_name == "simple_mlp":
model = SimpleMLP(
num_classes=num_classes,
input_size=3 * args.image_size * args.image_size,
hidden_size=512,
)
model.classifier = nn.Identity()
if no_head:
model = FeatureExtractorModel(
model,
nn.Identity(),
)
else:
model = FeatureExtractorModel(
model,
nn.Linear(512, num_classes)
)
elif model_name == "resnet_18":
model = resnet18(pretrained=False)
in_features = model.fc.in_features
model.fc = nn.Identity()
if no_head:
model = FeatureExtractorModel(
model,
nn.Identity(),
)
else:
model = FeatureExtractorModel(
model,
nn.Linear(in_features, num_classes)
)
elif model_name == "resnet32s":
model = ResNet(BasicBlock, [5, 5, 5], num_classes=num_classes)
in_features = model.fc.in_features
model.fc = nn.Identity()
if no_head:
model = FeatureExtractorModel(
model,
nn.Identity(),
)
else:
model = FeatureExtractorModel(
model,
nn.Linear(in_features, num_classes)
)
elif model_name == "resnet18_encoder" or model_name == "resnet18_mini_encoder":
model = ResNet18(mini_version=model_name == "resnet18_mini_encoder")
model = FeatureExtractorModel(
model,
nn.Identity(),
)
else:
raise NotImplementedError
return model.to(device)
def get_benchmark(benchmark_name, seed, train_transform, eval_transform, n_experiences=1, dataset_root=None):
base_params = {
"n_experiences": n_experiences,
"shuffle": True,
"seed": seed,
"train_transform": train_transform,
"eval_transform": eval_transform,
}
if dataset_root is not None:
base_params["dataset_root"] = dataset_root
if benchmark_name == "split_mnist":
benchmark_class = SplitMNIST
num_classes = 10
elif benchmark_name == "split_fashion_mnist":
benchmark_class = SplitFMNIST
num_classes = 10
elif benchmark_name == "split_cifar10":
benchmark_class = SplitCIFAR10
num_classes = 10
elif benchmark_name == "split_cifar100":
benchmark_class = SplitCIFAR100
num_classes = 100
elif benchmark_name == "concon_strict":
benchmark_class = ConConStrict
num_classes = 2
elif benchmark_name == "concon_disjoint":
benchmark_class = ConConDisjoint
num_classes = 2
elif benchmark_name == "concon_unconfounded":
benchmark_class = ConConUnconfounded
num_classes = 2
else:
raise NotImplementedError
benchmark = benchmark_class(**base_params)
# Add the name of the benchmark at the beginning of the name of all streams
benchmark.train_stream.name = f"{benchmark_name}_{benchmark.train_stream.name}"
benchmark.test_stream.name = f"{benchmark_name}_{benchmark.test_stream.name}"
# Update keys in stream_definitions
if hasattr(benchmark, "stream_definitions"):
benchmark.stream_definitions[f"{benchmark_name}_train"] = benchmark.stream_definitions.pop("train")
benchmark.stream_definitions[f"{benchmark_name}_test"] = benchmark.stream_definitions.pop("test")
# Update keys in streams
benchmark._streams[f"{benchmark_name}_train"] = benchmark._streams.pop("train")
benchmark._streams[f"{benchmark_name}_test"] = benchmark._streams.pop("test")
return benchmark, num_classes
def get_strategy(args, model, optimizer, device, plugins, eval_plugin):
strategy_class = None
base_params = {
"model": model,
"optimizer": optimizer,
"train_epochs": args.epochs,
"train_mb_size": args.batch_size,
"eval_mb_size": args.batch_size,
"device": device,
"plugins": plugins,
"evaluator": eval_plugin,
"eval_every": args.eval_every,
}
if args.criterion == "CE":
assert args.loss_type == "supervised"
base_params["criterion"] = CrossEntropyLoss()
elif args.criterion == "barlow_twins":
assert args.loss_type == "self_supervised"
base_params["criterion"] = BarlowTwinsLoss()
base_params["ss_augmentations"] = BTTrainingAugmentations(
image_size=args.image_size
)
elif args.criterion == "emp_ssl":
assert args.loss_type == "self_supervised"
base_params["criterion"] = EMPSLLLoss()
base_params["ss_augmentations"] = EMPSSLTrainingAugmentations(
image_size=args.image_size,
num_patch=100
)
else:
raise NotImplementedError
if args.strategy == "naive":
if args.loss_type == "self_supervised":
strategy_class = SelfSupervisedNaive
base_params["eval_criterion"] = torch.nn.CrossEntropyLoss()
elif args.loss_type == "supervised":
strategy_class = Naive
else:
raise NotImplementedError
elif args.strategy == "cumulative":
strategy_class = Cumulative
# ADD YOUR CUSTOM STRATEGIES HERE
else:
raise NotImplementedError
return strategy_class(**base_params)
def evaluate_strategy(strategy, eval_benchmarks):
for benchmark, benchmark_name, current_classnames, current_bias_list in eval_benchmarks:
print(f"Evaluating benchmark {benchmark_name}")
strategy.eval(
benchmark.test_stream,
benchmark_name=benchmark_name,
current_classnames=current_classnames,
current_bias_list=current_bias_list,
)
def run_experiment(args, seed):
# --- CONFIG
RNGManager.set_random_seeds(seed)
torch.backends.cudnn.deterministic = True
device = torch.device(
f"cuda:{args.cuda}" if torch.cuda.is_available(
) and args.cuda >= 0 else "cpu"
)
run_name = f"{args.strategy}_w_{args.model}_on_{args.benchmark}_loss({args.loss_type})_criterion({args.criterion})"
run_name += f"_epochs({args.epochs})_exps({args.n_experiences})_lr({args.lr})_bs({args.batch_size})"
# ADD CUSTOM PARAMETERS TO THE RUN NAME HERE
if "lwf" in args.plugins:
run_name += f"_lwf_alpha({args.lwf_alpha})_temp({args.lwf_temperature})"
if "feature_distillation" in args.plugins:
run_name += f"_fd_alpha({args.fd_alpha})_mode({args.fd_mode})"
if "ewc" in args.plugins:
run_name += f"_ewc_lambda({args.ewc_lambda})"
if "si" in args.plugins:
run_name += f"_si_lambda({args.si_lambda})"
if "linear_probing" in args.plugins:
run_name += "_lp"
run_name += f"_lr({args.probe_lr})_epochs({args.probe_epochs})"
if "shrink_and_perturb" in args.plugins:
run_name += f"_shpe({args.shrink}_{args.perturb})"
run_name += f"_every({args.sp_every})"
if "random_perturb" in args.plugins:
run_name += f"_rp_std({args.rp_std})_sensitivity({args.rp_sensitivity})"
run_name += f"_every({args.rp_every})"
if "vanilla_model_merging" in args.plugins:
run_name += f"_vmm({args.merge_coeff})"
run_name += f"_every({args.mm_every})"
# ADD CUSTOM PLUGIN PARAMETERS TO THE RUN NAME HERE
output_dir = os.path.join(
args.output_dir, args.benchmark, run_name, str(seed))
os.makedirs(output_dir, exist_ok=True)
logs_dir = os.path.join(output_dir, "logs")
os.makedirs(logs_dir, exist_ok=True)
checkpoint_path = os.path.join(output_dir, "checkpoint.pkl")
shape = (args.image_size, args.image_size, 3)
if os.path.exists(os.path.join(output_dir, "completed.txt")):
print(f"Experiment with seed {seed} already completed")
return
# SAVE CONFIG
with open(os.path.join(logs_dir, "config.txt"), "w") as f:
f.write(str(args))
# TRANSFORMS CREATION
if args.transform == "none":
train_transform, eval_transform = None, None
elif args.transform == "mnist":
train_transform, eval_transform = MNISTTransform(args.image_size)
elif args.transform == "cifar":
train_transform, eval_transform = CIFARTransform(args.image_size)
elif args.transform == "barlow_twins":
train_transform, eval_transform = BarlowTwinsTransform(args.image_size)
elif args.transform == "emp_ssl":
train_transform, eval_transform = EMPSSLTransform(args.image_size)
else:
raise NotImplementedError
# TRAIN BENCHMARK CREATION
benchmark, num_classes = get_benchmark(args.benchmark, seed, train_transform, eval_transform,
n_experiences=args.n_experiences, dataset_root=args.dataset_root)
# EVAL BENCHMARK CREATION
eval_benchmarks = [
get_benchmark(benchmark_name, seed, train_transform, eval_transform,
n_experiences=args.n_experiences, dataset_root=args.dataset_root)[0]
for benchmark_name in args.eval_benchmarks
]
# MODEL CREATION
no_head = args.loss_type == "self_supervised"
model = get_model(args.model, num_classes, device, no_head=no_head)
# CREATE THE OPTIMIZER
if args.optimizer == "adam":
optimizer = torch.optim.Adam(
model.parameters(),
lr=args.lr)
elif args.optimizer == "sgd" or args.optimizer == "lars":
optimizer = torch.optim.SGD(
model.parameters(),
lr=args.lr,
momentum=args.momentum,
nesterov=args.nesterov,
weight_decay=args.weight_decay
)
if args.optimizer == "lars":
optimizer = LARSWrapper(
optimizer, eta=0.005, clip=True, exclude_bias_n_norm=True,)
else:
raise NotImplementedError
# LOGGERS
interactive_logger = InteractiveLogger()
csv_logger = CSVLogger(logs_dir)
loggers = [interactive_logger, csv_logger]
if args.wandb:
all_configs = {
"args": vars(args),
}
loggers.append(WandBLogger(
project_name=args.project_name,
run_name=run_name,
config=all_configs,
))
# METRICS
metrics = []
if "loss" in args.metrics:
metrics.append(loss_metrics(
minibatch=True, epoch=True, experience=True, stream=True))
if "accuracy" in args.metrics:
keep_track_during_training = args.loss_type != "self_supervised"
metrics.append(accuracy_metrics(
minibatch=keep_track_during_training,
epoch=keep_track_during_training,
experience=True, stream=True
))
if "forgetting" in args.metrics:
metrics.append(forgetting_metrics(experience=True, stream=True))
eval_plugin = EvaluationPlugin(
*metrics,
loggers=loggers,
)
# CREATE THE PLUGINS
plugins = []
if "lwf" in args.plugins:
plugins.append(LwFPlugin(
alpha=args.lwf_alpha,
temperature=args.lwf_temperature,
))
if "feature_distillation" in args.plugins:
plugins.append(FeatureDistillationPlugin(
alpha=args.fd_alpha,
mode=args.fd_mode,
))
if "ewc" in args.plugins:
plugins.append(EWCPlugin(
ewc_lambda=args.ewc_lambda,
))
if "si" in args.plugins:
plugins.append(SynapticIntelligencePlugin(
si_lambda=args.si_lambda,
))
if "linear_probing" in args.plugins:
plugins.append(LinearProbingPlugin(
benchmark=get_benchmark(args.benchmark, seed, train_transform, eval_transform,
n_experiences=1, dataset_root=args.dataset_root)[0],
num_classes=num_classes,
epochs=args.probe_epochs,
lr=args.probe_lr,
))
if "shrink_and_perturb" in args.plugins:
plugins.append(ShrinkAndPerturbPlugin(
shrink=args.shrink,
perturb=args.perturb,
every=args.sp_every
))
if "random_perturb" in args.plugins:
plugins.append(RandomPerturbPlugin(
perturb_std_ratio=args.rp_std,
magnitude_sensitivity=args.rp_sensitivity,
every=args.rp_every
))
if "vanilla_model_merging" in args.plugins:
plugins.append(VanillaModelMergingPlugin(
merge_coeff=args.merge_coeff,
every=args.mm_every
))
# CREATE THE STRATEGY INSTANCE
cl_strategy = get_strategy(
args, model, optimizer, device, plugins, eval_plugin)
if args.resume_from_checkpoint:
cl_strategy, initial_exp = maybe_load_checkpoint(
cl_strategy, checkpoint_path)
else:
initial_exp = 0
# TRAINING LOOP
print("Starting experiment...")
for experience in benchmark.train_stream[initial_exp:]:
print("Start of experience ", experience.current_experience)
cl_strategy.train(experience)
print("Training completed")
print("Evaluation of the current strategy:")
for eval_benchmark in eval_benchmarks:
cl_strategy.eval(eval_benchmark.test_stream)
print("Evaluation completed")
# print("Saving checkpoint")
# save_checkpoint(cl_strategy, checkpoint_path)
with open(os.path.join(output_dir, "completed.txt"), "w") as f:
f.write(":)")
if args.remove_checkpoints:
os.system(f"rm -rf {os.path.join(output_dir, 'checkpoints')}")
def main(args):
for seed in args.seeds:
run_experiment(args, seed)
if __name__ == "__main__":
args = parse_args()
main(args)