-
Notifications
You must be signed in to change notification settings - Fork 128
/
main_monodepth_pytorch.py
322 lines (281 loc) · 12.4 KB
/
main_monodepth_pytorch.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
import argparse
import time
import torch
import numpy as np
import torch.optim as optim
# custom modules
from loss import MonodepthLoss
from utils import get_model, to_device, prepare_dataloader
# plot params
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (15, 10)
def return_arguments():
parser = argparse.ArgumentParser(description='PyTorch Monodepth')
parser.add_argument('data_dir',
help='path to the dataset folder. \
It should contain subfolders with following structure:\
"image_02/data" for left images and \
"image_03/data" for right images'
)
parser.add_argument('val_data_dir',
help='path to the validation dataset folder. \
It should contain subfolders with following structure:\
"image_02/data" for left images and \
"image_03/data" for right images'
)
parser.add_argument('model_path', help='path to the trained model')
parser.add_argument('output_directory',
help='where save dispairities\
for tested images'
)
parser.add_argument('--input_height', type=int, help='input height',
default=256)
parser.add_argument('--input_width', type=int, help='input width',
default=512)
parser.add_argument('--model', default='resnet18_md',
help='encoder architecture: ' +
'resnet18_md or resnet50_md ' + '(default: resnet18)'
+ 'or torchvision version of any resnet model'
)
parser.add_argument('--pretrained', default=False,
help='Use weights of pretrained model'
)
parser.add_argument('--mode', default='train',
help='mode: train or test (default: train)')
parser.add_argument('--epochs', default=50,
help='number of total epochs to run')
parser.add_argument('--learning_rate', default=1e-4,
help='initial learning rate (default: 1e-4)')
parser.add_argument('--batch_size', default=256,
help='mini-batch size (default: 256)')
parser.add_argument('--adjust_lr', default=True,
help='apply learning rate decay or not\
(default: True)'
)
parser.add_argument('--device',
default='cuda:0',
help='choose cpu or cuda:0 device"'
)
parser.add_argument('--do_augmentation', default=True,
help='do augmentation of images or not')
parser.add_argument('--augment_parameters', default=[
0.8,
1.2,
0.5,
2.0,
0.8,
1.2,
],
help='lowest and highest values for gamma,\
brightness and color respectively'
)
parser.add_argument('--print_images', default=False,
help='print disparity and image\
generated from disparity on every iteration'
)
parser.add_argument('--print_weights', default=False,
help='print weights of every layer')
parser.add_argument('--input_channels', default=3,
help='Number of channels in input tensor')
parser.add_argument('--num_workers', default=4,
help='Number of workers in dataloader')
parser.add_argument('--use_multiple_gpu', default=False)
args = parser.parse_args()
return args
def adjust_learning_rate(optimizer, epoch, learning_rate):
"""Sets the learning rate to the initial LR\
decayed by 2 every 10 epochs after 30 epoches"""
if epoch >= 30 and epoch < 40:
lr = learning_rate / 2
elif epoch >= 40:
lr = learning_rate / 4
else:
lr = learning_rate
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def post_process_disparity(disp):
(_, h, w) = disp.shape
l_disp = disp[0, :, :]
r_disp = np.fliplr(disp[1, :, :])
m_disp = 0.5 * (l_disp + r_disp)
(l, _) = np.meshgrid(np.linspace(0, 1, w), np.linspace(0, 1, h))
l_mask = 1.0 - np.clip(20 * (l - 0.05), 0, 1)
r_mask = np.fliplr(l_mask)
return r_mask * l_disp + l_mask * r_disp + (1.0 - l_mask - r_mask) * m_disp
class Model:
def __init__(self, args):
self.args = args
# Set up model
self.device = args.device
self.model = get_model(args.model, input_channels=args.input_channels, pretrained=args.pretrained)
self.model = self.model.to(self.device)
if args.use_multiple_gpu:
self.model = torch.nn.DataParallel(self.model)
if args.mode == 'train':
self.loss_function = MonodepthLoss(
n=4,
SSIM_w=0.85,
disp_gradient_w=0.1, lr_w=1).to(self.device)
self.optimizer = optim.Adam(self.model.parameters(),
lr=args.learning_rate)
self.val_n_img, self.val_loader = prepare_dataloader(args.val_data_dir, args.mode,
args.augment_parameters,
False, args.batch_size,
(args.input_height, args.input_width),
args.num_workers)
else:
self.model.load_state_dict(torch.load(args.model_path))
args.augment_parameters = None
args.do_augmentation = False
args.batch_size = 1
# Load data
self.output_directory = args.output_directory
self.input_height = args.input_height
self.input_width = args.input_width
self.n_img, self.loader = prepare_dataloader(args.data_dir, args.mode, args.augment_parameters,
args.do_augmentation, args.batch_size,
(args.input_height, args.input_width),
args.num_workers)
if 'cuda' in self.device:
torch.cuda.synchronize()
def train(self):
losses = []
val_losses = []
best_loss = float('Inf')
best_val_loss = float('Inf')
running_val_loss = 0.0
self.model.eval()
for data in self.val_loader:
data = to_device(data, self.device)
left = data['left_image']
right = data['right_image']
disps = self.model(left)
loss = self.loss_function(disps, [left, right])
val_losses.append(loss.item())
running_val_loss += loss.item()
running_val_loss /= self.val_n_img / self.args.batch_size
print('Val_loss:', running_val_loss)
for epoch in range(self.args.epochs):
if self.args.adjust_lr:
adjust_learning_rate(self.optimizer, epoch,
self.args.learning_rate)
c_time = time.time()
running_loss = 0.0
self.model.train()
for data in self.loader:
# Load data
data = to_device(data, self.device)
left = data['left_image']
right = data['right_image']
# One optimization iteration
self.optimizer.zero_grad()
disps = self.model(left)
loss = self.loss_function(disps, [left, right])
loss.backward()
self.optimizer.step()
losses.append(loss.item())
# Print statistics
if self.args.print_weights:
j = 1
for (name, parameter) in self.model.named_parameters():
if name.split(sep='.')[-1] == 'weight':
plt.subplot(5, 9, j)
plt.hist(parameter.data.view(-1))
plt.xlim([-1, 1])
plt.title(name.split(sep='.')[0])
j += 1
plt.show()
if self.args.print_images:
print('disp_left_est[0]')
plt.imshow(np.squeeze(
np.transpose(self.loss_function.disp_left_est[0][0,
:, :, :].cpu().detach().numpy(),
(1, 2, 0))))
plt.show()
print('left_est[0]')
plt.imshow(np.transpose(self.loss_function\
.left_est[0][0, :, :, :].cpu().detach().numpy(),
(1, 2, 0)))
plt.show()
print('disp_right_est[0]')
plt.imshow(np.squeeze(
np.transpose(self.loss_function.disp_right_est[0][0,
:, :, :].cpu().detach().numpy(),
(1, 2, 0))))
plt.show()
print('right_est[0]')
plt.imshow(np.transpose(self.loss_function.right_est[0][0,
:, :, :].cpu().detach().numpy(), (1, 2,
0)))
plt.show()
running_loss += loss.item()
running_val_loss = 0.0
self.model.eval()
for data in self.val_loader:
data = to_device(data, self.device)
left = data['left_image']
right = data['right_image']
disps = self.model(left)
loss = self.loss_function(disps, [left, right])
val_losses.append(loss.item())
running_val_loss += loss.item()
# Estimate loss per image
running_loss /= self.n_img / self.args.batch_size
running_val_loss /= self.val_n_img / self.args.batch_size
print (
'Epoch:',
epoch + 1,
'train_loss:',
running_loss,
'val_loss:',
running_val_loss,
'time:',
round(time.time() - c_time, 3),
's',
)
self.save(self.args.model_path[:-4] + '_last.pth')
if running_val_loss < best_val_loss:
self.save(self.args.model_path[:-4] + '_cpt.pth')
best_val_loss = running_val_loss
print('Model_saved')
print ('Finished Training. Best loss:', best_loss)
self.save(self.args.model_path)
def save(self, path):
torch.save(self.model.state_dict(), path)
def load(self, path):
self.model.load_state_dict(torch.load(path))
def test(self):
self.model.eval()
disparities = np.zeros((self.n_img,
self.input_height, self.input_width),
dtype=np.float32)
disparities_pp = np.zeros((self.n_img,
self.input_height, self.input_width),
dtype=np.float32)
with torch.no_grad():
for (i, data) in enumerate(self.loader):
# Get the inputs
data = to_device(data, self.device)
left = data.squeeze()
# Do a forward pass
disps = self.model(left)
disp = disps[0][:, 0, :, :].unsqueeze(1)
disparities[i] = disp[0].squeeze().cpu().numpy()
disparities_pp[i] = \
post_process_disparity(disps[0][:, 0, :, :]\
.cpu().numpy())
np.save(self.output_directory + '/disparities.npy', disparities)
np.save(self.output_directory + '/disparities_pp.npy',
disparities_pp)
print('Finished Testing')
def main(args):
args = return_arguments()
if args.mode == 'train':
model = Model(args)
model.train()
elif args.mode == 'test':
model_test = Model(args)
model_test.test()
if __name__ == '__main__':
main()