scannet_dataset.py 12.2 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
2
import warnings
zhangwenwei's avatar
zhangwenwei committed
3
from os import path as osp
ZCMax's avatar
ZCMax committed
4
from typing import Callable, List, Optional, Union
5

6
7
import numpy as np

8
from mmdet3d.registry import DATASETS
zhangshilong's avatar
zhangshilong committed
9
from mmdet3d.structures import DepthInstance3DBoxes
jshilong's avatar
jshilong committed
10
from .det3d_dataset import Det3DDataset
ZCMax's avatar
ZCMax committed
11
from .seg3d_dataset import Seg3DDataset
12
13
14


@DATASETS.register_module()
jshilong's avatar
jshilong committed
15
class ScanNetDataset(Det3DDataset):
16
    r"""ScanNet Dataset for Detection Task.
17

wangtai's avatar
wangtai committed
18
19
    This class serves as the API for experiments on the ScanNet Dataset.

zhangwenwei's avatar
zhangwenwei committed
20
21
    Please refer to the `github repo <https://github.com/ScanNet/ScanNet>`_
    for data downloading.
wangtai's avatar
wangtai committed
22
23
24
25

    Args:
        data_root (str): Path of dataset root.
        ann_file (str): Path of annotation file.
jshilong's avatar
jshilong committed
26
27
28
29
30
31
32
        metainfo (dict, optional): Meta information for dataset, such as class
            information. Defaults to None.
        data_prefix (dict): Prefix for data. Defaults to
            `dict(pts='points',
                pts_isntance_mask='instance_mask',
                pts_semantic_mask='semantic_mask')`.
        pipeline (list[dict]): Pipeline used for data processing.
wangtai's avatar
wangtai committed
33
            Defaults to None.
jshilong's avatar
jshilong committed
34
        modality (dict): Modality to specify the sensor data used
wangtai's avatar
wangtai committed
35
            as input. Defaults to None.
jshilong's avatar
jshilong committed
36
        box_type_3d (str): Type of 3D box of this dataset.
wangtai's avatar
wangtai committed
37
38
            Based on the `box_type_3d`, the dataset will encapsulate the box
            to its original format then converted them to `box_type_3d`.
39
            Defaults to 'Depth' in this dataset. Available options includes:
wangtai's avatar
wangtai committed
40

wangtai's avatar
wangtai committed
41
42
43
            - 'LiDAR': Box in LiDAR coordinates.
            - 'Depth': Box in depth coordinates, usually for indoor dataset.
            - 'Camera': Box in camera coordinates.
jshilong's avatar
jshilong committed
44
        filter_empty_gt (bool): Whether to filter empty GT.
wangtai's avatar
wangtai committed
45
            Defaults to True.
jshilong's avatar
jshilong committed
46
        test_mode (bool): Whether the dataset is in test mode.
wangtai's avatar
wangtai committed
47
48
            Defaults to False.
    """
jshilong's avatar
jshilong committed
49
50
51
52
    METAINFO = {
        'CLASSES':
        ('cabinet', 'bed', 'chair', 'sofa', 'table', 'door', 'window',
         'bookshelf', 'picture', 'counter', 'desk', 'curtain', 'refrigerator',
53
54
55
56
57
58
         'showercurtrain', 'toilet', 'sink', 'bathtub', 'garbagebin'),
        # the valid ids of segmentation annotations
        'seg_valid_class_ids':
        (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36, 39),
        'seg_all_class_ids':
        tuple(range(1, 41))
jshilong's avatar
jshilong committed
59
    }
