nuscenes_dataset.py 4.59 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
VVsssssk's avatar
VVsssssk committed
2
from typing import Dict, List
3

zhangwenwei's avatar
zhangwenwei committed
4
5
import numpy as np

6
from mmdet3d.registry import DATASETS
VVsssssk's avatar
VVsssssk committed
7
from ..core.bbox import LiDARInstance3DBoxes
jshilong's avatar
jshilong committed
8
from .det3d_dataset import Det3DDataset
zhangwenwei's avatar
zhangwenwei committed
9
10


11
@DATASETS.register_module()
jshilong's avatar
jshilong committed
12
class NuScenesDataset(Det3DDataset):
wangtai's avatar
wangtai committed
13
    r"""NuScenes Dataset.
wangtai's avatar
wangtai committed
14
15
16

    This class serves as the API for experiments on the NuScenes Dataset.

zhangwenwei's avatar
zhangwenwei committed
17
18
    Please refer to `NuScenes Dataset <https://www.nuscenes.org/download>`_
    for data downloading.
wangtai's avatar
wangtai committed
19
20

    Args:
VVsssssk's avatar
VVsssssk committed
21
        data_root (str): Path of dataset root.
wangtai's avatar
wangtai committed
22
23
24
        ann_file (str): Path of annotation file.
        pipeline (list[dict], optional): Pipeline used for data processing.
            Defaults to None.
VVsssssk's avatar
VVsssssk committed
25
        box_type_3d (str): Type of 3D box of this dataset.
wangtai's avatar
wangtai committed
26
27
            Based on the `box_type_3d`, the dataset will encapsulate the box
            to its original format then converted them to `box_type_3d`.
yinchimaoliang's avatar
yinchimaoliang committed
28
            Defaults to 'LiDAR' in this dataset. Available options includes.
VVsssssk's avatar
VVsssssk committed
29

wangtai's avatar
wangtai committed
30
31
32
            - 'LiDAR': Box in LiDAR coordinates.
            - 'Depth': Box in depth coordinates, usually for indoor dataset.
            - 'Camera': Box in camera coordinates.
VVsssssk's avatar
VVsssssk committed
33
34
35
        modality (dict, optional): Modality to specify the sensor data used
            as input. Defaults to dict(use_camera=False,use_lidar=True).
        filter_empty_gt (bool): Whether to filter empty GT.
wangtai's avatar
wangtai committed
36
            Defaults to True.
VVsssssk's avatar
VVsssssk committed
37
        test_mode (bool): Whether the dataset is in test mode.
wangtai's avatar
wangtai committed
38
            Defaults to False.
VVsssssk's avatar
VVsssssk committed
39
40
41
        with_velocity (bool): Whether include velocity prediction
            into the experiments. Defaults to True.
        use_valid_flag (bool): Whether to use `use_valid_flag` key
42
43
            in the info file as mask to filter gt_boxes and gt_names.
            Defaults to False.
wangtai's avatar
wangtai committed
44
    """
VVsssssk's avatar
VVsssssk committed
45
46
47
48
49
50
    METAINFO = {
        'CLASSES':
        ('car', 'truck', 'trailer', 'bus', 'construction_vehicle', 'bicycle',
         'motorcycle', 'pedestrian', 'traffic_cone', 'barrier'),
        'version':
        'v1.0-trainval'
zhangwenwei's avatar
zhangwenwei committed
51
52
53
    }

    def __init__(self,
VVsssssk's avatar
VVsssssk committed
54
55
56
57
58
59
60
61
62
63
64
65
66
                 data_root: str,
                 ann_file: str,
                 pipeline: List[dict] = None,
                 box_type_3d: str = 'LiDAR',
                 modality: Dict = dict(
                     use_camera=False,
                     use_lidar=True,
                 ),
                 filter_empty_gt: bool = True,
                 test_mode: bool = False,
                 with_velocity: bool = True,
                 use_valid_flag: bool = False,
                 **kwargs):
