-
Notifications
You must be signed in to change notification settings - Fork 7
/
train_single_task.py
333 lines (268 loc) · 14.8 KB
/
train_single_task.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
322
323
324
325
326
327
328
329
330
331
332
333
import pdb
import torch
import time
import argparse
import os
import sys
import logging
from loss.coord import get_cam_mat, scene_coords_regression_loss
from loss.depth import depth_regression_loss
from loss.normal import normal_regression_loss
from loss.semantics import semantics_classification_loss, CrossEntropyLoss2d
from utils.learning import get_pixel_grid, get_nodata_value, set_random_seed, config_dataloader, config_network
from utils.io import read_training_log, config_log
PROJECT_DIR = os.path.abspath(os.path.join(__file__, '..'))
# sys.path.insert(0, PROJECT_DIR)
def _config_parser():
"""
Task specific argument parser
"""
parser = argparse.ArgumentParser(
description='Initialize a scene coordinate regression network.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
"""General training parameter"""
# Dataset and dataloader
parser.add_argument('scene', help='name of a scene in the dataset folder')
parser.add_argument('--batch_size', type=int, default=4,
help='batch size of the dataloader.')
parser.add_argument('--grayscale', '-grayscale', action='store_true',
help='use grayscale image as model input')
parser.add_argument('--real_data_domain', type=str, default='in_place',
help="to select the domain of pairwise sim-to-real data, i.e., in_place or out_of_place")
parser.add_argument('--real_data_chunk', type=float, default=1.0,
help='to chunk the pairwise sim-to-real data with given proportion')
parser.add_argument('--real_only', action='store_true',
help='to use real data only')
parser.add_argument('--sim_data_chunk', type=float, default=1.0,
help='to chunk the task-agnostic LHS synthetic data with given proportion')
parser.add_argument('--task', type=str, required=True,
help='specify the single regression task, should be "coord", "depth", "normal" or "semantics"')
parser.add_argument('--epoch_plus', '-epoch_plus', action='store_true',
help='extend training by epochs, a already well-trained model w/ the same configurations must '
'be found (except for the epochs).')
# Network structure
parser.add_argument('--network_in', type=str, default=None,
help='file name of a network initialized for the scene')
parser.add_argument('--tiny', '-tiny', action='store_true',
help='train a model with massively reduced capacity for a low memory footprint.')
parser.add_argument('--fullsize', '-fullsize', action='store_true',
help='to output fillsize prediction w/o down-sampling.')
# Optimizer
parser.add_argument('--epochs', '-e', type=int, default=50,
help='number of training iterations, i.e. number of model updates')
parser.add_argument('--learningrate', '-lr', type=float, default=0.0002,
help='learning rate')
parser.add_argument('--no_lr_scheduling', action='store_true',
help='To disable learning rate scheduler.')
"""I/O parameters"""
parser.add_argument('--session', '-sid', default='',
help='custom session name appended to output files, useful to separate different runs of a script')
parser.add_argument('--ckpt_dir', type=str, default='',
help="directory to save checkpoint models.")
parser.add_argument('--auto_resume', action='store_true',
help='resume training, including: to load the latest weight and keep the checkpoint directory, '
'to read and concatenate output logging and tune the scheduler accordingly')
"""Scene coordinate regression task parameters (taken from DSAC*)"""
# Note: in depth training mode, mindepth, softclamp and hardclamp parameters are used.
# in normal training mode, softclamp and hardclamp parameters are used.
parser.add_argument('--inittolerance', '-itol', type=float, default=50.0,
help='coord only, turn on reprojection error optimization when the predicted scene coordinates'
'projected into camera coord frame are within this tolerance, in meters')
parser.add_argument('--mindepth', '-mind', type=float, default=0.1,
help='coord: enforce predicted scene coordinates projected into camera coord frame '
' to be this far in front of the camera plane, in meters;'
'depth: min depth threshold for valid prediction, in meters')
parser.add_argument('--softclamp', '-sc', type=float, default=100,
help='coord only, robust square root loss after this threshold, applied to '
'reprojection error, in pixels.')
parser.add_argument('--hardclamp', '-hc', type=float, default=1000,
help='coord: clamp loss with this threshold, applied to reprojection error, in pixels;'
'depth: max error threshold for valid prediction (not for loss), in meters;'
'normal: max error threshold for valid prediction (not for loss), in degrees')
parser.add_argument('--debug', action='store_true',
help='enter debug mode')
"""Uncertainty loss parameter"""
parser.add_argument('--uncertainty', '-uncertainty', default=None, type=str,
help='enable uncertainty learning')
opt = parser.parse_args()
if isinstance(opt.uncertainty, str):
if opt.uncertainty.lower() == 'none':
opt.uncertainty = None
elif opt.uncertainty.lower() == 'mle':
opt.uncertainty = 'MLE'
assert opt.uncertainty in [None, 'MLE'], \
'--uncertainty {} is not supported!'.format(opt.uncertainty)
assert opt.real_data_domain in ['in_place', 'out_of_place'], \
'--real_data_domain {:} is not supported!'.format(opt.real_data_domain)
if opt.real_only:
assert opt.sim_data_chunk == 0
return opt
def get_output_path(opt):
"""
Task-specific output directory name.
"""
basename = opt.scene + '-{:s}'.format(opt.task)
if opt.session != '':
basename += '-s' + opt.session
if opt.grayscale:
basename += '-gray'
if opt.uncertainty is None:
basename += '-no_unc'
else:
basename += '-unc-{:s}'.format(opt.uncertainty)
if opt.fullsize:
basename += '-fullsize'
if opt.learningrate >= 1e-4:
basename += '-e{:d}-lr{:.4f}'.format(opt.epochs, opt.learningrate)
else:
basename += '-e{:d}-lr{:.6f}'.format(opt.epochs, opt.learningrate)
if opt.real_data_chunk == 0.0:
assert opt.sim_data_chunk > 0 # use all or a subset of LHS-sim data
basename += '-sim_only'
basename += '-sc{:.2f}'.format(opt.sim_data_chunk)
else:
if opt.real_only:
basename += '-real_only' # real_data only
else:
basename += '-pairs' # fine-tune encoders
if opt.real_data_domain == 'in_place':
basename += '-ip'
elif opt.real_data_domain == 'out_of_place':
basename += '-oop'
else:
raise NotImplementedError
basename += '-rc{:.2f}'.format(opt.real_data_chunk)
if opt.tiny:
basename += '-tiny'
if opt.network_in is not None:
basename += '-finetune'
if opt.debug:
basename += '-DEBUG'
# now = datetime.now()
# start_time = now.strftime("-T%H.%M.%S-%d.%m.%y")
# basename += start_time
output_dir = os.path.abspath(os.path.join(PROJECT_DIR, 'output', basename))
return output_dir
def main():
"""
Main function.
"""
"""Initialization"""
set_random_seed(2021)
opt = _config_parser()
output_dir, ckpt_output_dir = config_log(opt, get_output_path(opt))
nodata_value = get_nodata_value(opt.scene)
trainset, trainset_loader, mean = config_dataloader(opt.scene, opt.task, opt.grayscale,
opt.real_data_domain, opt.real_data_chunk, opt.sim_data_chunk,
opt.fullsize,
opt.batch_size, nodata_value, opt.real_only)
network, optimizer, model_path, scheduler = config_network(opt.scene, opt.task, opt.tiny, opt.grayscale,
opt.uncertainty, opt.fullsize, mean,
opt.learningrate, opt.no_lr_scheduling,
opt.auto_resume, opt.epoch_plus,
opt.network_in, output_dir)
save_period = 5 # to save a checkpoint model every N epochs
if opt.task == 'coord':
pixel_grid = get_pixel_grid(network.OUTPUT_SUBSAMPLE)
elif opt.task == 'semantics':
semantic_criterion = CrossEntropyLoss2d()
save_period = 1
"""Training loop"""
epochs = opt.epochs
if opt.auto_resume or opt.epoch_plus:
iteration, start_epoch = read_training_log(os.path.join(os.path.dirname(opt.network_in), 'output.log'),
len(trainset))
save_counter = (start_epoch + 1) * len(trainset)
epoch_de_facto = start_epoch
_last_ckpt_iteration = (start_epoch // 5 * 5) * len(trainset)
# refresh learning rate
optimizer.step()
optimizer.zero_grad()
[scheduler.step() for e in range(start_epoch)]
else:
iteration, start_epoch, save_counter, epoch_de_facto, _last_ckpt_iteration = 0, 0, 0, 0, 0
for epoch in range(epochs):
if epoch < start_epoch:
continue
else:
logging.info("Optimizer works effectively with a learning rate of {:.6f}".format(
optimizer.param_groups[0]['lr']))
logging.info("=== Epoch: %d ======================================" % epoch)
for idx, (images, gt_poses, gt_labels, focal_lengths, file_names) in enumerate(trainset_loader):
start_time = time.time()
"""Data pre-processing"""
focal_length = float(focal_lengths.view(-1)[0])
"""
@images [B, C, H, W] ---> [B, 3, 480, 720] by default w/o augmentation, RGB image
@gt_poses [B, 4, 4], camera to world matrix
@gt_labels [B, C, H_ds, W_ds] ---> [B, C, 60, 90] by default w/o augmentation
@focal_length [1], adapted to augmentation
@file_names a list size of B
"""
cam_mat = get_cam_mat(images.size(3), images.size(2), focal_length)
gt_poses = gt_poses.cuda()
gt_labels = gt_labels.cuda()
"""Forward pass"""
predictions = network(images.cuda())
if opt.fullsize:
assert predictions.size(2) == images.size(2) and predictions.size(3) == images.size(3)
assert predictions.size(2) == gt_labels.size(2) and predictions.size(3) == gt_labels.size(3)
if opt.uncertainty is None:
uncertainty_map = None
elif opt.uncertainty == 'MLE':
predictions, uncertainty_map = torch.split(predictions,
[network.num_task_channel, network.num_pos_channel],
dim=1) # typically [B, C, H, W] + [B, 1, H, W]
else:
raise NotImplementedError
"""Backward loop"""
# regression loss
reduction = 'mean'
if opt.task == 'coord':
loss, valid_pred_rate = scene_coords_regression_loss(opt.mindepth, opt.softclamp, opt.hardclamp,
opt.inittolerance, opt.uncertainty,
pixel_grid, nodata_value, cam_mat,
predictions, uncertainty_map, gt_poses, gt_labels,
reduction)
elif opt.task == 'depth':
loss, valid_pred_rate = depth_regression_loss(opt.mindepth, opt.hardclamp,
opt.uncertainty, nodata_value, predictions,
uncertainty_map, gt_labels, reduction)
elif opt.task == 'normal':
loss, valid_pred_rate = normal_regression_loss(opt.hardclamp, opt.uncertainty,
nodata_value, predictions, uncertainty_map,
gt_labels, reduction)
elif opt.task == 'semantics':
loss, valid_pred_rate = semantics_classification_loss(opt.uncertainty, predictions, uncertainty_map,
gt_labels, semantic_criterion, reduction)
else:
raise NotImplementedError
loss.backward()
optimizer.step()
optimizer.zero_grad()
torch.cuda.empty_cache()
"""Training process record."""
batch_size = len(images)
time_avg = (time.time() - start_time) / batch_size
iteration = iteration + batch_size
logging.info(
'Iteration: %7d, Epoch: %3d, Total loss: %.2f, Valid: %.1f%%, Avg Time: %.3fs' % (
iteration, epoch, loss.item(), valid_pred_rate * 100, time_avg))
if iteration > save_counter:
logging.info('Saving snapshot of the network to %s.' % model_path)
torch.save(network.state_dict(), model_path)
save_counter = iteration + len(trainset) # every one de-facto epoch
epoch_de_facto += 1
scheduler.step()
# save checkpoint every N de-facto epochs
if iteration > _last_ckpt_iteration + save_period * len(trainset) or _last_ckpt_iteration == 0:
torch.save(network.state_dict(),
os.path.join(ckpt_output_dir, 'ckpt_iter_{:07d}.net'.format(iteration)))
_last_ckpt_iteration = iteration
logging.info('Saving snapshot of the network to %s.' % model_path)
torch.save(network.state_dict(), model_path)
logging.info('Done without errors.')
torch.save(None, os.path.join(output_dir, 'FLAG_training_done.nodata'))
torch.save(None, os.path.join(ckpt_output_dir, 'FLAG_training_done.nodata'))
if __name__ == '__main__':
main()