-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain.py
213 lines (184 loc) · 10.2 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
import argparse
from model.SPFNet import *
from model.common import *
from data.nyu_dataloader import *
from data.rgbdd_dataloader import *
from data.tofdsr_dataloader import *
from utils import calc_rmse, rgbdd_calc_rmse, tofdsr_calc_rmse
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import torch.optim as optim
import torch.nn as nn
import torch.optim.lr_scheduler as lr_scheduler
from tqdm import tqdm
import logging
from datetime import datetime
import os
parser = argparse.ArgumentParser()
parser.add_argument('--scale', type=int, default=4, help='scale factor')
parser.add_argument('--lr', default='0.0001', type=float, help='learning rate')
parser.add_argument('--result', default='experiment', help='learning rate')
parser.add_argument('--epoch', default=200, type=int, help='max epoch')
parser.add_argument('--device', default="0", type=str, help='which gpu use')
parser.add_argument("--decay_iterations", type=list, default=[5e4, 1e5, 2e5], help="steps to start lr decay")
parser.add_argument("--num_feats", type=int, default=42, help="channel number of the middle hidden layer")
parser.add_argument("--gamma", type=float, default=0.2, help="decay rate of learning rate")
parser.add_argument("--root_dir", type=str, default='./dataset/NYU-v2', help="root dir of dataset")
parser.add_argument("--batchsize", type=int, default=1, help="batchsize of training dataloader")
parser.add_argument('--tiny_model', action='store_true', help='tiny model')
opt = parser.parse_args()
print(opt)
os.environ["CUDA_VISIBLE_DEVICES"] = opt.device
s = datetime.now().strftime('%Y%m%d%H%M%S')
dataset_name = opt.root_dir.split('/')[-1]
result_root = '%s/%s-lr_%s-s_%s-%s-b_%s' % (opt.result, s, opt.lr, opt.scale, dataset_name, opt.batchsize)
if not os.path.exists(result_root):
os.mkdir(result_root)
logging.basicConfig(filename='%s/train.log' % result_root, format='%(asctime)s %(message)s', level=logging.INFO)
logging.info(opt)
net = SPFNet(num_feats=opt.num_feats, kernel_size=3, scale=opt.scale, reduction=4, tiny_model=opt.tiny_model).cuda()
criterion = nn.L1Loss()
optimizer = optim.Adam(net.parameters(), lr=opt.lr)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=opt.decay_iterations, gamma=opt.gamma)
net.train()
data_transform = transforms.Compose([transforms.ToTensor()])
up = nn.Upsample(scale_factor=opt.scale, mode='bicubic')
if dataset_name == 'NYU-v2':
test_minmax = np.load('%s/test_minmax.npy' % opt.root_dir)
train_dataset = NYU_v2_datset(root_dir=opt.root_dir, scale=opt.scale, transform=data_transform, train=True)
test_dataset = NYU_v2_datset(root_dir=opt.root_dir, scale=opt.scale, transform=data_transform, train=False)
if dataset_name == 'RGB-D-D':
train_dataset = RGBDD_Dataset(root_dir=opt.root_dir, scale=opt.scale, downsample='real', train=True, transform=data_transform)
test_dataset = RGBDD_Dataset(root_dir=opt.root_dir, scale=opt.scale, downsample='real', train=False, transform=data_transform)
if dataset_name == 'TOFDSR':
train_dataset = TOFDSR_Dataset(root_dir=opt.root_dir, scale=opt.scale, downsample='real', train=True,
txt_file="./data/TOFDSR_Filled_Train.txt", transform=data_transform)
test_dataset = TOFDSR_Dataset(root_dir=opt.root_dir, scale=opt.scale, downsample='real', train=False,
txt_file="./data/TOFDSR_Filled_Test.txt", transform=data_transform)
train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=opt.batchsize, shuffle=True, num_workers=8)
test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=8)
max_epoch = opt.epoch
num_train = len(train_dataloader)
best_rmse = 10.0
best_epoch = 0
for epoch in range(max_epoch):
# ---------
# Training
# ---------
net.train()
running_loss = 0.0
t = tqdm(iter(train_dataloader), leave=True, total=len(train_dataloader))
for idx, data in enumerate(t):
batches_done = num_train * epoch + idx
optimizer.zero_grad()
guidance, lr, gt, seg, ns = data['guidance'].cuda(), data['lr'].cuda(), data['gt'].cuda(), data[
'seg'].cuda(), data['ns'].cuda()
out = net((guidance, lr, seg,ns))
if dataset_name == 'TOFDSR':
mask = (gt >= 0.02) & (gt <= 1)
gt = gt[mask]
out = out[mask]
loss = criterion(out, gt)
loss.backward()
optimizer.step()
scheduler.step()
running_loss += loss.data.item()
running_loss_50 = running_loss
if idx % 50 == 0:
running_loss_50 /= 50
t.set_description('[train epoch:%d] loss: %.8f' % (epoch + 1, running_loss_50))
t.refresh()
logging.info('epoch:%d iteration:%d running_loss:%.10f' % (epoch + 1, batches_done + 1, running_loss / num_train))
# -----------
# Validating
# -----------
if (epoch % 2 == 0) and (epoch < 30):
with torch.no_grad():
net.eval()
if dataset_name == 'NYU-v2':
rmse = np.zeros(449)
if dataset_name == 'RGB-D-D':
rmse = np.zeros(405)
if dataset_name == 'TOFDSR':
rmse = np.zeros(560)
t = tqdm(iter(test_dataloader), leave=True, total=len(test_dataloader))
for idx, data in enumerate(t):
if dataset_name == 'NYU-v2':
guidance, lr, gt, seg, ns = data['guidance'].cuda(), data['lr'].cuda(), data['gt'].cuda(), data[
'seg'].cuda(), data['ns'].cuda()
out = net((guidance, lr, seg, ns))
minmax = test_minmax[:, idx]
minmax = torch.from_numpy(minmax).cuda()
rmse[idx] = calc_rmse(gt[0, 0], out[0, 0], minmax)
if dataset_name == 'RGB-D-D':
guidance, lr, gt, seg, ns, max, min = data['guidance'].cuda(), data['lr'].cuda(), data['gt'].cuda(), data['seg'].cuda(), data['ns'].cuda(), data[
'max'].cuda(), data['min'].cuda()
out = net((guidance, lr, seg, ns))
minmax = [max, min]
rmse[idx] = rgbdd_calc_rmse(gt[0, 0], out[0, 0], minmax)
if dataset_name == 'TOFDSR':
guidance, lr, gt, seg, ns, max, min = data['guidance'].cuda(), data['lr'].cuda(), data['gt'].cuda(), \
data['seg'].cuda(), data['ns'].cuda(), data[
'max'].cuda(), data['min'].cuda()
out = net((guidance, lr, seg, ns))
minmax = [max, min]
rmse[idx] = tofdsr_calc_rmse(gt[0, 0], out[0, 0], minmax)
t.set_description('[validate] rmse: %f' % rmse[:idx + 1].mean())
t.refresh()
r_mean = rmse.mean()
if r_mean < best_rmse:
best_rmse = r_mean
best_epoch = epoch
torch.save(net.state_dict(),
os.path.join(result_root, "modelbest%f_8%d.pth" % (best_rmse, best_epoch + 1)))
logging.info(
'---------------------------------------------------------------------------------------------------------------------------')
logging.info('epoch:%d lr:%f-------mean_rmse:%f (BEST: %f @epoch%d)' % (
epoch + 1, scheduler.get_last_lr()[0], r_mean, best_rmse, best_epoch + 1))
logging.info(
'---------------------------------------------------------------------------------------------------------------------------')
elif epoch >= 30:
with torch.no_grad():
net.eval()
if dataset_name == 'NYU-v2':
rmse = np.zeros(449)
if dataset_name == 'RGB-D-D':
rmse = np.zeros(405)
if dataset_name == 'TOFDSR':
rmse = np.zeros(560)
t = tqdm(iter(test_dataloader), leave=True, total=len(test_dataloader))
for idx, data in enumerate(t):
if dataset_name == 'NYU-v2_Our':
guidance, lr, gt, seg, ns = data['guidance'].cuda(), data['lr'].cuda(), data['gt'].cuda(), data[
'seg'].cuda(), data['ns'].cuda()
out = net((guidance, lr, seg, ns))
minmax = test_minmax[:, idx]
minmax = torch.from_numpy(minmax).cuda()
rmse[idx] = calc_rmse(gt[0, 0], out[0, 0], minmax)
if dataset_name == 'RGB-D-D':
guidance, lr, gt, seg, ns, max, min = data['guidance'].cuda(), data['lr'].cuda(), data['gt'].cuda(), data['seg'].cuda(), data['ns'].cuda(), data[
'max'].cuda(), data['min'].cuda()
out = net((guidance, lr, seg, ns))
minmax = [max, min]
rmse[idx] = rgbdd_calc_rmse(gt[0, 0], out[0, 0], minmax)
if dataset_name == 'TOFDSR':
guidance, lr, gt, seg, ns, max, min = data['guidance'].cuda(), data['lr'].cuda(), data['gt'].cuda(), \
data['seg'].cuda(), data['ns'].cuda(), data[
'max'].cuda(), data['min'].cuda()
out = net((guidance, lr, seg, ns))
minmax = [max, min]
rmse[idx] = tofdsr_calc_rmse(gt[0, 0], out[0, 0], minmax)
t.set_description('[validate] rmse: %f' % rmse[:idx + 1].mean())
t.refresh()
r_mean = rmse.mean()
if r_mean < best_rmse:
best_rmse = r_mean
best_epoch = epoch
torch.save(net.state_dict(),
os.path.join(result_root, "modelRmse%f_8%d.pth" % (r_mean, epoch + 1)))
logging.info(
'---------------------------------------------------------------------------------------------------------------------------')
logging.info('epoch:%d lr:%f-------mean_rmse:%f (BEST: %f @epoch%d)' % (
epoch + 1, scheduler.get_last_lr()[0], r_mean, best_rmse, best_epoch + 1))
logging.info(
'---------------------------------------------------------------------------------------------------------------------------')