yinchimaoliang's avatar
yinchimaoliang committed
67
        self.use_valid_flag = use_valid_flag
VVsssssk's avatar
VVsssssk committed
68
69
        self.with_velocity = with_velocity
        assert box_type_3d.lower() == 'lidar'
zhangwenwei's avatar
zhangwenwei committed
70
71
72
73
        super().__init__(
            data_root=data_root,
            ann_file=ann_file,
            modality=modality,
VVsssssk's avatar
VVsssssk committed
74
            pipeline=pipeline,
75
76
            box_type_3d=box_type_3d,
            filter_empty_gt=filter_empty_gt,
VVsssssk's avatar
VVsssssk committed
77
78
            test_mode=test_mode,
            **kwargs)
yinchimaoliang's avatar
yinchimaoliang committed
79

VVsssssk's avatar
VVsssssk committed
80
    def parse_ann_info(self, info: dict) -> dict:
81
82
83
        """Get annotation info according to the given index.

        Args:
VVsssssk's avatar
VVsssssk committed
84
            info (dict): Data information of single data sample.
85
86

        Returns:
VVsssssk's avatar
VVsssssk committed
87
            dict: annotation information consists of the following keys:
88

89
                - gt_bboxes_3d (:obj:`LiDARInstance3DBoxes`):
VVsssssk's avatar
VVsssssk committed
90
                    3D ground truth bboxes.
wangtai's avatar
wangtai committed
91
                - gt_labels_3d (np.ndarray): Labels of ground truths.
92
        """
VVsssssk's avatar
VVsssssk committed
93
94
95
96
97
98
99
        ann_info = super().parse_ann_info(info)
        if ann_info is None:
            # empty instance
            anns_results = dict()
            anns_results['gt_bboxes_3d'] = np.zeros((0, 7), dtype=np.float32)
            anns_results['gt_labels_3d'] = np.zeros(0, dtype=np.int64)
            return anns_results
yinchimaoliang's avatar
yinchimaoliang committed
100
        if self.use_valid_flag:
VVsssssk's avatar
VVsssssk committed
101
            mask = ann_info['bbox_3d_isvalid']
yinchimaoliang's avatar
yinchimaoliang committed
102
        else:
VVsssssk's avatar
VVsssssk committed
103
104
105
            mask = ann_info['num_lidar_pts'] > 0
        gt_bboxes_3d = ann_info['gt_bboxes_3d'][mask]
        gt_labels_3d = ann_info['gt_labels_3d'][mask]
zhangwenwei's avatar
zhangwenwei committed
106
107

        if self.with_velocity:
VVsssssk's avatar
VVsssssk committed
108
            gt_velocity = ann_info['velocity'][mask]
zhangwenwei's avatar
zhangwenwei committed
109
110
111
112
            nan_mask = np.isnan(gt_velocity[:, 0])
            gt_velocity[nan_mask] = [0.0, 0.0]
            gt_bboxes_3d = np.concatenate([gt_bboxes_3d, gt_velocity], axis=-1)

wangtai's avatar
wangtai committed
113
        # the nuscenes box center is [0.5, 0.5, 0.5], we change it to be
wuyuefeng's avatar
wuyuefeng committed
114
        # the same as KITTI (0.5, 0.5, 0)
zhangwenwei's avatar
zhangwenwei committed
115
116
117
        gt_bboxes_3d = LiDARInstance3DBoxes(
            gt_bboxes_3d,
            box_dim=gt_bboxes_3d.shape[-1],
wuyuefeng's avatar
wuyuefeng committed
118
            origin=(0.5, 0.5, 0.5)).convert_to(self.box_mode_3d)
zhangwenwei's avatar
zhangwenwei committed
119

zhangwenwei's avatar
zhangwenwei committed
120
        anns_results = dict(
VVsssssk's avatar
VVsssssk committed
121
            gt_bboxes_3d=gt_bboxes_3d, gt_labels_3d=gt_labels_3d)
zhangwenwei's avatar
zhangwenwei committed
122
        return anns_results