forked from Paper99/SRFBN_CVPR19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·131 lines (99 loc) · 5.22 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
import argparse, random
from tqdm import tqdm
import torch
import options.options as option
from utils import util
from solvers import create_solver
from data import create_dataloader
from data import create_dataset
def main():
parser = argparse.ArgumentParser(description='Train Super Resolution Models')
parser.add_argument('-opt', type=str, required=True, help='Path to options JSON file.')
opt = option.parse(parser.parse_args().opt)
# random seed
seed = opt['solver']['manual_seed']
if seed is None: seed = random.randint(1, 10000)
print("===> Random Seed: [%d]"%seed)
random.seed(seed)
torch.manual_seed(seed)
# create train and val dataloader
for phase, dataset_opt in sorted(opt['datasets'].items()):
if phase == 'train':
train_set = create_dataset(dataset_opt)
train_loader = create_dataloader(train_set, dataset_opt)
print('===> Train Dataset: %s Number of images: [%d]' % (train_set.name(), len(train_set)))
if train_loader is None: raise ValueError("[Error] The training data does not exist")
elif phase == 'val':
val_set = create_dataset(dataset_opt)
val_loader = create_dataloader(val_set, dataset_opt)
print('===> Val Dataset: %s Number of images: [%d]' % (val_set.name(), len(val_set)))
else:
raise NotImplementedError("[Error] Dataset phase [%s] in *.json is not recognized." % phase)
solver = create_solver(opt)
scale = opt['scale']
model_name = opt['networks']['which_model'].upper()
print('===> Start Train')
print("==================================================")
solver_log = solver.get_current_log()
NUM_EPOCH = int(opt['solver']['num_epochs'])
start_epoch = solver_log['epoch']
print("Method: %s || Scale: %d || Epoch Range: (%d ~ %d)"%(model_name, scale, start_epoch, NUM_EPOCH))
for epoch in range(start_epoch, NUM_EPOCH + 1):
print('\n===> Training Epoch: [%d/%d]... Learning Rate: %f'%(epoch,
NUM_EPOCH,
solver.get_current_learning_rate()))
# Initialization
solver_log['epoch'] = epoch
# Train model
train_loss_list = []
with tqdm(total=len(train_loader), desc='Epoch: [%d/%d]'%(epoch, NUM_EPOCH), miniters=1) as t:
for iter, batch in enumerate(train_loader):
solver.feed_data(batch)
iter_loss = solver.train_step()
batch_size = batch['LR'].size(0)
train_loss_list.append(iter_loss*batch_size)
t.set_postfix_str("Batch Loss: %.4f" % iter_loss)
t.update()
solver_log['records']['train_loss'].append(sum(train_loss_list)/len(train_set))
solver_log['records']['lr'].append(solver.get_current_learning_rate())
print('\nEpoch: [%d/%d] Avg Train Loss: %.6f' % (epoch,
NUM_EPOCH,
sum(train_loss_list)/len(train_set)))
print('===> Validating...',)
psnr_list = []
ssim_list = []
val_loss_list = []
for iter, batch in enumerate(val_loader):
solver.feed_data(batch)
iter_loss = solver.test()
val_loss_list.append(iter_loss)
# calculate evaluation metrics
visuals = solver.get_current_visual()
psnr, ssim = util.calc_metrics(visuals['SR'], visuals['HR'], crop_border=scale)
psnr_list.append(psnr)
ssim_list.append(ssim)
if opt["save_image"]:
solver.save_current_visual(epoch, iter)
solver_log['records']['val_loss'].append(sum(val_loss_list)/len(val_loss_list))
solver_log['records']['psnr'].append(sum(psnr_list)/len(psnr_list))
solver_log['records']['ssim'].append(sum(ssim_list)/len(ssim_list))
# record the best epoch
epoch_is_best = False
if solver_log['best_pred'] < (sum(psnr_list)/len(psnr_list)):
solver_log['best_pred'] = (sum(psnr_list)/len(psnr_list))
epoch_is_best = True
solver_log['best_epoch'] = epoch
print("[%s] PSNR: %.2f SSIM: %.4f Loss: %.6f Best PSNR: %.2f in Epoch: [%d]" % (val_set.name(),
sum(psnr_list)/len(psnr_list),
sum(ssim_list)/len(ssim_list),
sum(val_loss_list)/len(val_loss_list),
solver_log['best_pred'],
solver_log['best_epoch']))
solver.set_current_log(solver_log)
solver.save_checkpoint(epoch, epoch_is_best)
solver.save_current_log()
# update lr
solver.update_learning_rate(epoch)
print('===> Finished !')
if __name__ == '__main__':
main()