-
Notifications
You must be signed in to change notification settings - Fork 3
/
Derain_add_densenet.py
371 lines (296 loc) · 15.5 KB
/
Derain_add_densenet.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
364
365
366
367
368
369
370
371
# coding=utf-8
import os
import re
import torch
import argparse
import urllib.request
from Utils.utils import *
from Utils.Vidsom import *
from Utils.model_init import *
from Utils.ssim_map import SSIM_MAP
from Utils.torch_ssim import SSIM
from torch import nn, optim
from torch.backends import cudnn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torchvision.utils import make_grid
from MyDataset.Datasets import derain_test_datasets , derain_train_datasets
from torchvision.transforms import Compose, ToTensor, Resize, Normalize, CenterCrop, RandomCrop
from net.model import w_net as net1
from net.model import u_net as net2
from net.model import res_net as net3
from net.model import Net4 as net4
from net.model import refineNet as refine
parser = argparse.ArgumentParser(description="PyTorch Derain")
#root
parser.add_argument("--train", default="/home/ws/Desktop/PL/Derain_Dataset2018/train", type=str,
help="path to load train datasets(default: none)")
parser.add_argument("--test", default="/home/ws/Desktop/PL/Derain_Dataset2018/test", type=str,
help="path to load test datasets(default: none)")
parser.add_argument("--save_image_root", default='./result', type=str,
help="save test image root")
parser.add_argument("--save_root", default="/home/ws/Desktop/derain2020/checkpoints", type=str,
help="path to save networks")
parser.add_argument("--pretrain_root", default="/home/ws/Desktop/derain2020/checkpoints", type=str,
help="path to pretrained net1 net2 net3 root")
#hypeparameters
parser.add_argument("--batchSize", type=int, default=8, help="training batch size")
parser.add_argument("--nEpoch", type=int, default=500, help="number of epochs to train for")
parser.add_argument("--lr", type=float, default=1e-4, help="Learning Rate. Default=1e-4")
parser.add_argument("--lr1", type=float, default=5e-5, help="Learning Rate For pretrained net. Default=1e-5")
parser.add_argument("--p", default=0.8, type=float, help="probability of normal conditions")
parser.add_argument("--train_print_fre", type=int, default=200, help="frequency of print train loss on train phase")
parser.add_argument("--test_frequency", type=int, default=1, help="frequency of test")
parser.add_argument("--test_print_fre", type=int, default=200, help="frequency of print train loss on test phase")
parser.add_argument("--cuda",type=str, default="Ture", help="Use cuda?")
parser.add_argument("--gpus", type=int, default=1, help="nums of gpu to use")
parser.add_argument("--startweights", default= 32, type=int, help="start number of net's weight , 0 is None")
parser.add_argument("--initmethod", default='xavier', type=str, help="xavier , kaiming , normal ,orthogonal ,default : xavier")
parser.add_argument("--startepoch", default=38, type=int, help="Manual epoch number (useful on restarts)")
parser.add_argument("--works", type=int, default=8, help="Number of works for data loader to use, Default: 1")
parser.add_argument("--momentum", default=0.9, type=float, help="SGD Momentum, Default: 0.9")
parser.add_argument("--report", default=False, type=bool, help="report to wechat")
parser.add_argument("--save_image", default=False, type=bool, help="save test image")
parser.add_argument("--pretrain_epoch", default=[93,169,123], type=list, help="pretrained epoch for Net1 Net2 Net3")
def main():
global opt, Net1 , Net2 , Net3 , Net4 , RefineNet , criterion_mse , criterion_ssim_map,criterion_ssim,criterion_ace
opt = parser.parse_args()
print(opt)
cuda = opt.cuda
if cuda and not torch.cuda.is_available():
raise Exception("No GPU found, please run without --cuda")
seed = 1334
torch.manual_seed(seed)
if cuda:
torch.cuda.manual_seed(seed)
cudnn.benchmark = True
print("==========> Loading datasets")
train_dataset = derain_train_datasets( data_root= opt.train, transform=Compose([
ToTensor()
]))
test_dataset = derain_test_datasets(opt.test, transform=Compose([
ToTensor()
]))
training_data_loader = DataLoader(dataset=train_dataset, num_workers=opt.works, batch_size=opt.batchSize,
pin_memory=True, shuffle=True)
testing_data_loader = DataLoader(dataset=test_dataset, num_workers=opt.works, batch_size=1, pin_memory=True,
shuffle=True)
if opt.initmethod == 'orthogonal':
init_function = weights_init_orthogonal
elif opt.initmethod == 'kaiming':
init_function = weights_init_kaiming
elif opt.initmethod == 'normal':
init_function = weights_init_normal
else:
init_function = weights_init_xavier
Net1 = net1()
Net1.apply(init_function)
Net2 = net2()
Net2.apply(init_function)
Net3 = net3()
Net3.apply(init_function)
Net4 = net4()
Net4.apply(init_function)
RefineNet = refine()
RefineNet.apply(init_function)
criterion_mse = nn.MSELoss(size_average=True)
criterion_ssim_map = SSIM_MAP()
criterion_ssim = SSIM()
criterion_ace = nn.SmoothL1Loss()
print("==========> Setting GPU")
#if cuda:
if opt.cuda:
Net1 = nn.DataParallel(Net1, device_ids=[i for i in range(opt.gpus)]).cuda()
Net2 = nn.DataParallel(Net2, device_ids=[i for i in range(opt.gpus)]).cuda()
Net3 = nn.DataParallel(Net3, device_ids=[i for i in range(opt.gpus)]).cuda()
Net4 = nn.DataParallel(Net4, device_ids=[i for i in range(opt.gpus)]).cuda()
RefineNet = nn.DataParallel(RefineNet, device_ids=[i for i in range(opt.gpus)]).cuda()
criterion_ssim = criterion_ssim.cuda()
criterion_ssim_map = criterion_ssim_map.cuda()
criterion_mse= criterion_mse.cuda()
criterion_ace = criterion_ace.cuda()
else:
raise Exception("it takes a long time without cuda ")
#print(net)
if opt.pretrain_root:
if os.path.exists(opt.pretrain_root):
print("=> loading net from '{}'".format(opt.pretrain_root))
weights = torch.load(opt.pretrain_root +"/w/%s.pth"%opt.pretrain_epoch[0])
Net1.load_state_dict(weights['state_dict'] )
weights = torch.load(opt.pretrain_root + "/u/%s.pth" % opt.pretrain_epoch[1])
Net2.load_state_dict(weights['state_dict'] )
weights = torch.load(opt.pretrain_root + "/res/%s.pth" % opt.pretrain_epoch[2])
Net3.load_state_dict(weights['state_dict'])
del weights
else:
print("=> no net found at '{}'".format(opt.pretrain_root))
# weights start from early
if opt.startweights:
if os.path.exists(opt.save_root):
print("=> loading checkpoint '{}'".format(opt.save_root))
weights = torch.load(opt.save_root + '/Net1/%s.pth'%opt.startweights)
Net1.load_state_dict(weights["state_dict"] )
weights = torch.load(opt.save_root + '/Net2/%s.pth' % opt.startweights)
Net2.load_state_dict(weights["state_dict"])
weights = torch.load(opt.save_root + '/Net3/%s.pth' % opt.startweights)
Net3.load_state_dict(weights["state_dict"])
weights = torch.load(opt.save_root + '/Net4/%s.pth' % opt.startweights)
Net4.load_state_dict(weights["state_dict"])
weights = torch.load(opt.save_root + '/refine/%s.pth' % opt.startweights)
RefineNet.load_state_dict(weights["state_dict"])
del weights
else:
raise Exception("'{}' is not a file , Check out it again".format(opt.save_root))
print("==========> Setting Optimizer")
optimizer1 = optim.Adam(filter(lambda p: p.requires_grad, Net1.parameters()), lr=opt.lr1)
optimizer2 = optim.Adam(filter(lambda p: p.requires_grad, Net2.parameters()), lr=opt.lr1)
optimizer3 = optim.Adam(filter(lambda p: p.requires_grad, Net3.parameters()), lr=opt.lr1)
optimizer4 = optim.Adam(filter(lambda p: p.requires_grad, Net4.parameters()), lr=opt.lr)
optimizer_Refine = optim.Adam(filter(lambda p: p.requires_grad, RefineNet.parameters()), lr=opt.lr)
optimizer = [ 1 , optimizer1 , optimizer2 , optimizer3 , optimizer4 , optimizer_Refine ]
print("==========> Training")
for epoch in range(opt.startepoch, opt.nEpoch + 1):
if epoch > 10 :
opt.lr = 1e-4
optimizer[1] = optim.Adam(filter(lambda p: p.requires_grad, Net1.parameters()), lr=opt.lr1)
optimizer[2] = optim.Adam(filter(lambda p: p.requires_grad, Net2.parameters()), lr=opt.lr1)
optimizer[3] = optim.Adam(filter(lambda p: p.requires_grad, Net3.parameters()), lr=opt.lr1)
optimizer[4] = optim.Adam(filter(lambda p: p.requires_grad, Net4.parameters()), lr=opt.lr)
optimizer[5] = optim.Adam(filter(lambda p: p.requires_grad, RefineNet.parameters()), lr=opt.lr)
# train(training_data_loader, optimizer, epoch)
if epoch % opt.test_frequency == 0 :
test(testing_data_loader ,epoch)
def train(training_data_loader, optimizer, epoch):
print("training ==========> epoch =", epoch, "lr =", opt.lr)
Net1.train()
Net2.train()
Net3.train()
Net4.train()
RefineNet.train()
t_loss = [] # save trainloss
for step, (data, label) in enumerate(training_data_loader, 1):
if opt.cuda and torch.cuda.is_available():
data = data.clone().detach().requires_grad_(True).cuda()
label = label.cuda()
else:
raise Exception("it takes a long time without cuda ")
data = data.cpu()
label = label.cpu()
Net1_out = Net1(data)
Net2_out = Net2(Net1_out)
Net3_out = Net3(Net2_out)
Net4_out = Net4( data - Net1_out ,data - Net2_out ,data - Net3_out )
RefineNet_out =RefineNet( Net1_out , Net2_out , Net3_out , data - Net4_out )
init_map = torch.ones(size=Net1_out.size()).cuda()
ssim_map1 = torch.mul(criterion_ssim_map(Net1_out , label) , init_map )
ssim_map2 = torch.mul(criterion_ssim_map(Net2_out , label) , ssim_map1 )
ssim_map3 = torch.mul(criterion_ssim_map(Net3_out , label) , ssim_map2 )
loss1 = torch.mul((1 - ssim_map1) , torch.abs(Net1_out - label)).mean()
loss2 = torch.mul((1 - ssim_map2) , torch.abs(Net2_out - label)).mean()
loss3 = torch.mul((1 - ssim_map3) , torch.abs(Net3_out - label)).mean()
new_loss = torch.mul((1-criterion_ssim_map(RefineNet_out , label)) ,torch.abs(RefineNet_out-label)).mean().cuda()
ssim_loss = 1- criterion_ssim(RefineNet_out , label)
loss = new_loss + 0.01 * (loss1 + loss2 +loss3)
del Net1_out , Net2_out , Net3_out , Net4_out
Net1.zero_grad()
Net2.zero_grad()
Net3.zero_grad()
Net4.zero_grad()
RefineNet.zero_grad()
optimizer[1].zero_grad()
optimizer[2].zero_grad()
optimizer[3].zero_grad()
optimizer[4].zero_grad()
optimizer[5].zero_grad()
loss.backward()
optimizer[1].step()
optimizer[2].step()
optimizer[3].step()
optimizer[4].step()
optimizer[5].step()
if step % opt.train_print_fre == 0:
print("epoch{} step {} loss {:6f} new_loss {:6f} ssimloss {:6f} loss1 {:6f} loss2 {:6f} loss3 {:6f}".format(epoch, step,
loss.item(),
new_loss.item(),
ssim_loss.item(),
loss1.item(),
loss2.item(),
loss3.item()))
t_loss.append(loss.item())
del loss1, loss2, loss3 , loss
else:
# displaying to train loss
updata_epoch_loss_display( train_loss= t_loss , v_epoch= epoch , envr= "derain train")
import time
def test(test_data_loader, epoch):
print("------> testing")
Net1.eval()
Net2.eval()
Net3.eval()
Net4.eval()
RefineNet.eval()
torch.cuda.empty_cache()
starttime = 0
endtime = 0
with torch.no_grad():
test_Psnr_sum = 0.0
test_Ssim_sum = 0.0
# showing list
test_Psnr_loss = []
test_Ssim_loss = []
dict_psnr_ssim = {}
starttime = time.time()
for test_step, (data, label, data_path) in enumerate(test_data_loader, 1):
data = data.cuda()
label = label.cuda()
Net1_out = Net1(data).cuda()
Net2_out = Net2(Net1_out).cuda()
Net3_out = Net3(Net2_out).cuda()
Net4_out = Net4(data - Net1_out , data - Net2_out , data - Net3_out).cuda() #best rain streaks
refineNet_out = RefineNet(Net1_out , Net2_out , Net3_out , data - Net4_out ).cuda()
del Net1_out, Net2_out, Net3_out
loss = criterion_mse(refineNet_out, label)
Psnr, Ssim = get_psnr_ssim(refineNet_out, label)
Psnr = round(Psnr.item(), 4)
Ssim = round(Ssim.item(), 4)
# del derain , label
test_Psnr_sum += Psnr
test_Ssim_sum += Ssim
#if opt.save_image == True:
# dict_psnr_ssim["Psnr%s_Ssim%s" % (Psnr, Ssim)] = data_path
# out = refineNet_out.cpu().data[0]
# out = ToPILImage()(out)
# image_number = re.findall(r'\d+', data_path[0])[1]
# out.save( opt.save_image_root + "/%s_p:%s_s:%s.jpg" % (image_number, Psnr, Ssim))
if test_step % opt.test_print_fre == 0:
print("epoch={} Psnr={} Ssim={} loss{}".format(epoch, Psnr, Ssim, loss.item()))
test_Psnr_loss.append(test_Psnr_sum / test_step)
test_Ssim_loss.append(test_Ssim_sum / test_step)
else:
del loss
print("epoch={} avr_Psnr ={} avr_Ssim={}".format(epoch, test_Psnr_sum / test_step,
test_Ssim_sum / test_step))
write_test_perform("./perform_test.txt", test_Psnr_sum / test_step, test_Ssim_sum / test_step)
# visdom showing
print("---->testing over show in visdom")
display_Psnr_Ssim(Psnr=test_Psnr_sum / test_step, Ssim=test_Ssim_sum / test_step, v_epoch=epoch,
env="derain_test")
endtime = time.time()
print("----------TestTime:{}".format(endtime - starttime))
print("epoch {} train over-----> save net".format(epoch))
print("saving checkpoint save_root{}".format(opt.save_root))
if os.path.exists(opt.save_root):
save_checkpoint(root=opt.save_root, model=Net1, epoch=epoch, model_stage="Net1")
save_checkpoint(root=opt.save_root, model=Net2, epoch=epoch, model_stage="Net2")
save_checkpoint(root=opt.save_root, model=Net3, epoch=epoch, model_stage="Net3")
save_checkpoint(root=opt.save_root, model=Net4, epoch=epoch, model_stage="Net4")
save_checkpoint(root=opt.save_root, model=RefineNet, epoch=epoch, model_stage="refine")
print("finish save epoch{} checkporint".format({epoch}))
else:
raise Exception("saveroot :{} not found , Checkout it".format(opt.save_root))
#
print("all epoch is over ------ ")
print("show epoch and epoch_loss in visdom")
if __name__ == "__main__":
os.system('clear')
main()