-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTrainCL_all.py
1651 lines (1416 loc) · 73.1 KB
/
TrainCL_all.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
import os
import time
import scipy.stats
import numpy as np
import torch
from torch.utils.data import DataLoader
from torch.optim import lr_scheduler
from torch.autograd import Variable
from torchvision import transforms
import torch.nn as nn
import torch.nn.functional as F
from autoencoder import Autoencoder, exp_lr_scheduler
import pandas as pd
from MNL_Loss import Fidelity_Loss
from Transformers import AdaptiveResize
from copy import deepcopy
from BaseCNN_all import BaseCNN_vanilla, MetaIQA
from E2euiqa import E2EUIQA
from KonCept import KonCept
from tqdm import tqdm
from sklearn.cluster import KMeans
from sklearn.metrics.pairwise import euclidean_distances
from utils import set_dataset, set_dataset2
from DBCNN import DBCNN, DBCNN2
import warnings
warnings.filterwarnings("ignore")
import random
from torch.cuda.amp import autocast as autocast
from torch.cuda.amp import GradScaler as GradScaler
def fix_bn(m):
classname = m.__class__.__name__
if classname.find('BatchNorm2d') != -1:
m.eval()
class Trainer(object):
def __init__(self, config):
torch.manual_seed(config.seed)
random.seed(config.seed)
np.random.seed(config.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
self.config = config
# for replay
self.replayers = {}
if self.config.amp:
self.scaler = GradScaler()
if not self.config.train:
self.config.verbose = False
self.train_transform = transforms.Compose([
AdaptiveResize(768),
transforms.RandomCrop(config.image_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225))
# transforms.Normalize(mean=(0.5, 0.5, 0.5),
# std=(0.5, 0.5, 0.5))
])
self.test_transform = transforms.Compose([
AdaptiveResize(768),
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225))
# transforms.Normalize(mean=(0.5, 0.5, 0.5),
# std=(0.5, 0.5, 0.5))
])
self.train_batch_size = config.batch_size
self.test_batch_size = 1
self.ranking = config.ranking
if self.config.GDumb:
csv_file = os.path.join(config.trainset, 'gdumb', str(config.indicator), config.train_txt),
else:
csv_file = os.path.join(config.trainset, 'splits2', str(config.split), config.train_txt),
self.train_data, self.train_loader = set_dataset(config, csv_file[0], config.trainset,
self.train_transform, num_workers=12, shuffle=True,
test=(not config.ranking), verbose=self.config.verbose)
#for k-means
if self.config.train_kmeans:
self.train_data_kmeans, _ = set_dataset(config, csv_file[0], config.trainset,
self.test_transform, num_workers=12, shuffle=False,
test=(not config.ranking), verbose=self.config.verbose)
self.kmeans_loader = DataLoader(self.train_data_kmeans,
batch_size=1,
shuffle=False,
pin_memory=True,
num_workers=12)
# testing and validation sets configuration
csv_file = os.path.join(config.live_set, 'splits2', str(config.split), 'live_test.txt')
self.live_data, self.live_loader = set_dataset(config, csv_file, config.live_set,
self.test_transform, num_workers=1, shuffle=False, test=True,
verbose=self.config.verbose)
csv_file = os.path.join(config.live_set, 'splits2', str(config.split), 'live_val.txt')
self.live_data_val, self.live_loader_val = set_dataset(config, csv_file, config.live_set,
self.test_transform, num_workers=1, shuffle=False, test=True,
verbose=self.config.verbose)
csv_file = os.path.join(config.csiq_set, 'splits2', str(config.split), 'csiq_test.txt')
self.csiq_data, self.csiq_loader = set_dataset(config, csv_file, config.csiq_set,
self.test_transform, num_workers=1, shuffle=False, test=True,
verbose=self.config.verbose)
csv_file = os.path.join(config.csiq_set, 'splits2', str(config.split), 'csiq_val.txt')
self.csiq_data_val, self.csiq_loader_val = set_dataset(config, csv_file, config.csiq_set,
self.test_transform, num_workers=1, shuffle=False, test=True,
verbose=self.config.verbose)
csv_file = os.path.join(config.bid_set, 'splits2', str(config.split), 'bid_test.txt')
self.bid_data, self.bid_loader = set_dataset(config, csv_file, config.bid_set,
self.test_transform, num_workers=1, shuffle=False, test=True,
verbose=self.config.verbose)
csv_file = os.path.join(config.bid_set, 'splits2', str(config.split), 'bid_val.txt')
self.bid_data_val, self.bid_loader_val = set_dataset(config, csv_file, config.bid_set,
self.test_transform, num_workers=1, shuffle=False, test=True,
verbose=self.config.verbose)
csv_file = os.path.join(config.clive_set, 'splits2', str(config.split), 'clive_test.txt')
self.clive_data, self.clive_loader = set_dataset(config, csv_file, config.clive_set,
self.test_transform, num_workers=1, shuffle=False, test=True,
verbose=self.config.verbose)
csv_file = os.path.join(config.clive_set, 'splits2', str(config.split), 'clive_val.txt')
self.clive_data_val, self.clive_loader_val = set_dataset(config, csv_file, config.clive_set,
self.test_transform, num_workers=1, shuffle=False, test=True,
verbose=self.config.verbose)
csv_file = os.path.join(config.koniq10k_set, 'splits2', str(config.split), 'koniq10k_test.txt')
self.koniq10k_data, self.koniq10k_loader = set_dataset(config, csv_file, config.koniq10k_set,
self.test_transform, num_workers=1, shuffle=False, test=True,
verbose=self.config.verbose)
csv_file = os.path.join(config.koniq10k_set, 'splits2', str(config.split), 'koniq10k_val.txt')
self.koniq10k_data_val, self.koniq10k_loader_val = set_dataset(config, csv_file, config.koniq10k_set,
self.test_transform, num_workers=1, shuffle=False, test=True,
verbose=self.config.verbose)
csv_file = os.path.join(config.kadid10k_set, 'splits2', str(config.split), 'kadid10k_test.txt')
self.kadid10k_data, self.kadid10k_loader = set_dataset(config, csv_file, config.kadid10k_set,
self.test_transform, num_workers=1, shuffle=False,
test=True, verbose=self.config.verbose)
csv_file = os.path.join(config.kadid10k_set, 'splits2', str(config.split), 'kadid10k_val.txt')
self.kadid10k_data_val, self.kadid10k_loader_val = set_dataset(config, csv_file, config.kadid10k_set,
self.test_transform, num_workers=1, shuffle=False,
test=True, verbose=self.config.verbose)
self.task2loader = {'live': self.live_loader,
'csiq': self.csiq_loader,
'bid': self.bid_loader,
'clive': self.clive_loader,
'koniq10k': self.koniq10k_loader,
'kadid10k': self.kadid10k_loader}
self.task2loader_val = {'live': self.live_loader_val,
'csiq': self.csiq_loader_val,
'bid': self.bid_loader_val,
'clive': self.clive_loader_val,
'koniq10k': self.koniq10k_loader_val,
'kadid10k': self.kadid10k_loader_val}
self.device = torch.device("cuda" if torch.cuda.is_available() and config.use_cuda else "cpu")
if self.config.shared_head:
self.config.n_task = 1
self.config.task_id = 0
# initialize the model
if not self.config.train:
if config.network == 'basecnn':
self.model = BaseCNN_vanilla(config)
elif config.network == 'dbcnn':
self.model = DBCNN(config)
#self.model.train()
self.model.sfeatures.apply(fix_bn)
if self.config.fc:
self.model.backbone.apply(fix_bn)
elif config.network == 'dbcnn2':
self.model = DBCNN2(config)
#self.model.train()
if self.config.fc:
self.model.backbone.features.apply(fix_bn)
self.model.sfeatures.apply(fix_bn)
elif config.network == 'metaiqa':
self.model = MetaIQA(config)
if config.fc:
self.model.resnet_layer.apply(fix_bn)
elif config.network == 'ma19':
self.model = E2EUIQA()
self.model.init_model('./saved_weights/f48_max_f128_a9.pt')
elif config.network == 'koncept':
self.model = KonCept(config)
self.model.eval()
else:
if config.network == 'basecnn':
self.model = BaseCNN_vanilla(config)
self.model.train()
#freeze bn running_stats
#self.model.scnn.apply(fix_bn)
if self.config.fc:
self.model.backbone.apply(fix_bn)
elif config.network == 'dbcnn':
self.model = DBCNN(config)
self.model.train()
self.model.sfeatures.apply(fix_bn)
if self.config.fc:
self.model.backbone.apply(fix_bn)
elif config.network == 'dbcnn2':
self.model = DBCNN2(config)
self.model.train()
if self.config.fc:
self.model.backbone.apply(fix_bn)
self.model.sfeatures.apply(fix_bn)
elif config.network == 'metaiqa':
self.model = MetaIQA(config)
self.model.train()
if config.fc:
self.model.resnet_layer.apply(fix_bn)
elif config.network == 'koncept':
self.model = KonCept(config)
if config.fc:
self.model.base.apply(fix_bn)
else:
raise NotImplementedError("Not supported network, need to be added!")
#summary(self.model, (3,384,384))
self.model.to(self.device)
self.model_name = type(self.model).__name__
# from ptflops import get_model_complexity_info
# flops, params = get_model_complexity_info(self.model, (3, 384, 384), as_strings=True, print_per_layer_stat=True)
# print(flops, params)
# inputs = torch.randn(1,3,384,384).to(self.device)
# from thop import profile
# flops, params = profile(self.model, (inputs, ), verbose=True)
# print('flops: ', flops, 'params: ', params)
if self.config.verbose:
print(self.model)
# loss function
if config.ranking:
if config.fidelity | config.b_fidelity:
print('use fidelity loss')
self.loss_fn = Fidelity_Loss()
else:
print('use cross entropy loss')
self.loss_fn = nn.BCEWithLogitsLoss()
else:
print('use mse loss')
self.loss_fn = nn.MSELoss()
self.loss_fn.to(self.device)
self.initial_lr = config.lr
if self.initial_lr is None:
lr = 0.0005
else:
lr = self.initial_lr
self.optimizer = torch.optim.Adam(
self.model.parameters(), lr=lr,
weight_decay=5e-4)
# some states
self.best_srcc = 0
self.start_epoch = 0
self.start_step = 0
self.train_loss = []
self.test_results_srcc = {'live': [], 'csiq': [], 'bid': [], 'clive': [], 'koniq10k': [], 'kadid10k': []}
self.val_results_srcc = {'live': [], 'csiq': [], 'bid': [], 'clive': [], 'koniq10k': [], 'kadid10k': []}
self.ckpt_path = config.ckpt_path
self.ckpt_resume_path = config.ckpt_resume_path
self.resume_new = config.resume_new
self.max_epochs = config.max_epochs
self.epochs_per_eval = config.epochs_per_eval
self.epochs_per_save = config.epochs_per_save
self.indicator = config.indicator
###prepare for regularization-based continual learning methods
#EWC
if self.config.network == 'metaiqa':
self.params = {n: p for n, p in self.model.resnet_layer.named_parameters() if
(p.requires_grad) & (not 'fc' in n)}
elif self.config.network == 'ma19':
self.params = {}
elif self.config.network == 'koncept':
self.params = {}
else:
self.params = {n:p for n,p in self.model.backbone.named_parameters() if (p.requires_grad) & (not 'fc' in n)}
self.regularization_terms = {}
self.task_count = self.config.indicator
self.online_reg = self.config.online_reg
# SI
if self.config.SI:
self.damping_factor = 0.1
self.w = {}
for n, p in self.params.items():
self.w[n] = p.clone().detach().zero_()
# The initial_params will only be used in the first task (when the regularization_terms is empty)
self.initial_params = {}
for n, p in self.params.items():
self.initial_params[n] = p.clone().detach()
# try load the model
if config.resume or not config.train:
if (not self.config.train) & (self.config.network != 'ma19'):
ckpt = self._get_checkpoint_new(path=config.ckpt_resume_path, resume_best=config.resume_best)
self._load_checkpoint(ckpt=ckpt)
else:
if config.ckpt:
ckpt = os.path.join(config.ckpt_resume_path, config.ckpt)
else:
ckpt = self._get_checkpoint_new(path=config.ckpt_resume_path, resume_best=config.resume_best)
if (self.config.network != 'ma19'):
self._load_checkpoint(ckpt=ckpt)
if self.resume_new:
self.start_epoch = 0
self.start_step = 0
self.train_loss = []
self.best_srcc = 0
self.test_results_srcc = {'live': [], 'csiq': [], 'bid': [], 'clive': [],
'koniq10k': [], 'kadid10k': []}
self.val_results_srcc = {'live': [], 'csiq': [], 'bid': [], 'clive': [],
'koniq10k': [], 'kadid10k': []}
self.optimizer = torch.optim.Adam(
self.model.parameters(), lr=lr,
weight_decay=5e-4)
if config.lwf:
self.model_old = deepcopy(self.model)
self.model_old.eval()
if self.config.train_kmeans:
task_folder = 'train_on_' + self.config.id2dataset[self.config.task_id]
if self.config.train:
self.scheduler = lr_scheduler.StepLR(self.optimizer,
last_epoch=self.start_epoch - 1,
step_size=config.decay_interval,
gamma=config.decay_ratio)
#setting replayers
if self.config.replay:
self.replayer_list = {}
curr_train_len = len(self.train_data)
num_replay = len(self.replayers)
if self.config.sample_strategy == 'all':
each_sample = curr_train_len // num_replay
#each_sample = curr_train_len
each_batch = self.config.batch_size // num_replay
#each_batch = self.config.batch_size
elif self.config.sample_strategy == 'random':
each_sample = curr_train_len
each_batch = self.config.batch_size if each_sample >= self.config.batch_size else each_sample
else:
raise NotImplementedError('Not implemented !')
if num_replay == 1:
each_batch = 32
elif num_replay == 2:
each_batch = 16
else:
each_batch = 8
for i in range(num_replay):
task_name = self.config.id2dataset[i]
replayer = self.replayers[task_name]
replay_data, replay_loader = set_dataset2(batch_size=each_batch, num_sample=each_sample, pandas_object=replayer,
data_set=config.trainset_dict[task_name], transfrom=self.train_transform, num_workers=12)
self.replayer_list[task_name] = replay_loader
if self.config.GDumb:
self.replayer_list = {}
curr_train_len = len(self.train_data)
num_replay = self.config.task_id + 1
def train_expert_gates(self):
if os.path.exists(os.path.join(self.ckpt_path, 'features.npy')):
features = np.load(os.path.join(self.ckpt_path, 'features.npy'))
print('features loaded!')
else:
assert self.config.network == 'dbcnn'
with torch.no_grad():
features = self.compute_features_single()
print('features saved!')
self.expert_gates = Autoencoder().to(self.device)
encoder_criterion = nn.MSELoss()
encoder_criterion.to(self.device)
num_epochs = 20
lr = 0.003
num_feat = np.shape(features)[0]
batch_size = 16
indexes = np.arange(0, num_feat)
optimizer = torch.optim.Adam(
self.expert_gates.parameters(), lr=lr,
weight_decay=5e-4)
for epoch in range(0, num_epochs):
np.random.shuffle(indexes)
optimizer = exp_lr_scheduler(optimizer, epoch, lr)
print("Epoch {}/{}".format(epoch + 1, num_epochs))
print("-" * 10)
running_loss = 0
self.expert_gates.train(True)
idx = 0
num_iteration = num_feat // batch_size
last_batch_size = num_feat % batch_size
min_err = 9999999
if last_batch_size != 0:
num_iteration = num_iteration - 1
for _ in range(num_iteration):
batch_idx = indexes[idx:idx + batch_size]
batch_feat = features[batch_idx, :]
batch_feat = torch.from_numpy(batch_feat).to(self.device)
optimizer.zero_grad()
self.expert_gates.zero_grad()
outputs = self.expert_gates(batch_feat)
loss = encoder_criterion(outputs, batch_feat.detach())
loss.backward()
optimizer.step()
idx += batch_size
running_loss += loss.item()
#handling last batch
if last_batch_size != 0:
batch_idx = indexes[idx:idx + last_batch_size]
last_batch = features[batch_idx, :]
last_batch = torch.from_numpy(last_batch).to(self.device)
optimizer.zero_grad()
self.expert_gates.zero_grad()
outputs = self.expert_gates(last_batch)
loss = encoder_criterion(outputs, outputs)
loss.backward()
optimizer.step()
running_loss += loss.item()
epoch_loss = running_loss / num_iteration
print('Epoch Loss:{}'.format(epoch_loss))
if epoch_loss < min_err:
torch.save(self.expert_gates.state_dict(), self.ckpt_path + "/expert.pth")
min_err = epoch_loss
def train_kmeans(self):
if os.path.exists(os.path.join(self.ckpt_path, 'features.npy')):
features = np.load(os.path.join(self.ckpt_path, 'features.npy'))
print('features loaded!')
else:
if self.config.network == 'dbcnn':
features = self.compute_features_single()
np.save(os.path.join(self.ckpt_path, 'features.npy'), features)
else:
with torch.no_grad():
features = self.compute_features_single()
np.save(os.path.join(self.ckpt_path, 'features.npy'), features)
print('features saved!')
# compare
if not os.path.exists(os.path.join(self.ckpt_path, 'cluster.pt')):
estimator = KMeans(n_clusters=self.config.num_cluster, n_init=20, verbose=True, max_iter=1000)
estimator.fit(features)
cluster_name = os.path.join(self.ckpt_path, 'cluster.pt')
torch.save(torch.from_numpy(estimator.cluster_centers_), cluster_name)
# mean features baseline
features = np.mean(features, axis=0)
features = features / np.linalg.norm(features)
mean_name = os.path.join(self.ckpt_path, 'mean_feature.pt')
torch.save(torch.from_numpy(features), mean_name)
print('mean features saved!')
def fit(self):
if self.ranking:
for epoch in range(self.start_epoch, self.max_epochs):
_ = self._train_single_epoch(epoch)
self.scheduler.step()
if self.config.reg_trigger:
# 2.Backup the weight of current task
# not works for fc layers warm-up
if (not self.config.fc):
model_name_best = os.path.join(self.ckpt_path, self.model_name + '_best.pt')
self._load_checkpoint(model_name_best)
self.params = {n: p for n, p in self.model.backbone.named_parameters() if
(p.requires_grad) & (not 'fc' in n)}
task_param = {}
for n, p in self.params.items():
task_param[n] = p.clone().detach()
# 3.Calculate the importance of weights for current task
importance = self.calculate_importance()
if self.online_reg and len(self.regularization_terms) > 0:
self.regularization_terms[1] = {'importance': importance, 'task_param': task_param}
else:
self.regularization_terms[self.task_count] = {'importance': importance,
'task_param': task_param}
model_name = type(self.model).__name__
model_name_best = os.path.join(self.ckpt_path, model_name + '_best.pt')
epoch = self.start_epoch - 1
if self.config.amp:
self._save_checkpoint({
'epoch': epoch,
'state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'train_loss': self.train_loss,
'test_results_srcc': self.test_results_srcc,
'val_results_srcc': self.val_results_srcc,
'best_srcc': self.best_srcc,
'regularization_terms': self.regularization_terms,
'replayers': self.replayers,
'scaler': self.scaler.state_dict(),
'w': self.w
}, model_name_best)
else:
self._save_checkpoint({
'epoch': epoch,
'state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'train_loss': self.train_loss,
'test_results_srcc': self.test_results_srcc,
'val_results_srcc': self.val_results_srcc,
'best_srcc': self.best_srcc,
'regularization_terms': self.regularization_terms,
'replayers': self.replayers,
'w': self.w
}, model_name_best)
else:
raise NotImplementedError("Only support ranking now!")
def do_batch(self, x1, x2):
y1, _ = self.model(x1)
y2, _ = self.model(x2)
output = []
for y11, y22 in zip(y1, y2):
y_diff = y11 - y22
p = y_diff
if self.config.fidelity:
constant = torch.sqrt(torch.Tensor([2])).to(self.device)
p = 0.5 * (1 + torch.erf(p / constant))
output.append(p)
return output
def do_batch_old(self, x1, x2):
if self.config.shared_head:
n_old_task = 1
else:
n_old_task = self.config.task_id
ps = []
y1, _ = self.model_old(x1)
y2, _ = self.model_old(x2)
for task_idx in range(n_old_task):
y_diff = y1[task_idx] - y2[task_idx]
p = y_diff
if self.config.fidelity:
constant = torch.sqrt(torch.Tensor([2])).to(self.device)
p = 0.5 * (1 + torch.erf(p / constant))
ps.append(p)
#p = 0.5 * (1 + torch.erf(p / constant))
return ps
def _train_single_epoch(self, epoch):
# initialize logging system
num_steps_per_epoch = len(self.train_loader)
local_counter = epoch * num_steps_per_epoch + 1
start_time = time.time()
beta = 0.9
running_loss = 0 if epoch == 0 else self.train_loss[-1]
loss_corrected = 0.0
running_duration = 0.0
#replay_iter = {}
if self.config.replay:
replay_loaders = {}
for idx, task_name in enumerate(self.replayers):
replay_loaders[task_name] = iter(self.replayer_list[task_name])
#replay_iter[task_name] = 1
if self.config.sample_strategy == 'random':
picker = np.random.randint(low=0, high=len(self.replayers), size=len(self.train_loader))
# start training
print('Adam learning rate: {:.8f}'.format(self.optimizer.param_groups[0]['lr']))
for step, sample_batched in enumerate(self.train_loader, 0):
if step < self.start_step:
continue
# for SI
if self.config.SI:
# 1.Save current parameters
old_params = {}
for n, p in self.params.items():
old_params[n] = p.clone().detach()
if self.config.replay:
if self.config.shared_head:
replay_sample1 = []
replay_sample2 = []
replay_y = []
else:
replay_sample1 = {}
replay_sample2 = {}
replay_y = {}
if self.config.sample_strategy == 'all':
for idx, task_name in enumerate(self.replayers):
# task_name = self.config.id2dataset[idx]
replayer = replay_loaders[task_name]
try:
replay_batch = next(replayer)
except StopIteration:
replayer = iter(self.replayer_list[task_name])
replay_batch = next(replayer)
replay_loaders[task_name] = replayer
r1, r2, yr = replay_batch['I1'], replay_batch['I2'], replay_batch['yb']
yr = yr.view(-1, 1)
if self.config.shared_head:
replay_sample1.append(r1.to(self.device))
replay_sample2.append(r2.to(self.device))
replay_y.append(yr.to(self.device))
else:
replay_sample1[task_name] = r1.to(self.device)
replay_sample2[task_name] = r2.to(self.device)
replay_y[task_name] = yr.to(self.device)
else:
replay_task_id = picker[step]
task_name = self.config.id2dataset[replay_task_id]
replayer = replay_loaders[task_name]
try:
replay_batch = next(replayer)
except StopIteration:
replayer = iter(self.replayer_list[task_name])
replay_batch = next(replayer)
replay_loaders[task_name] = replayer
r1, r2, yr = replay_batch['I1'], replay_batch['I2'], replay_batch['yb']
yr = yr.view(-1, 1)
if self.config.shared_head:
replay_sample1.append(r1.to(self.device))
replay_sample2.append(r2.to(self.device))
replay_y.append(yr.to(self.device))
else:
replay_sample1[task_name] = r1.to(self.device)
replay_sample2[task_name] = r2.to(self.device)
replay_y[task_name] = yr.to(self.device)
if (self.config.shared_head):
replay_sample1 = torch.cat(replay_sample1, dim=0)
replay_sample2 = torch.cat(replay_sample2, dim=0)
replay_y = torch.cat(replay_y, dim=0)
elif self.config.new_replay:
replay_sample11 = []
replay_sample22 = []
replay_yy = []
for i, item in enumerate(replay_sample1):
replay_sample11.append(replay_sample1[item])
replay_sample22.append(replay_sample2[item])
replay_yy.append(replay_y[item])
replay_sample11 = torch.cat(replay_sample11, dim=0)
replay_sample22 = torch.cat(replay_sample22, dim=0)
replay_yy = torch.cat(replay_yy, dim=0)
x1, x2, g, _, _, yb = sample_batched['I1'], sample_batched['I2'], sample_batched['y'], \
sample_batched['std1'], sample_batched['std2'], sample_batched['yb']
x1 = Variable(x1)
x2 = Variable(x2)
g = Variable(g).view(-1, 1)
yb = Variable(yb).view(-1, 1)
x1 = x1.to(self.device)
x2 = x2.to(self.device)
g = g.to(self.device)
yb = yb.to(self.device)
if (self.config.replay) & (self.config.shared_head):
x1 = torch.cat([x1, replay_sample1], dim=0)
x2 = torch.cat([x2, replay_sample2], dim=0)
yb = torch.cat([yb, replay_y], dim=0)
elif (self.config.replay) & (self.config.new_replay):
x1 = torch.cat([x1, replay_sample11], dim=0)
x2 = torch.cat([x2, replay_sample22], dim=0)
yb = torch.cat([yb, replay_yy], dim=0)
# if step == 236:
# print('pause')
if self.config.amp:
with autocast():
p = self.do_batch(x1, x2)
if (not self.config.b_fidelity) & (self.config.fidelity):
self.loss = self.loss_fn(p[self.config.task_id], g.detach())
else:
self.loss = self.loss_fn(p[self.config.task_id], yb.detach())
else:
p = self.do_batch(x1, x2)
if (not self.config.b_fidelity) & (self.config.fidelity):
self.loss = self.loss_fn(p[self.config.task_id], g.detach())
else:
self.loss = self.loss_fn(p[self.config.task_id], yb.detach())
if torch.isnan(self.loss):
print('skip nan loss')
continue
reg_loss = 0
# replay-free training
if ((self.config.lwf) & (not self.config.icarl)): #lwf
if self.config.amp:
with autocast():
with torch.no_grad():
p_old = self.do_batch_old(x1, x2)
if ((self.config.lwf) & (not self.config.icarl)):
for i, old_pred in enumerate(p_old):
reg_loss += self.config.reg_weight * self.loss_fn(p[i], old_pred.detach())
else:
with torch.no_grad():
p_old = self.do_batch_old(x1, x2)
if ((self.config.lwf) & (not self.config.icarl)):
for i, old_pred in enumerate(p_old):
reg_loss += self.config.reg_weight * self.loss_fn(p[i], old_pred.detach())
else: ##weight importance regularization, i.e., other regurlarizers except for lwf-based methods
if self.config.amp:
with autocast():
reg_loss = self.reg_criterion()
else:
reg_loss = self.reg_criterion()
self.loss += reg_loss
if torch.isnan(self.loss):
print('skip nan loss')
continue
#replay-based training
if (self.config.replay) & (not self.config.shared_head):
if self.config.sample_strategy == 'all':
replay_loss = 0
for idx in range(self.config.task_id):
task_name = self.config.id2dataset[idx]
r1 = replay_sample1[task_name]
r2 = replay_sample2[task_name]
yr = replay_y[task_name]
if self.config.icarl:
if self.config.amp:
with autocast():
with torch.no_grad():
p_distillation = self.do_batch_old(r1, r2) # iCaRL: distillation
p_old_replay = self.do_batch(r1, r2)
replay_loss += self.config.reg_weight * self.loss_fn(p_old_replay[idx],
p_distillation[idx].detach())
else:
with torch.no_grad():
p_distillation = self.do_batch_old(r1, r2) # iCaRL: distillation
p_old_replay = self.do_batch(r1, r2)
replay_loss += self.config.reg_weight * self.loss_fn(p_old_replay[idx],
p_distillation[idx].detach())
else:
if self.config.amp:
with autocast():
p_old_replay = self.do_batch(r1, r2) # ER: supervised by GT
replay_loss += self.config.reg_weight * self.loss_fn(p_old_replay[idx], yr.detach())
else:
p_old_replay = self.do_batch(r1, r2) # ER: supervised by GT
replay_loss += self.config.reg_weight * self.loss_fn(p_old_replay[idx], yr.detach())
# if not self.config.icarl:
# replay_loss = replay_loss / self.config.task_id #average replay_loss TODO:verfiy this
self.loss += replay_loss
elif self.config.sample_strategy == 'random':
raise NotImplementedError
self.optimizer.zero_grad()
if self.config.amp:
self.scaler.scale(self.loss).backward()
self.scaler.step(self.optimizer)
self.scaler.update()
else:
self.loss.backward()
self.optimizer.step()
if self.config.SI:
# 3.Accumulate the w
for n, p in self.params.items():
delta = p.detach() - old_params[n]
delta = delta
if p.grad is not None: # In multi-head network, some head could have no grad (lazy) since no loss go through it.
self.w[n] -= p.grad * delta # w[n] is >=0
else:
self.w = {}
# statistics
running_loss = beta * running_loss + (1 - beta) * self.loss.data.item()
loss_corrected = running_loss / (1 - beta ** local_counter)
current_time = time.time()
duration = current_time - start_time
running_duration = beta * running_duration + (1 - beta) * duration
duration_corrected = running_duration / (1 - beta ** local_counter)
examples_per_sec = self.train_batch_size / duration_corrected
format_str = ('(E:%d, S:%d / %d) [Loss = %.4f] [Reg Loss = %.4f] (%.1f samples/sec; %.3f '
'sec/batch)')
print(format_str % (epoch, step + 1, num_steps_per_epoch, loss_corrected, reg_loss,
examples_per_sec, duration_corrected))
local_counter += 1
self.start_step = 0
start_time = time.time()
self.train_loss.append(loss_corrected)
save_best = False
if (epoch+1) % self.epochs_per_eval == 0:
#if (not self.config.fc) & ((epoch + 1) % self.epochs_per_eval == 0):
# evaluate after every other epoch
test_results_srcc, val_results_srcc, all_hat_test, all_hat_val = self.eval()
self.test_results_srcc['live'].append(test_results_srcc['live'])
self.test_results_srcc['csiq'].append(test_results_srcc['csiq'])
self.test_results_srcc['kadid10k'].append(test_results_srcc['kadid10k'])
self.test_results_srcc['bid'].append(test_results_srcc['bid'])
self.test_results_srcc['clive'].append(test_results_srcc['clive'])
self.test_results_srcc['koniq10k'].append(test_results_srcc['koniq10k'])
self.val_results_srcc['live'].append(val_results_srcc['live'])
self.val_results_srcc['csiq'].append(val_results_srcc['csiq'])
self.val_results_srcc['kadid10k'].append(val_results_srcc['kadid10k'])
self.val_results_srcc['bid'].append(val_results_srcc['bid'])
self.val_results_srcc['clive'].append(val_results_srcc['clive'])
self.val_results_srcc['koniq10k'].append(val_results_srcc['koniq10k'])
out_str = 'Testing: LIVE SRCC: {:.4f} CSIQ SRCC: {:.4f} BID SRCC: {:.4f}\n' \
'CLIVE SRCC: {:.4f} KONIQ10K SRCC: {:.4f} KADID10K SRCC: {:.4f}'.format(
test_results_srcc['live'],
test_results_srcc['csiq'],
test_results_srcc['bid'],
test_results_srcc['clive'],
test_results_srcc['koniq10k'],
test_results_srcc['kadid10k'],
)
out_str2 = 'Validation: LIVE SRCC: {:.4f} CSIQ SRCC: {:.4f} BID SRCC: {:.4f}\n' \
'CLIVE SRCC: {:.4f} KONIQ10K SRCC: {:.4f} KADID10K SRCC: {:.4f}'.format(
val_results_srcc['live'],
val_results_srcc['csiq'],
val_results_srcc['bid'],
val_results_srcc['clive'],
val_results_srcc['koniq10k'],
val_results_srcc['kadid10k'],
)
print(out_str)
print(out_str2)
task = self.config.id2dataset[self.indicator-1]
if self.config.JL:
indicator = 0
for idx, item in enumerate(val_results_srcc):
indicator += val_results_srcc[item]
indicator = indicator / len(val_results_srcc)
elif (self.config.JL_CL) | (self.config.GDumb):
indicator = 0
for idx in range(self.config.task_id + 1):
indicator += val_results_srcc[self.config.id2dataset[idx]]
indicator = indicator / (self.config.task_id + 1)
else:
indicator = val_results_srcc[task]
if indicator > self.best_srcc:
self.best_srcc = indicator
print('new best ! SRCC = {}'.format(self.best_srcc))
save_best = True
if (epoch == (self.config.max_epochs2-1)) & self.config.save_replay:
# sampling for replay
task_name = task
replay_txt = task_name + '_train_score.txt'
csv_file = os.path.join(self.config.trainset, 'splits2', str(self.config.split), replay_txt)
replay_data = pd.read_csv(csv_file, sep='\t', header=None)
num_data = len(replay_data)
if self.config.task_id == 0: # first, no previously stored sample
#self.replayers = {}
slice_idx = np.arange(0, num_data)
np.random.shuffle(slice_idx)
if num_data > self.config.replay_memory:
slice_idx = slice_idx[0:self.config.replay_memory]
replay_data = replay_data.iloc[slice_idx, :]
self.replayers[task_name] = replay_data
else:
previous_length = 0
for i in range(self.config.task_id): # traverse previous task(s)
previous_name = self.config.id2dataset[i]
previous_length += len(self.replayers[previous_name])
remaining_budget = self.config.replay_memory - previous_length
if remaining_budget >= num_data: #have enough room, just save all data
self.replayers[task_name] = replay_data
else: # insufficient memory, randomly remove some samples from previous memory
current_task_num = self.config.task_id + 1 # number of tasks up till now
each_num = self.config.replay_memory // current_task_num
slice_idx = np.arange(0, each_num)
np.random.shuffle(slice_idx)
# handling previously stored samples
for i in range(self.config.task_id):
previous_name = self.config.id2dataset[i]
previous_data = self.replayers[previous_name]
previous_len = len(previous_data)
if previous_len > each_num: # the i-th previous task has more samples than the average num
previous_slice = np.arange(0, previous_len)
np.random.shuffle(previous_slice)
previous_slice = previous_slice[0:each_num]
previous_data = previous_data.iloc[previous_slice, :]
self.replayers[previous_name] = previous_data
else:
continue # do nothing if the i-th old task has less samples than the average num
# sampling on the current task
if num_data > each_num:
slice_idx = np.arange(0, num_data)
np.random.shuffle(slice_idx)
slice_idx = slice_idx[0:each_num]
replay_data = replay_data.iloc[slice_idx, :]
self.replayers[task_name] = replay_data
if (epoch + 1) % self.epochs_per_save == 0:
model_name = self.model_name
model_name_latest = os.path.join(self.ckpt_path, model_name + '_latest.pt')
#'scaler': self.scaler.state_dict()
if self.config.amp:
self._save_checkpoint({
'epoch': epoch,
'state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'train_loss': self.train_loss,
'test_results_srcc': self.test_results_srcc,
'val_results_srcc': self.val_results_srcc,
'best_srcc': self.best_srcc,
'regularization_terms': self.regularization_terms,
'replayers': self.replayers,
'scaler': self.scaler.state_dict(),
'w': self.w
}, model_name_latest)
if save_best:
model_name_best = os.path.join(self.ckpt_path, model_name + '_best.pt')
self._save_checkpoint({
'epoch': epoch,
'state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),