-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
198 lines (161 loc) · 7.21 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
from __future__ import print_function
import argparse
import os
import sys
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
from model import DTTDnet
cudnn.benchmark = True
cudnn.fastest = True
import torch.optim as optim
import torch.utils.data
import torchvision.utils as vutils
from model.DTTDnet import DenseNet_pytorch
from torch.autograd import Variable
from util import *
from util import lr_scheduler as lrs
from tqdm import tqdm
from model import myloss
from dataloader import NYUdata_loader as dataloader
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', required=False,
default='SCIE', help='')
parser.add_argument('--dataroot', required=False,
default='', help='path to trn dataset')
parser.add_argument('--valDataroot', required=False,
default='', help='path to val dataset')
parser.add_argument('--nesterov', '-n', action='store_true', help='enables Nesterov momentum')
parser.add_argument('--mode', type=str, default='B2A', help='B2A: facade, A2B: edges2shoes')
parser.add_argument('--batchSize', type=int, default=16, help='input batch size')
parser.add_argument('--valBatchSize', type=int, default=16, help='input batch size')
parser.add_argument('--originalSize', type=int,
default=256, help='the height / width of the original input image')
parser.add_argument('--imageSize', type=int,
default=256, help='the height / width of the cropped input image to network')
parser.add_argument('--inputChannelSize', type=int,
default=3, help='size of the input channels')
parser.add_argument('--outputChannelSize', type=int,
default=3, help='size of the output channels')
parser.add_argument('--ngf', type=int, default=64)
parser.add_argument('--ndf', type=int, default=64)
parser.add_argument('--niter', type=int, default=60, help='number of epochs to train for')
parser.add_argument('--lrD', type=float, default=0.0002, help='learning rate, default=0.0002')
parser.add_argument('--lrG', type=float, default=0.0002, help='learning rate, default=0.0002')
parser.add_argument('--annealStart', type=int, default=0, help='annealing learning rate start to')
parser.add_argument('--annealEvery', type=int, default=400, help='epoch to reaching at learning rate of 0')
parser.add_argument('--poolSize', type=int, default=50,
help='Buffer size for storing previously generated samples from G')
parser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam')
parser.add_argument('--netDetail', default='/home/ubuntu/PycharmProjects/Pytorch_ECTDnet/sample/DetailNetnet_epoch_26.pth', help="path to netG (to continue training)")
parser.add_argument('--netL', default='', help="path to netD (to continue training)")
parser.add_argument('--workers', type=int, help='number of data loading workers', default=1)
parser.add_argument('--exp', default='sample', help='folder to output images and model checkpoints')
parser.add_argument('--evalIter', type=int, default=50,
help='interval for evauating(generating) images from valDataroot')
opt = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES']= '0'
print(opt)
os.makedirs(opt.exp)
# 设置随机种子
opt.manualSeed = random.randint(1, 10000)
# opt.manualSeed = 101
random.seed(opt.manualSeed)
torch.manual_seed(opt.manualSeed)
torch.cuda.manual_seed_all(opt.manualSeed)
print("Random Seed: ", opt.manualSeed)
opt.workers = 1
# get logger
# window:
trainLogger = open('%s/train.log' % opt.exp, 'w')
whatnet = 'DetailNet'
ngf = opt.ngf
ndf = opt.ndf
inputChannelSize = opt.inputChannelSize
outputChannelSize = opt.outputChannelSize
opt.netDetail = ''
net = DenseNet_pytorch(inputChannelSize,outputChannelSize)
print(opt.netDetail)
print(net)
use_cuda = torch.cuda.is_available()
if use_cuda:
net.cuda()
assert torch.cuda.device_count() == 1, 'only support single gpu'
net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count()))
cudnn.benchmark = True
trainset = dataloader.NYUDataset(root_dir='',split='train')
valset = dataloader.NYUDataset(root_dir='',split='val')
trainloader = torch.utils.data.DataLoader(trainset,
batch_size=opt.batchSize,
shuffle=True,
num_workers=2,
pin_memory=True,
drop_last=True)
valloader = torch.utils.data.DataLoader(valset,
batch_size=opt.valBatchSize,
shuffle=False,
num_workers=2,
pin_memory=True,
drop_last=True)
MAE = nn.L1Loss()
depthloss = myloss.depthloss()
depthloss = depthloss.cuda()
MAE.cuda()
# get optimizer
optimizer = optim.Adam(net.parameters(), lr=opt.lrG, betas=(opt.beta1, 0.999), weight_decay=0.00005)
scheduler = lrs.ReduceLROnPlateau(optimizer, 'min') # set up scheduler
matlabpath = 'C:\Program Files\Polyspace\R2019a'
def train(epoch):
net.train()
depthloss_a = 0.0
tranloss_a = 0.0
tbar = tqdm(trainloader)
for i, sample in enumerate(tbar):
[haze, depth] = [sample['haze'], sample['depth']]
if use_cuda:
haze, depth,tran = haze.cuda(), depth.cuda()
optimizer.zero_grad()
haze, depth = Variable(haze), Variable(depth)
depth_p,tran_p = net(haze)
depth_loss = depthloss(depth, depth_p)
depth_loss.backward(retain_graph=True)
trainLogger.write('%d\t%d\t depthloss:%f ,\t\n ' % (epoch, i, depthloss.item()))
trainLogger.flush()
optimizer.step()
depthloss_a += depthloss.item()
error_str = 'Epoch: %d, depth_loss=%.4f' % (epoch, depthloss_a / (i + 1))
tbar.set_description(error_str)
if epoch % 2 == 0:
torch.save(net.module.state_dict(), '%s/Densedepth_NET%d.pth' % (opt.exp, epoch))
def val(epoch):
net.eval()
eval_loss = 0.0
tran_loss = 0.0
# NOTE valing loop
count = 0
tbar = tqdm(valloader)
for i, sample in enumerate(tbar):
[haze, depth] = [sample['haze'], sample['depth']]
with torch.no_grad():
if use_cuda:
haze, depth = haze.cuda(), depth.cuda()
haze, depth = Variable(haze,volatile=True), Variable(depth)
depth_p,tran_p = net(haze)
depth_loss = depthloss(depth, depth_p)
loss = depth_loss.data.cpu()
eval_loss += loss.item()
count = count+1
error_str = 'Epoch: %d, depth_val_loss=%.4f' % (epoch, eval_loss / (i + 1))
tbar.set_description(error_str)
# print(count)
avg_loss = eval_loss/(count*opt.valBatchSize)
scheduler.step(avg_loss, epoch)
def train_val():
for epoch in range(opt.niter):
train(epoch)
val(epoch)
trainLogger.close()
if __name__ == '__main__':
train_val()