-
Notifications
You must be signed in to change notification settings - Fork 54
/
trainer.py
executable file
·313 lines (236 loc) · 12.1 KB
/
trainer.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
# Copyright Niantic 2020. Patent Pending. All rights reserved.
#
# This software is licensed under the terms of the Stereo-from-mono licence
# which allows for non-commercial use only, the full terms of which are made
# available in the LICENSE file.
import os
from collections import defaultdict
import json
import time
from collections import defaultdict
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"
os.environ["OMP_NUM_THREADS"] = "1"
import numpy as np
from skimage.filters import prewitt_h
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, ConcatDataset
from tensorboardX import SummaryWriter
import torch.nn.functional as F
from datasets import SceneFlowDataset, MSCOCODataset, ADE20KDataset, \
KITTIStereoDataset, DIWDataset, DiodeDataset, MapillaryDataset
from model_manager import ModelManager
from utils import readlines, normalise_image, MyRandomSampler, load_config
dataset_lookup = {'mscoco': MSCOCODataset,
'ADE20K': ADE20KDataset,
'sceneflow': SceneFlowDataset,
'diw': DIWDataset,
'diode': DiodeDataset,
'mapillary': MapillaryDataset,
'kitti2015': KITTIStereoDataset
}
class TrainManager:
"""
Main training script called from main.py.
"""
def __init__(self, options):
print('---------------')
print('setting up...')
self.opt = options
# Create network and optimiser
self.model_manager = ModelManager(self.opt)
if self.opt.load_path is not None:
self.model_manager.load_model(weights_path=self.opt.load_path, load_optimiser=False)
# extract model, optimiser and scheduler for easier access
self.model = self.model_manager.model
self.optimiser = self.model_manager.optimiser
self.scheduler = self.model_manager.scheduler
self.scales = self.model_manager.scales
print('models done!')
path_info = load_config(self.opt.config_path)
train_datasets = []
val_datasets = []
for dataset_type in self.opt.training_datasets:
dataset_path = path_info[dataset_type]
train_filenames = readlines(os.path.join('splits', dataset_type, 'train_files_all.txt'))
val_filenames = 'val_files_all.txt' if dataset_type != 'sceneflow' else 'test_files.txt'
val_filenames = readlines(os.path.join('splits', dataset_type, val_filenames))
dataset_class = dataset_lookup[dataset_type]
# subsample data optionally
if self.opt.data_sampling != 1.0:
sampling = self.opt.data_sampling
assert sampling > 0
assert sampling < 1.0
train_filenames = list(np.random.choice(np.array(train_filenames),
int(sampling * len(train_filenames)),
replace=False))
train_dataset = dataset_class(dataset_path,
train_filenames, self.opt.height,
self.opt.width, is_train=True,
disable_normalisation=self.opt.disable_normalisation,
max_disparity=self.opt.max_disparity,
keep_aspect_ratio=True,
disable_synthetic_augmentation=
self.opt.disable_synthetic_augmentation,
disable_sharpening=self.opt.disable_sharpening,
monodepth_model=self.opt.monodepth_model,
disable_background=self.opt.disable_background
)
val_dataset = dataset_class(dataset_path, val_filenames,
self.opt.height,
self.opt.width, is_train=False,
disable_normalisation=self.opt.disable_normalisation,
max_disparity=self.opt.max_disparity,
keep_aspect_ratio=True,
disable_synthetic_augmentation=
self.opt.disable_synthetic_augmentation,
disable_sharpening=self.opt.disable_sharpening,
monodepth_model=self.opt.monodepth_model,
disable_background=self.opt.disable_background
)
train_datasets.append(train_dataset)
val_datasets.append(val_dataset)
self.train_dataset = ConcatDataset(train_datasets)
self.val_dataset = ConcatDataset(val_datasets)
# use custom sampler so we can continue from specific step
my_sampler = MyRandomSampler(self.train_dataset, start_step=self.opt.start_step)
self.train_loader = DataLoader(self.train_dataset, sampler=my_sampler, drop_last=True,
num_workers=self.opt.num_workers,
batch_size=self.opt.batch_size)
self.val_loader = DataLoader(self.val_dataset, shuffle=True, drop_last=True,
num_workers=1,
batch_size=self.opt.batch_size)
self.val_iter = iter(self.val_loader)
print('datasets done!')
print('dataset info:')
print('training on {} images, validating on {} images'.format(len(self.train_dataset),
len(self.val_dataset)))
# Set up tensorboard writers and logger
self.train_writer = SummaryWriter(os.path.join(self.opt.log_path,
self.opt.model_name, 'train'))
self.val_writer = SummaryWriter(os.path.join(self.opt.log_path,
self.opt.model_name, 'val'))
os.makedirs(self.opt.log_path, exist_ok=True)
self.step = 0
self.epoch = 0
self.training_complete = False
print('training setup complete!')
print('---------------')
def train(self):
print('training...')
while not self.training_complete:
self.run_epoch()
self.epoch += 1
print('training complete!')
def run_epoch(self):
if self.step < self.opt.start_step:
print('skipping up to step {}'.format(self.opt.start_step))
for idx, inputs in enumerate(self.train_loader):
start_time = time.time()
outputs, losses = self.process_batch(inputs, compute_loss=True)
# Update weights
loss = losses['loss']
self.model.zero_grad()
loss.backward()
self.optimiser.step()
for group in self.optimiser.param_groups:
self.lr = group['lr']
print('step {} - time {}'.format(self.step, round(time.time() - start_time, 3)))
# validate and log
if self.step % self.opt.log_freq == 0:
self.log(self.train_writer, inputs, outputs, losses)
self.model.eval()
self.val()
self.model.train()
if self.step % 10000 == 0:
self.model_manager.save_model(folder_name='weights_{}'.format(self.step))
self.step += 1
if self.step >= self.opt.training_steps:
self.training_complete = True
break
print('Epoch {} complete!'.format(self.epoch))
self.model_manager.save_model(folder_name='weights_{}'.format(self.step))
self.scheduler.step()
def val(self):
with torch.no_grad():
try:
inputs = self.val_iter.next()
except StopIteration:
self.val_iter = iter(self.val_loader)
inputs = self.val_iter.next()
outputs, losses = self.process_batch(inputs, compute_loss=True)
self.log(self.val_writer, inputs, outputs, losses)
def process_batch(self, inputs, compute_loss=False):
# move to GPU
if torch.cuda.is_available():
for key, val in inputs.items():
inputs[key] = val.cuda()
outputs = self.model(inputs['image'], inputs['stereo_image'])
for scale in range(self.scales):
# upsample to full resolution
pred = F.interpolate(outputs[('raw', scale)], mode='bilinear',
size=(self.opt.height, self.opt.width),
align_corners=True)
pred_disp = pred[:, 0]
outputs[('disp', scale)] = pred_disp
# get losses
if compute_loss:
losses = self.compute_losses(inputs, outputs)
else:
losses = {}
return outputs, losses
def compute_losses(self, inputs, outputs):
losses = {}
total_loss = 0
for scale in range(self.scales):
pred_disp = outputs[('disp', scale)]
# compute loss on disparity
target_disp = torch.clamp(inputs['disparity'], max=self.opt.max_disparity)
disparity_loss = (torch.abs(pred_disp - target_disp) * (target_disp > 0).float()).mean()
total_loss += disparity_loss
losses['disp_loss/{}'.format(scale)] = disparity_loss
total_loss /= self.scales
losses['loss'] = total_loss
return losses
def warp_stereo_image(self, stereo_image, disparity):
"""Note - for logging only"""
height, width = disparity.shape
xs, ys = np.meshgrid(range(width), range(height))
xs, ys = torch.from_numpy(xs).float(), torch.from_numpy(ys).float()
xs = xs - disparity
xs = ((xs / (width - 1)) - 0.5) * 2
ys = ((ys / (height - 1)) - 0.5) * 2
sample_pix = torch.stack([xs, ys], 2)
warped_image = F.grid_sample(stereo_image.unsqueeze(0), sample_pix.unsqueeze(0),
padding_mode='border', align_corners=True)
return warped_image[0]
def log(self, writer, inputs, outputs, losses):
print('logging')
writer.add_scalar('lr', self.lr, self.step)
# write to tensorboard
for loss_type, loss in losses.items():
writer.add_scalar('{}'.format(loss_type), loss, self.step)
for i in range(min(4, len(inputs['image']))):
writer.add_image('image_l/{}'.format(i), normalise_image(inputs['image'][i]), self.step)
writer.add_image('image_r/{}'.format(i), normalise_image(inputs['stereo_image'][i]), self.step)
if inputs.get('disparity') is not None:
writer.add_image('disp_target/{}'.format(i), normalise_image(inputs['disparity'][i]),
self.step)
warped_image = self.warp_stereo_image(inputs['stereo_image'][i].cpu(),
inputs['disparity'][i].cpu())
writer.add_image('warped_gt_image/{}'.format(i), normalise_image(warped_image),
self.step)
if inputs.get('mono_disparity') is not None:
writer.add_image('mono_disparity/{}'.format(i),
normalise_image(inputs['mono_disparity'][i]),
self.step)
if inputs.get('occlusion_mask') is not None:
writer.add_image('occlusion_mask/{}'.format(i),
normalise_image(inputs['occlusion_mask'][i]),
self.step)
writer.add_image('disp_pred/{}'.format(i), normalise_image(outputs[('disp', 0)][i]),
self.step)
warped_image = self.warp_stereo_image(inputs['stereo_image'][i].cpu(), outputs[('disp', 0)][i].cpu())
writer.add_image('warped_image/{}'.format(i), normalise_image(warped_image),
self.step)