-
Notifications
You must be signed in to change notification settings - Fork 298
/
nusc_common.py
711 lines (623 loc) · 25.2 KB
/
nusc_common.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
import os.path as osp
import numpy as np
import pickle
import random
from pathlib import Path
from functools import reduce
from typing import Tuple, List
from tqdm import tqdm
from pyquaternion import Quaternion
from nuscenes import NuScenes
from nuscenes.utils import splits
from nuscenes.utils.data_classes import LidarPointCloud
from nuscenes.utils.geometry_utils import transform_matrix
from nuscenes.utils.data_classes import Box
from nuscenes.eval.detection.config import config_factory
from nuscenes.eval.detection.evaluate import NuScenesEval
general_to_detection = {
"human.pedestrian.adult": "pedestrian",
"human.pedestrian.child": "pedestrian",
"human.pedestrian.wheelchair": "ignore",
"human.pedestrian.stroller": "ignore",
"human.pedestrian.personal_mobility": "ignore",
"human.pedestrian.police_officer": "pedestrian",
"human.pedestrian.construction_worker": "pedestrian",
"animal": "ignore",
"vehicle.car": "car",
"vehicle.motorcycle": "motorcycle",
"vehicle.bicycle": "bicycle",
"vehicle.bus.bendy": "bus",
"vehicle.bus.rigid": "bus",
"vehicle.truck": "truck",
"vehicle.construction": "construction_vehicle",
"vehicle.emergency.ambulance": "ignore",
"vehicle.emergency.police": "ignore",
"vehicle.trailer": "trailer",
"movable_object.barrier": "barrier",
"movable_object.trafficcone": "traffic_cone",
"movable_object.pushable_pullable": "ignore",
"movable_object.debris": "ignore",
"static_object.bicycle_rack": "ignore",
}
cls_attr_dist = {
"barrier": {
"cycle.with_rider": 0,
"cycle.without_rider": 0,
"pedestrian.moving": 0,
"pedestrian.sitting_lying_down": 0,
"pedestrian.standing": 0,
"vehicle.moving": 0,
"vehicle.parked": 0,
"vehicle.stopped": 0,
},
"bicycle": {
"cycle.with_rider": 2791,
"cycle.without_rider": 8946,
"pedestrian.moving": 0,
"pedestrian.sitting_lying_down": 0,
"pedestrian.standing": 0,
"vehicle.moving": 0,
"vehicle.parked": 0,
"vehicle.stopped": 0,
},
"bus": {
"cycle.with_rider": 0,
"cycle.without_rider": 0,
"pedestrian.moving": 0,
"pedestrian.sitting_lying_down": 0,
"pedestrian.standing": 0,
"vehicle.moving": 9092,
"vehicle.parked": 3294,
"vehicle.stopped": 3881,
},
"car": {
"cycle.with_rider": 0,
"cycle.without_rider": 0,
"pedestrian.moving": 0,
"pedestrian.sitting_lying_down": 0,
"pedestrian.standing": 0,
"vehicle.moving": 114304,
"vehicle.parked": 330133,
"vehicle.stopped": 46898,
},
"construction_vehicle": {
"cycle.with_rider": 0,
"cycle.without_rider": 0,
"pedestrian.moving": 0,
"pedestrian.sitting_lying_down": 0,
"pedestrian.standing": 0,
"vehicle.moving": 882,
"vehicle.parked": 11549,
"vehicle.stopped": 2102,
},
"ignore": {
"cycle.with_rider": 307,
"cycle.without_rider": 73,
"pedestrian.moving": 0,
"pedestrian.sitting_lying_down": 0,
"pedestrian.standing": 0,
"vehicle.moving": 165,
"vehicle.parked": 400,
"vehicle.stopped": 102,
},
"motorcycle": {
"cycle.with_rider": 4233,
"cycle.without_rider": 8326,
"pedestrian.moving": 0,
"pedestrian.sitting_lying_down": 0,
"pedestrian.standing": 0,
"vehicle.moving": 0,
"vehicle.parked": 0,
"vehicle.stopped": 0,
},
"pedestrian": {
"cycle.with_rider": 0,
"cycle.without_rider": 0,
"pedestrian.moving": 157444,
"pedestrian.sitting_lying_down": 13939,
"pedestrian.standing": 46530,
"vehicle.moving": 0,
"vehicle.parked": 0,
"vehicle.stopped": 0,
},
"traffic_cone": {
"cycle.with_rider": 0,
"cycle.without_rider": 0,
"pedestrian.moving": 0,
"pedestrian.sitting_lying_down": 0,
"pedestrian.standing": 0,
"vehicle.moving": 0,
"vehicle.parked": 0,
"vehicle.stopped": 0,
},
"trailer": {
"cycle.with_rider": 0,
"cycle.without_rider": 0,
"pedestrian.moving": 0,
"pedestrian.sitting_lying_down": 0,
"pedestrian.standing": 0,
"vehicle.moving": 3421,
"vehicle.parked": 19224,
"vehicle.stopped": 1895,
},
"truck": {
"cycle.with_rider": 0,
"cycle.without_rider": 0,
"pedestrian.moving": 0,
"pedestrian.sitting_lying_down": 0,
"pedestrian.standing": 0,
"vehicle.moving": 21339,
"vehicle.parked": 55626,
"vehicle.stopped": 11097,
},
}
def box_velocity(
nusc, sample_annotation_token: str, max_time_diff: float = 1.5
) -> np.ndarray:
"""
Estimate the velocity for an annotation.
If possible, we compute the centered difference between the previous and next frame.
Otherwise we use the difference between the current and previous/next frame.
If the velocity cannot be estimated, values are set to np.nan.
:param sample_annotation_token: Unique sample_annotation identifier.
:param max_time_diff: Max allowed time diff between consecutive samples that are used to estimate velocities.
:return: <np.float: 3>. Velocity in x/y/z direction in m/s.
"""
current = nusc.get("sample_annotation", sample_annotation_token)
has_prev = current["prev"] != ""
has_next = current["next"] != ""
# Cannot estimate velocity for a single annotation.
if not has_prev and not has_next:
return np.array([np.nan, np.nan, np.nan])
if has_prev:
first = nusc.get("sample_annotation", current["prev"])
else:
first = current
if has_next:
last = nusc.get("sample_annotation", current["next"])
else:
last = current
pos_last = np.array(last["translation"])
pos_first = np.array(first["translation"])
pos_diff = pos_last - pos_first
time_last = 1e-6 * nusc.get("sample", last["sample_token"])["timestamp"]
time_first = 1e-6 * nusc.get("sample", first["sample_token"])["timestamp"]
time_diff = time_last - time_first
if has_next and has_prev:
# If doing centered difference, allow for up to double the max_time_diff.
max_time_diff *= 2
if time_diff > max_time_diff:
# If time_diff is too big, don't return an estimate.
return np.array([np.nan, np.nan, np.nan])
else:
return pos_diff / time_diff
def remove_close(points, radius: float) -> None:
"""
Removes point too close within a certain radius from origin.
:param radius: Radius below which points are removed.
"""
x_filt = np.abs(points[0, :]) < radius
y_filt = np.abs(points[1, :]) < radius
not_close = np.logical_not(np.logical_and(x_filt, y_filt))
points = points[:, not_close]
return points
def _second_det_to_nusc_box(detection):
box3d = detection["box3d_lidar"].detach().cpu().numpy()
scores = detection["scores"].detach().cpu().numpy()
labels = detection["label_preds"].detach().cpu().numpy()
box3d[:, -1] = -box3d[:, -1] - np.pi / 2
box_list = []
for i in range(box3d.shape[0]):
quat = Quaternion(axis=[0, 0, 1], radians=box3d[i, -1])
velocity = (*box3d[i, 6:8], 0.0)
box = Box(
box3d[i, :3],
box3d[i, 3:6],
quat,
label=labels[i],
score=scores[i],
velocity=velocity,
)
box_list.append(box)
return box_list
def _lidar_nusc_box_to_global(nusc, boxes, sample_token):
try:
s_record = nusc.get("sample", sample_token)
sample_data_token = s_record["data"]["LIDAR_TOP"]
except:
sample_data_token = sample_token
sd_record = nusc.get("sample_data", sample_data_token)
cs_record = nusc.get("calibrated_sensor", sd_record["calibrated_sensor_token"])
sensor_record = nusc.get("sensor", cs_record["sensor_token"])
pose_record = nusc.get("ego_pose", sd_record["ego_pose_token"])
data_path = nusc.get_sample_data_path(sample_data_token)
box_list = []
for box in boxes:
# Move box to ego vehicle coord system
box.rotate(Quaternion(cs_record["rotation"]))
box.translate(np.array(cs_record["translation"]))
# Move box to global coord system
box.rotate(Quaternion(pose_record["rotation"]))
box.translate(np.array(pose_record["translation"]))
box_list.append(box)
return box_list
def _get_available_scenes(nusc):
available_scenes = []
print("total scene num:", len(nusc.scene))
for scene in nusc.scene:
scene_token = scene["token"]
scene_rec = nusc.get("scene", scene_token)
sample_rec = nusc.get("sample", scene_rec["first_sample_token"])
sd_rec = nusc.get("sample_data", sample_rec["data"]["LIDAR_TOP"])
has_more_frames = True
scene_not_exist = False
while has_more_frames:
lidar_path, boxes, _ = nusc.get_sample_data(sd_rec["token"])
if not Path(lidar_path).exists():
scene_not_exist = True
break
else:
break
if not sd_rec["next"] == "":
sd_rec = nusc.get("sample_data", sd_rec["next"])
else:
has_more_frames = False
if scene_not_exist:
continue
available_scenes.append(scene)
print("exist scene num:", len(available_scenes))
return available_scenes
def get_sample_data(
nusc, sample_data_token: str, selected_anntokens: List[str] = None
) -> Tuple[str, List[Box], np.array]:
"""
Returns the data path as well as all annotations related to that sample_data.
Note that the boxes are transformed into the current sensor's coordinate frame.
:param sample_data_token: Sample_data token.
:param selected_anntokens: If provided only return the selected annotation.
:return: (data_path, boxes, camera_intrinsic <np.array: 3, 3>)
"""
# Retrieve sensor & pose records
sd_record = nusc.get("sample_data", sample_data_token)
cs_record = nusc.get("calibrated_sensor", sd_record["calibrated_sensor_token"])
sensor_record = nusc.get("sensor", cs_record["sensor_token"])
pose_record = nusc.get("ego_pose", sd_record["ego_pose_token"])
data_path = nusc.get_sample_data_path(sample_data_token)
if sensor_record["modality"] == "camera":
cam_intrinsic = np.array(cs_record["camera_intrinsic"])
imsize = (sd_record["width"], sd_record["height"])
else:
cam_intrinsic = None
imsize = None
# Retrieve all sample annotations and map to sensor coordinate system.
if selected_anntokens is not None:
boxes = list(map(nusc.get_box, selected_anntokens))
else:
boxes = nusc.get_boxes(sample_data_token)
# Make list of Box objects including coord system transforms.
box_list = []
for box in boxes:
# Move box to ego vehicle coord system
box.translate(-np.array(pose_record["translation"]))
box.rotate(Quaternion(pose_record["rotation"]).inverse)
# Move box to sensor coord system
box.translate(-np.array(cs_record["translation"]))
box.rotate(Quaternion(cs_record["rotation"]).inverse)
box_list.append(box)
return data_path, box_list, cam_intrinsic
def get_sample_ground_plane(root_path, version):
nusc = NuScenes(version=version, dataroot=root_path, verbose=True)
rets = {}
for sample in tqdm(nusc.sample):
chan = "LIDAR_TOP"
sd_token = sample["data"][chan]
sd_rec = nusc.get("sample_data", sd_token)
lidar_path, _, _ = get_sample_data(nusc, sd_token)
points = read_file(lidar_path)
points = np.concatenate((points[:, :3], np.ones((points.shape[0], 1))), axis=1)
plane, inliers, outliers = fit_plane_LSE_RANSAC(
points, return_outlier_list=True
)
xx = points[:, 0]
yy = points[:, 1]
zz = (-plane[0] * xx - plane[1] * yy - plane[3]) / plane[2]
rets.update({sd_token: {"plane": plane, "height": zz,}})
with open(nusc.root_path / "infos_trainval_ground_plane.pkl", "wb") as f:
pickle.dump(rets, f)
def _fill_trainval_infos(nusc, train_scenes, val_scenes, test=False, nsweeps=10):
from nuscenes.utils.geometry_utils import transform_matrix
train_nusc_infos = []
val_nusc_infos = []
ref_chan = "LIDAR_TOP" # The radar channel from which we track back n sweeps to aggregate the point cloud.
chan = "LIDAR_TOP" # The reference channel of the current sample_rec that the point clouds are mapped to.
for sample in tqdm(nusc.sample):
""" Manual save info["sweeps"] """
# Get reference pose and timestamp
# ref_chan == "LIDAR_TOP"
ref_sd_token = sample["data"][ref_chan]
ref_sd_rec = nusc.get("sample_data", ref_sd_token)
ref_cs_rec = nusc.get(
"calibrated_sensor", ref_sd_rec["calibrated_sensor_token"]
)
ref_pose_rec = nusc.get("ego_pose", ref_sd_rec["ego_pose_token"])
ref_time = 1e-6 * ref_sd_rec["timestamp"]
ref_lidar_path, ref_boxes, _ = get_sample_data(nusc, ref_sd_token)
ref_cam_front_token = sample["data"]["CAM_FRONT"]
ref_cam_path, _, ref_cam_intrinsic = nusc.get_sample_data(ref_cam_front_token)
# Homogeneous transform from ego car frame to reference frame
ref_from_car = transform_matrix(
ref_cs_rec["translation"], Quaternion(ref_cs_rec["rotation"]), inverse=True
)
# Homogeneous transformation matrix from global to _current_ ego car frame
car_from_global = transform_matrix(
ref_pose_rec["translation"],
Quaternion(ref_pose_rec["rotation"]),
inverse=True,
)
info = {
"lidar_path": ref_lidar_path,
"cam_front_path": ref_cam_path,
"cam_intrinsic": ref_cam_intrinsic,
"token": sample["token"],
"sweeps": [],
"ref_from_car": ref_from_car,
"car_from_global": car_from_global,
"timestamp": ref_time,
}
sample_data_token = sample["data"][chan]
curr_sd_rec = nusc.get("sample_data", sample_data_token)
sweeps = []
while len(sweeps) < nsweeps - 1:
if curr_sd_rec["prev"] == "":
if len(sweeps) == 0:
sweep = {
"lidar_path": ref_lidar_path,
"sample_data_token": curr_sd_rec["token"],
"transform_matrix": None,
"time_lag": curr_sd_rec["timestamp"] * 0,
# time_lag: 0,
}
sweeps.append(sweep)
else:
sweeps.append(sweeps[-1])
else:
curr_sd_rec = nusc.get("sample_data", curr_sd_rec["prev"])
# Get past pose
current_pose_rec = nusc.get("ego_pose", curr_sd_rec["ego_pose_token"])
global_from_car = transform_matrix(
current_pose_rec["translation"],
Quaternion(current_pose_rec["rotation"]),
inverse=False,
)
# Homogeneous transformation matrix from sensor coordinate frame to ego car frame.
current_cs_rec = nusc.get(
"calibrated_sensor", curr_sd_rec["calibrated_sensor_token"]
)
car_from_current = transform_matrix(
current_cs_rec["translation"],
Quaternion(current_cs_rec["rotation"]),
inverse=False,
)
tm = reduce(
np.dot,
[ref_from_car, car_from_global, global_from_car, car_from_current],
)
lidar_path = nusc.get_sample_data_path(curr_sd_rec["token"])
time_lag = ref_time - 1e-6 * curr_sd_rec["timestamp"]
sweep = {
"lidar_path": lidar_path,
"sample_data_token": curr_sd_rec["token"],
"transform_matrix": tm,
"global_from_car": global_from_car,
"car_from_current": car_from_current,
"time_lag": time_lag,
}
sweeps.append(sweep)
info["sweeps"] = sweeps
assert (
len(info["sweeps"]) == nsweeps - 1
), f"sweep {curr_sd_rec['token']} only has {len(info['sweeps'])} sweeps, you should duplicate to sweep num {nsweeps-1}"
""" read from api """
# sd_record = nusc.get('sample_data', sample['data']['LIDAR_TOP'])
#
# # Get boxes in lidar frame.
# lidar_path, boxes, cam_intrinsic = nusc.get_sample_data(
# sample['data']['LIDAR_TOP'])
#
# # Get aggregated point cloud in lidar frame.
# sample_rec = nusc.get('sample', sd_record['sample_token'])
# chan = sd_record['channel']
# ref_chan = 'LIDAR_TOP'
# pc, times = LidarPointCloud.from_file_multisweep(nusc,
# sample_rec,
# chan,
# ref_chan,
# nsweeps=nsweeps)
# lidar_path = osp.join(nusc.dataroot, "sample_10sweeps/LIDAR_TOP",
# sample['data']['LIDAR_TOP'] + ".bin")
# pc.points.astype('float32').tofile(open(lidar_path, "wb"))
#
# info = {
# "lidar_path": lidar_path,
# "token": sample["token"],
# # "timestamp": times,
# }
if not test:
annotations = [
nusc.get("sample_annotation", token) for token in sample["anns"]
]
# the filtering gives 0.5~1 map improvement
mask = np.array([(anno['num_lidar_pts'] + anno['num_radar_pts'])>0 for anno in annotations], dtype=bool).reshape(-1)
locs = np.array([b.center for b in ref_boxes]).reshape(-1, 3)
dims = np.array([b.wlh for b in ref_boxes]).reshape(-1, 3)
# rots = np.array([b.orientation.yaw_pitch_roll[0] for b in ref_boxes]).reshape(-1, 1)
velocity = np.array([b.velocity for b in ref_boxes]).reshape(-1, 3)
rots = np.array([quaternion_yaw(b.orientation) for b in ref_boxes]).reshape(
-1, 1
)
names = np.array([b.name for b in ref_boxes])
tokens = np.array([b.token for b in ref_boxes])
gt_boxes = np.concatenate(
[locs, dims, velocity[:, :2], -rots - np.pi / 2], axis=1
)
# gt_boxes = np.concatenate([locs, dims, rots], axis=1)
assert len(annotations) == len(gt_boxes) == len(velocity)
info["gt_boxes"] = gt_boxes[mask, :]
info["gt_boxes_velocity"] = velocity[mask, :]
info["gt_names"] = np.array([general_to_detection[name] for name in names])[mask]
info["gt_boxes_token"] = tokens[mask]
if sample["scene_token"] in train_scenes:
train_nusc_infos.append(info)
else:
val_nusc_infos.append(info)
return train_nusc_infos, val_nusc_infos
def quaternion_yaw(q: Quaternion) -> float:
"""
Calculate the yaw angle from a quaternion.
Note that this only works for a quaternion that represents a box in lidar or global coordinate frame.
It does not work for a box in the camera frame.
:param q: Quaternion of interest.
:return: Yaw angle in radians.
"""
# Project into xy plane.
v = np.dot(q.rotation_matrix, np.array([1, 0, 0]))
# Measure yaw using arctan.
yaw = np.arctan2(v[1], v[0])
return yaw
def create_nuscenes_infos_test(root_path, version="v1.0-trainval", nsweeps=10):
nusc = NuScenes(version=version, dataroot=root_path, verbose=True)
available_vers = ["v1.0-trainval", "v1.0-test", "v1.0-mini"]
assert version in available_vers
if version == "v1.0-trainval":
train_scenes = splits.train
# random.shuffle(train_scenes)
# train_scenes = train_scenes[:int(len(train_scenes)*0.2)]
val_scenes = splits.val
elif version == "v1.0-test":
train_scenes = splits.test
val_scenes = []
elif version == "v1.0-mini":
train_scenes = splits.mini_train
val_scenes = splits.mini_val
else:
raise ValueError("unknown")
test = "test" in version
root_path = Path(root_path)
# filter exist scenes. you may only download part of dataset.
available_scenes = _get_available_scenes(nusc)
available_scene_names = [s["name"] for s in available_scenes]
train_scenes = list(filter(lambda x: x in available_scene_names, train_scenes))
val_scenes = list(filter(lambda x: x in available_scene_names, val_scenes))
train_scenes = set(
[
available_scenes[available_scene_names.index(s)]["token"]
for s in train_scenes
]
)
val_scenes = set(
[available_scenes[available_scene_names.index(s)]["token"] for s in val_scenes]
)
if test:
print(f"test scene: {len(train_scenes)}")
else:
print(f"train scene: {len(train_scenes)}, val scene: {len(val_scenes)}")
train_nusc_infos, val_nusc_infos = _fill_trainval_infos(
nusc, train_scenes, val_scenes, test, nsweeps=nsweeps
)
if test:
print(f"test sample: {len(train_nusc_infos)}")
with open(
root_path / "infos_test_{:02d}sweeps_withvelo.pkl".format(nsweeps), "wb"
) as f:
pickle.dump(train_nusc_infos, f)
else:
print(
f"train sample: {len(train_nusc_infos)}, val sample: {len(val_nusc_infos)}"
)
with open(
root_path / "infos_train_{:02d}sweeps_withvelo.pkl".format(nsweeps), "wb"
) as f:
pickle.dump(train_nusc_infos, f)
with open(
root_path / "infos_val_{:02d}sweeps_withvelo.pkl".format(nsweeps), "wb"
) as f:
pickle.dump(val_nusc_infos, f)
def create_nuscenes_infos(root_path, version="v1.0-trainval", nsweeps=10):
nusc = NuScenes(version=version, dataroot=root_path, verbose=True)
available_vers = ["v1.0-trainval", "v1.0-test", "v1.0-mini"]
assert version in available_vers
if version == "v1.0-trainval":
train_scenes = splits.train
# random.shuffle(train_scenes)
# train_scenes = train_scenes[:int(len(train_scenes)*0.2)]
val_scenes = splits.val
elif version == "v1.0-test":
train_scenes = splits.test
val_scenes = []
elif version == "v1.0-mini":
train_scenes = splits.mini_train
val_scenes = splits.mini_val
else:
raise ValueError("unknown")
test = "test" in version
root_path = Path(root_path)
# filter exist scenes. you may only download part of dataset.
available_scenes = _get_available_scenes(nusc)
available_scene_names = [s["name"] for s in available_scenes]
train_scenes = list(filter(lambda x: x in available_scene_names, train_scenes))
val_scenes = list(filter(lambda x: x in available_scene_names, val_scenes))
train_scenes = set(
[
available_scenes[available_scene_names.index(s)]["token"]
for s in train_scenes
]
)
val_scenes = set(
[available_scenes[available_scene_names.index(s)]["token"] for s in val_scenes]
)
if test:
print(f"test scene: {len(train_scenes)}")
else:
print(f"train scene: {len(train_scenes)}, val scene: {len(val_scenes)}")
train_nusc_infos, val_nusc_infos = _fill_trainval_infos(
nusc, train_scenes, val_scenes, test, nsweeps=nsweeps
)
if test:
print(f"test sample: {len(train_nusc_infos)}")
with open(
root_path / "infos_test_{:02d}sweeps_withvelo.pkl".format(nsweeps), "wb"
) as f:
pickle.dump(train_nusc_infos, f)
else:
print(
f"train sample: {len(train_nusc_infos)}, val sample: {len(val_nusc_infos)}"
)
with open(
root_path / "infos_train_{:02d}sweeps_withvelo.pkl".format(nsweeps), "wb"
) as f:
pickle.dump(train_nusc_infos, f)
with open(
root_path / "infos_val_{:02d}sweeps_withvelo.pkl".format(nsweeps), "wb"
) as f:
pickle.dump(val_nusc_infos, f)
def get_box_mean(info_path, class_name="vehicle.car"):
with open(info_path, "rb") as f:
nusc_infos = pickle.load(f)
gt_boxes_list = []
for info in nusc_infos:
mask = np.array([s == class_name for s in info["gt_names"]], dtype=np.bool_)
gt_boxes_list.append(info["gt_boxes"][mask].reshape(-1, 7))
gt_boxes_list = np.concatenate(gt_boxes_list, axis=0)
print(gt_boxes_list.mean(0))
def eval_main(nusc, eval_version, res_path, eval_set, output_dir):
# nusc = NuScenes(version=version, dataroot=str(root_path), verbose=True)
cfg = config_factory(eval_version)
nusc_eval = NuScenesEval(
nusc,
config=cfg,
result_path=res_path,
eval_set=eval_set,
output_dir=output_dir,
verbose=True,
)
metrics_summary = nusc_eval.main(plot_examples=0,)