60
61

    def __init__(self,
jshilong's avatar
jshilong committed
62
63
                 data_root: str,
                 ann_file: str,
64
                 metainfo: Optional[dict] = None,
jshilong's avatar
jshilong committed
65
66
                 data_prefix: dict = dict(
                     pts='points',
67
                     pts_instance_mask='instance_mask',
jshilong's avatar
jshilong committed
68
69
                     pts_semantic_mask='semantic_mask'),
                 pipeline: List[Union[dict, Callable]] = [],
70
                 modality: dict = dict(use_camera=False, use_lidar=True),
jshilong's avatar
jshilong committed
71
72
73
                 box_type_3d: str = 'Depth',
                 filter_empty_gt: bool = True,
                 test_mode: bool = False,
74
                 **kwargs):
75
76
77
78
79
80
81
82
83
84
85

        # construct seg_label_mapping for semantic mask
        seg_max_cat_id = len(self.METAINFO['seg_all_class_ids'])
        seg_valid_cat_ids = self.METAINFO['seg_valid_class_ids']
        neg_label = len(seg_valid_cat_ids)
        seg_label_mapping = np.ones(
            seg_max_cat_id + 1, dtype=np.int) * neg_label
        for cls_idx, cat_id in enumerate(seg_valid_cat_ids):
            seg_label_mapping[cat_id] = cls_idx
        self.seg_label_mapping = seg_label_mapping

86
87
88
        super().__init__(
            data_root=data_root,
            ann_file=ann_file,
jshilong's avatar
jshilong committed
89
90
            metainfo=metainfo,
            data_prefix=data_prefix,
91
92
93
94
            pipeline=pipeline,
            modality=modality,
            box_type_3d=box_type_3d,
            filter_empty_gt=filter_empty_gt,
95
96
            test_mode=test_mode,
            **kwargs)
97
98

        self.metainfo['seg_label_mapping'] = self.seg_label_mapping
99
        assert 'use_camera' in self.modality and \
jshilong's avatar
jshilong committed
100
101
               'use_lidar' in self.modality
        assert self.modality['use_camera'] or self.modality['use_lidar']
102

jshilong's avatar
jshilong committed
103
    @staticmethod
104
    def _get_axis_align_matrix(info: dict) -> np.ndarray:
jshilong's avatar
jshilong committed
105
        """Get axis_align_matrix from info. If not exist, return identity mat.
106
107

        Args:
jshilong's avatar
jshilong committed
108
            info (dict): Info of a single sample data.
109
110

        Returns:
jshilong's avatar
jshilong committed
111
            np.ndarray: 4x4 transformation matrix.
112
        """
jshilong's avatar
jshilong committed
113
114
        if 'axis_align_matrix' in info:
            return np.array(info['axis_align_matrix'])
115
        else:
jshilong's avatar
jshilong committed
116
117
118
119
            warnings.warn(
                'axis_align_matrix is not found in ScanNet data info, please '
                'use new pre-process scripts to re-generate ScanNet data')
            return np.eye(4).astype(np.float32)
liyinhao's avatar
liyinhao committed
120

jshilong's avatar
jshilong committed
121
122
    def parse_data_info(self, info: dict) -> dict:
        """Process the raw data info.
123

jshilong's avatar
jshilong committed
124
125
        The only difference with it in `Det3DDataset`
        is the specific process for `axis_align_matrix'.
126
127

        Args:
jshilong's avatar
jshilong committed
128
            info (dict): Raw info dict.
129
130

        Returns:
jshilong's avatar
jshilong committed
131
            dict: Data information that will be passed to the data
zhangshilong's avatar
zhangshilong committed
132
            preprocessing transforms. It includes the following keys:
133
        """
jshilong's avatar
jshilong committed
134
135
136
137
138
139
140
141
142
        info['axis_align_matrix'] = self._get_axis_align_matrix(info)
        info['pts_instance_mask_path'] = osp.join(
            self.data_prefix.get('pts_instance_mask', ''),
            info['pts_instance_mask_path'])
        info['pts_semantic_mask_path'] = osp.join(
            self.data_prefix.get('pts_semantic_mask', ''),
            info['pts_semantic_mask_path'])

        info = super().parse_data_info(info)
