-
Notifications
You must be signed in to change notification settings - Fork 13
/
train.py
436 lines (364 loc) · 19.1 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
# FeatureFlow
import argparse
import torch
import os
from skimage.measure import compare_ssim
import torchvision
from torch.nn import functional as F
import torchvision.transforms as transforms
import torch.optim as optim
import torch.nn as nn
import time
import src.dataloader as dataloader
import src.layers as layers
from math import log10
import datetime
from tqdm import tqdm
import visdom
import models.bdcn.bdcn as bdcn
from utils.visualize import feature_transform, edge_transform
import numpy as np
# For parsing commandline arguments
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
parser = argparse.ArgumentParser()
parser.add_argument("--dataset_root", type=str, required=True,
help='[important]path to dataset folder containing train-test-validation folders')
parser.add_argument("--checkpoint_dir", type=str, required=True, help='path to folder for saving checkpoints')
parser.add_argument("--checkpoint", type=str, help='path of checkpoint for pretrained model')
parser.add_argument("--train_continue", action='store_true',
help='If resuming from checkpoint, set to True and set `checkpoint` path. Default: False.')
parser.add_argument("--epochs", type=int, default=200, help='[You can terminate program as our paper described]number of epochs to train. Default: 200.')
parser.add_argument("--train_batch_size", type=int, default=6, help='batch size for training. Default: 6.')
parser.add_argument("--validation_batch_size", type=int, default=10, help='[It depends]batch size for validation. Default: 10.')
parser.add_argument("--init_learning_rate", type=float, default=0.0001,
help='[Require]set initial learning rate. Default: 0.0001.')
parser.add_argument("--milestones", type=int, required=True, nargs='+',
help='Set to epoch values where you want to decrease learning rate by a factor of 0.1. '
'Default: [100, 150]')
parser.add_argument("--progress_iter", type=int, default=100,
help='[Default]frequency of reporting progress and validation. N: after every N iterations. Default: 100.')
parser.add_argument("--checkpoint_epoch", type=int, default=5,
help='checkpoint saving frequency. N: after every N epochs. Each checkpoint is roughly of '
'size 151 MB.Default: 5.')
parser.add_argument('--visdom_env', type=str, default='FeFlow_0', help='Environment for visdom show')
parser.add_argument('--vimeo90k', action='store_true', help='[Must be used]use this flag if using Vimeo-90K dataset')
parser.add_argument('--GEN_DE', type=str2bool, nargs='?', const=True, default=False, help='[Important]True: train generator, False: train DE')
parser.add_argument('--test', action='store_true', help='if debug network by using 1 image, Default:False')
parser.add_argument('--fp16', action='store_true', help='[discarded]using apex for fp16')
parser.add_argument("--local_rank", default=0, type=int)
parser.add_argument('--feature_level', type=int, default=3, help='[Do not change it if you do not know what it means]Using feature_level=? in GEN, Default:3')
parser.add_argument('--bdcn_model', default='./models/bdcn/final-model/bdcn_pretrained_on_bsds500.pth')
parser.add_argument('--DE_pretrained', action='store_true', help='[You do not need to use it for training.]using this flag if training the model from pretrained parameters.')
parser.add_argument('--DE_ckpt', type=str, help='path to DE checkpoint')
parser.add_argument('--final', action='store_true', help='[Do not use it.]True: train all together')
args = parser.parse_args()
# for saving checkpoint
if not os.path.isdir(args.checkpoint_dir):
os.system('mkdir -p %s' % args.checkpoint_dir)
# --For visualizing loss and interpolated frames--
# Visdom for real-time visualizing
vis = visdom.Visdom(env=args.visdom_env, port=8098)
# device
device_count = torch.cuda.device_count()
# --Initialize network--
bdcn = bdcn.BDCN()
bdcn.cuda()
structure_gen = nn.DataParallel(layers.StructureGen(feature_level=args.feature_level))
structure_gen.cuda()
detail_enhance = nn.DataParallel(layers.DetailEnhance())
detail_enhance.cuda()
if args.final:
detail_enhance_last = nn.DataParallel(layers.DetailEnhance_last())
detail_enhance_last.cuda()
# --Load Datasets--
# Channel wise mean calculated on adobe240-fps training dataset
mean = [0.5, 0.5, 0.5]
std = [0.5, 0.5, 0.5]
normalize = transforms.Normalize(mean=mean,
std=std)
transform = transforms.Compose([transforms.ToTensor(), normalize])
if args.vimeo90k:
trainset = dataloader.FeFlow_vimeo90k(root=args.dataset_root, transform=transform, train=True)
else:
trainset = dataloader.FeFlow(root=args.dataset_root + '/train', transform=transform, train=True)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.train_batch_size * device_count, shuffle=True)
if args.vimeo90k:
validationset = dataloader.FeFlow_vimeo90k(root=args.dataset_root, transform=transform,
randomCropSize=(448, 256), train=False)
else:
validationset = dataloader.FeFlow(root=args.dataset_root + '/validation', transform=transform,
randomCropSize=(448, 256), train=False)
validationloader = torch.utils.data.DataLoader(validationset, batch_size=args.validation_batch_size * device_count, shuffle=False)
print(trainset, validationset)
# --Create transform to display image from tensor--
negmean = [-1 for x in mean]
restd = [2, 2, 2]
revNormalize = transforms.Normalize(mean=negmean, std=restd)
TP = transforms.Compose([revNormalize, transforms.ToPILImage()])
# --Utils--
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group['lr']
# --Loss and Optimizer--
if args.final:
for param in structure_gen.parameters(True):
param.requires_grad = False
for param in detail_enhance.parameters(True):
param.requires_grad = False
optimizer = optim.Adam(detail_enhance_last.parameters(), lr=args.init_learning_rate)
else:
if args.GEN_DE:
for param in detail_enhance.parameters(True):
param.requires_grad = False
optimizer = optim.Adam(structure_gen.parameters(), lr=args.init_learning_rate)
else:
for param in structure_gen.parameters(True):
param.requires_grad = False
optimizer = optim.Adam(detail_enhance.parameters(), lr=args.init_learning_rate)
for param in bdcn.parameters(True):
param.requires_grad = False
# scheduler to decrease learning rate by a factor of 10 at milestones.
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=args.milestones, gamma=0.25)
# --Validation function--
#
def validate():
# For details see training.
psnr = 0
ssim = 0
tloss = 0
flag = 1
with torch.no_grad():
for validationIndex, validationData in enumerate(validationloader, 0):
frame0, frameT, frame1 = validationData
img0 = frame0.cuda()
img1 = frame1.cuda()
IFrame = frameT.cuda()
img0_e = torch.cat([img0, torch.tanh(bdcn(img0)[0])], dim=1)
img1_e = torch.cat([img1, torch.tanh(bdcn(img1)[0])], dim=1)
IFrame_e = torch.cat([IFrame, torch.tanh(bdcn(IFrame)[0])], dim=1)
if args.final:
_, _, ref_imgt = structure_gen((img0_e, img1_e, IFrame_e))
_, _, _, ref_imgt2 = detail_enhance((img0, img1, IFrame, ref_imgt))
loss, MSE_val, imgt = detail_enhance_last((img0, img1, IFrame, ref_imgt2))
SSIM = compare_ssim(IFrame.reshape(1, -1, 256, 448).squeeze(0).cpu().numpy().transpose(1, 2, 0),
imgt.reshape(1, -1, 256, 448).squeeze(0).cpu().numpy().transpose(1, 2, 0),
data_range=2, multichannel=True)
else:
if args.GEN_DE:
loss, MSE_val, ref_imgt = structure_gen((img0_e, img1_e, IFrame_e))
SSIM = compare_ssim(IFrame.reshape(1, -1, 256, 448).squeeze(0).cpu().numpy().transpose(1, 2, 0),
ref_imgt.reshape(1, -1, 256, 448).squeeze(0).cpu().numpy().transpose(1, 2, 0),
data_range=2, multichannel=True)
else:
_, _, ref_imgt = structure_gen((img0_e, img1_e, IFrame_e))
loss, MSE_val, _, imgt = detail_enhance((img0, img1, IFrame, ref_imgt))
SSIM = compare_ssim(IFrame.reshape(1, -1, 256, 448).squeeze(0).cpu().numpy().transpose(1, 2, 0),
imgt.reshape(1, -1, 256, 448).squeeze(0).cpu().numpy().transpose(1, 2, 0), data_range=2, multichannel=True)
loss = torch.mean(loss)
MSE_val = torch.mean(MSE_val)
if (flag):
if args.final:
retImg = torch.cat([revNormalize(frame0[0]).unsqueeze(0), revNormalize(frame1[0]).unsqueeze(0),
revNormalize(imgt.cpu()[0]).unsqueeze(0), revNormalize(frameT[0]).unsqueeze(0),
revNormalize(ref_imgt2.cpu()[0]).unsqueeze(0), revNormalize(ref_imgt.cpu()[0]).unsqueeze(0)],
dim=0)
else:
if args.GEN_DE:
retImg = torch.cat([revNormalize(frame0[0]).unsqueeze(0), revNormalize(frame1[0]).unsqueeze(0),
revNormalize(ref_imgt.cpu()[0]).unsqueeze(0), revNormalize(frameT[0]).unsqueeze(0)],
dim=0)
else:
retImg = torch.cat([revNormalize(frame0[0]).unsqueeze(0), revNormalize(frame1[0]).unsqueeze(0),
revNormalize(imgt.cpu()[0]).unsqueeze(0), revNormalize(frameT[0]).unsqueeze(0),
revNormalize(ref_imgt.cpu()[0]).unsqueeze(0)],
dim=0)
flag = 0
# psnr
tloss += loss.item()
psnr += (10 * log10(4 / MSE_val.item()))
ssim += SSIM
return (psnr / len(validationloader)), (tloss / len(validationloader)), retImg, MSE_val, (ssim / len(validationloader))
# --Initialization--
bdcn.load_state_dict(torch.load('%s' % (args.bdcn_model)))
if args.train_continue:
dict1 = torch.load(args.checkpoint)
if args.final:
structure_gen.module.load_state_dict(dict1['state_dictGEN'], strict=False)
detail_enhance.module.load_state_dict(dict1['state_dictDE'])
detail_enhance_last.module.load_state_dict(dict1['state_dictDE_last'])
else:
if args.GEN_DE:
structure_gen.module.load_state_dict(dict1['state_dictGEN'])
else:
structure_gen.module.load_state_dict(dict1['state_dictGEN'], strict=False)
detail_enhance.module.load_state_dict(dict1['state_dictDE'])
for _ in range(dict1['epoch']):
scheduler.step()
else:
if args.final:
dict1 = torch.load(args.checkpoint)
structure_gen.module.load_state_dict(dict1['state_dictGEN'])
detail_enhance.module.load_state_dict(dict1['state_dictDE'])
else:
if not args.GEN_DE:
dict1 = torch.load(args.checkpoint)
structure_gen.module.load_state_dict(dict1['state_dictGEN'])
if args.DE_pretrained:
dict2 = torch.load(args.DE_ckpt)
detail_enhance.module.load_state_dict(dict2['state_dictDE'])
dict1 = {'loss': [], 'valLoss': [], 'valPSNR': [], 'epoch': -1}
# --Training--
start = time.time()
cLoss = dict1['loss']
valLoss = dict1['valLoss']
valPSNR = dict1['valPSNR']
checkpoint_counter = int((dict1['epoch'] + 1) / args.checkpoint_epoch)
if args.final:
structure_gen.eval()
detail_enhance.eval()
detail_enhance_last.train()
else:
if args.GEN_DE:
structure_gen.train()
else:
structure_gen.eval()
detail_enhance.train()
bdcn.eval()
# --Main training loop--
for epoch in range(dict1['epoch'] + 1, args.epochs):
print("Epoch: ", epoch)
# Append and reset
cLoss.append([])
valLoss.append([])
valPSNR.append([])
iLoss = 0
# Increment scheduler count
scheduler.step()
if args.test:
iter_data = iter(trainloader)
test_batch = iter_data.next()
for i in range(3):
test_batch = iter_data.next()
for trainIndex, trainData in tqdm(enumerate(trainloader, 0)):
# Getting the input and the target from the training set
start_time = time.time()
frame0, frameT, frame1 = trainData
if args.test:
"""
just for 1 batch test
"""
frame0, frameT, frame1 = test_batch
img0 = frame0.cuda()
img1 = frame1.cuda()
IFrame = frameT.cuda()
img0_e = torch.cat([img0, torch.tanh(bdcn(img0)[0])], dim=1)
img1_e = torch.cat([img1, torch.tanh(bdcn(img1)[0])], dim=1)
IFrame_e = torch.cat([IFrame, torch.tanh(bdcn(IFrame)[0])], dim=1)
if args.final:
_, _, ref_imgt = structure_gen((img0_e, img1_e, IFrame_e))
_, _, _, ref_imgt2 = detail_enhance((img0, img1, IFrame, ref_imgt))
loss, MSE_val, imgt = detail_enhance_last((img0, img1, IFrame, ref_imgt2))
else:
if args.GEN_DE:
loss, _, ref_imgt = structure_gen((img0_e, img1_e, IFrame_e))
else:
_, _, ref_imgt = structure_gen((img0_e, img1_e, IFrame_e))
loss, _, _, imgt = detail_enhance((img0, img1, IFrame, ref_imgt))
# print(torch.max(torch.abs(ref_imgt - IFrame)))
# loss, _ = loss_function(img0, img1, IFrame, *output)
loss = torch.mean(loss)
# Backpropagate
optimizer.zero_grad()
loss.backward()
# with amp.scale_loss(loss, optimizer) as scaled_loss:
# scaled_loss.backward()
optimizer.step()
iLoss += loss.item()
if trainIndex % 5 == 4:
if args.final:
vImg = torch.cat([revNormalize(frame0[0]).unsqueeze(0), revNormalize(frame1[0]).unsqueeze(0),
revNormalize(imgt.cpu()[0]).unsqueeze(0), revNormalize(frameT[0]).unsqueeze(0),
revNormalize(ref_imgt2.cpu()[0]).unsqueeze(0), revNormalize(ref_imgt.cpu()[0]).unsqueeze(0)],
dim=0)
else:
if args.GEN_DE:
vImg = torch.cat([revNormalize(frame0[0]).unsqueeze(0), revNormalize(frame1[0]).unsqueeze(0),
revNormalize(ref_imgt.cpu()[0]).unsqueeze(0), revNormalize(frameT[0]).unsqueeze(0)],
dim=0)
else:
vImg = torch.cat([revNormalize(frame0[0]).unsqueeze(0), revNormalize(frame1[0]).unsqueeze(0),
revNormalize(imgt.cpu()[0]).unsqueeze(0), revNormalize(frameT[0]).unsqueeze(0),
revNormalize(ref_imgt.cpu()[0]).unsqueeze(0)],
dim=0)
vImg = torch.clamp(vImg, max=1., min=0)
vis.images(vImg, win='vImage', env=args.visdom_env, nrow=2, opts={'title': 'visual_image'})
# Validation and progress every `args.progress_iter` iterations
if (trainIndex % args.progress_iter) == (args.progress_iter - 1) and not args.test:
end = time.time()
if args.final:
detail_enhance_last.eval()
else:
if args.GEN_DE:
structure_gen.eval()
else:
detail_enhance.eval()
psnr, vLoss, valImg, MSE_val, ssim = validate()
if args.final:
detail_enhance_last.train()
else:
if args.GEN_DE:
structure_gen.train()
else:
detail_enhance.train()
valPSNR[epoch].append(psnr)
valLoss[epoch].append(vLoss)
itr = trainIndex + epoch * (len(trainloader))
# Visdom
vis.line(win='Loss', name='trainLoss', Y=torch.FloatTensor([iLoss / args.progress_iter]),
X=torch.FloatTensor([itr]), env=args.visdom_env, update='append')
vis.line(win='Loss', name='validationLoss', Y=torch.FloatTensor([vLoss]),
X=torch.FloatTensor([itr]), env=args.visdom_env, update='append')
vis.line(win='PSNR', name='PSNR', Y=torch.FloatTensor([psnr]), X=torch.FloatTensor([itr]),
env=args.visdom_env, update='append')
vis.line(win='SSIM', name='SSIM', Y=torch.FloatTensor([ssim]), X=torch.FloatTensor([itr]),
env=args.visdom_env, update='append')
vis.line(win='MSE_val', name='MSE_val', Y=torch.FloatTensor([MSE_val]), X=torch.FloatTensor([itr]),
env=args.visdom_env, update='append')
# clamp for those value like: 1.0000012 (bug avoiding)
valImg = torch.clamp(valImg, max=1.)
vis.images(valImg, win='Image', env=args.visdom_env, nrow=2, opts={'title': 'img0_GT_SD_img1'})
endVal = time.time()
print(
" Loss: %0.6f Iterations: %4d/%4d TrainExecTime: %0.1f ValLoss:%0.6f ValPSNR: %0.4f "
"ValEvalTime: %0.2f LearningRate: %f" % (
iLoss / args.progress_iter, trainIndex, len(trainloader), end - start, vLoss, psnr, endVal - end,
get_lr(optimizer)))
cLoss[epoch].append(iLoss / args.progress_iter)
iLoss = 0
start = time.time()
# Create checkpoint after every `args.checkpoint_epoch` epochs
if (epoch % args.checkpoint_epoch) == args.checkpoint_epoch - 1:
dict1 = {
'Detail': "End to end FeFlow.",
'epoch': epoch,
'timestamp': datetime.datetime.now(),
'trainBatchSz': args.train_batch_size,
'validationBatchSz': args.validation_batch_size,
'learningRate': get_lr(optimizer),
'loss': cLoss,
'valLoss': valLoss,
'valPSNR': valPSNR,
'state_dictGEN': structure_gen.module.state_dict(),
'state_dictDE': detail_enhance.module.state_dict()
# 'state_dictDE_last': detail_enhance_last.module.state_dict()
}
torch.save(dict1, args.checkpoint_dir + "/FeFlow" + str(checkpoint_counter) + ".ckpt")
checkpoint_counter += 1