-
Notifications
You must be signed in to change notification settings - Fork 15
/
train.py
267 lines (236 loc) · 11.6 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
import os
import torch
import random
from torchvision import transforms
import torch.optim as optim
import torch.backends.cudnn as cudnn
import numpy as np
from torch.utils.data import DataLoader
from net.CIDNet import CIDNet
from data.options import option
from measure import metrics
from eval import eval
from data.data import *
from loss.losses import *
from data.scheduler import *
from tqdm import tqdm
opt = option().parse_args()
def seed_torch():
seed = random.randint(1, 1000000)
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
def train_init():
seed_torch()
cudnn.benchmark = True
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
cuda = opt.gpu_mode
if cuda and not torch.cuda.is_available():
raise Exception("No GPU found, please run without --cuda")
def train(epoch):
model.train()
loss_print = 0
pic_cnt = 0
loss_last_10 = 0
pic_last_10 = 0
train_len = len(training_data_loader)
iter = 0
torch.autograd.set_detect_anomaly(True)
for batch in tqdm(training_data_loader):
im1, im2, path1, path2 = batch[0], batch[1], batch[2], batch[3]
im1 = im1.cuda()
im2 = im2.cuda()
output_rgb = model(im1)
gt_rgb = im2
output_hvi = model.HVIT(output_rgb)
gt_hvi = model.HVIT(gt_rgb)
loss_hvi = L1_loss(output_hvi, gt_hvi) + D_loss(output_hvi, gt_hvi) + E_loss(output_hvi, gt_hvi) + opt.P_weight * P_loss(output_hvi, gt_hvi)[0]
loss_rgb = L1_loss(output_rgb, gt_rgb) + D_loss(output_rgb, gt_rgb) + E_loss(output_rgb, gt_rgb) + opt.P_weight * P_loss(output_rgb, gt_rgb)[0]
loss = loss_rgb + opt.HVI_weight * loss_hvi
iter += 1
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_print = loss_print + loss.item()
loss_last_10 = loss_last_10 + loss.item()
pic_cnt += 1
pic_last_10 += 1
if iter == train_len:
print("===> Epoch[{}]: Loss: {:.4f} || Learning rate: lr={}.".format(epoch,
loss_last_10/pic_last_10, optimizer.param_groups[0]['lr']))
loss_last_10 = 0
pic_last_10 = 0
output_img = transforms.ToPILImage()((output_rgb)[0].squeeze(0))
gt_img = transforms.ToPILImage()((gt_rgb)[0].squeeze(0))
if not os.path.exists(opt.val_folder+'training'):
os.mkdir(opt.val_folder+'training')
output_img.save(opt.val_folder+'training/test.png')
gt_img.save(opt.val_folder+'training/gt.png')
return loss_print, pic_cnt
def checkpoint(epoch):
if not os.path.exists("./weights"):
os.mkdir("./weights")
if not os.path.exists("./weights/train"):
os.mkdir("./weights/train")
model_out_path = "./weights/train/epoch_{}.pth".format(epoch)
torch.save(model.state_dict(), model_out_path)
print("Checkpoint saved to {}".format(model_out_path))
return model_out_path
def load_datasets():
print('===> Loading datasets')
if opt.lol_v1 or opt.lol_blur or opt.lolv2_real or opt.lolv2_syn or opt.SID or opt.SICE_mix or opt.SICE_grad:
if opt.lol_v1:
train_set = get_lol_training_set(opt.data_train_lol_v1,size=opt.cropSize)
training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True)
test_set = get_eval_set(opt.data_val_lol_v1)
testing_data_loader = DataLoader(dataset=test_set, num_workers=opt.threads, batch_size=1, shuffle=False)
if opt.lol_blur:
train_set = get_training_set_blur(opt.data_train_lol_blur,size=opt.cropSize)
training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True)
test_set = get_eval_set(opt.data_val_lol_blur)
testing_data_loader = DataLoader(dataset=test_set, num_workers=opt.threads, batch_size=1, shuffle=False)
if opt.lolv2_real:
train_set = get_lol_v2_training_set(opt.data_train_lolv2_real,size=opt.cropSize)
training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True)
test_set = get_eval_set(opt.data_val_lolv2_real)
testing_data_loader = DataLoader(dataset=test_set, num_workers=opt.threads, batch_size=1, shuffle=False)
if opt.lolv2_syn:
train_set = get_lol_v2_syn_training_set(opt.data_train_lolv2_syn,size=opt.cropSize)
training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True)
test_set = get_eval_set(opt.data_val_lolv2_syn)
testing_data_loader = DataLoader(dataset=test_set, num_workers=opt.threads, batch_size=1, shuffle=False)
if opt.SID:
train_set = get_SID_training_set(opt.data_train_SID,size=opt.cropSize)
training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True)
test_set = get_eval_set(opt.data_val_SID)
testing_data_loader = DataLoader(dataset=test_set, num_workers=opt.threads, batch_size=1, shuffle=False)
if opt.SICE_mix:
train_set = get_SICE_training_set(opt.data_train_SICE,size=opt.cropSize)
training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True)
test_set = get_SICE_eval_set(opt.data_val_SICE_mix)
testing_data_loader = DataLoader(dataset=test_set, num_workers=opt.threads, batch_size=1, shuffle=False)
if opt.SICE_grad:
train_set = get_SICE_training_set(opt.data_train_SICE,size=opt.cropSize)
training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True)
test_set = get_SICE_eval_set(opt.data_val_SICE_grad)
testing_data_loader = DataLoader(dataset=test_set, num_workers=opt.threads, batch_size=1, shuffle=False)
else:
raise Exception("should choose a dataset")
return training_data_loader, testing_data_loader
def build_model():
print('===> Building model ')
model = CIDNet().cuda()
if opt.start_epoch > 0:
pth = f"./weights/train/epoch_{opt.start_epoch}.pth"
model.load_state_dict(torch.load(pth, map_location=lambda storage, loc: storage))
return model
def make_scheduler():
optimizer = optim.Adam(model.parameters(), lr=opt.lr)
if opt.cos_restart_cyclic:
if opt.start_warmup:
scheduler_step = CosineAnnealingRestartCyclicLR(optimizer=optimizer, periods=[(opt.nEpochs//4)-opt.warmup_epochs, (opt.nEpochs*3)//4], restart_weights=[1,1],eta_mins=[0.0002,0.0000001])
scheduler = GradualWarmupScheduler(optimizer, multiplier=1, total_epoch=opt.warmup_epochs, after_scheduler=scheduler_step)
else:
scheduler = CosineAnnealingRestartCyclicLR(optimizer=optimizer, periods=[opt.nEpochs//4, (opt.nEpochs*3)//4], restart_weights=[1,1],eta_mins=[0.0002,0.0000001])
elif opt.cos_restart:
if opt.start_warmup:
scheduler_step = CosineAnnealingRestartLR(optimizer=optimizer, periods=[opt.nEpochs - opt.warmup_epochs - opt.start_epoch], restart_weights=[1],eta_min=1e-7)
scheduler = GradualWarmupScheduler(optimizer, multiplier=1, total_epoch=opt.warmup_epochs, after_scheduler=scheduler_step)
else:
scheduler = CosineAnnealingRestartLR(optimizer=optimizer, periods=[opt.nEpochs - opt.start_epoch], restart_weights=[1],eta_min=1e-7)
else:
raise Exception("should choose a scheduler")
return optimizer,scheduler
def init_loss():
L1_weight = opt.L1_weight
D_weight = opt.D_weight
E_weight = opt.E_weight
P_weight = 1.0
L1_loss= L1Loss(loss_weight=L1_weight, reduction='mean').cuda()
D_loss = SSIM(weight=D_weight).cuda()
E_loss = EdgeLoss(loss_weight=E_weight).cuda()
P_loss = PerceptualLoss({'conv1_2': 1, 'conv2_2': 1,'conv3_4': 1,'conv4_4': 1}, perceptual_weight = P_weight ,criterion='mse').cuda()
return L1_loss,P_loss,E_loss,D_loss
if __name__ == '__main__':
'''
preparision
'''
train_init()
training_data_loader, testing_data_loader = load_datasets()
model = build_model()
optimizer,scheduler = make_scheduler()
L1_loss,P_loss,E_loss,D_loss = init_loss()
'''
train
'''
psnr = []
ssim = []
lpips = []
start_epoch=0
if opt.start_epoch > 0:
start_epoch = opt.start_epoch
if not os.path.exists(opt.val_folder):
os.mkdir(opt.val_folder)
for epoch in range(start_epoch+1, opt.nEpochs + start_epoch + 1):
epoch_loss, pic_num = train(epoch)
scheduler.step()
if epoch % opt.snapshots == 0:
model_out_path = checkpoint(epoch)
norm_size = True
# LOL three subsets
if opt.lol_v1:
output_folder = 'LOLv1/'
label_dir = opt.data_valgt_lol_v1
if opt.lolv2_real:
output_folder = 'LOLv2_real/'
label_dir = opt.data_valgt_lolv2_real
if opt.lolv2_syn:
output_folder = 'LOLv2_syn/'
label_dir = opt.data_valgt_lolv2_syn
# LOL-blur dataset with low_blur and high_sharp_scaled
if opt.lol_blur:
output_folder = 'LOL_blur/'
label_dir = opt.data_valgt_lol_blur
if opt.SID:
output_folder = 'SID/'
label_dir = opt.data_valgt_SID
npy = True
if opt.SICE_mix:
output_folder = 'SICE_mix/'
label_dir = opt.data_valgt_SICE_mix
norm_size = False
if opt.SICE_grad:
output_folder = 'SICE_grad/'
label_dir = opt.data_valgt_SICE_grad
norm_size = False
im_dir = opt.val_folder + output_folder + '*.png'
eval(model, testing_data_loader, model_out_path, opt.val_folder+output_folder,
norm_size=norm_size, LOL=opt.lol_v1, v2=opt.lolv2_real, alpha=0.8)
avg_psnr, avg_ssim, avg_lpips = metrics(im_dir, label_dir, use_GT_mean=False)
print("===> Avg.PSNR: {:.4f} dB ".format(avg_psnr))
print("===> Avg.SSIM: {:.4f} ".format(avg_ssim))
print("===> Avg.LPIPS: {:.4f} ".format(avg_lpips))
psnr.append(avg_psnr)
ssim.append(avg_ssim)
lpips.append(avg_lpips)
print(psnr)
print(ssim)
print(lpips)
torch.cuda.empty_cache()
with open("./results/training/metrics.md", "w") as f:
f.write("dataset: "+ output_folder + "\n")
f.write(f"lr: {opt.lr}\n")
f.write(f"batch size: {opt.batchSize}\n")
f.write(f"crop size: {opt.cropSize}\n")
f.write(f"HVI_weight: {opt.HVI_weight}\n")
f.write(f"L1_weight: {opt.L1_weight}\n")
f.write(f"D_weight: {opt.D_weight}\n")
f.write(f"E_weight: {opt.E_weight}\n")
f.write(f"P_weight: {opt.P_weight}\n")
f.write("| Epochs | PSNR | SSIM | LPIPS |\n")
f.write("|----------------------|----------------------|----------------------|----------------------|\n")
for i in range(len(psnr)):
f.write(f"| {(i+1)*10} | { psnr[i]:.4f} | {ssim[i]:.4f} | {lpips[i]:.4f} |\n")