143
144
145
        # only be used in `PointSegClassMapping` in pipeline
        # to map original semantic class to valid category ids.
        info['seg_label_mapping'] = self.seg_label_mapping
jshilong's avatar
jshilong committed
146
147
148
149
        return info

    def parse_ann_info(self, info: dict) -> dict:
        """Process the `instances` in data info to `ann_info`
150
151

        Args:
jshilong's avatar
jshilong committed
152
            info (dict): Info dict.
153
154

        Returns:
jshilong's avatar
jshilong committed
155
            dict: Processed `ann_info`
156
        """
jshilong's avatar
jshilong committed
157
        ann_info = super().parse_ann_info(info)
158
159
        # empty gt
        if ann_info is None:
jshilong's avatar
jshilong committed
160
            ann_info = dict()
161
162
            ann_info['gt_bboxes_3d'] = np.zeros((0, 6), dtype=np.float32)
            ann_info['gt_labels_3d'] = np.zeros((0, ), dtype=np.int64)
jshilong's avatar
jshilong committed
163
        # to target box structure
164

jshilong's avatar
jshilong committed
165
166
167
168
169
170
171
        ann_info['gt_bboxes_3d'] = DepthInstance3DBoxes(
            ann_info['gt_bboxes_3d'],
            box_dim=ann_info['gt_bboxes_3d'].shape[-1],
            with_yaw=False,
            origin=(0.5, 0.5, 0.5)).convert_to(self.box_mode_3d)

        return ann_info
172

173
174

@DATASETS.register_module()
ZCMax's avatar
ZCMax committed
175
class ScanNetSegDataset(Seg3DDataset):
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    r"""ScanNet Dataset for Semantic Segmentation Task.

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

    Please refer to the `github repo <https://github.com/ScanNet/ScanNet>`_
    for data downloading.

    Args:
        data_root (str): Path of dataset root.
        ann_file (str): Path of annotation file.
        pipeline (list[dict], optional): Pipeline used for data processing.
            Defaults to None.
        classes (tuple[str], optional): Classes used in the dataset.
            Defaults to None.
        palette (list[list[int]], optional): The palette of segmentation map.
            Defaults to None.
        modality (dict, optional): Modality to specify the sensor data used
            as input. Defaults to None.
        test_mode (bool, optional): Whether the dataset is in test mode.
            Defaults to False.
196
        ignore_index (int, optional): The label index to be ignored, e.g.
197
198
199
200
201
202
            unannotated points. If None is given, set to len(self.CLASSES).
            Defaults to None.
        scene_idxs (np.ndarray | str, optional): Precomputed index to load
            data. For scenes with many points, we may sample it several times.
            Defaults to None.
    """
ZCMax's avatar
ZCMax committed
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
    METAINFO = {
        'CLASSES':
        ('wall', 'floor', 'cabinet', 'bed', 'chair', 'sofa', 'table', 'door',
         'window', 'bookshelf', 'picture', 'counter', 'desk', 'curtain',
         'refrigerator', 'showercurtrain', 'toilet', 'sink', 'bathtub',
         'otherfurniture'),
        'PALETTE': [
            [174, 199, 232],
            [152, 223, 138],
            [31, 119, 180],
            [255, 187, 120],
            [188, 189, 34],
            [140, 86, 75],
            [255, 152, 150],
            [214, 39, 40],
            [197, 176, 213],
            [148, 103, 189],
            [196, 156, 148],
            [23, 190, 207],
            [247, 182, 210],
            [219, 219, 141],
            [255, 127, 14],
            [158, 218, 229],
            [44, 160, 44],
            [112, 128, 144],
            [227, 119, 194],
            [82, 84, 163],
        ],
231
232
233
        'seg_valid_class_ids': (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16,
                                24, 28, 33, 34, 36, 39),
        'seg_all_class_ids':
ZCMax's avatar
ZCMax committed
234
235
        tuple(range(41)),
    }
