-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·214 lines (170 loc) · 8.32 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
import argparse, time, os
import random
import torch
import torchvision.utils as thutil
import pandas as pd
from tqdm import tqdm
import options.options as option
from utils import util
from models.SRModel import SRModel, SRModelCurriculum
from data import create_dataloader
from data import create_dataset
import matplotlib.pyplot as plt
import numpy as np
import scipy.misc as misc
def main():
# os.environ['CUDA_VISIBLE_DEVICES']='1' # You can specify your GPU device here. I failed to perform it by `torch.cuda.set_device()`.
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)
if opt['train']['resume'] is False:
util.mkdir_and_rename(opt['path']['exp_root']) # rename old experiments if exists
util.mkdirs((path for key, path in opt['path'].items() if not key == 'exp_root' and \
not key == 'pretrain_G' and not key == 'pretrain_D'))
option.save(opt)
opt = option.dict_to_nonedict(opt) # Convert to NoneDict, which return None for missing key.
else:
opt = option.dict_to_nonedict(opt)
if opt['train']['resume_path'] is None:
raise ValueError("The 'resume_path' does not declarate")
if opt['exec_debug']:
NUM_EPOCH = 100
opt['datasets']['train']['dataroot_HR'] = opt['datasets']['train']['dataroot_HR_debug']
opt['datasets']['train']['dataroot_LR'] = opt['datasets']['train']['dataroot_LR_debug']
else:
NUM_EPOCH = int(opt['train']['num_epochs'])
# random seed
seed = opt['train']['manual_seed']
if seed is None:
seed = random.randint(1, 10000)
print("Random Seed: ", seed)
random.seed(seed)
torch.manual_seed(seed)
# create train and val dataloader
for phase, dataset_opt in opt['datasets'].items():
if phase == 'train':
train_set = create_dataset(dataset_opt)
train_loader = create_dataloader(train_set, dataset_opt)
print('Number of train images in [%s]: %d' % (dataset_opt['name'], len(train_set)))
elif phase == 'val':
val_set = create_dataset(dataset_opt)
val_loader = create_dataloader(val_set, dataset_opt)
print('Number of val images in [%s]: %d' % (dataset_opt['name'], len(val_set)))
elif phase == 'test':
pass
else:
raise NotImplementedError("Phase [%s] is not recognized." % phase)
if train_loader is None:
raise ValueError("The training data does not exist")
# TODO: design an exp that can obtain the location of the biggest error
if opt['mode'] == 'sr':
solver = SRModel(opt)
elif opt['mode'] == 'sr_curriculum':
solver = SRModelCurriculum(opt)
solver.summary(train_set[0]['LR'].size())
solver.net_init()
print('[Start Training]')
start_time = time.time()
start_epoch = 1
if opt['train']['resume']:
start_epoch = solver.load()
for epoch in range(start_epoch, NUM_EPOCH + 1):
# Initialization
solver.training_loss = 0.0
epoch_loss_log = 0.0
if opt['mode'] == 'sr' or opt['mode'] == 'sr_curriculum':
training_results = {'batch_size': 0, 'training_loss': 0.0}
else:
pass # TODO
train_bar = tqdm(train_loader)
# Train model
for iter, batch in enumerate(train_bar):
solver.feed_data(batch)
iter_loss = solver.train_step()
epoch_loss_log += iter_loss.item()
batch_size = batch['LR'].size(0)
training_results['batch_size'] += batch_size
if opt['mode'] == 'sr':
training_results['training_loss'] += iter_loss * batch_size
train_bar.set_description(desc='[%d/%d] Loss: %.4f ' % (
epoch, NUM_EPOCH, iter_loss))
elif opt['mode'] == 'sr_curriculum':
training_results['training_loss'] += iter_loss.data * batch_size
train_bar.set_description(desc='[%d/%d] Loss: %.4f ' % (
epoch, NUM_EPOCH, iter_loss))
else:
pass # TODO
solver.last_epoch_loss = epoch_loss_log / (len(train_bar))
train_bar.close()
time_elapse = time.time() - start_time
start_time = time.time()
print('Train Loss: %.4f' % (training_results['training_loss'] / training_results['batch_size']))
# validate
val_results = {'batch_size': 0, 'val_loss': 0.0, 'psnr': 0.0, 'ssim': 0.0}
if epoch % solver.val_step == 0 and epoch != 0:
print('[Validating...]')
start_time = time.time()
solver.val_loss = 0.0
vis_index = 1
for iter, batch in enumerate(val_loader):
visuals_list = []
solver.feed_data(batch)
iter_loss = solver.test(opt['chop'])
batch_size = batch['LR'].size(0)
val_results['batch_size'] += batch_size
visuals = solver.get_current_visual() # float cpu tensor
sr_img = np.transpose(util.quantize(visuals['SR'], opt['rgb_range']).numpy(), (1,2,0)).astype(np.uint8)
gt_img = np.transpose(util.quantize(visuals['HR'], opt['rgb_range']).numpy(), (1,2,0)).astype(np.uint8)
# calculate PSNR
crop_size = opt['scale']
cropped_sr_img = sr_img[crop_size:-crop_size, crop_size:-crop_size, :]
cropped_gt_img = gt_img[crop_size:-crop_size, crop_size:-crop_size, :]
val_results['val_loss'] += iter_loss * batch_size
val_results['psnr'] += util.calc_psnr(cropped_sr_img, cropped_gt_img)
val_results['ssim'] += util.calc_ssim(cropped_sr_img, cropped_gt_img)
if opt['mode'] == 'srgan':
pass # TODO
if opt['save_image']:
visuals_list.extend([util.quantize(visuals['HR'].squeeze(0), opt['rgb_range']),
util.quantize(visuals['SR'].squeeze(0), opt['rgb_range'])])
images = torch.stack(visuals_list)
img = thutil.make_grid(images, nrow=2, padding=5)
ndarr = img.byte().permute(1, 2, 0).numpy()
misc.imsave(os.path.join(solver.vis_dir, 'epoch_%d_%d.png' % (epoch, vis_index)), ndarr)
vis_index += 1
avg_psnr = val_results['psnr']/val_results['batch_size']
avg_ssim = val_results['ssim']/val_results['batch_size']
print('Valid Loss: %.4f | Avg. PSNR: %.4f | Avg. SSIM: %.4f | Learning Rate: %f'%(val_results['val_loss']/val_results['batch_size'], avg_psnr, avg_ssim, solver.current_learning_rate()))
time_elapse = start_time - time.time()
#if epoch%solver.log_step == 0 and epoch != 0:
# tensorboard visualization
solver.training_loss = training_results['training_loss'] / training_results['batch_size']
solver.val_loss = val_results['val_loss'] / val_results['batch_size']
solver.tf_log(epoch)
# statistics
if opt['mode'] == 'sr' or opt['mode'] == 'sr_curriculum':
solver.results['training_loss'].append(solver.training_loss.cpu().data.item())
solver.results['val_loss'].append(solver.val_loss.cpu().data.item())
solver.results['psnr'].append(avg_psnr)
solver.results['ssim'].append(avg_ssim)
else:
pass # TODO
is_best = False
if solver.best_prec < solver.results['psnr'][-1]:
solver.best_prec = solver.results['psnr'][-1]
is_best = True
solver.save(epoch, is_best)
# update lr
solver.update_learning_rate(epoch)
data_frame = pd.DataFrame(
data={'training_loss': solver.results['training_loss']
, 'val_loss': solver.results['val_loss']
, 'psnr': solver.results['psnr']
, 'ssim': solver.results['ssim']
},
index=range(1, NUM_EPOCH+1)
)
data_frame.to_csv(os.path.join(solver.results_dir, 'train_results.csv'),
index_label='Epoch')
if __name__ == '__main__':
main()