-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_train_stganmr.py
363 lines (307 loc) · 16.6 KB
/
main_train_stganmr.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
'''
# -----------------------------------------
Main Program for Training
ST-GAN for MRI_Recon (EMBC2022)
by Jiahao Huang (j.huang21@imperial.ac.uk)
# -----------------------------------------
'''
import os
import sys
import math
import argparse
import random
import cv2
import numpy as np
import logging
import time
import torch
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
from utils import utils_logger
from utils import utils_image as util
from utils import utils_option as option
from utils.utils_dist import get_dist_info, init_dist
from utils import utils_early_stopping
from data.select_dataset import define_Dataset
from models.select_model import define_Model
from tensorboardX import SummaryWriter
from collections import OrderedDict
from skimage.transform import resize
import lpips
def main(json_path=''):
'''
# ----------------------------------------
# Step--1 (prepare opt)
# ----------------------------------------
'''
parser = argparse.ArgumentParser()
parser.add_argument('--opt', type=str, default=json_path, help='Path to option JSON file.')
parser.add_argument('--launcher', default='pytorch', help='job launcher')
parser.add_argument('--local_rank', type=int, default=0)
# parser.add_argument('--dist', default=False)
opt = option.parse(parser.parse_args().opt, is_train=True)
# opt['dist'] = parser.parse_args().dist
# distributed settings
if opt['dist']:
init_dist('pytorch')
opt['rank'], opt['world_size'] = get_dist_info()
if opt['rank'] == 0:
util.mkdirs((path for key, path in opt['path'].items() if 'pretrained' not in key))
# update opt
init_iter_G, init_path_G = option.find_last_checkpoint(opt['path']['models'], net_type='G')
init_iter_D, init_path_D = option.find_last_checkpoint(opt['path']['models'], net_type='D')
init_iter_E, init_path_E = option.find_last_checkpoint(opt['path']['models'], net_type='E')
opt['path']['pretrained_netG'] = init_path_G
opt['path']['pretrained_netD'] = init_path_D
opt['path']['pretrained_netE'] = init_path_E
init_iter_optimizerG, init_path_optimizerG = option.find_last_checkpoint(opt['path']['models'], net_type='optimizerG')
init_iter_optimizerD, init_path_optimizerD = option.find_last_checkpoint(opt['path']['models'], net_type='optimizerD')
opt['path']['pretrained_optimizerG'] = init_path_optimizerG
opt['path']['pretrained_optimizerD'] = init_path_optimizerD
current_step = max(init_iter_G, init_iter_D, init_iter_E, init_iter_optimizerG, init_iter_optimizerD)
# save opt to a '../option.json' file
if opt['rank'] == 0:
option.save(opt)
# return None for missing key
opt = option.dict_to_nonedict(opt)
# configure logger
if opt['rank'] == 0:
# logger
logger_name = 'train'
utils_logger.logger_info(logger_name, os.path.join(opt['path']['log'], logger_name+'.log'))
logger = logging.getLogger(logger_name)
logger.info(option.dict2str(opt))
# tensorbordX log
logger_tensorboard = SummaryWriter(os.path.join(opt['path']['log']))
# set seed
seed = opt['manual_seed']
if seed is None:
seed = random.randint(1, 10000)
print('Random seed: {}'.format(seed))
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
'''
# ----------------------------------------
# Step--2 (creat dataloader)
# ----------------------------------------
'''
# ----------------------------------------
# 1) create_dataset
# 2) creat_dataloader for train and test
# ----------------------------------------
for phase, dataset_opt in opt['datasets'].items():
if phase == 'train':
train_set = define_Dataset(dataset_opt)
train_size = int(math.ceil(len(train_set) / dataset_opt['dataloader_batch_size']))
if opt['rank'] == 0:
logger.info('Number of train images: {:,d}, iters: {:,d}'.format(len(train_set), train_size))
if opt['dist']:
train_sampler = DistributedSampler(train_set, shuffle=dataset_opt['dataloader_shuffle'], drop_last=True, seed=seed)
train_loader = DataLoader(train_set,
batch_size=dataset_opt['dataloader_batch_size']//opt['num_gpu'],
shuffle=False,
num_workers=dataset_opt['dataloader_num_workers']//opt['num_gpu'],
drop_last=True,
pin_memory=False,
sampler=train_sampler)
else:
train_loader = DataLoader(train_set,
batch_size=dataset_opt['dataloader_batch_size'],
shuffle=dataset_opt['dataloader_shuffle'],
num_workers=dataset_opt['dataloader_num_workers'],
drop_last=True,
pin_memory=False)
elif phase == 'test':
test_set = define_Dataset(dataset_opt)
test_loader = DataLoader(test_set, batch_size=1,
shuffle=False, num_workers=1,
drop_last=False, pin_memory=False)
else:
raise NotImplementedError("Phase [%s] is not recognized." % phase)
'''
# ----------------------------------------
# Step--3 (initialize model)
# ----------------------------------------
'''
# define model
model = define_Model(opt)
model.init_train()
# define LPIPS function
loss_fn_alex = lpips.LPIPS(net='alex').to(model.device)
# define early stopping
if opt['train']['is_early_stopping']:
early_stopping = utils_early_stopping.EarlyStopping(patience=opt['train']['early_stopping_num'])
# record
if opt['rank'] == 0:
logger.info(model.info_network())
logger.info(model.info_params())
'''
# ----------------------------------------
# Step--4 (main training)
# ----------------------------------------
'''
for epoch in range(100000000): # keep running
if opt['dist']:
train_sampler.set_epoch(epoch)
for i, train_data in enumerate(train_loader):
current_step += 1
# -------------------------------
# 1) update learning rate
# -------------------------------
model.update_learning_rate(current_step)
# -------------------------------
# 2) feed patch pairs
# -------------------------------
model.feed_data(train_data)
# -------------------------------
# 3) optimize parameters
# -------------------------------
model.optimize_parameters(current_step)
# -------------------------------
# 4) training information
# -------------------------------
if current_step % opt['train']['checkpoint_print'] == 0 and opt['rank'] == 0:
logs = model.current_log()
message = '<epoch:{:3d}, iter:{:8,d}, lr:{:.3e}> '.format(epoch, current_step, model.current_learning_rate())
for k, v in logs.items():
message += '{:s}: {:.3e} '.format(k, v)
logger.info(message)
# record train loss
logger_tensorboard.add_scalar('Learning Rate', model.current_learning_rate(), global_step=current_step)
logger_tensorboard.add_scalar('TRAIN Generator LOSS/G_loss', logs['G_loss'], global_step=current_step)
if 'G_loss_image' in logs.keys():
logger_tensorboard.add_scalar('TRAIN Generator LOSS/G_loss_image', logs['G_loss_image'], global_step=current_step)
if 'G_loss_frequency' in logs.keys():
logger_tensorboard.add_scalar('TRAIN Generator LOSS/G_loss_frequency', logs['G_loss_frequency'], global_step=current_step)
if 'G_loss_preceptual' in logs.keys():
logger_tensorboard.add_scalar('TRAIN Generator LOSS/G_loss_preceptual', logs['G_loss_preceptual'], global_step=current_step)
if 'G_loss_adversarial' in logs.keys():
logger_tensorboard.add_scalar('TRAIN Generator LOSS/G_loss_adversarial', logs['G_loss_adversarial'], global_step=current_step)
if 'D_loss_real' in logs.keys():
logger_tensorboard.add_scalar('TRAIN Discriminator LOSS/D_loss_real', logs['D_loss_real'], global_step=current_step)
if 'D_loss_fake' in logs.keys():
logger_tensorboard.add_scalar('TRAIN Discriminator LOSS/D_loss_fake', logs['D_loss_fake'], global_step=current_step)
# -------------------------------
# 5) save model
# -------------------------------
if current_step % opt['train']['checkpoint_save'] == 0 and opt['rank'] == 0:
logger.info('Saving the model.')
model.save(current_step)
# -------------------------------
# 6) testing
# -------------------------------
if current_step % opt['train']['checkpoint_test'] == 0 and opt['rank'] == 0:
# create folder for FID
img_dir_tmp_H = os.path.join(opt['path']['images'], 'tempH')
util.mkdir(img_dir_tmp_H)
img_dir_tmp_E = os.path.join(opt['path']['images'], 'tempE')
util.mkdir(img_dir_tmp_E)
img_dir_tmp_L = os.path.join(opt['path']['images'], 'tempL')
util.mkdir(img_dir_tmp_L)
# create result dict
test_results = OrderedDict()
test_results['psnr'] = []
test_results['ssim'] = []
test_results['lpips'] = []
test_results['G_loss'] = []
test_results['G_loss_adversarial'] = []
test_results['G_loss_image'] = []
test_results['G_loss_frequency'] = []
test_results['G_loss_preceptual'] = []
for idx, test_data in enumerate(test_loader):
with torch.no_grad():
img_info = test_data['img_info'][0]
img_dir = os.path.join(opt['path']['images'], img_info)
# testing and adjust resolution
model.feed_data(test_data)
model.check_windowsize()
model.test()
model.recover_windowsize()
model.record_loss_for_val()
logs = model.current_log()
test_results['G_loss'].append(logs['G_loss'])
test_results['G_loss_adversarial'].append(logs['G_loss_adversarial'])
test_results['G_loss_image'].append(logs['G_loss_image'])
test_results['G_loss_frequency'].append(logs['G_loss_frequency'])
test_results['G_loss_preceptual'].append(logs['G_loss_preceptual'])
# acquire test result
results = model.current_results_gpu()
# calculate LPIPS (GPU | torch.tensor)
L_img = results['L']
E_img = results['E']
H_img = results['H']
current_lpips = util.calculate_lpips_single(loss_fn_alex, H_img, E_img).data.squeeze().float().cpu().numpy()
# calculate PSNR SSIM (CPU | np.float)
L_img = util.tensor2float(L_img)
E_img = util.tensor2float(E_img)
H_img = util.tensor2float(H_img)
current_psnr = util.calculate_psnr_single(H_img, E_img, border=0)
current_ssim = util.calculate_ssim_single(H_img, E_img, border=0)
# record metrics
test_results['psnr'].append(current_psnr)
test_results['ssim'].append(current_ssim)
test_results['lpips'].append(current_lpips)
# save samples
if idx < 5:
util.mkdir(img_dir)
cv2.imwrite(os.path.join(img_dir, 'ZF_{:05d}.png'.format(current_step)), np.clip(L_img, 0, 1) * 255)
cv2.imwrite(os.path.join(img_dir, 'Recon_{:05d}.png'.format(current_step)), np.clip(E_img, 0, 1) * 255)
cv2.imwrite(os.path.join(img_dir, 'GT_{:05d}.png'.format(current_step)), np.clip(H_img, 0, 1) * 255)
if opt['datasets']['test']['resize_for_fid']:
resize_for_fid = opt['datasets']['test']['resize_for_fid']
cv2.imwrite(os.path.join(img_dir_tmp_L, 'ZF_{:05d}.png'.format(idx)), resize(np.clip(L_img, 0, 1), (resize_for_fid[0], resize_for_fid[1])) * 255)
cv2.imwrite(os.path.join(img_dir_tmp_E, 'Recon_{:05d}.png'.format(idx)), resize(np.clip(E_img, 0, 1), (resize_for_fid[0], resize_for_fid[1])) * 255)
cv2.imwrite(os.path.join(img_dir_tmp_H, 'GT_{:05d}.png'.format(idx)), resize(np.clip(H_img, 0, 1), (resize_for_fid[0], resize_for_fid[1])) * 255)
else:
cv2.imwrite(os.path.join(img_dir_tmp_L, 'ZF_{:05d}.png'.format(idx)), np.clip(L_img, 0, 1) * 255)
cv2.imwrite(os.path.join(img_dir_tmp_E, 'Recon_{:05d}.png'.format(idx)), np.clip(E_img, 0, 1) * 255)
cv2.imwrite(os.path.join(img_dir_tmp_H, 'GT_{:05d}.png'.format(idx)), np.clip(H_img, 0, 1) * 255)
# summarize psnr/ssim/lpips
ave_psnr = np.mean(test_results['psnr'])
# std_psnr = np.std(test_results['psnr'], ddof=1)
ave_ssim = np.mean(test_results['ssim'])
# std_ssim = np.std(test_results['ssim'], ddof=1)
ave_lpips = np.mean(test_results['lpips'])
# std_lpips = np.std(test_results['lpips'], ddof=1)
# calculate FID
if opt['dist']:
# DistributedDataParallel (If multiple GPUs are used to train, use the 2nd GPU for FID calculation.)
log = os.popen("{} -m pytorch_fid {} {} ".format(
sys.executable,
img_dir_tmp_H,
img_dir_tmp_E)).read()
else:
# DataParallel (If multiple GPUs are used to train, use the 2nd GPU for FID calculation for unbalance of GPU menory use.)
if len(opt['gpu_ids']) > 1:
log = os.popen("{} -m pytorch_fid --device cuda:1 {} {} ".format(
sys.executable,
img_dir_tmp_H,
img_dir_tmp_E)).read()
else:
log = os.popen("{} -m pytorch_fid {} {} ".format(
sys.executable,
img_dir_tmp_H,
img_dir_tmp_E)).read()
print(log)
fid = eval(log.replace('FID: ', ''))
# testing log
logger.info('<epoch:{:3d}, iter:{:8,d}, Average PSNR : {:<.2f}; Average Average SSIM : {:<.4f}; LPIPS : {:<.4f}; FID : {:<.2f}'
.format(epoch, current_step, ave_psnr, ave_ssim, ave_lpips, fid))
logger_tensorboard.add_scalar('VALIDATION PSNR', ave_psnr, global_step=current_step)
logger_tensorboard.add_scalar('VALIDATION SSIM', ave_ssim, global_step=current_step)
logger_tensorboard.add_scalar('VALIDATION LPIPS', ave_lpips, global_step=current_step)
logger_tensorboard.add_scalar('VALIDATION FID', fid, global_step=current_step)
# # early stopping
# if opt['train']['is_early_stopping']:
# early_stopping(ave_psnr, model, epoch, current_step)
# if early_stopping.is_save:
# logger.info('Saving the model by early stopping')
# model.save(f'best_{current_step}')
# if early_stopping.early_stop:
# print("Early stopping!")
# break
print("Training Stop")
if __name__ == '__main__':
main()