-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
626 lines (540 loc) · 30.7 KB
/
train.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
import argparse
import os
import shutil
from datetime import datetime
import torch.backends.cudnn as cudnn
import torch.multiprocessing as mp
import torch.nn.utils.prune as prune
import torch.optim as optim
import torch.utils.data
import torchvision.transforms as transforms
from torch.utils.tensorboard import SummaryWriter
from models.model_best import CustomDataSet, Generator
from utils import *
def main():
parser = argparse.ArgumentParser()
# dataset parameters
parser.add_argument('--vid', default=[None], type=int, nargs='+', help='video id list for training')
parser.add_argument('--scale', type=int, default=1, help='scale-up facotr for data transformation, added to suffix!!!!')
parser.add_argument('--frame_gap', type=int, default=1, help='frame selection gap')
parser.add_argument('--augment', type=int, default=0, help='augment frames between frames, added to suffix!!!!')
parser.add_argument('--dataset', type=str, default='UVG', help='dataset',)
parser.add_argument('--test_gap', default=1, type=int, help='evaluation gap')
# NERV architecture parameters
# embedding parameters
parser.add_argument('--embed', type=str, default='1.25_80', help='base value/embed length for position encoding')
# FC + Conv parameters
parser.add_argument('--stem_dim_num', type=str, default='1024_1', help='hidden dimension and length')
parser.add_argument('--fc_hw_dim', type=str, default='9_16_128', help='out size (h,w) for mlp')
parser.add_argument('--expansion', type=float, default=8, help='channel expansion from fc to conv')
parser.add_argument('--reduction', type=int, default=2)
parser.add_argument('--strides', type=int, nargs='+', default=[5, 3, 2, 2, 2], help='strides list')
parser.add_argument('--num-blocks', type=int, default=1)
parser.add_argument('--norm', default='none', type=str, help='norm layer for generator', choices=['none', 'bn', 'in'])
parser.add_argument('--act', type=str, default='gelu', help='activation to use', choices=['relu', 'leaky', 'leaky01', 'relu6', 'gelu', 'swish', 'softplus', 'hardswish'])
parser.add_argument('--lower-width', type=int, default=32, help='lowest channel width for output feature maps')
parser.add_argument("--single_res", action='store_true', help='single resolution, added to suffix!!!!')
parser.add_argument("--conv_type", default='conv', type=str, help='upscale methods, can add bilinear and deconvolution methods', choices=['conv', 'deconv', 'bilinear'])
# General training setups
parser.add_argument('-j', '--workers', type=int, help='number of data loading workers', default=4)
parser.add_argument('-b', '--batchSize', type=int, default=1, help='input batch size')
parser.add_argument('--not_resume_epoch', action='store_true', help='resuming start_epoch from checkpoint')
parser.add_argument('-e', '--epochs', type=int, default=150, help='number of epochs to train for')
parser.add_argument('--cycles', type=int, default=1, help='epoch cycles for training')
parser.add_argument('--warmup', type=float, default=0.2, help='warmup epoch ratio compared to the epochs, default=0.2, added to suffix!!!!')
parser.add_argument('--lr', type=float, default=0.001, help='learning rate, default=0.0002')
parser.add_argument('--lr_type', type=str, default='cosine', help='learning rate type, default=cosine')
parser.add_argument('--lr_steps', default=[], type=float, nargs="+", metavar='LRSteps', help='epochs to decay learning rate by 10, added to suffix!!!!')
parser.add_argument('--beta', type=float, default=0.5, help='beta for adam. default=0.5, added to suffix!!!!')
parser.add_argument('--loss_type', type=str, default='L2', help='loss type, default=L2')
parser.add_argument('--lw', type=float, default=1.0, help='loss weight, added to suffix!!!!')
parser.add_argument('--sigmoid', action='store_true', help='using sigmoid for output prediction')
# evaluation parameters
parser.add_argument('--eval_only', action='store_true', default=False, help='do evaluation only')
parser.add_argument('--eval_freq', type=int, default=50, help='evaluation frequency, added to suffix!!!!')
parser.add_argument('--quant_bit', type=int, default=-1, help='bit length for model quantization')
parser.add_argument('--quant_axis', type=int, default=0, help='quantization axis (-1 means per tensor)')
parser.add_argument('--dump_images', action='store_true', default=False, help='dump the prediction images')
parser.add_argument('--eval_fps', action='store_true', default=False, help='fwd multiple times to test the fps ')
# pruning paramaters
parser.add_argument('--prune_steps', type=float, nargs='+', default=[0.,], help='prune steps')
parser.add_argument('--prune_ratio', type=float, default=1.0, help='pruning ratio')
# distribute learning parameters
parser.add_argument('--manualSeed', type=int, default=1, help='manual seed')
parser.add_argument('--init_method', default='tcp://127.0.0.1:9888', type=str,
help='url used to set up distributed training')
parser.add_argument('-d', '--distributed', action='store_true', default=False, help='distributed training, added to suffix!!!!')
# logging, output directory,
parser.add_argument('--debug', action='store_true', help='defbug status, earlier for train/eval')
parser.add_argument('-p', '--print-freq', default=50, type=int,)
parser.add_argument('--weight', default='None', type=str, help='pretrained weights for ininitialization')
parser.add_argument('--pruned_weight', action='store_true', help='when using pruned model latest weights for ininitialization')
parser.add_argument('--overwrite', action='store_true', help='overwrite the output dir if already exists')
parser.add_argument('--outf', default='unify', help='folder to output images and model checkpoints')
parser.add_argument('--suffix', default='', help="suffix str for outf")
args = parser.parse_args()
args.warmup = int(args.warmup * args.epochs)
print(args)
torch.set_printoptions(precision=4)
if args.debug:
args.eval_freq = 1
args.outf = 'output/nerv_plus/debug'
else:
args.outf = os.path.join('output/nerv_plus', args.outf)
if args.prune_ratio < 1 and not args.eval_only:
prune_str = '_Prune{}_{}'.format(args.prune_ratio, ','.join([str(x) for x in args.prune_steps]))
else:
prune_str = ''
extra_str = '_Strd{}_{}Res{}{}'.format( ','.join([str(x) for x in args.strides]), 'Sin' if args.single_res else f'_lw{args.lw}_multi',
'_dist' if args.distributed else '', f'_eval' if args.eval_only else '')
norm_str = '' if args.norm == 'none' else args.norm
exp_id = f'{args.dataset}/embed{args.embed}_{args.stem_dim_num}_fc_{args.fc_hw_dim}__exp{args.expansion}_reduce{args.reduction}_low{args.lower_width}_blk{args.num_blocks}_cycle{args.cycles}' + \
f'_gap{args.frame_gap}_e{args.epochs}_warm{args.warmup}_b{args.batchSize}_{args.conv_type}_lr{args.lr}_{args.lr_type}' + \
f'_{args.loss_type}{norm_str}{extra_str}{prune_str}'
exp_id += f'_act{args.act}_{args.suffix}'
args.exp_id = exp_id
args.outf = os.path.join(args.outf, exp_id)
if args.overwrite and os.path.isdir(args.outf):
print('Will overwrite the existing output dir!')
shutil.rmtree(args.outf)
if not os.path.isdir(args.outf):
os.makedirs(args.outf)
port = hash(args.exp_id) % 20000 + 10000
args.init_method = f'tcp://127.0.0.1:{port}'
print(f'init_method: {args.init_method}', flush=True)
torch.set_printoptions(precision=2)
args.ngpus_per_node = torch.cuda.device_count()
if args.distributed and args.ngpus_per_node > 1:
mp.spawn(train, nprocs=args.ngpus_per_node, args=(args,))
else:
train(None, args)
def train(local_rank, args):
cudnn.benchmark = True
torch.manual_seed(args.manualSeed)
np.random.seed(args.manualSeed)
random.seed(args.manualSeed)
train_best_psnr, train_best_msssim, val_best_psnr, val_best_msssim = [torch.tensor(0) for _ in range(4)]
is_train_best, is_val_best = False, False
PE = PositionalEncoding(args.embed)
args.embed_length = PE.embed_length
model = Generator(embed_length=args.embed_length, stem_dim_num=args.stem_dim_num, fc_hw_dim=args.fc_hw_dim, expansion=args.expansion,
num_blocks=args.num_blocks, norm=args.norm, act=args.act, bias = True, reduction=args.reduction, conv_type=args.conv_type,
stride_list=args.strides, sin_res=args.single_res, lower_width=args.lower_width, sigmoid=args.sigmoid)
#---------- qat ---------------
#model.eval()
#model.qconfig = torch.ao.quantization.get_default_qat_qconfig('x86')
##model_fused = torch.ao.quantization.fuse_modules(model, [['conv', 'bn', 'relu']])
#model = torch.ao.quantization.prepare_qat(model.train())
# -----------------------------
##### prune model params and flops #####
prune_net = args.prune_ratio < 1
# import pdb; pdb.set_trace; from IPython import embed; embed()
if prune_net:
param_list = []
param_list.append(model.stem.fc1)
param_list.append(model.stem.fc2)
##---- update if changing from gcnn block
for layer_ind in range(5):
param_list.append(model.layers[layer_ind].proj.conv_block[0])
param_list.append(model.layers[layer_ind].gcnn.fc1)
param_list.append(model.layers[layer_ind].gcnn.conv)
param_list.append(model.layers[layer_ind].gcnn.fc2)
param_list.append(model.layers[layer_ind].nerv_block[0].conv)
param_list.append(model.layers[layer_ind].rffn.ffn[0])
param_list.append(model.layers[layer_ind].rffn.ffn[2])
param_list.append(model.layers[layer_ind].rffn.ffn[5])
print('#### param_list ####', param_list)
param_to_prune = [(ele, 'weight') for ele in param_list]
prune_base_ratio = args.prune_ratio ** (1. / len(args.prune_steps))
args.prune_steps = [int(x * args.epochs) for x in args.prune_steps]
prune_num = 0
if args.eval_only:
prune.global_unstructured(
param_to_prune,
pruning_method=prune.L1Unstructured,
amount=1 - prune_base_ratio ** prune_num,
)
##### get model params and flops #####
total_params = sum([p.data.nelement() for p in model.parameters()]) / 1e6
if local_rank in [0, None]:
params = sum([p.data.nelement() for p in model.parameters()]) / 1e6
print(f'{args}\n {model}\n Model Params: {params}M')
with open('{}/rank0.txt'.format(args.outf), 'a') as f:
f.write(str(model) + '\n' + f'Params: {params}M\n')
writer = SummaryWriter(os.path.join(args.outf, f'param_{total_params}M', 'tensorboard'))
else:
writer = None
# distrite model to gpu or parallel
print("Use GPU: {} for training".format(local_rank))
if args.distributed and args.ngpus_per_node > 1:
torch.distributed.init_process_group(
backend='nccl',
init_method=args.init_method,
world_size=args.ngpus_per_node,
rank=local_rank,
)
torch.cuda.set_device(local_rank)
assert torch.distributed.is_initialized()
args.batchSize = int(args.batchSize / args.ngpus_per_node)
model = torch.nn.parallel.DistributedDataParallel(model.to(local_rank), device_ids=[local_rank], \
output_device=local_rank, find_unused_parameters=False)
elif args.ngpus_per_node > 1:
model = torch.nn.DataParallel(model).cuda()
else:
model = model.cuda()
optimizer = optim.Adam(model.parameters(), betas=(args.beta, 0.999))
# resume from args.weight
checkpoint = None
loc = 'cuda:{}'.format(local_rank if local_rank is not None else 0)
if args.weight != 'None':
print("=> loading checkpoint '{}'".format(args.weight))
checkpoint_path = args.weight
checkpoint = torch.load(checkpoint_path, map_location='cpu')
orig_ckt = checkpoint['state_dict']
new_ckt = {k.replace('blocks.0.',''):v for k,v in orig_ckt.items()}
### added to load ckpts from a pruned model without need to redefine state dict cause of mask or orig ###
if prune_net and args.pruned_weight:
prune.global_unstructured(
param_to_prune,
pruning_method=prune.L1Unstructured,
amount=1 - prune_base_ratio ** prune_num,
)
sparisity_num = 0.
for param in param_list:
sparisity_num += (param.weight == 0).sum()
print(f'Model sparsity: {sparisity_num / 1e6 / total_params}')
model.load_state_dict(orig_ckt)
#########################################################################################################
elif 'module' in list(orig_ckt.keys())[0] and not hasattr(model, 'module'):
new_ckt={k.replace('module.',''):v for k,v in new_ckt.items()}
model.load_state_dict(new_ckt)
elif 'module' not in list(orig_ckt.keys())[0] and hasattr(model, 'module'):
model.module.load_state_dict(new_ckt)
else:
model.load_state_dict(new_ckt)
print("=> loaded checkpoint '{}' (epoch {})".format(args.weight, checkpoint['epoch']))
# resume from model_latest
checkpoint_path = os.path.join(args.outf, 'model_latest.pth')
if os.path.isfile(checkpoint_path):
checkpoint = torch.load(checkpoint_path, map_location='cpu')
if prune_net:
prune.global_unstructured(
param_to_prune,
pruning_method=prune.L1Unstructured,
amount=1 - prune_base_ratio ** prune_num,
)
sparisity_num = 0.
for param in param_list:
sparisity_num += (param.weight == 0).sum()
print(f'Model sparsity: {sparisity_num / 1e6 / total_params}')
model.load_state_dict(checkpoint['state_dict'])
print("=> Auto resume loaded checkpoint '{}' (epoch {})".format(checkpoint_path, checkpoint['epoch']))
else:
print("=> No resume checkpoint found at '{}'".format(checkpoint_path))
args.start_epoch = 0
if checkpoint is not None:
args.start_epoch = checkpoint['epoch']
train_best_psnr = checkpoint['train_best_psnr'].to(torch.device(loc))
train_best_msssim = checkpoint['train_best_msssim'].to(torch.device(loc))
val_best_psnr = checkpoint['val_best_psnr'].to(torch.device(loc))
val_best_msssim = checkpoint['val_best_msssim'].to(torch.device(loc))
optimizer.load_state_dict(checkpoint['optimizer']) # if other optim init make it in a comment
if args.not_resume_epoch:
args.start_epoch = 0
# setup dataloader
img_transforms = transforms.ToTensor()
DataSet = CustomDataSet
#train_data_dir = f'./data/META/{args.dataset.lower()}'
#val_data_dir = f'./data/META/{args.dataset.lower()}'
#train_data_dir = f'./data/UVG/1080p/{args.dataset.lower()}'
#val_data_dir = f'./data/UVG/1080p/{args.dataset.lower()}'
#train_data_dir = f'./data/MCL_JVC/1080p/{args.dataset.lower()}'
#val_data_dir = f'./data/MCL_JVC/1080p/{args.dataset.lower()}'
#train_data_dir = f'./data/MCL_JVC/720p/{args.dataset.lower()}'
#val_data_dir = f'./data/MCL_JVC/720p/{args.dataset.lower()}'
train_data_dir = f'./data/{args.dataset.lower()}'
val_data_dir = f'./data/{args.dataset.lower()}'
train_dataset = DataSet(train_data_dir, img_transforms,vid_list=args.vid, frame_gap=args.frame_gap, )
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset) if args.distributed else None
train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batchSize, shuffle=(train_sampler is None),
num_workers=args.workers, pin_memory=True, sampler=train_sampler, drop_last=True, worker_init_fn=worker_init_fn)
val_dataset = DataSet(val_data_dir, img_transforms, vid_list=args.vid, frame_gap=args.test_gap, )
val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset) if args.distributed else None
val_dataloader = torch.utils.data.DataLoader(val_dataset, batch_size=args.batchSize, shuffle=False,
num_workers=args.workers, pin_memory=True, sampler=val_sampler, drop_last=False, worker_init_fn=worker_init_fn)
data_size = len(train_dataset)
if args.eval_only:
print('Evaluation ...')
time_str = datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
print_str = f'{time_str}\t Results for checkpoint: {args.weight}\n'
if prune_net:
for param in param_to_prune:
prune.remove(param[0], param[1])
sparisity_num = 0.
for param in param_list:
sparisity_num += (param.weight == 0).sum()
print_str += f'Model sparsity at Epoch{args.start_epoch}: {sparisity_num / 1e6 / total_params}\n'
# import pdb; pdb.set_trace; from IPython import embed; embed()
val_psnr, val_msssim = evaluate(model, val_dataloader, PE, local_rank, args)
print_str += f'PSNR/ms_ssim on validate set for bit {args.quant_bit} with axis {args.quant_axis}: {round(val_psnr.item(),2)}/{round(val_msssim.item(),4)}'
print(print_str)
with open('{}/eval.txt'.format(args.outf), 'a') as f:
f.write(print_str + '\n\n')
return
# Training
start = datetime.now()
total_epochs = args.epochs * args.cycles
'''
#---- c qat ------
activation_bitwidth = 8
fq_activation = torch.quantization.FakeQuantize.with_args(
observer=torch.quantization.MovingAverageMinMaxObserver.with_args(
quant_min=0,
quant_max=2**activation_bitwidth-1,
# quant_max=2**(bitwidth*2)-1,
dtype=torch.quint8,
qscheme=torch.per_tensor_affine,
reduce_range=True
)
)
bitwidth = 8
fq_weights = torch.quantization.FakeQuantize.with_args(
observer = torch.quantization.MovingAveragePerChannelMinMaxObserver.with_args(
quant_min=-(2 ** bitwidth) // 2,
quant_max=(2 ** bitwidth) // 2 - 1,
dtype=torch.qint8,
qscheme=torch.per_channel_symmetric,
reduce_range=False,
ch_axis=0
)
)
intB_qat_qconfig = torch.quantization.QConfig(activation=fq_activation, weight=fq_weights)
model.eval()
model.qconfig = intB_qat_qconfig
model_prepared = torch.quantization.prepare_qat(model.train())
#--------------------
#'''
'''
# ------ a qat --------------
# model must be set to eval for fusion to work
model.eval()
backend1, backend2 = 'fbgemm', 'qnnpack'
model.qconfig = torch.quantization.get_default_qat_qconfig(backend1)
#model_fused = torch.ao.quantization.fuse_modules(model, [['conv', 'bn', 'relu']])
model_prepared = torch.quantization.prepare_qat(model.train())
# --------------------
#'''
for epoch in range(args.start_epoch, total_epochs):
model.train()
##### prune the network if needed #####
if prune_net and epoch in args.prune_steps:
prune_num += 1
prune.global_unstructured(
param_to_prune,
pruning_method=prune.L1Unstructured,
amount=1 - prune_base_ratio ** prune_num,
)
sparisity_num = 0.
for param in param_list:
sparisity_num += (param.weight == 0).sum()
print(f'Model sparsity at Epoch{epoch}: {sparisity_num / 1e6 / total_params}')
epoch_start_time = datetime.now()
psnr_list = []
msssim_list = []
# iterate over dataloader
for i, (data, norm_idx) in enumerate(train_dataloader):
if i > 10 and args.debug:
break
embed_input = PE(norm_idx)
if local_rank is not None:
data = data.cuda(local_rank, non_blocking=True)
embed_input = embed_input.cuda(local_rank, non_blocking=True)
else:
data, embed_input = data.cuda(non_blocking=True), embed_input.cuda(non_blocking=True)
# forward and backward
output_list = model(embed_input)
target_list = [F.adaptive_avg_pool2d(data, x.shape[-2:]) for x in output_list]
loss_list = [loss_fn(output, target, args) for output, target in zip(output_list, target_list)]
loss_list = [loss_list[i] * (args.lw if i < len(loss_list) - 1 else 1) for i in range(len(loss_list))]
loss_sum = sum(loss_list)
lr = adjust_lr(optimizer, epoch % args.epochs, i, data_size, args)
optimizer.zero_grad()
loss_sum.backward()
optimizer.step()
# compute psnr and msssim
psnr_list.append(psnr_fn(output_list, target_list))
msssim_list.append(msssim_fn(output_list, target_list))
if i % args.print_freq == 0 or i == len(train_dataloader) - 1:
train_psnr = torch.cat(psnr_list, dim=0) #(batchsize, num_stage)
train_psnr = torch.mean(train_psnr, dim=0) #(num_stage)
train_msssim = torch.cat(msssim_list, dim=0) #(batchsize, num_stage)
train_msssim = torch.mean(train_msssim.float(), dim=0) #(num_stage)
time_now_string = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
print_str = '[{}] Rank:{}, Epoch[{}/{}], Step [{}/{}], lr:{:.2e} PSNR: {}, MSSSIM: {}'.format(
time_now_string, local_rank, epoch+1, args.epochs, i+1, len(train_dataloader), lr,
RoundTensor(train_psnr, 2, False), RoundTensor(train_msssim, 4, False))
print(print_str, flush=True)
if local_rank in [0, None]:
with open('{}/rank0.txt'.format(args.outf), 'a') as f:
f.write(print_str + '\n')
# Convert the model to a quantized version
##model.to('cpu')
#model.eval()
#model_int = torch.ao.quantization.convert(model)
##model_int = model_int.cuda()
# collect numbers from other gpus
if args.distributed and args.ngpus_per_node > 1:
train_psnr = all_reduce([train_psnr.to(local_rank)])
train_msssim = all_reduce([train_msssim.to(local_rank)])
# ADD train_PSNR TO TENSORBOARD
if local_rank in [0, None]:
h, w = output_list[-1].shape[-2:]
is_train_best = train_psnr[-1] > train_best_psnr
train_best_psnr = train_psnr[-1] if train_psnr[-1] > train_best_psnr else train_best_psnr
train_best_msssim = train_msssim[-1] if train_msssim[-1] > train_best_msssim else train_best_msssim
writer.add_scalar(f'Train/PSNR_{h}X{w}_gap{args.frame_gap}', train_psnr[-1].item(), epoch+1)
writer.add_scalar(f'Train/MSSSIM_{h}X{w}_gap{args.frame_gap}', train_msssim[-1].item(), epoch+1)
writer.add_scalar(f'Train/best_PSNR_{h}X{w}_gap{args.frame_gap}', train_best_psnr.item(), epoch+1)
writer.add_scalar(f'Train/best_MSSSIM_{h}X{w}_gap{args.frame_gap}', train_best_msssim, epoch+1)
print_str = '\t{}p: current: {:.2f}\t best: {:.2f}\t msssim_best: {:.4f}\t'.format(h, train_psnr[-1].item(), train_best_psnr.item(), train_best_msssim.item())
print(print_str, flush=True)
with open('{}/rank0.txt'.format(args.outf), 'a') as f:
f.write(print_str + '\n')
writer.add_scalar('Train/lr', lr, epoch+1)
epoch_end_time = datetime.now()
print("Time/epoch: \tCurrent:{:.2f} \tAverage:{:.2f}".format( (epoch_end_time - epoch_start_time).total_seconds(), \
(epoch_end_time - start).total_seconds() / (epoch + 1 - args.start_epoch) ))
state_dict = model.state_dict()
save_checkpoint = {
'epoch': epoch+1,
'state_dict': state_dict,
'train_best_psnr': train_best_psnr,
'train_best_msssim': train_best_msssim,
'val_best_psnr': val_best_psnr,
'val_best_msssim': val_best_msssim,
'optimizer': optimizer.state_dict(),
}
# evaluation
if (epoch + 1) % args.eval_freq == 0 or epoch > total_epochs - 10:
val_start_time = datetime.now()
val_psnr, val_msssim = evaluate(model, val_dataloader, PE, local_rank, args)
val_end_time = datetime.now()
if args.distributed and args.ngpus_per_node > 1:
val_psnr = all_reduce([val_psnr.to(local_rank)])
val_msssim = all_reduce([val_msssim.to(local_rank)])
if local_rank in [0, None]:
# ADD val_PSNR TO TENSORBOARD
h, w = output_list[-1].shape[-2:]
print_str = f'Eval best_PSNR at epoch{epoch+1}:'
is_val_best = val_psnr[-1] > val_best_psnr
val_best_psnr = val_psnr[-1] if is_val_best else val_best_psnr
val_best_msssim = val_msssim[-1] if val_msssim[-1] > val_best_msssim else val_best_msssim
writer.add_scalar(f'Val/PSNR_{h}X{w}_gap{args.test_gap}', val_psnr[-1], epoch+1)
writer.add_scalar(f'Val/MSSSIM_{h}X{w}_gap{args.test_gap}', val_msssim[-1], epoch+1)
writer.add_scalar(f'Val/best_PSNR_{h}X{w}_gap{args.test_gap}', val_best_psnr, epoch+1)
writer.add_scalar(f'Val/best_MSSSIM_{h}X{w}_gap{args.test_gap}', val_best_msssim, epoch+1)
print_str += '\t{}p: current: {:.2f}\tbest: {:.2f} \tbest_msssim: {:.4f}\t Time/epoch: {:.2f}'.format(h, val_psnr[-1].item(), \
val_best_psnr.item(), val_best_msssim.item(), (val_end_time - val_start_time).total_seconds())
print(print_str)
with open('{}/rank0.txt'.format(args.outf), 'a') as f:
f.write(print_str + '\n')
if is_val_best:
torch.save(save_checkpoint, '{}/model_val_best.pth'.format(args.outf))
if local_rank in [0, None]:
# state_dict = model.module.state_dict() if hasattr(model, 'module') else model.state_dict()
torch.save(save_checkpoint, '{}/model_latest.pth'.format(args.outf))
if is_train_best:
torch.save(save_checkpoint, '{}/model_train_best.pth'.format(args.outf))
print("Training complete in: " + str(datetime.now() - start))
@torch.no_grad()
def evaluate(model, val_dataloader, pe, local_rank, args):
# Model Quantization
if args.quant_bit != -1:
cur_ckt = model.state_dict()
from dahuffman import HuffmanCodec
quant_weitht_list = []
for k,v in cur_ckt.items():
large_tf = (v.dim() in {2,4} and 'bias' not in k)
quant_v, new_v = quantize_per_tensor(v, args.quant_bit, args.quant_axis if large_tf else -1)
valid_quant_v = quant_v[v!=0] # only include non-zero weights
quant_weitht_list.append(valid_quant_v.flatten())
cur_ckt[k] = new_v
cat_param = torch.cat(quant_weitht_list)
input_code_list = cat_param.tolist()
unique, counts = np.unique(input_code_list, return_counts=True)
num_freq = dict(zip(unique, counts))
# generating HuffmanCoding table
codec = HuffmanCodec.from_data(input_code_list)
sym_bit_dict = {}
for k, v in codec.get_code_table().items():
sym_bit_dict[k] = v[0]
total_bits = 0
for num, freq in num_freq.items():
total_bits += freq * sym_bit_dict[num]
avg_bits = total_bits / len(input_code_list)
# import pdb; pdb.set_trace; from IPython import embed; embed()
encoding_efficiency = avg_bits / args.quant_bit
print_str = f'Entropy encoding efficiency for bit {args.quant_bit}: {encoding_efficiency}'
print(print_str)
if local_rank in [0, None]:
with open('{}/eval.txt'.format(args.outf), 'a') as f:
f.write(print_str + '\n')
model.load_state_dict(cur_ckt)
# import pdb; pdb.set_trace; from IPython import embed; embed()
psnr_list = []
msssim_list = []
if args.dump_images:
from torchvision.utils import save_image
visual_dir = f'{args.outf}/visualize'
print(f'Saving predictions to {visual_dir}')
if not os.path.isdir(visual_dir):
os.makedirs(visual_dir)
time_list = []
model.eval()
for i, (data, norm_idx) in enumerate(val_dataloader):
if i > 10 and args.debug:
break
embed_input = pe(norm_idx)
if local_rank is not None:
data = data.cuda(local_rank, non_blocking=True)
embed_input = embed_input.cuda(local_rank, non_blocking=True)
else:
data, embed_input = data.cuda(non_blocking=True), embed_input.cuda(non_blocking=True)
# compute psnr and msssim
fwd_num = 10 if args.eval_fps else 1
for _ in range(fwd_num):
# embed_input = embed_input.half()
# model = model.half()
start_time = datetime.now()
output_list = model(embed_input)
torch.cuda.synchronize()
# torch.cuda.current_stream().synchronize()
time_list.append((datetime.now() - start_time).total_seconds())
# dump predictions
if args.dump_images:
for batch_ind in range(args.batchSize):
full_ind = i * args.batchSize + batch_ind
save_image(output_list[-1][batch_ind], f'{visual_dir}/pred_{full_ind}.png')
save_image(data[batch_ind], f'{visual_dir}/gt_{full_ind}.png')
# compute psnr and ms-ssim
target_list = [F.adaptive_avg_pool2d(data, x.shape[-2:]) for x in output_list]
psnr_list.append(psnr_fn(output_list, target_list))
msssim_list.append(msssim_fn(output_list, target_list))
val_psnr = torch.cat(psnr_list, dim=0) #(batchsize, num_stage)
val_psnr = torch.mean(val_psnr, dim=0) #(num_stage)
val_msssim = torch.cat(msssim_list, dim=0) #(batchsize, num_stage)
val_msssim = torch.mean(val_msssim.float(), dim=0) #(num_stage)
if i % args.print_freq == 0:
fps = fwd_num * (i+1) * args.batchSize / sum(time_list)
print_str = 'Rank:{}, Step [{}/{}], PSNR: {}, MSSSIM: {} FPS: {}'.format(
local_rank, i+1, len(val_dataloader),
RoundTensor(val_psnr, 2, False), RoundTensor(val_msssim, 4, False), round(fps, 2))
print(print_str)
if local_rank in [0, None]:
with open('{}/rank0.txt'.format(args.outf), 'a') as f:
f.write(print_str + '\n')
model.train()
return val_psnr, val_msssim
if __name__ == '__main__':
#torch.autograd.set_detect_anomaly(True)
main()