-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_train_psnr.py
310 lines (254 loc) · 11.1 KB
/
main_train_psnr.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
import os.path
import math
import pickle
import argparse
import random
import numpy as np
import logging
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
import torch
import os.path
import math
import argparse
import time
import random
import numpy as np
import os
from PIL import Image
from torch.utils.data import Dataset
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
import pickle
import pickle
from collections import OrderedDict
from torch.utils.data import Dataset
import logging
import torch
from torch.utils.data import DataLoader
from utils import utils_logger
from utils import utils_image as util
from utils import utils_option as option
from data.select_dataset import define_Dataset
from models.select_model import define_Model
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 data.select_dataset import define_Dataset
from models.select_model import define_Model
'''
# --------------------------------------------
# training code for MSRResNet
# --------------------------------------------
# Kai Zhang ([email protected])
# github: https://github.com/cszn/KAIR
# --------------------------------------------
# https://github.com/xinntao/BasicSR
# --------------------------------------------
'''
class DefectDataset(Dataset):
def __init__(self, root_dir,load_type, transform=None):
self.root_dir = root_dir
self.transform = transform
self.triplets = self._load_triplets(load_type)
def _load_triplets(self,load_type):
triplets = []
for object_name in os.listdir(self.root_dir):
#print(object_name)
object_path = os.path.join(self.root_dir, object_name, load_type)
degraded_path = os.path.join(object_path, 'Degraded_image')
mask_path = os.path.join(object_path, 'Defect_mask')
clean_path = os.path.join(object_path, 'GT_clean_image')
# Iterate through each defect type (broken_large, broken_small, etc.)
for defect_type in os.listdir(degraded_path):
#print(defect_type)
degraded_defect_dir = os.path.join(degraded_path, defect_type)
mask_defect_dir = os.path.join(mask_path, defect_type)
clean_defect_dir = os.path.join(clean_path, defect_type)
# Match image triplets by name in each defect folder
for img_name in os.listdir(degraded_defect_dir):
degraded_img = os.path.join(degraded_defect_dir, img_name)
mask_img = os.path.join(mask_defect_dir, img_name.replace(".png", "_mask.png"))
clean_img = os.path.join(clean_defect_dir, img_name)
# Only add the triplet if all three files exist
if os.path.exists(degraded_img) and os.path.exists(mask_img) and os.path.exists(clean_img):
triplets.append((degraded_img,clean_img))
return triplets
def __len__(self):
return len(self.triplets)
def __getitem__(self, idx):
degraded_img_path, clean_img_path = self.triplets[idx]
degraded_img = Image.open(degraded_img_path).convert("RGB")
clean_img = Image.open(clean_img_path).convert("RGB")
# Apply transforms, if any
if self.transform:
degraded_img = self.transform(degraded_img)
# mask_img = self.transform(mask_img)
clean_img = self.transform(clean_img)
return degraded_img, clean_img
transform = transforms.Compose([
transforms.Resize((900, 900)), #change the size here according to the model
transforms.RandomHorizontalFlip(),
transforms.ToTensor(), # Normalize images
])
def main(json_path='options/train_msrresnet_psnr.json'):
'''
# ----------------------------------------
# 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_E, init_path_E = option.find_last_checkpoint(opt['path']['models'], net_type='E')
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
current_step = max(init_iter_G, init_iter_E, init_iter_optimizerG)
border = opt['scale']
# --<--<--<--<--<--<--<--<--<--<--<--<--<-
# ----------------------------------------
# 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_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))
# ----------------------------------------
# seed
# ----------------------------------------
seed = opt['train']['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
# ----------------------------------------
train_file = "/home/navin/coursework/kla-challenge-dli/KAIR-DLI/datasets-dli/dataloader_train_revisedv3.pkl"
with open(train_file, 'rb') as f:
train_loader = pickle.load(f)
print("_________________")
test_file = "/home/navin/coursework/kla-challenge-dli/KAIR-DLI/datasets-dli/dataloader_val_revisedv3.pkl"
with open(test_file, 'rb') as f:
test_loader = pickle.load(f)
'''
# ----------------------------------------
# Step--3 (initialize model)
# ----------------------------------------
'''
model = define_Model(opt)
model.init_train()
if opt['rank'] == 0:
logger.info(model.info_network())
logger.info(model.info_params())
'''
# ----------------------------------------
# Step--4 (main training)
# ----------------------------------------
'''
for epoch in range(1000000): # keep running
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
degraded_imgs, clean_imgs = train_data
degraded_imgs = degraded_imgs.to("cuda")
clean_imgs = clean_imgs.to("cuda")
# -------------------------------
model.feed_data(degraded_imgs, clean_imgs)
# -------------------------------
# 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() # such as loss
message = '<epoch:{:3d}, iter:{:8,d}, lr:{:.3e}> '.format(epoch, current_step, model.current_learning_rate())
for k, v in logs.items(): # merge log information into message
message += '{:s}: {:.3e} '.format(k, v)
logger.info(message)
# -------------------------------
# 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:
avg_psnr = 0.0
idx = 0
for test_data in test_loader:
idx += 1
degraded_imgs, clean_imgs = test_data
degraded_imgs = degraded_imgs.to("cuda")
clean_imgs = clean_imgs.to("cuda")
img_dir = opt['path']['images']
util.mkdir(img_dir)
model.feed_data(degraded_imgs, clean_imgs)
model.test()
visuals = model.current_visuals()
E_img = util.tensor2uint(visuals['E'])
H_img = util.tensor2uint(visuals['H'])
# -----------------------
# save estimated image E
# -----------------------
save_img_path = os.path.join(img_dir, '{:d}_{:d}.png'.format(idx, current_step))
util.imsave(E_img, save_img_path)
# -----------------------
# calculate PSNR
# -----------------------
current_psnr = util.calculate_psnr(E_img, H_img, border=border)
logger.info('{:->4d}--> {:>10s} | {:<4.2f}dB'.format(idx, save_img_path, current_psnr))
avg_psnr += current_psnr
avg_psnr = avg_psnr / idx
# testing log
logger.info('<epoch:{:3d}, iter:{:8,d}, Average PSNR : {:<.2f}dB\n'.format(epoch, current_step, avg_psnr))
if __name__ == '__main__':
main()