-
Notifications
You must be signed in to change notification settings - Fork 6
/
train_rfda_ft.py
616 lines (536 loc) · 21.6 KB
/
train_rfda_ft.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
import os
os.environ['CUDA_VISIBLE_DEVICES'] = "0,1"
import math
import time
import yaml
import argparse
import random
import torch
import torch.optim as optim
import torch.nn as nn
import os.path as op
import numpy as np
from tqdm import tqdm
from torch.nn.parallel import DistributedDataParallel as DDP
from collections import OrderedDict
from PIL import Image
import utils # my tool box
import dataset
from net_rfda import RFDA
# torch.autograd.set_detect_anomaly(True)
def receive_arg():
"""Process all hyper-parameters and experiment settings.
Record in opts_dict."""
parser = argparse.ArgumentParser()
parser.add_argument(
'--opt_path', type=str, default='option_R3_mfqev2_2G.yml',
help='Path to option YAML file.'
)
parser.add_argument(
'--local_rank', type=int, default=0,
help='Distributed launcher requires.'
)
args = parser.parse_args()
with open(args.opt_path, 'r') as fp:
opts_dict = yaml.load(fp, Loader=yaml.FullLoader)
opts_dict['opt_path'] = args.opt_path
opts_dict['train']['rank'] = args.local_rank
if opts_dict['train']['exp_name'] == None:
opts_dict['train']['exp_name'] = utils.get_timestr()
opts_dict['train']['log_path'] = op.join(
"exp", opts_dict['train']['exp_name'], "log.log"
)
opts_dict['train']['checkpoint_save_path_pre'] = op.join(
"exp", opts_dict['train']['exp_name'], "ckp_"
)
opts_dict['train']['num_gpu'] = torch.cuda.device_count()
if opts_dict['train']['num_gpu'] > 1:
opts_dict['train']['is_dist'] = True
else:
opts_dict['train']['is_dist'] = False
opts_dict['test']['restore_iter'] = int(
opts_dict['test']['restore_iter']
)
return opts_dict
def get_lr(lr,milestones,it,gamma):
count=0
for milestone in milestones:
if(it>milestone):count+=1
return lr*pow(gamma,count)
def main():
# ==========
# parameters
# ==========
opts_dict = receive_arg()
rank = opts_dict['train']['rank']
unit = opts_dict['train']['criterion']['unit']
num_iter = int(opts_dict['train']['num_iter'])
interval_print = int(opts_dict['train']['interval_print'])
interval_val = int(opts_dict['train']['interval_val'])
# ==========
# init distributed training
# ==========
# opts_dict['train']['is_dist'] = False
if opts_dict['train']['is_dist']:
utils.init_dist(
local_rank=rank,
backend='nccl'
)
# TO-DO: load resume states if exists
pass
# ==========
# create logger
# ==========
if rank == 0:
log_dir = op.join("exp", opts_dict['train']['exp_name'])
if not os.path.exists(log_dir):
utils.mkdir(log_dir)
log_fp = open(opts_dict['train']['log_path'], 'a')
# log all parameters
msg = (
f"{'<' * 10} Hello {'>' * 10}\n"
f"Timestamp: [{utils.get_timestr()}]\n"
f"\n{'<' * 10} Options {'>' * 10}\n"
f"{utils.dict2str(opts_dict)}"
)
print(msg)
log_fp.write(msg + '\n')
log_fp.flush()
# ==========
# TO-DO: init tensorboard
# ==========
pass
# ==========
# fix random seed
# ==========
seed = opts_dict['train']['random_seed']
# >I don't know why should rs + rank
utils.set_random_seed(seed + rank)
# ==========
# Ensure reproducibility or Speed up
# ==========
#torch.backends.cudnn.benchmark = False # if reproduce
#torch.backends.cudnn.deterministic = True # if reproduce
torch.backends.cudnn.benchmark = True # speed up
# ==========
# create train and val data prefetchers
# ==========
# create datasets
train_ds_type = opts_dict['dataset']['train']['type']
val_ds_type = opts_dict['dataset']['val']['type']
radius = opts_dict['network']['radius']
radius_real = 3
assert train_ds_type in dataset.__all__, \
"Not implemented!"
assert val_ds_type in dataset.__all__, \
"Not implemented!"
train_ds_cls = getattr(dataset, train_ds_type)
val_ds_cls = getattr(dataset, val_ds_type)
train_ds = train_ds_cls(
opts_dict=opts_dict['dataset']['train'],
radius=radius
)
val_ds = val_ds_cls(
opts_dict=opts_dict['dataset']['val'],
radius=radius
)
# create datasamplers
train_sampler = utils.DistSampler(
dataset=train_ds,
num_replicas=opts_dict['train']['num_gpu'],
rank=rank,
ratio=opts_dict['dataset']['train']['enlarge_ratio']
)
val_sampler = None # no need to sample val data
# create dataloaders
train_loader = utils.create_dataloader(
dataset=train_ds,
opts_dict=opts_dict,
sampler=train_sampler,
phase='train',
seed=opts_dict['train']['random_seed']
)
val_loader = utils.create_dataloader(
dataset=val_ds,
opts_dict=opts_dict,
sampler=val_sampler,
phase='val'
)
assert train_loader is not None
batch_size = opts_dict['dataset']['train']['batch_size_per_gpu'] * \
opts_dict['train']['num_gpu'] # divided by all GPUs
num_iter_per_epoch = math.ceil(len(train_ds) * \
opts_dict['dataset']['train']['enlarge_ratio'] / batch_size)
num_epoch = math.ceil(num_iter / num_iter_per_epoch)
val_num = len(val_ds)
# create dataloader prefetchers
tra_prefetcher = utils.CPUPrefetcher(train_loader)
val_prefetcher = utils.CPUPrefetcher(val_loader)
# ==========
# create model
# ==========
model = RFDA(opts_dict=opts_dict['network'])
model = model.to(rank)
# if opts_dict['train']['is_dist']:
# model = DDP(model, device_ids=[rank])
# ==========
# define loss func & optimizer & scheduler & criterion
# ==========
# define loss func
if opts_dict['train']['loss']['type'] == 'CharbonnierLoss':
print("CharbonnierLoss")
loss_func = utils.CharbonnierLoss(**opts_dict['train']['loss'])
elif opts_dict['train']['loss']['type'] == 'L1':
print("L1Loss")
loss_func = nn.L1Loss()
elif opts_dict['train']['loss']['type'] == 'L2':
print("MSELoss")
loss_func = nn.MSELoss()
# TODO
# loss_func = torch.nn.MSELoss()
# define optimizer
assert opts_dict['train']['optim'].pop('type') == 'Adam', \
"Not implemented."
# print(opts_dict['train']['optim'],"???")
# optimizer = optim.Adam(model.parameters(),**opts_dict['train']['optim'])
base_params = filter(lambda p: id(p) not in list(map(id, model.qenet.parameters()))+list(map(id, model.ffnet.parameters())),model.parameters())
optimizer = optim.Adam(
[
{"params": base_params},
{"params": model.qenet.parameters(), "lr": opts_dict['train']['optim']["lr"] / 10.0},
{"params": model.ffnet.parameters(), "lr": opts_dict['train']['optim']["lr"] / 10.0},
],
**opts_dict['train']['optim']
)
# define scheduler
milestones=[]
for milestone in opts_dict['train']['scheduler']['milestones']:
milestones.append(int(milestone*num_iter))
gamma=opts_dict['train']['scheduler']['gamma']
opt_lr=opts_dict['train']['optim']['lr']
set_milestones=set(milestones)
# define criterion
assert opts_dict['train']['criterion'].pop('type') == \
'PSNR', "Not implemented."
criterion = utils.PSNR()
start_iter = 0
# ==========
# TO-DO: resume training & load pre-trained model
# ==========
filename = '/remote-home/myzhao/MM_CKPS/R3_QP37.pt'
print("=> loading checkpoint '{}'".format(filename))
checkpoint = torch.load(filename, map_location=torch.device('cpu'))
# start_iter = checkpoint['num_iter_accum']
# optimizer.load_state_dict(checkpoint['optimizer'])
# if opts_dict['train']['scheduler']['is_on']:
# for i in range(start_iter):
# scheduler.step()
# 第一阶段先load一下原有模型
if 'module.' in list(checkpoint['state_dict'].keys())[0]: # multi-gpu training
new_state_dict = OrderedDict()
for k, v in checkpoint['state_dict'].items():
name = k[7:] # remove module
new_state_dict[name] = v
model.load_state_dict(new_state_dict,strict=False)
else: # single-gpu training
model.load_state_dict(checkpoint['state_dict'])
model = DDP(model, device_ids=[rank],find_unused_parameters=True)
now_lr=get_lr(opt_lr,milestones,start_iter,gamma)
# print(opt_lr,milestones,start_iter,gamma)
# print("now_lr",now_lr)
# if (opts_dict["network"]['qenet']['netname'] != 'C2CNET'):
# for param_group in optimizer.param_groups:
# param_group["lr"] = now_lr
optimizer.param_groups[0]["lr"] = now_lr
optimizer.param_groups[1]["lr"] = now_lr/10.0
optimizer.param_groups[2]["lr"] = now_lr/10.0
print("=> loaded checkpoint Success ")
torch.autograd.set_detect_anomaly(True)
# start_iter = 0 # should be restored
start_epoch = start_iter // num_iter_per_epoch
# display and log
if rank == 0:
msg = (
f"\n{'<' * 10} Dataloader {'>' * 10}\n"
f"total iters: [{num_iter}]\n"
f"total epochs: [{num_epoch}]\n"
f"iter per epoch: [{num_iter_per_epoch}]\n"
f"val sequence: [{val_num}]\n"
f"start from iter: [{start_iter}]\n"
f"start from epoch: [{start_epoch}]"
)
print(msg)
log_fp.write(msg + '\n')
log_fp.flush()
# Test LR
# for i in range(num_iter):
# optimizer.step()
# scheduler.step()
# lr = optimizer.param_groups[0]['lr']
# if i%1000==0:
# print(i,lr,i/num_iter)
# os._exit(233)
# ==========
# evaluate original performance, e.g., PSNR before enhancement
# ==========
vid_num = val_ds.get_vid_num()
if opts_dict['train']['pre-val'] and rank == 0:
msg = f"\n{'<' * 10} Pre-evaluation {'>' * 10}"
print(msg)
log_fp.write(msg + '\n')
per_aver_dict = {}
for i in range(vid_num):
per_aver_dict[i] = utils.Counter()
pbar = tqdm(
total=val_num,
ncols=opts_dict['train']['pbar_len']
)
# fetch the first batch
val_prefetcher.reset()
val_data = val_prefetcher.next()
while val_data is not None:
# get data
gt_data = val_data['gt'].to(rank) # (B [RGB] H W)
lq_data = val_data['lq'].to(rank) # (B T [RGB] H W)
index_vid = val_data['index_vid'].item()
name_vid = val_data['name_vid'][0] # bs must be 1!
b, _, _, _, _ = lq_data.shape
# 只算Y通道的!!!
# lq_data = lq_data[:,:,0,...]*0.299 + lq_data[:,:,1,...]*0.587 + lq_data[:,:,2,...]*0.114
# gt_data = gt_data[:,0,...]*0.299 + gt_data[:,1,...]*0.587 + gt_data[:,2,...]*0.114
# eval
batch_perf = np.mean(
[criterion(lq_data[i,radius,...], gt_data[i]) for i in range(b)]
) # bs must be 1!
# log
per_aver_dict[index_vid].accum(volume=batch_perf)
# display
pbar.set_description(
"{:s}: [{:.3f}] {:s}".format(name_vid, batch_perf, unit)
)
pbar.update()
# fetch next batch
val_data = val_prefetcher.next()
pbar.close()
# log
ave_performance = np.mean([
per_aver_dict[index_vid].get_ave() for index_vid in range(vid_num)
])
msg = "> ori performance: [{:.3f}] {:s}".format(ave_performance, unit)
print(msg)
log_fp.write(msg + '\n')
log_fp.flush()
if opts_dict['train']['is_dist']:
torch.distributed.barrier() # all processes wait for ending
if rank == 0:
msg = f"\n{'<' * 10} Training {'>' * 10}"
print(msg)
log_fp.write(msg + '\n')
# create timer
total_timer = utils.Timer() # total tra + val time of each epoch
# ==========
# start training + validation (test)
# ==========
model.train()
num_iter_accum = start_iter
for current_epoch in range(start_epoch, num_epoch + 1):
# shuffle distributed subsamplers before each epoch
if opts_dict['train']['is_dist']:
train_sampler.set_epoch(current_epoch)
# fetch the first batch
tra_prefetcher.reset()
train_data = tra_prefetcher.next()
# train this epoch
while train_data is not None:
# over sign
num_iter_accum += 1
if num_iter_accum > num_iter:
break
# get data
gt_data = train_data['gt'].to(rank) # (B 3 [RGB] H W)
lq_data = train_data['lq'].to(rank) # (B T [RGB] H W)
# print(gt_data.size(),"vs",lq_data.size())
b, t, c, _, _ = lq_data.shape
# 重新装载
if not opts_dict['network']['stdf'].__contains__('netname') or opts_dict['network']['stdf']['netname']=='default':
input_data = torch.cat(
[lq_data[:,:,i,...] for i in range(c)],
dim=1
) # B [R1 ... R7 G1 ... G7 B1 ... B7] H W
else:
input_data = lq_data
loss_sum = 0
enhanced_datas = []
for i in range(t):
# print("i=",i,'t=',t)
neighbor_list = list(range(i - radius_real, i + radius_real + 1))
neighbor_list = list(np.clip(neighbor_list, 0, t - 1))
frm_list = []
# print(neighbor_list,'vs',lq_data.size())
for _i in neighbor_list:
# print("i=",i)
# print(lq_data[:,int(i),:...].size())
frm_list.append(lq_data[:,int(_i),...])
frm_list = torch.cat(frm_list,1).to(rank)
if i==0:
enhanced_data,hint = model(frm_list)
enhanced_datas.append(enhanced_data)
# loss = torch.mean(torch.stack(
# [loss_func(enhanced_data[j], gt_data[j,i,...]) for j in range(b)]
# ))
else:
enhanced_data,hint = model(frm_list,hint)
enhanced_datas.append(enhanced_data)
# loss = loss + torch.mean(torch.stack(
# [loss_func(enhanced_data[j], gt_data[j,i,...]) for j in range(b)]
# ))
optimizer.zero_grad() # zero grad
# 计算损失
loss = 0
for i in range(t):
loss = loss + torch.mean(torch.stack(
[loss_func(enhanced_datas[i][j,...], gt_data[j,i,...]) for j in range(b)]
))
loss.backward() # cal grad
optimizer.step() # update parameters
# os._exit(233)
# update learning rate
if (num_iter_accum % interval_print == 0) and (rank == 0):
now_lr = get_lr(opt_lr, milestones, num_iter_accum, gamma)
if (now_lr != optimizer.param_groups[0]['lr']):
# for param_group in optimizer.param_groups:
# param_group["lr"] = now_lr
optimizer.param_groups[0]["lr"] = now_lr
optimizer.param_groups[1]["lr"] = now_lr/10.0
optimizer.param_groups[2]["lr"] = now_lr/10.0
# display & log
lr = optimizer.param_groups[0]['lr']
loss_item = loss.item()
msg = (
f"iter: [{num_iter_accum}]/{num_iter}, "
f"epoch: [{current_epoch}]/{num_epoch - 1}, "
"lr: [{:.3f}]x1e-4, loss: [{:.4f}]".format(
lr*1e4, loss_item
)
)
print(msg)
log_fp.write(msg + '\n')
if ((num_iter_accum % interval_val == 0) or \
(num_iter_accum == num_iter)) and (rank == 0):
# save model
checkpoint_save_path = (
f"{opts_dict['train']['checkpoint_save_path_pre']}"
f"{num_iter_accum}"
".pt"
)
state = {
'num_iter_accum': num_iter_accum,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
}
torch.save(state, checkpoint_save_path)
# print("continue")
# continuecontinue
# validation
with torch.no_grad():
per_aver_dict = {}
for index_vid in range(vid_num):
per_aver_dict[index_vid] = utils.Counter()
pbar = tqdm(
total=7980,
ncols=opts_dict['train']['pbar_len']
)
# train -> eval
model.eval()
# fetch the first batch
val_prefetcher.reset()
val_data = val_prefetcher.next()
while val_data is not None:
# get data
gt_data = val_data['gt'] # (B T [RGB] H W)
lq_data = val_data['lq'] # (B T [RGB] H W)
# print(gt_data.size(),'vs',lq_data.size())
# print(gt_data)
# os._exit(2333)
index_vid = val_data['index_vid'].item()
name_vid = val_data['name_vid'][0] # bs must be 1!
b, t, c, _, _ = lq_data.shape
# input_data = torch.cat(
# [lq_data[:,:,i,...] for i in range(c)],
# dim=1
# ) # B [R1 ... R7 G1 ... G7 B1 ... B7] H W
for i in range(t):
neighbor_list = list(range(i - radius_real, i + radius_real + 1))
neighbor_list = list(np.clip(neighbor_list, 0, t - 1))
frm_list = []
# print(neighbor_list,'vs',lq_data.size())
for _i in neighbor_list:
# print("i=",i)
# print(lq_data[:,int(i),:...].size())
frm_list.append(lq_data[:,int(_i),...])
frm_list = torch.cat(frm_list,1).to(rank)
if i==0:
# print("first size",first.size())
enhanced_data,hint = model(frm_list) # (B [RGB] H W)
else:
enhanced_data,hint = model(frm_list,hint)
# 注意了,接下来我们只算Y通道的,为了公平的比较.
# enhanced_data = enhanced_data[:,0,...]*0.299 + enhanced_data[:,1,...]*0.587 + enhanced_data[:,2,...]*0.114
# gt_data = gt_data[:,0,...]*0.299 + gt_data[:,1,...]*0.587 + gt_data[:,2,...]*0.114
# eval
batch_perf = np.mean(
[criterion(enhanced_data[j], gt_data[j,i,...].to(rank)) for j in range(b)]
) # bs must be 1!
# display
pbar.set_description(
"{:s}: [{:.3f}] {:s}"
.format(name_vid, batch_perf, unit)
)
pbar.update()
# log
per_aver_dict[index_vid].accum(volume=batch_perf)
# torch.cuda.empty_cache()
# fetch next batch
torch.cuda.empty_cache()
val_data = val_prefetcher.next()
# return
# end of val
pbar.close()
# eval -> train
model.train()
# log
ave_per = np.mean([
per_aver_dict[index_vid].get_ave() for index_vid in range(vid_num)
])
msg = (
"> model saved at {:s}\n"
"> ave val per: [{:.3f}] {:s}"
).format(
checkpoint_save_path, ave_per, unit
)
print(msg)
log_fp.write(msg + '\n')
log_fp.flush()
if opts_dict['train']['is_dist']:
torch.distributed.barrier() # all processes wait for ending
# fetch next batch
train_data = tra_prefetcher.next()
# end of this epoch (training dataloader exhausted)
# end of all epochs
# ==========
# final log & close logger
# ==========
if rank == 0:
total_time = total_timer.get_interval() / 3600
msg = "TOTAL TIME: [{:.1f}] h".format(total_time)
print(msg)
log_fp.write(msg + '\n')
msg = (
f"\n{'<' * 10} Goodbye {'>' * 10}\n"
f"Timestamp: [{utils.get_timestr()}]"
)
print(msg)
log_fp.write(msg + '\n')
log_fp.close()
if __name__ == '__main__':
main()