-
Notifications
You must be signed in to change notification settings - Fork 54
/
train_sl.py
907 lines (787 loc) · 45.8 KB
/
train_sl.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
import logging
import wandb
import os
import os.path as osp
import sys
import time
import torch
import math
import numpy as np
import torch.nn as nn
import torch.optim as optim
from ray import tune
from cords.selectionstrategies.helpers.ssl_lib.param_scheduler import scheduler as step_scheduler
from cords.utils.data.data_utils import WeightedSubset
from cords.utils.data.dataloader.SL.adaptive import GLISTERDataLoader, AdaptiveRandomDataLoader, StochasticGreedyDataLoader,\
CRAIGDataLoader, GradMatchDataLoader, RandomDataLoader, WeightedRandomDataLoader, MILODataLoader, SELCONDataLoader
from cords.utils.data.dataloader.SL.nonadaptive import FacLocDataLoader, MILOFixedDataLoader
from cords.utils.data.datasets.SL import gen_dataset
from cords.utils.models import *
from cords.utils.data.data_utils.collate import *
import pickle
from datetime import datetime
class TrainClassifier:
def __init__(self, config_file_data):
self.cfg = config_file_data
results_dir = osp.abspath(osp.expanduser(self.cfg.train_args.results_dir))
if self.cfg.dss_args.type in ['StochasticGreedyExploration', 'WeightedRandomExploration', 'SGE', 'WRE']:
subset_selection_name = self.cfg.dss_args.type + "_" + self.cfg.dss_args.submod_function + "_" + str(self.cfg.dss_args.kw)
elif self.cfg.dss_args.type in ['MILO']:
subset_selection_name = self.cfg.dss_args.type + "_" + self.cfg.dss_args.submod_function + "_" + str(self.cfg.dss_args.gc_ratio) + "_" + str(self.cfg.dss_args.kw)
else:
subset_selection_name = self.cfg.dss_args.type
all_logs_dir = os.path.join(results_dir,
self.cfg.setting,
self.cfg.dataset.name,
subset_selection_name,
self.cfg.model.architecture,
str(self.cfg.dss_args.fraction),
str(self.cfg.dss_args.select_every),
str(self.cfg.train_args.run))
os.makedirs(all_logs_dir, exist_ok=True)
# setup logger
plain_formatter = logging.Formatter("[%(asctime)s] %(name)s %(levelname)s: %(message)s",
datefmt="%m/%d %H:%M:%S")
now = datetime.now()
current_time = now.strftime("%y/%m/%d %H:%M:%S")
self.logger = logging.getLogger(__name__+" " + current_time)
self.logger.setLevel(logging.INFO)
s_handler = logging.StreamHandler(stream=sys.stdout)
s_handler.setFormatter(plain_formatter)
s_handler.setLevel(logging.INFO)
self.logger.addHandler(s_handler)
f_handler = logging.FileHandler(os.path.join(all_logs_dir, self.cfg.dataset.name + "_" +
self.cfg.dss_args.type + ".log"), mode='w')
f_handler.setFormatter(plain_formatter)
f_handler.setLevel(logging.DEBUG)
self.logger.addHandler(f_handler)
self.logger.propagate = False
"""
############################## Loss Evaluation ##############################
"""
def model_eval_loss(self, data_loader, model, criterion):
total_loss = 0
with torch.no_grad():
for _, (inputs, targets) in enumerate(data_loader):
inputs, targets = inputs.to(self.cfg.train_args.device), \
targets.to(self.cfg.train_args.device, non_blocking=True)
outputs = model(inputs)
loss = criterion(outputs, targets)
total_loss += loss.item()
return total_loss
"""
############################## Model Creation ##############################
"""
def create_model(self):
if self.cfg.model.architecture == 'RegressionNet':
model = RegressionNet(self.cfg.model.input_dim)
elif self.cfg.model.architecture == 'ResNet18':
model = ResNet18(self.cfg.model.numclasses)
if self.cfg.dataset.name in ['cifar10', 'cifar100', 'tinyimagenet']:
model.conv1 = nn.Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
model.maxpool = nn.Identity()
elif self.cfg.model.architecture == 'ResNet101':
model = ResNet101(self.cfg.model.numclasses)
if self.cfg.dataset.name in ['cifar10', 'cifar100', 'tinyimagenet']:
model.conv1 = nn.Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
model.maxpool = nn.Identity()
elif self.cfg.model.architecture == 'MnistNet':
model = MnistNet()
elif self.cfg.model.architecture == 'ResNet164':
model = ResNet164(self.cfg.model.numclasses)
elif self.cfg.model.architecture == 'MobileNet':
model = MobileNet(self.cfg.model.numclasses)
elif self.cfg.model.architecture == 'MobileNetV2':
model = MobileNetV2(self.cfg.model.numclasses)
elif self.cfg.model.architecture == 'MobileNet2':
model = MobileNet2(output_size=self.cfg.model.numclasses)
elif self.cfg.model.architecture == 'HyperParamNet':
model = HyperParamNet(self.cfg.model.l1, self.cfg.model.l2)
elif self.cfg.model.architecture == 'ThreeLayerNet':
model = ThreeLayerNet(self.cfg.model.input_dim, self.cfg.model.num_classes, self.cfg.model.h1, self.cfg.model.h2)
elif self.cfg.model.architecture == 'LSTM':
model = LSTMClassifier(self.cfg.model.numclasses, self.cfg.model.wordvec_dim, \
self.cfg.model.weight_path, self.cfg.model.num_layers, self.cfg.model.hidden_size)
else:
raise(NotImplementedError)
model = model.to(self.cfg.train_args.device)
return model
"""
############################## Loss Type, Optimizer and Learning Rate Scheduler ##############################
"""
def loss_function(self):
if self.cfg.loss.type == "CrossEntropyLoss":
criterion = nn.CrossEntropyLoss()
criterion_nored = nn.CrossEntropyLoss(reduction='none')
elif self.cfg.loss.type == "MeanSquaredLoss":
criterion = nn.MSELoss()
criterion_nored = nn.MSELoss(reduction='none')
return criterion, criterion_nored
def optimizer_with_scheduler(self, model):
if self.cfg.optimizer.type == 'sgd':
if ('ResNet' in self.cfg.model.architecture) and ('lr1' in self.cfg.optimizer.keys()) and ('lr2' in self.cfg.optimizer.keys()) and ('lr3' in self.cfg.optimizer.keys()):
optimizer = optim.SGD( [{"params": model.linear.parameters(), "lr": self.cfg.optimizer.lr1},
{"params": model.layer4.parameters(), "lr": self.cfg.optimizer.lr2},
{"params": model.layer3.parameters(), "lr": self.cfg.optimizer.lr2},
{"params": model.layer2.parameters(), "lr": self.cfg.optimizer.lr2},
{"params": model.layer1.parameters(), "lr": self.cfg.optimizer.lr2},
{"params": model.conv1.parameters(), "lr": self.cfg.optimizer.lr3}],
lr=self.cfg.optimizer.lr,
momentum=self.cfg.optimizer.momentum,
weight_decay=self.cfg.optimizer.weight_decay,
nesterov=self.cfg.optimizer.nesterov)
else:
optimizer = optim.SGD(model.parameters(),
lr=self.cfg.optimizer.lr,
momentum=self.cfg.optimizer.momentum,
weight_decay=self.cfg.optimizer.weight_decay,
nesterov=self.cfg.optimizer.nesterov)
elif self.cfg.optimizer.type == "adam":
optimizer = optim.Adam(model.parameters(), lr=self.cfg.optimizer.lr, weight_decay=self.cfg.optimizer.weight_decay)
elif self.cfg.optimizer.type == "rmsprop":
optimizer = optim.RMSprop(model.parameters(), lr=self.cfg.optimizer.lr)
if self.cfg.scheduler.type == 'cosine_annealing':
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer,
T_max=self.cfg.scheduler.T_max)
elif self.cfg.scheduler.type == 'cosine_annealing_WS':
scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer,
T_0=self.cfg.scheduler.T_0,
T_mult=self.cfg.scheduler.T_mult)
elif self.cfg.scheduler.type == 'linear_decay':
scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
step_size=self.cfg.scheduler.stepsize,
gamma=self.cfg.scheduler.gamma)
elif self.cfg.scheduler.type == 'multistep':
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=self.cfg.scheduler.milestones,
gamma=self.cfg.scheduler.gamma)
elif self.cfg.scheduler.type == 'cosine_annealing_step':
scheduler = step_scheduler.CosineAnnealingLR(optimizer, max_iteration=self.cfg.scheduler.max_steps)
else:
scheduler = None
return optimizer, scheduler
@staticmethod
def generate_cumulative_timing(mod_timing):
tmp = 0
mod_cum_timing = np.zeros(len(mod_timing))
for i in range(len(mod_timing)):
tmp += mod_timing[i]
mod_cum_timing[i] = tmp
return mod_cum_timing
@staticmethod
def save_ckpt(state, ckpt_path):
torch.save(state, ckpt_path)
@staticmethod
def load_ckpt(ckpt_path, model, optimizer):
checkpoint = torch.load(ckpt_path)
start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
loss = checkpoint['loss']
metrics = checkpoint['metrics']
return start_epoch, model, optimizer, loss, metrics
def count_pkl(self, path):
if not osp.exists(path):
return -1
return_val = 0
file = open(path, 'rb')
while(True):
try:
_ = pickle.load(file)
return_val += 1
except EOFError:
break
file.close()
return return_val
def train(self, **kwargs):
"""
############################## General Training Loop with Data Selection Strategies ##############################
"""
# Loading the Dataset
logger = self.logger
if ('trainset' in kwargs) and ('validset' in kwargs) and ('testset' in kwargs) and ('num_cls' in kwargs):
trainset, validset, testset, num_cls = kwargs['trainset'], kwargs['validset'], kwargs['testset'], kwargs['num_cls']
else:
#logger.info(self.cfg)
if self.cfg.dataset.feature == 'classimb':
trainset, validset, testset, num_cls = gen_dataset(self.cfg.dataset.datadir,
self.cfg.dataset.name,
self.cfg.dataset.feature,
classimb_ratio=self.cfg.dataset.classimb_ratio, dataset=self.cfg.dataset)
else:
trainset, validset, testset, num_cls = gen_dataset(self.cfg.dataset.datadir,
self.cfg.dataset.name,
self.cfg.dataset.feature, dataset=self.cfg.dataset)
trn_batch_size = self.cfg.dataloader.batch_size
val_batch_size = self.cfg.dataloader.batch_size
tst_batch_size = self.cfg.dataloader.batch_size
batch_sampler = lambda _, __ : None
drop_last = False
if self.cfg.dss_args.type in ['SELCON']:
drop_last = True
assert(self.cfg.dataset.name in ['LawSchool_selcon', 'Community_Crime'])
if self.cfg.dss_arg.batch_sampler == 'sequential':
batch_sampler = lambda dataset, bs : torch.utils.data.BatchSampler(
torch.utils.data.SequentialSampler(dataset), batch_size=bs, drop_last=True
) # sequential
elif self.cfg.dss_arg.batch_sampler == 'random':
batch_sampler = lambda dataset, bs : torch.utils.data.BatchSampler(
torch.utils.data.RandomSampler(dataset), batch_size=bs, drop_last=True
) # random
if self.cfg.dataset.name == "sst2_facloc" and self.count_pkl(self.cfg.dataset.ss_path) == 1 and self.cfg.dss_args.type == 'FacLoc':
self.cfg.dss_args.type = 'Full'
file_ss = open(self.cfg.dataset.ss_path, 'rb')
ss_indices = pickle.load(file_ss)
file_ss.close()
trainset = torch.utils.data.Subset(trainset, ss_indices)
if 'collate_fn' not in self.cfg.dataloader.keys():
collate_fn = None
else:
collate_fn = self.cfg.dataloader.collate_fn
# Creating the Data Loaders
trainloader = torch.utils.data.DataLoader(trainset, batch_size=trn_batch_size, sampler=batch_sampler(trainset, trn_batch_size),
shuffle=False, pin_memory=True, collate_fn = collate_fn, drop_last=drop_last)
valloader = torch.utils.data.DataLoader(validset, batch_size=val_batch_size, sampler=batch_sampler(validset, val_batch_size),
shuffle=False, pin_memory=True, collate_fn = collate_fn, drop_last=drop_last)
testloader = torch.utils.data.DataLoader(testset, batch_size=tst_batch_size, sampler=batch_sampler(testset, tst_batch_size),
shuffle=False, pin_memory=True, collate_fn = collate_fn, drop_last=drop_last)
train_eval_loader = torch.utils.data.DataLoader(trainset, batch_size=trn_batch_size * 20, sampler=batch_sampler(trainset, trn_batch_size),
shuffle=False, pin_memory=True, collate_fn = collate_fn, drop_last=drop_last)
val_eval_loader = torch.utils.data.DataLoader(validset, batch_size=val_batch_size * 20, sampler=batch_sampler(validset, val_batch_size),
shuffle=False, pin_memory=True, collate_fn = collate_fn, drop_last=drop_last)
test_eval_loader = torch.utils.data.DataLoader(testset, batch_size=tst_batch_size * 20, sampler=batch_sampler(testset, tst_batch_size),
shuffle=False, pin_memory=True, collate_fn = collate_fn, drop_last=drop_last)
substrn_losses = list() # np.zeros(cfg['train_args']['num_epochs'])
trn_losses = list()
val_losses = list() # np.zeros(cfg['train_args']['num_epochs'])
tst_losses = list()
subtrn_losses = list()
timing = []
trn_acc = list()
val_acc = list() # np.zeros(cfg['train_args']['num_epochs'])
tst_acc = list() # np.zeros(cfg['train_args']['num_epochs'])
best_acc = list()
curr_best_acc = 0
subtrn_acc = list() # np.zeros(cfg['train_args']['num_epochs'])
# Checkpoint file
checkpoint_dir = osp.abspath(osp.expanduser(self.cfg.ckpt.dir))
if self.cfg.dss_args.type in ['StochasticGreedyExploration', 'WeightedRandomExploration', 'SGE', 'WRE']:
subset_selection_name = self.cfg.dss_args.type + "_" + self.cfg.dss_args.submod_function + "_" + str(self.cfg.dss_args.kw)
elif self.cfg.dss_args.type in ['MILO']:
subset_selection_name = self.cfg.dss_args.type + "_" + self.cfg.dss_args.submod_function + "_" + str(self.cfg.dss_args.gc_ratio) + "_" + str(self.cfg.dss_args.kw)
else:
subset_selection_name = self.cfg.dss_args.type
ckpt_dir = os.path.join(checkpoint_dir,
self.cfg.setting,
self.cfg.dataset.name,
subset_selection_name,
self.cfg.model.architecture,
str(self.cfg.dss_args.fraction),
str(self.cfg.dss_args.select_every),
str(self.cfg.train_args.run))
checkpoint_path = os.path.join(ckpt_dir, 'model.pt')
os.makedirs(ckpt_dir, exist_ok=True)
# Model Creation
model = self.create_model()
if self.cfg.train_args.wandb:
wandb.watch(model)
# model1 = self.create_model()
#Initial Checkpoint Directory
init_ckpt_dir = os.path.abspath(os.path.expanduser("checkpoints"))
os.makedirs(init_ckpt_dir, exist_ok=True)
model_name = ""
for key in self.cfg.model.keys():
if r"/" not in str(self.cfg.model[key]):
model_name += (str(self.cfg.model[key]) + "_")
if model_name[-1] == "_":
model_name = model_name[:-1]
if not os.path.exists(os.path.join(init_ckpt_dir, model_name + ".pt")):
ckpt_state = {'state_dict': model.state_dict()}
# save checkpoint
self.save_ckpt(ckpt_state, os.path.join(init_ckpt_dir, model_name + ".pt"))
else:
checkpoint = torch.load(os.path.join(init_ckpt_dir, model_name + ".pt"))
model.load_state_dict(checkpoint['state_dict'])
# Loss Functions
criterion, criterion_nored = self.loss_function()
if self.cfg.scheduler.type == "cosine_annealing_step":
if self.cfg.dss_args.type == "Full":
self.cfg.scheduler.max_steps = math.ceil(len(list(dataloader.batch_sampler)) * self.cfg.train_args.num_epochs)
else:
self.cfg.scheduler.max_steps = math.ceil(len(list(dataloader.subset_loader.batch_sampler)) * self.cfg.train_args.num_epochs)
# * self.cfg.dss_args.fraction)
# Getting the optimizer and scheduler
optimizer, scheduler = self.optimizer_with_scheduler(model)
"""
############################## Custom Dataloader Creation ##############################
"""
if 'collate_fn' not in self.cfg.dss_args:
self.cfg.dss_args.collate_fn = None
if self.cfg.dss_args.type in ['GradMatch', 'GradMatchPB', 'GradMatch-Warm', 'GradMatchPB-Warm']:
"""
############################## GradMatch Dataloader Additional Arguments ##############################
"""
self.cfg.dss_args.model = model
self.cfg.dss_args.loss = criterion_nored
self.cfg.dss_args.eta = self.cfg.optimizer.lr
self.cfg.dss_args.num_classes = self.cfg.model.numclasses
self.cfg.dss_args.num_epochs = self.cfg.train_args.num_epochs
self.cfg.dss_args.device = self.cfg.train_args.device
dataloader = GradMatchDataLoader(trainloader, valloader, self.cfg.dss_args, logger,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory,
collate_fn = self.cfg.dss_args.collate_fn)
elif self.cfg.dss_args.type in ['GLISTER', 'GLISTER-Warm', 'GLISTERPB', 'GLISTERPB-Warm']:
"""
############################## GLISTER Dataloader Additional Arguments ##############################
"""
self.cfg.dss_args.model = model
self.cfg.dss_args.loss = criterion_nored
self.cfg.dss_args.eta = self.cfg.optimizer.lr
self.cfg.dss_args.num_classes = self.cfg.model.numclasses
self.cfg.dss_args.num_epochs = self.cfg.train_args.num_epochs
self.cfg.dss_args.device = self.cfg.train_args.device
dataloader = GLISTERDataLoader(trainloader, valloader, self.cfg.dss_args, logger,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory,
collate_fn = self.cfg.dss_args.collate_fn)
elif self.cfg.dss_args.type in ['CRAIG', 'CRAIG-Warm', 'CRAIGPB', 'CRAIGPB-Warm']:
"""
############################## CRAIG Dataloader Additional Arguments ##############################
"""
self.cfg.dss_args.model = model
self.cfg.dss_args.loss = criterion_nored
self.cfg.dss_args.num_classes = self.cfg.model.numclasses
self.cfg.dss_args.num_epochs = self.cfg.train_args.num_epochs
self.cfg.dss_args.device = self.cfg.train_args.device
dataloader = CRAIGDataLoader(trainloader, valloader, self.cfg.dss_args, logger,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory,
collate_fn = self.cfg.dss_args.collate_fn)
elif self.cfg.dss_args.type in ['Random', 'Random-Warm']:
"""
############################## Random Dataloader Additional Arguments ##############################
"""
self.cfg.dss_args.device = self.cfg.train_args.device
self.cfg.dss_args.num_epochs = self.cfg.train_args.num_epochs
dataloader = RandomDataLoader(trainloader, self.cfg.dss_args, logger,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory,
collate_fn = self.cfg.dss_args.collate_fn)
elif self.cfg.dss_args.type in ['AdaptiveRandom', 'AdaptiveRandom-Warm']:
"""
############################## AdaptiveRandom Dataloader Additional Arguments ##############################
"""
self.cfg.dss_args.device = self.cfg.train_args.device
self.cfg.dss_args.num_epochs = self.cfg.train_args.num_epochs
dataloader = AdaptiveRandomDataLoader(trainloader, self.cfg.dss_args, logger,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory,
collate_fn = self.cfg.dss_args.collate_fn)
elif self.cfg.dss_args.type in ['MILOFixed', 'MILOFixed-Warm']:
"""
############################## MILOFixed Dataloader Additional Arguments ##############################
"""
self.cfg.dss_args.device = self.cfg.train_args.device
self.cfg.dss_args.num_epochs = self.cfg.train_args.num_epochs
dataloader = MILOFixedDataLoader(trainloader, self.cfg.dss_args, logger,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory,
collate_fn = self.cfg.dss_args.collate_fn)
elif self.cfg.dss_args.type in ['WeightedRandomExploration', 'WeightedRandomExploration-Warm', 'WRE', 'WRE-Warm']:
"""
############################## WeightedRandomDataloader Additional Arguments ##############################
"""
self.cfg.dss_args.device = self.cfg.train_args.device
self.cfg.dss_args.num_epochs = self.cfg.train_args.num_epochs
dataloader = WeightedRandomDataLoader(trainloader, self.cfg.dss_args, logger,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory,
collate_fn = self.cfg.dss_args.collate_fn)
elif self.cfg.dss_args.type in ['StochasticGreedyExploration', 'StochasticGreedyExploration-Warm', 'SGE', 'SGE-Warm']:
"""
############################## StochasticGreedyDataloader Additional Arguments ##############################
"""
self.cfg.dss_args.device = self.cfg.train_args.device
self.cfg.dss_args.num_epochs = self.cfg.train_args.num_epochs
dataloader = StochasticGreedyDataLoader(trainloader, self.cfg.dss_args, logger,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory,
collate_fn = self.cfg.dss_args.collate_fn)
elif self.cfg.dss_args.type in ['MILO']:
"""
############################## MILODataloader Additional Arguments ##############################
"""
self.cfg.dss_args.device = self.cfg.train_args.device
self.cfg.dss_args.num_epochs = self.cfg.train_args.num_epochs
dataloader = MILODataLoader(trainloader, self.cfg.dss_args, logger,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory,
collate_fn = self.cfg.dss_args.collate_fn)
elif self.cfg.dss_args.type == 'FacLoc':
"""
############################## Facility Location Dataloader Additional Arguments ##############################
"""
wt_trainset = WeightedSubset(trainset, list(range(len(trainset))), [1] * len(trainset))
self.cfg.dss_args.device = self.cfg.train_args.device
self.cfg.dss_args.model = model
self.cfg.dss_args.data_type = self.cfg.dataset.type
dataloader = FacLocDataLoader(trainloader, valloader, self.cfg.dss_args, logger,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory,
collate_fn = self.cfg.dss_args.collate_fn)
elif self.cfg.dss_args.type == 'Full':
"""
############################## Full Dataloader Additional Arguments ##############################
"""
wt_trainset = WeightedSubset(trainset, list(range(len(trainset))), [1] * len(trainset))
dataloader = torch.utils.data.DataLoader(wt_trainset,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory,
collate_fn=self.cfg.dss_args.collate_fn)
elif self.cfg.dss_args.type in ['SELCON']:
"""
############################## SELCON Dataloader Additional Arguments ##############################
"""
self.cfg.dss_args.model = model
self.cfg.dss_args.lr = self.cfg.optimizer.lr
self.cfg.dss_args.loss = criterion_nored # doubt: or criterion
self.cfg.dss_args.device = self.cfg.train_args.device
self.cfg.dss_args.optimizer = optimizer
self.cfg.dss_args.criterion = criterion
self.cfg.dss_args.num_classes = self.cfg.model.numclasses
self.cfg.dss_args.batch_size = self.cfg.dataloader.batch_size
# todo: not done yet
self.cfg.dss_args.delta = torch.tensor(self.cfg.dss_args.delta)
# self.cfg.dss_args.linear_layer = self.cfg.dss_args.linear_layer # already there, check glister init
self.cfg.dss_args.num_epochs = self.cfg.train_args.num_epochs
dataloader = SELCONDataLoader(trainset, validset, trainloader, valloader, self.cfg.dss_args, logger,
batch_size=self.cfg.dataloader.batch_size,
shuffle=self.cfg.dataloader.shuffle,
pin_memory=self.cfg.dataloader.pin_memory)
else:
raise NotImplementedError
if self.cfg.dss_args.type in ['SELCON']:
is_selcon = True
else:
is_selcon = False
"""
################################################# Checkpoint Loading #################################################
"""
if self.cfg.ckpt.is_load:
start_epoch, model, optimizer, ckpt_loss, load_metrics = self.load_ckpt(checkpoint_path, model, optimizer)
logger.info("Loading saved checkpoint model at epoch: {0:d}".format(start_epoch))
for arg in load_metrics.keys():
if arg == "val_loss":
val_losses = load_metrics['val_loss']
if arg == "val_acc":
val_acc = load_metrics['val_acc']
if arg == "tst_loss":
tst_losses = load_metrics['tst_loss']
if arg == "tst_acc":
tst_acc = load_metrics['tst_acc']
best_acc = load_metrics['best_acc']
if arg == "trn_loss":
trn_losses = load_metrics['trn_loss']
if arg == "trn_acc":
trn_acc = load_metrics['trn_acc']
if arg == "subtrn_loss":
subtrn_losses = load_metrics['subtrn_loss']
if arg == "subtrn_acc":
subtrn_acc = load_metrics['subtrn_acc']
if arg == "time":
timing = load_metrics['time']
else:
start_epoch = 0
"""
################################################# Training Loop #################################################
"""
train_time = 0
for epoch in range(start_epoch, self.cfg.train_args.num_epochs+1):
"""
################################################# Evaluation Loop #################################################
"""
print_args = self.cfg.train_args.print_args
if (epoch % self.cfg.train_args.print_every == 0) or (epoch == self.cfg.train_args.num_epochs) or (epoch == 0):
trn_loss = 0
trn_correct = 0
trn_total = 0
val_loss = 0
val_correct = 0
val_total = 0
tst_correct = 0
tst_total = 0
tst_loss = 0
model.eval()
logger_dict = {}
if ("trn_loss" in print_args) or ("trn_acc" in print_args):
samples=0
with torch.no_grad():
for _, data in enumerate(train_eval_loader):
if is_selcon:
inputs, targets, _ = data
else:
inputs, targets = data
inputs, targets = inputs.to(self.cfg.train_args.device), \
targets.to(self.cfg.train_args.device, non_blocking=True)
outputs = model(inputs)
loss = criterion(outputs, targets)
trn_loss += (loss.item() * train_eval_loader.batch_size)
samples += targets.shape[0]
if "trn_acc" in print_args:
if is_selcon: predicted = outputs
else: _, predicted = outputs.max(1)
trn_total += targets.size(0)
trn_correct += predicted.eq(targets).sum().item()
trn_loss = trn_loss/samples
trn_losses.append(trn_loss)
logger_dict['trn_loss'] = trn_loss
if "trn_acc" in print_args:
trn_acc.append(trn_correct / trn_total)
logger_dict['trn_acc'] = trn_correct / trn_total
if ("val_loss" in print_args) or ("val_acc" in print_args):
samples =0
with torch.no_grad():
for _, data in enumerate(val_eval_loader):
if is_selcon:
inputs, targets, _ = data
else:
inputs, targets = data
inputs, targets = inputs.to(self.cfg.train_args.device), \
targets.to(self.cfg.train_args.device, non_blocking=True)
outputs = model(inputs)
loss = criterion(outputs, targets)
val_loss += (loss.item() * val_eval_loader.batch_size)
samples += targets.shape[0]
if "val_acc" in print_args:
if is_selcon: predicted = outputs
else: _, predicted = outputs.max(1)
val_total += targets.size(0)
val_correct += predicted.eq(targets).sum().item()
val_loss = val_loss/samples
val_losses.append(val_loss)
logger_dict['val_loss'] = val_loss
if "val_acc" in print_args:
val_acc.append(val_correct / val_total)
logger_dict['val_acc'] = val_correct / val_total
if ("tst_loss" in print_args) or ("tst_acc" in print_args):
samples =0
with torch.no_grad():
for _, data in enumerate(test_eval_loader):
if is_selcon:
inputs, targets, _ = data
else:
inputs, targets = data
inputs, targets = inputs.to(self.cfg.train_args.device), \
targets.to(self.cfg.train_args.device, non_blocking=True)
outputs = model(inputs)
loss = criterion(outputs, targets)
tst_loss += (loss.item() * test_eval_loader.batch_size)
samples += targets.shape[0]
if "tst_acc" in print_args:
if is_selcon: predicted = outputs
else: _, predicted = outputs.max(1)
tst_total += targets.size(0)
tst_correct += predicted.eq(targets).sum().item()
tst_loss = tst_loss/samples
tst_losses.append(tst_loss)
logger_dict['tst_loss'] = tst_loss
if (tst_correct/tst_total) > curr_best_acc:
curr_best_acc = (tst_correct/tst_total)
if "tst_acc" in print_args:
tst_acc.append(tst_correct / tst_total)
best_acc.append(curr_best_acc)
logger_dict['tst_acc'] = tst_correct / tst_total
logger_dict['best_acc'] = curr_best_acc
if "subtrn_acc" in print_args:
if epoch == 0:
subtrn_acc.append(0)
logger_dict['subtrn_acc'] = 0
else:
subtrn_acc.append(subtrn_correct / subtrn_total)
logger_dict['subtrn_acc'] = subtrn_correct / subtrn_total
if "subtrn_losses" in print_args:
if epoch == 0:
subtrn_losses.append(0)
logger_dict['subtrn_loss'] = 0
else:
subtrn_losses.append(subtrn_loss)
logger_dict['subtrn_loss'] = subtrn_loss
print_str = "Epoch: " + str(epoch)
logger_dict['Epoch'] = epoch
logger_dict['Time'] = train_time
timing.append(train_time)
if self.cfg.train_args.wandb:
wandb.log(logger_dict)
"""
################################################# Results Printing #################################################
"""
for arg in print_args:
if arg == "val_loss":
print_str += " , " + "Validation Loss: " + str(val_losses[-1])
if arg == "val_acc":
print_str += " , " + "Validation Accuracy: " + str(val_acc[-1])
if arg == "tst_loss":
print_str += " , " + "Test Loss: " + str(tst_losses[-1])
if arg == "tst_acc":
print_str += " , " + "Test Accuracy: " + str(tst_acc[-1])
print_str += " , " + "Best Accuracy: " + str(best_acc[-1])
if arg == "trn_loss":
print_str += " , " + "Training Loss: " + str(trn_losses[-1])
if arg == "trn_acc":
print_str += " , " + "Training Accuracy: " + str(trn_acc[-1])
if arg == "subtrn_loss":
print_str += " , " + "Subset Loss: " + str(subtrn_losses[-1])
if arg == "subtrn_acc":
print_str += " , " + "Subset Accuracy: " + str(subtrn_acc[-1])
if arg == "time":
print_str += " , " + "Timing: " + str(timing[-1])
# report metric to ray for hyperparameter optimization
if 'report_tune' in self.cfg and self.cfg.report_tune and len(dataloader) and epoch > 0:
tune.report(mean_accuracy=np.array(val_acc).max())
logger.info(print_str)
subtrn_loss = 0
subtrn_correct = 0
subtrn_total = 0
model.train()
start_time = time.time()
for _, (inputs, targets, weights) in enumerate(dataloader):
inputs = inputs.to(self.cfg.train_args.device)
targets = targets.to(self.cfg.train_args.device, non_blocking=True)
weights = weights.to(self.cfg.train_args.device)
optimizer.zero_grad()
outputs = model(inputs)
losses = criterion_nored(outputs, targets)
loss = torch.dot(losses, weights / (weights.sum()))
loss.backward()
subtrn_loss += loss.item()
optimizer.step()
if self.cfg.scheduler.type == "cosine_annealing_step":
scheduler.step()
if not self.cfg.is_reg:
_, predicted = outputs.max(1)
subtrn_total += targets.size(0)
subtrn_correct += predicted.eq(targets).sum().item()
epoch_time = time.time() - start_time
if (scheduler is not None) and (self.cfg.scheduler.type != "cosine_annealing_step"):
scheduler.step()
# timing.append(epoch_time)
train_time += epoch_time
"""
################################################# Checkpoint Saving #################################################
"""
if ((epoch + 1) % self.cfg.ckpt.save_every == 0) and self.cfg.ckpt.is_save:
metric_dict = {}
for arg in print_args:
if arg == "val_loss":
metric_dict['val_loss'] = val_losses
if arg == "val_acc":
metric_dict['val_acc'] = val_acc
if arg == "tst_loss":
metric_dict['tst_loss'] = tst_losses
if arg == "tst_acc":
metric_dict['tst_acc'] = tst_acc
metric_dict['best_acc'] = best_acc
if arg == "trn_loss":
metric_dict['trn_loss'] = trn_losses
if arg == "trn_acc":
metric_dict['trn_acc'] = trn_acc
if arg == "subtrn_loss":
metric_dict['subtrn_loss'] = subtrn_losses
if arg == "subtrn_acc":
metric_dict['subtrn_acc'] = subtrn_acc
if arg == "time":
metric_dict['time'] = timing
ckpt_state = {
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'loss': self.loss_function(),
'metrics': metric_dict
}
# save checkpoint
self.save_ckpt(ckpt_state, checkpoint_path)
logger.info("Model checkpoint saved at epoch: {0:d}".format(epoch + 1))
"""
################################################# Results Summary #################################################
"""
original_idxs = set([x for x in range(len(trainset))])
encountered_idxs = []
if self.cfg.dss_args.type != 'Full':
for key in dataloader.selected_idxs.keys():
encountered_idxs.extend(dataloader.selected_idxs[key])
encountered_idxs = set(encountered_idxs)
rem_idxs = original_idxs.difference(encountered_idxs)
encountered_percentage = len(encountered_idxs)/len(original_idxs)
logger.info("Selected Indices: ")
logger.info(dataloader.selected_idxs)
logger.info("Percentages of data samples encountered during training: %.2f", encountered_percentage)
logger.info("Not Selected Indices: ")
logger.info(rem_idxs)
if self.cfg.train_args.wandb:
wandb.log({
"Data Samples Encountered(in %)": encountered_percentage
})
logger.info(self.cfg.dss_args.type + " Selection Run---------------------------------")
logger.info("Final SubsetTrn: {0:f}".format(subtrn_loss))
if "val_loss" in print_args:
if "val_acc" in print_args:
logger.info("Validation Loss: %.2f , Validation Accuracy: %.2f", val_loss, val_acc[-1])
else:
logger.info("Validation Loss: %.2f", val_loss)
if "tst_loss" in print_args:
if "tst_acc" in print_args:
logger.info("Test Loss: %.2f, Test Accuracy: %.2f, Best Accuracy: %.2f", tst_loss, tst_acc[-1], best_acc[-1])
else:
logger.info("Test Data Loss: %f", tst_loss)
logger.info('---------------------------------------------------------------------')
logger.info(self.cfg.dss_args.type)
logger.info('---------------------------------------------------------------------')
"""
################################################# Final Results Logging #################################################
"""
if "val_acc" in print_args:
val_str = "Validation Accuracy: "
for val in val_acc:
if val_str == "Validation Accuracy: ":
val_str = val_str + str(val)
else:
val_str = val_str + " , " + str(val)
logger.info(val_str)
if "tst_acc" in print_args:
tst_str = "Test Accuracy: "
for tst in tst_acc:
if tst_str == "Test Accuracy: ":
tst_str = tst_str + str(tst)
else:
tst_str = tst_str + " , " + str(tst)
logger.info(tst_str)
tst_str = "Best Accuracy: "
for tst in best_acc:
if tst_str == "Best Accuracy: ":
tst_str = tst_str + str(tst)
else:
tst_str = tst_str + " , " + str(tst)
logger.info(tst_str)
if "time" in print_args:
time_str = "Time: "
for t in timing:
if time_str == "Time: ":
time_str = time_str + str(t)
else:
time_str = time_str + " , " + str(t)
logger.info(time_str)
omp_timing = np.array(timing)
# omp_cum_timing = list(self.generate_cumulative_timing(omp_timing))
logger.info("Total time taken by %s = %.4f ", self.cfg.dss_args.type, omp_timing[-1])
return trn_acc, val_acc, tst_acc, best_acc, omp_timing