236
237

    def __init__(self,
ZCMax's avatar
ZCMax committed
238
239
240
241
242
243
244
                 data_root: Optional[str] = None,
                 ann_file: str = '',
                 metainfo: Optional[dict] = None,
                 data_prefix: dict = dict(
                     pts='points', img='', instance_mask='', semantic_mask=''),
                 pipeline: List[Union[dict, Callable]] = [],
                 modality: dict = dict(use_lidar=True, use_camera=False),
245
                 ignore_index=None,
246
                 scene_idxs=None,
ZCMax's avatar
ZCMax committed
247
248
                 test_mode=False,
                 **kwargs) -> None:
249
250
251
        super().__init__(
            data_root=data_root,
            ann_file=ann_file,
ZCMax's avatar
ZCMax committed
252
253
            metainfo=metainfo,
            data_prefix=data_prefix,
254
255
256
            pipeline=pipeline,
            modality=modality,
            ignore_index=ignore_index,
257
            scene_idxs=scene_idxs,
ZCMax's avatar
ZCMax committed
258
            test_mode=test_mode,
259
            **kwargs)
260

261
262
    def get_scene_idxs(self, scene_idxs):
        """Compute scene_idxs for data sampling.
263

264
        We sample more times for scenes with more points.
265
266
267
268
269
270
        """
        # when testing, we load one whole scene every time
        if not self.test_mode and scene_idxs is None:
            raise NotImplementedError(
                'please provide re-sampled scene indexes for training')

271
        return super().get_scene_idxs(scene_idxs)
272

273
274

@DATASETS.register_module()
ZCMax's avatar
ZCMax committed
275
class ScanNetInstanceSegDataset(Seg3DDataset):
276

ZCMax's avatar
ZCMax committed
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
    METAINFO = {
        'CLASSES':
        ('cabinet', 'bed', 'chair', 'sofa', 'table', 'door', 'window',
         'bookshelf', 'picture', 'counter', 'desk', 'curtain', 'refrigerator',
         'showercurtrain', 'toilet', 'sink', 'bathtub', 'garbagebin'),
        'PLATTE': [
            [174, 199, 232],
            [152, 223, 138],
            [31, 119, 180],
            [255, 187, 120],
            [188, 189, 34],
            [140, 86, 75],
            [255, 152, 150],
            [214, 39, 40],
            [197, 176, 213],
            [148, 103, 189],
            [196, 156, 148],
            [23, 190, 207],
            [247, 182, 210],
            [219, 219, 141],
            [255, 127, 14],
            [158, 218, 229],
            [44, 160, 44],
            [112, 128, 144],
            [227, 119, 194],
            [82, 84, 163],
        ],
304
        'seg_valid_class_ids':
ZCMax's avatar
ZCMax committed
305
        (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36, 39),
306
        'seg_all_class_ids':
ZCMax's avatar
ZCMax committed
307
308
        tuple(range(41))
    }
309

ZCMax's avatar
ZCMax committed
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
    def __init__(self,
                 data_root: Optional[str] = None,
                 ann_file: str = '',
                 metainfo: Optional[dict] = None,
                 data_prefix: dict = dict(
                     pts='points', img='', instance_mask='', semantic_mask=''),
                 pipeline: List[Union[dict, Callable]] = [],
                 modality: dict = dict(use_lidar=True, use_camera=False),
                 test_mode=False,
                 ignore_index=None,
                 scene_idxs=None,
                 file_client_args=dict(backend='disk'),
                 **kwargs) -> None:
        super().__init__(
            data_root=data_root,
            ann_file=ann_file,
            metainfo=metainfo,
            pipeline=pipeline,
            data_prefix=data_prefix,
            modality=modality,
            test_mode=test_mode,
            ignore_index=ignore_index,
            scene_idxs=scene_idxs,
            file_client_args=file_client_args,
            **kwargs)