-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
261 lines (187 loc) · 9.21 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
import os
import logging
import random
import numpy as np
import math
import cv2
import torch
import torch.multiprocessing as mp
from torch.distributed import init_process_group, destroy_process_group
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
from data import build_dataset
from utils import utils_logger
from utils import utils_image as util
from utils import util_calculate_psnr_ssim as util_calculate
from utils import utils_option as option
from models.model_sr import ModelSR as define_Model
from torchinfo import summary
class Trainer:
def __init__(
self,
opt: str,
) -> None:
self.local_rank = int(os.environ['LOCAL_RANK'])
self.opt = self.init_opt(opt)
self.init_logger()
self.init_seed()
self.prepare_dataloader()
self.model = define_Model(self.opt)
self.model.init_train()
self.print_arch()
def init_opt(self, opt):
opt = option.parse(opt, is_train=True)
self.init_checkpoint_path(opt)
self.current_step = self.update_checkpoint(opt)
# return None for missing key
opt = option.dict_to_nonedict(opt)
return opt
def init_checkpoint_path(self, opt):
if self.local_rank == 0:
util.mkdirs((path for key, path in opt['path'].items() if 'pretrained' not in key))
def update_checkpoint(self, opt):
init_iter_G, init_path_G = option.find_last_checkpoint(
opt['path']['models'], net_type='G', pretrained_path=opt['path']['pretrained_netG'])
init_iter_E, init_path_E = option.find_last_checkpoint(
opt['path']['models'], net_type='E', pretrained_path=opt['path']['pretrained_netE'])
opt['path']['pretrained_netG'] = init_path_G
opt['path']['pretrained_netE'] = init_path_E
init_iter_optimizerG, init_path_optimizerG = option.find_last_checkpoint(
opt['path']['models'], net_type='optimizerG')
opt['path']['pretrained_optimizerG'] = init_path_optimizerG
# save opt to a '../option.json' file
if self.local_rank == 0:
option.save(opt)
return max(init_iter_G, init_iter_E, init_iter_optimizerG)
def init_logger(self, logger_name='train'):
if self.local_rank == 0:
utils_logger.logger_info(logger_name, os.path.join(
self.opt['path']['log'], logger_name+'.log'))
self.logger = logging.getLogger(logger_name)
self.logger.info(option.dict2str(self.opt))
def init_seed(self):
seed = self.opt['train']['manual_seed']
if seed is None:
seed = random.randint(1, 10000)
if self.opt['local_rank'] == 0:
self.logger.info(f'Random seed: {seed}')
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
self.seed = seed
def prepare_dataloader(self):
for phase, dataset_opt in self.opt['datasets'].items():
if phase == 'train':
train_set = build_dataset(dataset_opt)
self.n_train_images = len(train_set)
self.train_size = int(math.ceil(self.n_train_images / dataset_opt['dataloader_batch_size']))
if self.local_rank == 0:
self.logger.info('Number of train images: {:,d}, iters: {:,d}'.format(
self.n_train_images, self.train_size))
self.train_sampler = DistributedSampler(
train_set,
shuffle=dataset_opt['dataloader_shuffle'],
drop_last=True,
seed=self.seed)
self.train_loader = DataLoader(
train_set,
batch_size=dataset_opt['dataloader_batch_size']//self.opt['num_gpu'],
shuffle=False,
num_workers=dataset_opt['dataloader_num_workers']//self.opt['num_gpu'],
drop_last=True,
pin_memory=True,
sampler=self.train_sampler)
elif phase == 'test':
test_set = build_dataset(dataset_opt)
self.test_loader = DataLoader(
test_set,
batch_size=1,
shuffle=False,
num_workers=1,
drop_last=False,
pin_memory=True)
else:
raise NotImplementedError("Phase [%s] is not recognized." % phase)
def print_arch(self, depth=5):
if self.local_rank == 0:
summary(self.model.netG, depth=depth)
def train(self):
epochs_run = self.current_step // self.train_size
for epoch in range(epochs_run, epochs_run + 1000000):
self.train_sampler.set_epoch(epoch)
for i, train_data in enumerate(self.train_loader):
self.current_step += 1
self.model.update_learning_rate(self.current_step)
self.model.feed_data(train_data)
self.model.optimize_parameters(self.current_step)
# print training information
if self.current_step % self.opt['train']['checkpoint_print'] == 0 and self.local_rank == 0:
logs = self.model.current_log() # such as loss
message = '<epoch:{:3d}, iter:{:8,d}, lr:{:.3e}> '.format(
epoch, self.current_step, self.model.current_learning_rate())
for k, v in logs.items(): # merge log information into message
message += '{:s}: {:.3e} '.format(k, v)
self.logger.info(message)
# save model
if self.current_step % self.opt['train']['checkpoint_save'] == 0 and self.local_rank == 0:
self.logger.info('Saving the model.')
self.model.save(self.current_step)
# testing
if self.current_step % self.opt['train']['checkpoint_test'] == 0 and self.local_rank == 0:
avg_psnr = 0.0
avg_psnr_y = 0.0
idx = 0
for test_data in self.test_loader:
idx += 1
image_name_ext = os.path.basename(test_data['L_path'][0])
img_name, ext = os.path.splitext(image_name_ext)
img_dir = os.path.join(self.opt['path']['images'], img_name)
util.mkdir(img_dir)
self.model.feed_data(test_data)
self.model.test()
visuals = self.model.current_visuals(need_HF=True)
SR_img = util.tensor2uint(visuals['SR']) # HWC-RGB
H_img = util.tensor2uint(visuals['H']) # HWC-RGB
# -----------------------
# save estimated image SR
# -----------------------
SR_save_img_path = os.path.join(
img_dir, f'{img_name}_{self.current_step}.png')
util.imsave(SR_img, SR_save_img_path)
# -----------------------
# calculate PSNR
# -----------------------
current_psnr = util_calculate.calculate_psnr(
SR_img,
H_img,
crop_border=self.opt['scale'])
current_psnr_y = util_calculate.calculate_psnr(
cv2.cvtColor(SR_img, cv2.COLOR_BGR2RGB),
cv2.cvtColor(H_img, cv2.COLOR_BGR2RGB),
crop_border=self.opt['scale'],
test_y_channel=True)
self.logger.info(
'{:->4d}--> {:>10s} | {:<4.2f}dB | (PSNR_Y {:<4.2f}dB)'.format(
idx, image_name_ext, current_psnr, current_psnr_y))
avg_psnr += current_psnr
avg_psnr_y += current_psnr_y
avg_psnr = avg_psnr / idx
avg_psnr_y = avg_psnr_y / idx
# testing log
self.logger.info('<epoch:{:3d}, iter:{:8,d}, Average PSNR : {:<.2f}dB, Average PSNR_Y : {:<.2f}dB'.format(
epoch, self.current_step, avg_psnr, avg_psnr_y))
if self.current_step == self.opt['train']['G_scheduler_milestones'][-1]:
if self.local_rank == 0:
self.logger.info(f'Total iteration: {self.current_step}')
break
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--opt', type=str, help='Training configuraion json file path')
# DDP(DistributedDataParallel) setup
torch.cuda.set_device(int(os.environ['LOCAL_RANK']))
init_process_group(backend='nccl')
trainer = Trainer(parser.parse_args().opt)
trainer.train()
destroy_process_group()