-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbev_stereo_lss_r50_256x704_128x128_24e_2key.py
511 lines (466 loc) · 17.1 KB
/
bev_stereo_lss_r50_256x704_128x128_24e_2key.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# Copyright (c) Megvii Inc. All rights reserved.
"""
mAP: 0.3456
mATE: 0.6589
mASE: 0.2774
mAOE: 0.5500
mAVE: 0.4980
mAAE: 0.2278
NDS: 0.4516
Eval time: 158.2s
Per-class results:
Object Class AP ATE ASE AOE AVE AAE
car 0.510 0.525 0.165 0.188 0.510 0.226
truck 0.288 0.698 0.220 0.205 0.443 0.227
bus 0.378 0.622 0.210 0.135 0.896 0.289
trailer 0.156 1.003 0.219 0.482 0.609 0.179
construction_vehicle 0.094 0.929 0.502 1.209 0.108 0.365
pedestrian 0.356 0.728 0.297 1.005 0.579 0.319
motorcycle 0.361 0.571 0.258 0.734 0.631 0.211
bicycle 0.318 0.533 0.269 0.793 0.208 0.007
traffic_cone 0.488 0.501 0.355 nan nan nan
barrier 0.506 0.478 0.277 0.200 nan nan
"""
from argparse import ArgumentParser, Namespace
import mmcv
import pytorch_lightning as pl
import torch
import torch.nn.functional as F
import torch.nn.parallel
import torch.utils.data
import torch.utils.data.distributed
import torchvision.models as models
from pytorch_lightning.core import LightningModule
from torch.cuda.amp.autocast_mode import autocast
from torch.optim.lr_scheduler import MultiStepLR
from dataset.nusc_mv_det_dataset import NuscMVDetDataset, collate_fn
from evaluators.det_mv_evaluators import DetMVNuscEvaluator
from models.bev_stereo import BEVStereo
from utils.torch_dist import all_gather_object, get_rank, synchronize
H = 900
W = 1600
final_dim = (256, 704)
img_conf = dict(img_mean=[123.675, 116.28, 103.53],
img_std=[58.395, 57.12, 57.375],
to_rgb=True)
backbone_conf = {
'x_bound': [-51.2, 51.2, 0.8],
'y_bound': [-51.2, 51.2, 0.8],
'z_bound': [-5, 3, 8],
'd_bound': [2.0, 58.0, 0.5],
'final_dim':
final_dim,
'output_channels':
80,
'downsample_factor':
16,
'img_backbone_conf':
dict(
type='ResNet',
depth=50,
frozen_stages=0,
out_indices=[0, 1, 2, 3],
norm_eval=False,
init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50'),
),
'img_neck_conf':
dict(
type='SECONDFPN',
in_channels=[256, 512, 1024, 2048],
upsample_strides=[0.25, 0.5, 1, 2],
out_channels=[128, 128, 128, 128],
),
'depth_net_conf':
dict(in_channels=512, mid_channels=512),
'num_ranges':
4,
'range_list': [[2, 8], [8, 16], [16, 28], [28, 58]],
}
ida_aug_conf = {
'resize_lim': (0.386, 0.55),
'final_dim':
final_dim,
'rot_lim': (-5.4, 5.4),
'H':
H,
'W':
W,
'rand_flip':
True,
'bot_pct_lim': (0.0, 0.0),
'cams': [
'CAM_FRONT_LEFT', 'CAM_FRONT', 'CAM_FRONT_RIGHT', 'CAM_BACK_LEFT',
'CAM_BACK', 'CAM_BACK_RIGHT'
],
'Ncams':
6,
}
bda_aug_conf = {
'rot_lim': (-22.5, 22.5),
'scale_lim': (0.95, 1.05),
'flip_dx_ratio': 0.5,
'flip_dy_ratio': 0.5
}
bev_backbone = dict(
type='ResNet',
in_channels=160,
depth=18,
num_stages=3,
strides=(1, 2, 2),
dilations=(1, 1, 1),
out_indices=[0, 1, 2],
norm_eval=False,
base_channels=160,
)
bev_neck = dict(type='SECONDFPN',
in_channels=[160, 160, 320, 640],
upsample_strides=[1, 2, 4, 8],
out_channels=[64, 64, 64, 64])
CLASSES = [
'car',
'truck',
'construction_vehicle',
'bus',
'trailer',
'barrier',
'motorcycle',
'bicycle',
'pedestrian',
'traffic_cone',
]
TASKS = [
dict(num_class=1, class_names=['car']),
dict(num_class=2, class_names=['truck', 'construction_vehicle']),
dict(num_class=2, class_names=['bus', 'trailer']),
dict(num_class=1, class_names=['barrier']),
dict(num_class=2, class_names=['motorcycle', 'bicycle']),
dict(num_class=2, class_names=['pedestrian', 'traffic_cone']),
]
common_heads = dict(reg=(2, 2),
height=(1, 2),
dim=(3, 2),
rot=(2, 2),
vel=(2, 2))
bbox_coder = dict(
type='CenterPointBBoxCoder',
post_center_range=[-61.2, -61.2, -10.0, 61.2, 61.2, 10.0],
max_num=500,
score_threshold=0.1,
out_size_factor=4,
voxel_size=[0.2, 0.2, 8],
pc_range=[-51.2, -51.2, -5, 51.2, 51.2, 3],
code_size=9,
)
train_cfg = dict(
point_cloud_range=[-51.2, -51.2, -5, 51.2, 51.2, 3],
grid_size=[512, 512, 1],
voxel_size=[0.2, 0.2, 8],
out_size_factor=4,
dense_reg=1,
gaussian_overlap=0.1,
max_objs=500,
min_radius=2,
code_weights=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
)
test_cfg = dict(
post_center_limit_range=[-61.2, -61.2, -10.0, 61.2, 61.2, 10.0],
max_per_img=500,
max_pool_nms=False,
thresh_scale=[0.6, 0.4, 0.4, 0.7, 0.8, 0.9],
score_threshold=0.1,
out_size_factor=4,
voxel_size=[0.2, 0.2, 8],
nms_type='circle',
pre_max_size=1000,
post_max_size=83,
nms_thr=0.2,
)
head_conf = {
'bev_backbone_conf': bev_backbone,
'bev_neck_conf': bev_neck,
'tasks': TASKS,
'common_heads': common_heads,
'bbox_coder': bbox_coder,
'train_cfg': train_cfg,
'test_cfg': test_cfg,
'in_channels': 256, # Equal to bev_neck output_channels.
'loss_cls': dict(type='GaussianFocalLoss', reduction='mean'),
'loss_bbox': dict(type='L1Loss', reduction='mean', loss_weight=0.25),
'gaussian_overlap': 0.1,
'min_radius': 2,
}
class BEVStereoLightningModel(LightningModule):
MODEL_NAMES = sorted(name for name in models.__dict__
if name.islower() and not name.startswith('__')
and callable(models.__dict__[name]))
def __init__(self,
gpus: int = 1,
data_root='data/nuScenes',
eval_interval=1,
batch_size_per_device=8,
class_names=CLASSES,
backbone_conf=backbone_conf,
head_conf=head_conf,
ida_aug_conf=ida_aug_conf,
bda_aug_conf=bda_aug_conf,
default_root_dir='./outputs/',
**kwargs):
super().__init__()
self.save_hyperparameters()
self.gpus = gpus
self.eval_interval = eval_interval
self.batch_size_per_device = batch_size_per_device
self.data_root = data_root
self.basic_lr_per_img = 2e-4 / 64
self.class_names = class_names
self.backbone_conf = backbone_conf
self.head_conf = head_conf
self.ida_aug_conf = ida_aug_conf
self.bda_aug_conf = bda_aug_conf
mmcv.mkdir_or_exist(default_root_dir)
self.default_root_dir = default_root_dir
self.evaluator = DetMVNuscEvaluator(class_names=self.class_names,
output_dir=self.default_root_dir)
self.model = BEVStereo(self.backbone_conf,
self.head_conf,
is_train_depth=True)
self.mode = 'valid'
self.img_conf = img_conf
self.data_use_cbgs = False
self.num_sweeps = 1
self.sweep_idxes = []
self.key_idxes = [-1]
self.data_return_depth = False
self.model_use_ema = True
self.data_return_depth = True
self.downsample_factor = self.backbone_conf['downsample_factor']
self.dbound = self.backbone_conf['d_bound']
self.depth_channels = int(
(self.dbound[1] - self.dbound[0]) / self.dbound[2])
def forward(self, sweep_imgs, mats):
return self.model(sweep_imgs, mats)
def training_step(self, batch):
(sweep_imgs, mats, _, _, gt_boxes, gt_labels, depth_labels) = batch
if torch.cuda.is_available():
for key, value in mats.items():
mats[key] = value.cuda()
sweep_imgs = sweep_imgs.cuda()
gt_boxes = [gt_box.cuda() for gt_box in gt_boxes]
gt_labels = [gt_label.cuda() for gt_label in gt_labels]
preds, depth_preds = self(sweep_imgs, mats)
if isinstance(self.model, torch.nn.parallel.DistributedDataParallel):
targets = self.model.module.get_targets(gt_boxes, gt_labels)
detection_loss = self.model.module.loss(targets, preds)
else:
targets = self.model.get_targets(gt_boxes, gt_labels)
detection_loss = self.model.loss(targets, preds)
if len(depth_labels.shape) == 5:
# only key-frame will calculate depth loss
depth_labels = depth_labels[:, 0, ...]
depth_loss = self.get_depth_loss(depth_labels.cuda(), depth_preds)
self.log('detection_loss', detection_loss)
self.log('depth_loss', depth_loss)
return detection_loss + depth_loss
def get_depth_loss(self, depth_labels, depth_preds):
depth_labels = self.get_downsampled_gt_depth(depth_labels)
depth_preds = depth_preds.permute(0, 2, 3, 1).contiguous().view(
-1, self.depth_channels)
fg_mask = torch.max(depth_labels, dim=1).values > 0.0
with autocast(enabled=False):
depth_loss = (F.binary_cross_entropy(
depth_preds[fg_mask],
depth_labels[fg_mask],
reduction='none',
).sum() / max(1.0, fg_mask.sum()))
return 3.0 * depth_loss
def get_downsampled_gt_depth(self, gt_depths):
"""
Input:
gt_depths: [B, N, H, W]
Output:
gt_depths: [B*N*h*w, d]
"""
B, N, H, W = gt_depths.shape
gt_depths = gt_depths.view(
B * N,
H // self.downsample_factor,
self.downsample_factor,
W // self.downsample_factor,
self.downsample_factor,
1,
)
gt_depths = gt_depths.permute(0, 1, 3, 5, 2, 4).contiguous()
gt_depths = gt_depths.view(
-1, self.downsample_factor * self.downsample_factor)
gt_depths_tmp = torch.where(gt_depths == 0.0,
1e5 * torch.ones_like(gt_depths),
gt_depths)
gt_depths = torch.min(gt_depths_tmp, dim=-1).values
gt_depths = gt_depths.view(B * N, H // self.downsample_factor,
W // self.downsample_factor)
gt_depths = (gt_depths -
(self.dbound[0] - self.dbound[2])) / self.dbound[2]
gt_depths = torch.where(
(gt_depths < self.depth_channels + 1) & (gt_depths >= 0.0),
gt_depths, torch.zeros_like(gt_depths))
gt_depths = F.one_hot(gt_depths.long(),
num_classes=self.depth_channels + 1).view(
-1, self.depth_channels + 1)[:, 1:]
return gt_depths.float()
def eval_step(self, batch, batch_idx, prefix: str):
(sweep_imgs, mats, _, img_metas, _, _) = batch
if torch.cuda.is_available():
for key, value in mats.items():
mats[key] = value.cuda()
sweep_imgs = sweep_imgs.cuda()
preds = self.model(sweep_imgs, mats)
if isinstance(self.model, torch.nn.parallel.DistributedDataParallel):
results = self.model.module.get_bboxes(preds, img_metas)
else:
results = self.model.get_bboxes(preds, img_metas)
for i in range(len(results)):
results[i][0] = results[i][0].tensor.detach().cpu().numpy()
results[i][1] = results[i][1].detach().cpu().numpy()
results[i][2] = results[i][2].detach().cpu().numpy()
results[i].append(img_metas[i])
return results
def validation_step(self, batch, batch_idx):
return self.eval_step(batch, batch_idx, 'val')
def validation_epoch_end(self, validation_step_outputs):
all_pred_results = list()
all_img_metas = list()
for validation_step_output in validation_step_outputs:
for i in range(len(validation_step_output)):
all_pred_results.append(validation_step_output[i][:3])
all_img_metas.append(validation_step_output[i][3])
synchronize()
len_dataset = len(self.val_dataloader().dataset)
all_pred_results = sum(
map(list, zip(*all_gather_object(all_pred_results))),
[])[:len_dataset]
all_img_metas = sum(map(list, zip(*all_gather_object(all_img_metas))),
[])[:len_dataset]
if get_rank() == 0:
self.evaluator.evaluate(all_pred_results, all_img_metas)
def test_epoch_end(self, test_step_outputs):
all_pred_results = list()
all_img_metas = list()
for test_step_output in test_step_outputs:
for i in range(len(test_step_output)):
all_pred_results.append(test_step_output[i][:3])
all_img_metas.append(test_step_output[i][3])
synchronize()
# TODO: Change another way.
dataset_length = len(self.val_dataloader().dataset)
all_pred_results = sum(
map(list, zip(*all_gather_object(all_pred_results))),
[])[:dataset_length]
all_img_metas = sum(map(list, zip(*all_gather_object(all_img_metas))),
[])[:dataset_length]
if get_rank() == 0:
self.evaluator.evaluate(all_pred_results, all_img_metas)
def configure_optimizers(self):
lr = self.basic_lr_per_img * \
self.batch_size_per_device * self.gpus
optimizer = torch.optim.AdamW(self.model.parameters(),
lr=lr,
weight_decay=1e-7)
scheduler = MultiStepLR(optimizer, [19, 23])
return [[optimizer], [scheduler]]
def train_dataloader(self):
train_dataset = NuscMVDetDataset(
ida_aug_conf=self.ida_aug_conf,
bda_aug_conf=self.bda_aug_conf,
classes=self.class_names,
data_root=self.data_root,
info_path='data/nuScenes/nuscenes_12hz_infos_train.pkl',
is_train=True,
use_cbgs=self.data_use_cbgs,
img_conf=self.img_conf,
num_sweeps=self.num_sweeps,
sweep_idxes=self.sweep_idxes,
key_idxes=self.key_idxes,
return_depth=self.data_return_depth,
)
from functools import partial
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=self.batch_size_per_device,
num_workers=4,
drop_last=True,
shuffle=False,
collate_fn=partial(collate_fn,
is_return_depth=self.data_return_depth),
sampler=None,
)
return train_loader
def val_dataloader(self):
val_dataset = NuscMVDetDataset(
ida_aug_conf=self.ida_aug_conf,
bda_aug_conf=self.bda_aug_conf,
classes=self.class_names,
data_root=self.data_root,
info_path='data/nuScenes/nuscenes_12hz_infos_val.pkl',
is_train=False,
img_conf=self.img_conf,
num_sweeps=self.num_sweeps,
sweep_idxes=self.sweep_idxes,
key_idxes=self.key_idxes,
return_depth=False,
)
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=self.batch_size_per_device,
shuffle=False,
collate_fn=collate_fn,
num_workers=4,
sampler=None,
)
return val_loader
def test_dataloader(self):
return self.val_dataloader()
def test_step(self, batch, batch_idx):
return self.eval_step(batch, batch_idx, 'test')
@staticmethod
def add_model_specific_args(parent_parser): # pragma: no-cover
return parent_parser
def main(args: Namespace) -> None:
if args.seed is not None:
pl.seed_everything(args.seed)
model = BEVStereoLightningModel(**vars(args))
trainer = pl.Trainer.from_argparse_args(args)
if args.evaluate:
trainer.test(model, ckpt_path=args.ckpt_path)
else:
trainer.fit(model)
def run_cli():
parent_parser = ArgumentParser(add_help=False)
parent_parser = pl.Trainer.add_argparse_args(parent_parser)
parent_parser.add_argument('-e',
'--evaluate',
dest='evaluate',
action='store_true',
help='evaluate model on validation set')
parent_parser.add_argument('-b', '--batch_size_per_device', type=int)
parent_parser.add_argument('--seed',
type=int,
default=0,
help='seed for initializing training.')
parent_parser.add_argument('--ckpt_path', type=str)
parser = BEVStereoLightningModel.add_model_specific_args(parent_parser)
parser.set_defaults(
profiler='simple',
deterministic=False,
max_epochs=24,
accelerator='ddp',
num_sanity_val_steps=0,
gradient_clip_val=5,
limit_val_batches=0,
enable_checkpointing=True,
precision=16,
default_root_dir='./outputs/bev_stereo_lss_r50_256x704_'
'128x128_24e_2key')
args = parser.parse_args()
main(args)
if __name__ == '__main__':
run_cli()