-
Notifications
You must be signed in to change notification settings - Fork 12
/
main_gray.py
245 lines (188 loc) · 8.96 KB
/
main_gray.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
import argparse, os
import torch
import random
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader
from DHDN_gray import Net
from dataset_gray import DatasetFromHdf5
import time, math
import numpy as np
import h5py
from torchsummary import summary
# Training settings
parser = argparse.ArgumentParser(description="PyTorch Densely Connected Hierarchical Network for Image Denoising")
parser.add_argument("--batchSize", type=int, default=16, help="Training batch size. Default: 16")
parser.add_argument("--nEpochs", type=int, default=100, help="Number of epochs to train for. Default: 100")
parser.add_argument("--lr", type=float, default=1e-4, help="Learning Rate. Default=1e-4")
parser.add_argument("--step", type=int, default=3,
help="Halves the learning rate for every n epochs. Default: n=3")
parser.add_argument("--cuda", action="store_true", help="Use cuda? Default: True")
parser.add_argument("--resume", default="", type=str, help="Path to checkpoint for resume. Default: None")
parser.add_argument("--start-epoch", default=1, type=int, help="Manual epoch number (useful on restarts). Default: 1")
parser.add_argument("--threads", type=int, default=0, help="Number of threads for data loader to use, Default: 0")
parser.add_argument("--momentum", default=0.9, type=float, help="Momentum, Default: 0.9")
parser.add_argument("--weight-decay", "--wd", default=1e-4, type=float, help="weight decay, Default: 1e-4")
parser.add_argument("--pretrained", default="", type=str, help="path to pretrained model. Default: None")
parser.add_argument("--train", default="./data/gaus_train_g_50.h5", type=str, help="training set path.")
parser.add_argument("--valid", default="./data/gaus_val_g_50.h5", type=str, help="validation set path.")
parser.add_argument("--gpu", default='0', help="GPU number to use when training. ex) 0,1 Default: 0")
parser.add_argument("--checkpoint", default="./checkpoint", type=str,
help="Checkpoint path. Default: ./checkpoint ")
def main():
global opt, model
opt = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = opt.gpu
print(opt)
cuda = opt.cuda
if cuda and not torch.cuda.is_available():
raise Exception("No GPU found, please run without --cuda")
opt.seed = random.randint(1, 10000)
print("Random Seed: ", opt.seed)
torch.manual_seed(opt.seed)
if cuda:
torch.cuda.manual_seed(opt.seed)
cudnn.benchmark = True
print("===> Loading datasets")
train_set = DatasetFromHdf5(opt.train)
valid_set = opt.valid
training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize,
shuffle=True)
val = h5py.File(valid_set)
data_val = val.get('data').value
label_val = val.get('label').value
print("===> Building model")
model = Net()
criterion = nn.L1Loss()
# criterion = nn.MSELoss()
print("===> Setting GPU")
if cuda:
model = nn.DataParallel(model).cuda()
criterion = criterion.cuda()
summary(model, (1, 64, 64))
# optionally resume from a checkpoint
if opt.resume:
if os.path.isfile(opt.resume):
print("=> loading checkpoint '{}'".format(opt.resume))
checkpoint = torch.load(opt.resume)
opt.start_epoch = checkpoint["epoch"] + 1
model.load_state_dict(checkpoint["model"].state_dict())
else:
print("=> no checkpoint found at '{}'".format(opt.resume))
# optionally copy weights from a checkpoint
if opt.pretrained:
if os.path.isfile(opt.pretrained):
print("=> loading model '{}'".format(opt.pretrained))
weights = torch.load(opt.pretrained)
model.load_state_dict(weights['model'].state_dict())
else:
print("=> no model found at '{}'".format(opt.pretrained))
print("===> Setting Optimizer")
optimizer = optim.Adam(model.parameters(), lr=opt.lr)
# optimizer = optim.SGD(model.parameters(), lr=opt.lr, momentum=opt.momentum, weight_decay=opt.weight_decay, nesterov=True)
print("===> Training")
for epoch in range(opt.start_epoch, opt.nEpochs + 1):
avg_loss = train(training_data_loader, optimizer, model, criterion, epoch, data_val, label_val)
model.eval()
psnr = 0
for i in range(data_val.shape[0]):
val_data = data_val[i, :, :]
val_label = label_val[i, :, :]
val_data = Variable(torch.from_numpy(val_data).float()).view(1, 1, val_data.shape[0], val_data.shape[1])
val_label = np.reshape(val_label, (1, val_label.shape[0], val_label.shape[1]))
if opt.cuda:
val_data = val_data.cuda()
with torch.no_grad():
val_out = model(val_data)
val_out = val_out.cpu().data[0].numpy()
psnr += output_psnr_mse(val_label, val_out)
psnr = psnr / (i + 1)
save_checkpoint(model, epoch, 99999, psnr, avg_loss)
def adjust_learning_rate(epoch):
lr = opt.lr
for i in range(epoch // opt.step):
lr = lr / 2
return lr
def train(training_data_loader, optimizer, model, criterion, epoch, data_val, label_val):
lr = adjust_learning_rate(epoch - 1)
for param_group in optimizer.param_groups:
param_group["lr"] = lr
print("epoch =", epoch, "lr =", optimizer.param_groups[0]["lr"])
model.train()
loss_sum = 0
max_psnr = 0
min_avg_loss = 1
st = time.time()
for iteration, batch in enumerate(training_data_loader, 1):
input, label = Variable(batch[0]), Variable(batch[1], requires_grad=False)
[num_bat, patch_h, patch_w] = input.shape
input = input.numpy()
label = label.numpy()
a = np.random.randint(4, size=1)[0]
for i in range(num_bat):
input[i, :, :] = np.rot90(input[i, :, :], a).copy()
label[i, :, :] = np.rot90(label[i, :, :], a).copy()
if np.random.randint(2, size=1)[0] == 1:
input = np.flip(input, axis=1).copy()
label = np.flip(label, axis=1).copy()
if np.random.randint(2, size=1)[0] == 1:
input = np.flip(input, axis=0).copy()
label = np.flip(label, axis=0).copy()
input = Variable(torch.from_numpy(input).float()).view(num_bat, 1, patch_h, patch_w)
label = Variable(torch.from_numpy(label).float()).view(num_bat, 1, patch_h, patch_w)
if opt.cuda:
input = input.cuda()
label = label.cuda()
out = model(input)
loss = criterion(out, label)
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_sum += loss.item()
if iteration % 6000 == 0:
model.eval()
psnr = 0
for i in range(data_val.shape[0]):
val_data = data_val[i, :, :]
val_label = label_val[i, :, :]
val_data = Variable(torch.from_numpy(val_data).float()).view(1, 1, val_data.shape[0], val_data.shape[1])
val_label = np.reshape(val_label, (1, val_label.shape[0], val_label.shape[1]))
if opt.cuda:
val_data = val_data.cuda()
with torch.no_grad():
val_out = model(val_data)
val_out = val_out.cpu().data[0].numpy()
psnr += output_psnr_mse(val_label, val_out)
psnr = psnr / (i + 1)
avg_loss = loss_sum / iteration
print("===> Epoch[{}]({}/{}): Train_Loss: {:.10f} Val_PSNR: {:.4f}".format(epoch, iteration,
len(training_data_loader),
avg_loss, psnr))
model.train()
if psnr > max_psnr or min_avg_loss > avg_loss:
if psnr > max_psnr:
max_psnr = psnr
if min_avg_loss > avg_loss:
min_avg_loss = avg_loss
save_checkpoint(model, epoch, iteration, psnr, avg_loss)
print("training_time ", time.time() - st)
avg_loss = loss_sum / len(training_data_loader)
return avg_loss
def save_checkpoint(model, epoch, iteration, psnr, loss):
model_folder = opt.checkpoint
model_out_path = model_folder + "/model_epoch_{}_iter_{}_PSNR_{:.4f}_loss_{:.8f}.pth".format(epoch, iteration, psnr,
loss)
state = {"epoch": epoch, "model": model}
if not os.path.exists(model_folder):
os.makedirs(model_folder)
torch.save(state, model_out_path)
print("Checkpoint saved to {}".format(model_out_path))
def output_psnr_mse(img_orig, img_out):
squared_error = np.square(img_orig - img_out)
mse = np.mean(squared_error)
psnr = 10 * np.log10(1.0 / mse)
return psnr
if __name__ == "__main__":
main()