-
Notifications
You must be signed in to change notification settings - Fork 25
/
train.py
275 lines (223 loc) · 10.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
from torch.utils.data import DataLoader
import importlib
from tqdm import tqdm
import torch.backends.cudnn as cudnn
from utils.utils import *
from utils.utils_datasets import TrainSetDataLoader, MultiTestSetDataLoader
from collections import OrderedDict
import imageio
def main(args):
''' Create Dir for Save'''
log_dir, checkpoints_dir, val_dir = create_dir(args)
''' Logger '''
logger = Logger(log_dir, args)
''' CPU or Cuda'''
device = torch.device(args.device)
if 'cuda' in args.device:
torch.cuda.set_device(device)
''' DATA Training LOADING '''
logger.log_string('\nLoad Training Dataset ...')
train_Dataset = TrainSetDataLoader(args)
logger.log_string("The number of training data is: %d" % len(train_Dataset))
train_loader = torch.utils.data.DataLoader(dataset=train_Dataset, num_workers=args.num_workers,
batch_size=args.batch_size, shuffle=True,)
''' DATA Validation LOADING '''
logger.log_string('\nLoad Validation Dataset ...')
test_Names, test_Loaders, length_of_tests = MultiTestSetDataLoader(args)
logger.log_string("The number of validation data is: %d" % length_of_tests)
''' MODEL LOADING '''
logger.log_string('\nModel Initial ...')
MODEL_PATH = 'model.' + args.task + '.' + args.model_name
MODEL = importlib.import_module(MODEL_PATH)
net = MODEL.get_model(args)
''' Load Pre-Trained PTH '''
if args.use_pre_ckpt == False:
net.apply(MODEL.weights_init)
start_epoch = 0
logger.log_string('Do not use pre-trained model!')
else:
try:
ckpt_path = args.path_pre_pth
checkpoint = torch.load(ckpt_path, map_location='cpu')
start_epoch = checkpoint['epoch']
try:
new_state_dict = OrderedDict()
for k, v in checkpoint['state_dict'].items():
name = 'module.' + k # add `module.`
new_state_dict[name] = v
# load params
net.load_state_dict(new_state_dict)
logger.log_string('Use pretrain model!')
except:
new_state_dict = OrderedDict()
for k, v in checkpoint['state_dict'].items():
new_state_dict[k] = v
# load params
net.load_state_dict(new_state_dict)
logger.log_string('Use pretrain model!')
except:
net = MODEL.get_model(args)
net.apply(MODEL.weights_init)
start_epoch = 0
logger.log_string('No existing model, starting training from scratch...')
pass
pass
net = net.to(device)
cudnn.benchmark = True
''' Print Parameters '''
logger.log_string('PARAMETER ...')
logger.log_string(args)
''' LOSS LOADING '''
criterion = MODEL.get_loss(args).to(device)
''' Optimizer '''
optimizer = torch.optim.Adam(
[paras for paras in net.parameters() if paras.requires_grad == True],
lr=args.lr,
betas=(0.9, 0.999),
eps=1e-08,
weight_decay=args.decay_rate
)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=args.n_steps, gamma=args.gamma)
''' TRAINING & TEST '''
logger.log_string('\nStart training...')
for idx_epoch in range(start_epoch, args.epoch):
logger.log_string('\nEpoch %d /%s:' % (idx_epoch + 1, args.epoch))
''' Training '''
loss_epoch_train, psnr_epoch_train, ssim_epoch_train = train(train_loader, device, net, criterion, optimizer)
logger.log_string('The %dth Train, loss is: %.5f, psnr is %.5f, ssim is %.5f' %
(idx_epoch + 1, loss_epoch_train, psnr_epoch_train, ssim_epoch_train))
''' Save PTH '''
if args.local_rank == 0:
save_ckpt_path = str(checkpoints_dir) + '/%s_%dx%d_%dx_epoch_%02d_model.pth' % (
args.model_name, args.angRes_in, args.angRes_in, args.scale_factor, idx_epoch + 1)
state = {
'epoch': idx_epoch + 1,
'state_dict': net.module.state_dict() if hasattr(net, 'module') else net.state_dict(),
}
torch.save(state, save_ckpt_path)
logger.log_string('Saving the epoch_%02d model at %s' % (idx_epoch + 1, save_ckpt_path))
''' Validation '''
step = 1
if (idx_epoch + 1)%step==0 or idx_epoch > args.epoch-step:
with torch.no_grad():
''' Create Excel for PSNR/SSIM '''
excel_file = ExcelFile()
psnr_testset = []
ssim_testset = []
for index, test_name in enumerate(test_Names):
test_loader = test_Loaders[index]
epoch_dir = val_dir.joinpath('VAL_epoch_%02d' % (idx_epoch + 1))
epoch_dir.mkdir(exist_ok=True)
save_dir = epoch_dir.joinpath(test_name)
save_dir.mkdir(exist_ok=True)
psnr_iter_test, ssim_iter_test, LF_name = test(test_loader, device, net, save_dir)
excel_file.write_sheet(test_name, LF_name, psnr_iter_test, ssim_iter_test)
psnr_epoch_test = float(np.array(psnr_iter_test).mean())
ssim_epoch_test = float(np.array(ssim_iter_test).mean())
psnr_testset.append(psnr_epoch_test)
ssim_testset.append(ssim_epoch_test)
logger.log_string('The %dth Test on %s, psnr/ssim is %.2f/%.3f' % (
idx_epoch + 1, test_name, psnr_epoch_test, ssim_epoch_test))
pass
psnr_mean_test = float(np.array(psnr_testset).mean())
ssim_mean_test = float(np.array(ssim_testset).mean())
logger.log_string('The mean psnr on testsets is %.5f, mean ssim is %.5f'
% (psnr_mean_test, ssim_mean_test))
excel_file.xlsx_file.save(str(epoch_dir) + '/evaluation.xls')
pass
pass
''' scheduler '''
scheduler.step()
pass
pass
def train(train_loader, device, net, criterion, optimizer):
''' training one epoch '''
psnr_iter_train = []
loss_iter_train = []
ssim_iter_train = []
for idx_iter, (data, label, data_info) in tqdm(enumerate(train_loader), total=len(train_loader), ncols=70):
[Lr_angRes_in, Lr_angRes_out] = data_info
data_info[0] = Lr_angRes_in[0].item()
data_info[1] = Lr_angRes_out[0].item()
data = data.to(device) # low resolution
label = label.to(device) # high resolution
out = net(data, data_info)
loss = criterion(out, label, data_info)
optimizer.zero_grad()
loss.backward()
optimizer.step()
torch.cuda.empty_cache()
loss_iter_train.append(loss.data.cpu())
psnr, ssim = cal_metrics(args, label, out)
psnr_iter_train.append(psnr)
ssim_iter_train.append(ssim)
pass
loss_epoch_train = float(np.array(loss_iter_train).mean())
psnr_epoch_train = float(np.array(psnr_iter_train).mean())
ssim_epoch_train = float(np.array(ssim_iter_train).mean())
return loss_epoch_train, psnr_epoch_train, ssim_epoch_train
def test(test_loader, device, net, save_dir=None):
LF_iter_test = []
psnr_iter_test = []
ssim_iter_test = []
for idx_iter, (Lr_SAI_y, Hr_SAI_y, Sr_SAI_cbcr, data_info, LF_name) in tqdm(enumerate(test_loader), total=len(test_loader), ncols=70):
[Lr_angRes_in, Lr_angRes_out] = data_info
data_info[0] = Lr_angRes_in[0].item()
data_info[1] = Lr_angRes_out[0].item()
Lr_SAI_y = Lr_SAI_y.squeeze().to(device) # numU, numV, h*angRes, w*angRes
Hr_SAI_y = Hr_SAI_y
Sr_SAI_cbcr = Sr_SAI_cbcr
''' Crop LFs into Patches '''
subLFin = LFdivide(Lr_SAI_y, args.angRes_in, args.patch_size_for_test, args.stride_for_test)
numU, numV, H, W = subLFin.size()
subLFin = rearrange(subLFin, 'n1 n2 a1h a2w -> (n1 n2) 1 a1h a2w')
subLFout = torch.zeros(numU * numV, 1, args.angRes_in * args.patch_size_for_test * args.scale_factor,
args.angRes_in * args.patch_size_for_test * args.scale_factor)
''' SR the Patches '''
for i in range(0, numU * numV, args.minibatch_for_test):
tmp = subLFin[i:min(i + args.minibatch_for_test, numU * numV), :, :, :]
with torch.no_grad():
net.eval()
torch.cuda.empty_cache()
out = net(tmp.to(device), data_info)
subLFout[i:min(i + args.minibatch_for_test, numU * numV), :, :, :] = out
subLFout = rearrange(subLFout, '(n1 n2) 1 a1h a2w -> n1 n2 a1h a2w', n1=numU, n2=numV)
''' Restore the Patches to LFs '''
Sr_4D_y = LFintegrate(subLFout, args.angRes_out, args.patch_size_for_test * args.scale_factor,
args.stride_for_test * args.scale_factor, Hr_SAI_y.size(-2)//args.angRes_out, Hr_SAI_y.size(-1)//args.angRes_out)
Sr_SAI_y = rearrange(Sr_4D_y, 'a1 a2 h w -> 1 1 (a1 h) (a2 w)')
''' Calculate the PSNR & SSIM '''
psnr, ssim = cal_metrics(args, Hr_SAI_y, Sr_SAI_y)
psnr_iter_test.append(psnr)
ssim_iter_test.append(ssim)
LF_iter_test.append(LF_name[0])
''' Save RGB '''
if save_dir is not None:
save_dir_ = save_dir.joinpath(LF_name[0])
save_dir_.mkdir(exist_ok=True)
views_dir = save_dir_.joinpath('views')
views_dir.mkdir(exist_ok=True)
Sr_SAI_ycbcr = torch.cat((Sr_SAI_y, Sr_SAI_cbcr), dim=1)
Sr_SAI_rgb = (ycbcr2rgb(Sr_SAI_ycbcr.squeeze().permute(1, 2, 0).numpy()).clip(0,1)*255).astype('uint8')
Sr_4D_rgb = rearrange(Sr_SAI_rgb, '(a1 h) (a2 w) c -> a1 a2 h w c', a1=args.angRes_out, a2=args.angRes_out)
# save the SAI
# path = str(save_dir_) + '/' + LF_name[0] + '_SAI.bmp'
# imageio.imwrite(path, Sr_SAI_rgb)
# save the center view
img = Sr_4D_rgb[args.angRes_out // 2, args.angRes_out // 2, :, :, :]
path = str(save_dir_) + '/' + LF_name[0] + '_' + 'CenterView.bmp'
imageio.imwrite(path, img)
# save all views
for i in range(args.angRes_out):
for j in range(args.angRes_out):
img = Sr_4D_rgb[i, j, :, :, :]
path = str(views_dir) + '/' + LF_name[0] + '_' + str(i) + '_' + str(j) + '.bmp'
imageio.imwrite(path, img)
pass
pass
pass
pass
return psnr_iter_test, ssim_iter_test, LF_iter_test
if __name__ == '__main__':
from option import args
main(args)