-
Notifications
You must be signed in to change notification settings - Fork 5
/
train_stereo_rt.py
250 lines (199 loc) · 10.2 KB
/
train_stereo_rt.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
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
import argparse
import logging
import numpy as np
from pathlib import Path
from tqdm import tqdm
from torch.utils.tensorboard import SummaryWriter
import torch
import torch.nn as nn
import torch.optim as optim
from core_rt.rt_igev_stereo import IGEVStereo
from evaluate_stereo_rt import *
import core.stereo_datasets as datasets
import torch.nn.functional as F
try:
from torch.cuda.amp import GradScaler
except:
class GradScaler:
def __init__(self):
pass
def scale(self, loss):
return loss
def unscale_(self, optimizer):
pass
def step(self, optimizer):
optimizer.step()
def update(self):
pass
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def sequence_loss(agg_pred, iter_preds, disp_gt, valid, loss_gamma=0.9):
""" Loss function defined over sequence of flow predictions """
n_predictions = len(iter_preds)
assert n_predictions >= 1
disp_loss = 0.0
mag = torch.sum(disp_gt**2, dim=1).sqrt()
valid = ((valid >= 0.5) & (mag < 192)).unsqueeze(1)
assert valid.shape == disp_gt.shape, [valid.shape, disp_gt.shape]
assert not torch.isinf(disp_gt[valid.bool()]).any()
disp_loss += 1.0 * F.smooth_l1_loss(agg_pred[valid.bool()], disp_gt[valid.bool()], reduction='mean')
for i in range(n_predictions):
adjusted_loss_gamma = loss_gamma**(15/(n_predictions - 1))
i_weight = adjusted_loss_gamma**(n_predictions - i - 1)
i_loss = (iter_preds[i] - disp_gt).abs()
assert i_loss.shape == valid.shape, [i_loss.shape, valid.shape, disp_gt.shape, iter_preds[i].shape]
disp_loss += i_weight * i_loss[valid.bool()].mean()
epe = torch.sum((iter_preds[-1] - disp_gt)**2, dim=1).sqrt()
epe = epe.view(-1)[valid.view(-1)]
metrics = {
'epe': epe.mean().item(),
'1px': (epe < 1).float().mean().item(),
'3px': (epe < 3).float().mean().item(),
'5px': (epe < 5).float().mean().item(),
}
return disp_loss, metrics
def fetch_optimizer(args, model):
""" Create the optimizer and learning rate scheduler """
optimizer = optim.AdamW(model.parameters(), lr=args.lr, weight_decay=args.wdecay, eps=1e-8)
scheduler = optim.lr_scheduler.OneCycleLR(optimizer, args.lr, args.num_steps+100,
pct_start=0.01, cycle_momentum=False, anneal_strategy='linear')
return optimizer, scheduler
class Logger:
SUM_FREQ = 100
def __init__(self, model, scheduler, logdir):
self.model = model
self.scheduler = scheduler
self.total_steps = 0
self.running_loss = {}
self.logdir = logdir
self.writer = SummaryWriter(log_dir=self.logdir)
def _print_training_status(self):
metrics_data = [self.running_loss[k]/Logger.SUM_FREQ for k in sorted(self.running_loss.keys())]
training_str = "[{:6d}, {:10.7f}] ".format(self.total_steps+1, self.scheduler.get_last_lr()[0])
metrics_str = ("{:10.4f}, "*len(metrics_data)).format(*metrics_data)
# print the training status
logging.info(f"Training Metrics ({self.total_steps}): {training_str + metrics_str}")
if self.writer is None:
self.writer = SummaryWriter(log_dir=self.logdir)
for k in self.running_loss:
self.writer.add_scalar(k, self.running_loss[k]/Logger.SUM_FREQ, self.total_steps)
self.running_loss[k] = 0.0
def push(self, metrics):
self.total_steps += 1
for key in metrics:
if key not in self.running_loss:
self.running_loss[key] = 0.0
self.running_loss[key] += metrics[key]
if self.total_steps % Logger.SUM_FREQ == Logger.SUM_FREQ-1:
self._print_training_status()
self.running_loss = {}
def write_dict(self, results):
if self.writer is None:
self.writer = SummaryWriter(log_dir=self.logdir)
for key in results:
self.writer.add_scalar(key, results[key], self.total_steps)
def close(self):
self.writer.close()
def train(args):
model = nn.DataParallel(IGEVStereo(args))
print("Parameter Count: %d" % count_parameters(model))
train_loader = datasets.fetch_dataloader(args)
optimizer, scheduler = fetch_optimizer(args, model)
total_steps = 0
logger = Logger(model, scheduler, args.logdir)
if args.restore_ckpt is not None:
assert args.restore_ckpt.endswith(".pth")
logging.info("Loading checkpoint...")
checkpoint = torch.load(args.restore_ckpt)
model.load_state_dict(checkpoint, strict=True)
logging.info(f"Done loading checkpoint")
model.cuda()
model.train()
model.module.freeze_bn() # We keep BatchNorm frozen
validation_frequency = 10000
scaler = GradScaler(enabled=args.mixed_precision)
should_keep_training = True
global_batch_num = 0
while should_keep_training:
for i_batch, (_, *data_blob) in enumerate(tqdm(train_loader)):
optimizer.zero_grad()
image1, image2, disp_gt, valid = [x.cuda() for x in data_blob]
assert model.training
agg_pred, iter_preds = model(image1, image2, iters=args.train_iters)
assert model.training
loss, metrics = sequence_loss(agg_pred, iter_preds, disp_gt, valid)
logger.writer.add_scalar("live_loss", loss.item(), global_batch_num)
logger.writer.add_scalar(f'learning_rate', optimizer.param_groups[0]['lr'], global_batch_num)
global_batch_num += 1
scaler.scale(loss).backward()
scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
scaler.step(optimizer)
scheduler.step()
scaler.update()
logger.push(metrics)
if total_steps % validation_frequency == validation_frequency - 1:
save_path = Path(args.logdir + '/%d_%s.pth' % (total_steps + 1, args.name))
logging.info(f"Saving file {save_path.absolute()}")
torch.save(model.state_dict(), save_path)
if 'sceneflow' in args.train_datasets:
results = validate_sceneflow(model.module, iters=args.valid_iters)
elif 'kitti' in args.train_datasets:
results = validate_kitti(model.module, iters=args.valid_iters)
elif 'middlebury' in args.train_datasets:
results = validate_middlebury(model.module, iters=args.valid_iters)
elif 'eth3d' in args.train_datasets:
results = validate_eth3d(model.module, iters=args.valid_iters)
else:
print(f"Val dataset is not supported.")
# logger.write_dict(results)
model.train()
model.module.freeze_bn()
total_steps += 1
if total_steps > args.num_steps:
should_keep_training = False
break
print("FINISHED TRAINING")
logger.close()
PATH = args.logdir + '/%s.pth' % args.name
torch.save(model.state_dict(), PATH)
return PATH
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--name', default='rt-igev-stereo', help="name your experiment")
parser.add_argument('--restore_ckpt', default=None, help='load the weights from a specific checkpoint')
parser.add_argument('--logdir', default='./checkpoints_rt', help='the directory to save logs and checkpoints')
parser.add_argument('--mixed_precision', default=True, action='store_true', help='use mixed precision')
# Training parameters
parser.add_argument('--batch_size', type=int, default=8, help="batch size used during training.")
parser.add_argument('--train_datasets', default='sceneflow', choices=['sceneflow', 'kitti'], help="training datasets.")
parser.add_argument('--lr', type=float, default=0.0002, help="max learning rate.")
parser.add_argument('--num_steps', type=int, default=200000, help="length of training schedule.")
parser.add_argument('--image_size', type=int, nargs='+', default=[320, 768], help="size of the random image crops used during training.")
parser.add_argument('--train_iters', type=int, default=22, help="number of updates to the disparity field in each forward pass.")
parser.add_argument('--wdecay', type=float, default=.00001, help="Weight decay in optimizer.")
# Validation parameters
parser.add_argument('--valid_iters', type=int, default=32, help='number of flow-field updates during validation forward pass')
# Architecure choices
parser.add_argument('--corr_levels', type=int, default=2, help="number of levels in the correlation pyramid")
parser.add_argument('--corr_radius', type=int, default=4, help="width of the correlation pyramid")
parser.add_argument('--n_downsample', type=int, default=2, help="resolution of the disparity field (1/2^K)")
parser.add_argument('--slow_fast_gru', action='store_true', help="iterate the low-res GRUs more frequently")
parser.add_argument('--n_gru_layers', type=int, default=1, help="number of hidden GRU levels")
parser.add_argument('--hidden_dim', nargs='+', type=int, default=96, help="hidden state and context dimensions")
parser.add_argument('--max_disp', type=int, default=192, help="max disp of geometry encoding volume")
# Data augmentation
parser.add_argument('--img_gamma', type=float, nargs='+', default=None, help="gamma range")
parser.add_argument('--saturation_range', type=float, nargs='+', default=[0, 1.4], help='color saturation')
parser.add_argument('--do_flip', default=False, choices=['h', 'v'], help='flip the images horizontally or vertically')
parser.add_argument('--spatial_scale', type=float, nargs='+', default=[-0.4, 0.8], help='re-scale the images randomly')
parser.add_argument('--noyjitter', action='store_true', help='don\'t simulate imperfect rectification')
args = parser.parse_args()
torch.manual_seed(666)
np.random.seed(666)
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s')
Path(args.logdir).mkdir(exist_ok=True, parents=True)
train(args)