-
Notifications
You must be signed in to change notification settings - Fork 16
/
train.py
210 lines (169 loc) · 7.57 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
# coding=utf-8
import argparse
import os
import urllib.request
import numpy as np
import torch
from torch import nn, optim
from torch.backends import cudnn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torchvision.transforms import Compose, ToTensor, Resize, Normalize, CenterCrop, RandomCrop
from tensorboardX import SummaryWriter
from torchvision.utils import make_grid
from data import DatasetFromFolder
from model.rpnet import Net
from utils import save_checkpoint
# Training settings
parser = argparse.ArgumentParser(description="PyTorch DeepDehazing")
parser.add_argument("--tag", type=str, help="tag for this training")
parser.add_argument("--rb", type=int, default=18, help="number of residual blocks")
parser.add_argument("--train", default="../datasets/IndoorTrain/", type=str,
help="path to load train datasets(default: none)")
parser.add_argument("--test", default="../datasets/IndoorTest/", type=str,
help="path to load test datasets(default: none)")
parser.add_argument("--batchSize", type=int, default=64, help="training batch size")
parser.add_argument("--nEpochs", type=int, default=300, help="number of epochs to train for")
parser.add_argument("--lr", type=float, default=0.0001, help="Learning Rate. Default=1e-4")
parser.add_argument("--step", type=int, default=2000, help="step to test the model performance. Default=2000")
parser.add_argument("--cuda", action="store_true", help="Use cuda?")
parser.add_argument("--gpus", type=int, default=4, help="nums of gpu to use")
parser.add_argument("--resume", default="", type=str, help="Path to checkpoint (default: none)")
parser.add_argument("--start-epoch", default=1, type=int, help="Manual epoch number (useful on restarts)")
parser.add_argument("--threads", type=int, default=8, help="Number of threads for data loader to use, Default: 1")
parser.add_argument("--momentum", default=0.9, type=float, help="Momentum, Default: 0.9")
parser.add_argument("--pretrained", default="", type=str, help="path to pretrained model (default: none)")
parser.add_argument("--report", default=False, type=bool, help="report to wechat")
def main():
global opt, name, logger, model, criterion
opt = parser.parse_args()
print(opt)
# Tag_ResidualBlocks_BatchSize
name = "%s_%d_%d" % (opt.tag, opt.rb, opt.batchSize)
logger = SummaryWriter("runs/" + name)
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 = DatasetFromFolder(opt.train, transform=Compose([
ToTensor()
]))
indoor_test_dataset = DatasetFromFolder(opt.test, transform=Compose([
ToTensor()
]))
training_data_loader = DataLoader(dataset=train_dataset, num_workers=opt.threads, batch_size=opt.batchSize,
pin_memory=True, shuffle=True)
indoor_test_loader = DataLoader(dataset=indoor_test_dataset, num_workers=opt.threads, batch_size=1, pin_memory=True,
shuffle=True)
print("==========> Building model")
model = Net(opt.rb)
criterion = nn.MSELoss(size_average=True)
print(model)
# 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"] // 2 + 1
model.load_state_dict(checkpoint["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['state_dict'].state_dict())
else:
print("=> no model found at '{}'".format(opt.pretrained))
print("==========> Setting GPU")
if cuda:
model = nn.DataParallel(model, device_ids=[i for i in range(opt.gpus)]).cuda()
criterion = criterion.cuda()
else:
model = model.cpu()
criterion = criterion.cpu()
print("==========> Setting Optimizer")
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=opt.lr)
print("==========> Training")
for epoch in range(opt.start_epoch, opt.nEpochs + 1):
train(training_data_loader, indoor_test_loader, optimizer, epoch)
save_checkpoint(model, epoch, name)
# test(indoor_test_loader, epoch)
def train(training_data_loader, indoor_test_loader, optimizer, epoch):
print("epoch =", epoch, "lr =", optimizer.param_groups[0]["lr"])
for iteration, batch in enumerate(training_data_loader, 1):
model.train()
model.zero_grad()
optimizer.zero_grad()
steps = len(training_data_loader) * (epoch-1) + iteration
data, label = \
Variable(batch[0]), \
Variable(batch[1], requires_grad=False)
if opt.cuda:
data = data.cuda()
label = label.cuda()
else:
data = data.cpu()
label = label.cpu()
output = model(data)
# loss = criterion(output, label) / (data.size()[0]*2)
loss = criterion(output, label)
loss.backward()
# torch.nn.utils.clip_grad_norm(model.parameters(), 0.1)
optimizer.step()
if iteration % 10 == 0:
print("===> Epoch[{}]({}/{}): Loss: {:.6f}".format(epoch, iteration, len(training_data_loader),
loss.data[0]))
logger.add_scalar('loss', loss.data[0], steps)
if iteration % opt.step == 0:
data_temp = make_grid(data.data)
label_temp = make_grid(label.data)
output_temp = make_grid(output.data)
logger.add_image('data_temp', data_temp, steps)
logger.add_image('label_temp', label_temp, steps)
logger.add_image('output_temp', output_temp, steps)
def test(test_data_loader, epoch):
psnrs = []
mses = []
for iteration, batch in enumerate(test_data_loader, 1):
model.eval()
data, label = \
Variable(batch[0], volatile=True), \
Variable(batch[1])
if opt.cuda:
data = data.cuda()
label = label.cuda()
else:
data = data.cpu()
label = label.cpu()
with torch.no_grad():
output = model(data)
output = torch.clamp(output, 0., 1.)
mse = nn.MSELoss()(output, label)
mses.append(mse.data[0])
psnr = 10 * np.log10(1.0 / mse.data[0])
psnrs.append(psnr)
psnr_mean = np.mean(psnrs)
mse_mean = np.mean(mses)
print("Vaild epoch %d psnr: %f" % (epoch, psnr_mean))
logger.add_scalar('psnr', psnr_mean, epoch)
logger.add_scalar('mse', mse_mean, epoch)
data = make_grid(data.data)
label = make_grid(label.data)
output = make_grid(output.data)
logger.add_image('data', data, epoch)
logger.add_image('label', label, epoch)
logger.add_image('output', output, epoch)
if opt.report:
urllib.request.urlopen(
"https://sc.ftqq.com/SCU21303T3ae6f3b60b71841d0def9295e4a500905a7524916a85c.send?text=epoch_{}_loss_{}".format(
epoch, psnr_mean))
if __name__ == "__main__":
os.system('clear')
main()