loading.py 25.2 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
zhangwenwei's avatar
zhangwenwei committed
2
3
4
import mmcv
import numpy as np

5
from mmdet3d.core.points import BasePoints, get_points_type
6
from mmdet.datasets.builder import PIPELINES
7
from mmdet.datasets.pipelines import LoadAnnotations, LoadImageFromFile
zhangwenwei's avatar
zhangwenwei committed
8
9


10
@PIPELINES.register_module()
zhangwenwei's avatar
zhangwenwei committed
11
class LoadMultiViewImageFromFiles(object):
zhangwenwei's avatar
zhangwenwei committed
12
    """Load multi channel images from a list of separate channel files.
zhangwenwei's avatar
zhangwenwei committed
13

liyinhao's avatar
liyinhao committed
14
15
16
    Expects results['img_filename'] to be a list of filenames.

    Args:
17
        to_float32 (bool, optional): Whether to convert the img to float32.
liyinhao's avatar
liyinhao committed
18
            Defaults to False.
19
20
        color_type (str, optional): Color type of the file.
            Defaults to 'unchanged'.
zhangwenwei's avatar
zhangwenwei committed
21
    """
zhangwenwei's avatar
zhangwenwei committed
22

zhangwenwei's avatar
zhangwenwei committed
23
24
25
    def __init__(self, to_float32=False, color_type='unchanged'):
        self.to_float32 = to_float32
        self.color_type = color_type
zhangwenwei's avatar
zhangwenwei committed
26
27

    def __call__(self, results):
28
29
30
31
32
33
        """Call function to load multi-view image from files.

        Args:
            results (dict): Result dict containing multi-view image filenames.

        Returns:
34
            dict: The result dict containing the multi-view image data.
35
36
37
38
39
40
41
42
43
44
                Added keys and values are described below.

                - filename (str): Multi-view image filenames.
                - img (np.ndarray): Multi-view image arrays.
                - img_shape (tuple[int]): Shape of multi-view image arrays.
                - ori_shape (tuple[int]): Shape of original image arrays.
                - pad_shape (tuple[int]): Shape of padded image arrays.
                - scale_factor (float): Scale factor.
                - img_norm_cfg (dict): Normalization configuration of images.
        """
zhangwenwei's avatar
zhangwenwei committed
45
        filename = results['img_filename']
46
        # img is of shape (h, w, c, num_views)
zhangwenwei's avatar
zhangwenwei committed
47
48
49
50
51
        img = np.stack(
            [mmcv.imread(name, self.color_type) for name in filename], axis=-1)
        if self.to_float32:
            img = img.astype(np.float32)
        results['filename'] = filename
52
        # unravel to list, see `DefaultFormatBundle` in formatting.py
53
54
        # which will transpose each image separately and then stack into array
        results['img'] = [img[..., i] for i in range(img.shape[-1])]
zhangwenwei's avatar
zhangwenwei committed
55
56
57
58
59
60
61
62
63
64
        results['img_shape'] = img.shape
        results['ori_shape'] = img.shape
        # Set initial values for default meta_keys
        results['pad_shape'] = img.shape
        results['scale_factor'] = 1.0
        num_channels = 1 if len(img.shape) < 3 else img.shape[2]
        results['img_norm_cfg'] = dict(
            mean=np.zeros(num_channels, dtype=np.float32),
            std=np.ones(num_channels, dtype=np.float32),
            to_rgb=False)
zhangwenwei's avatar
zhangwenwei committed
65
66
67
        return results

    def __repr__(self):
68
        """str: Return a string that describes the module."""
69
70
71
72
        repr_str = self.__class__.__name__
        repr_str += f'(to_float32={self.to_float32}, '
        repr_str += f"color_type='{self.color_type}')"
        return repr_str
zhangwenwei's avatar
zhangwenwei committed
73
74


75
76
77
78
79
80
@PIPELINES.register_module()
class LoadImageFromFileMono3D(LoadImageFromFile):
    """Load an image from file in monocular 3D object detection. Compared to 2D
    detection, additional camera parameters need to be loaded.

    Args:
81
        kwargs (dict): Arguments are the same as those in
82
83
84
85
86
87
88
89
90
91
92
93
94
            :class:`LoadImageFromFile`.
    """

    def __call__(self, results):
        """Call functions to load image and get image meta information.

        Args:
            results (dict): Result dict from :obj:`mmdet.CustomDataset`.

        Returns:
            dict: The dict contains loaded image and meta information.
        """
        super().__call__(results)
95
        results['cam2img'] = results['img_info']['cam_intrinsic']
96
97
98
        return results


zhangwenwei's avatar
zhangwenwei committed
99
100
@PIPELINES.register_module()
class LoadPointsFromMultiSweeps(object):
zhangwenwei's avatar
zhangwenwei committed
101
    """Load points from multiple sweeps.
zhangwenwei's avatar
zhangwenwei committed
102

zhangwenwei's avatar
zhangwenwei committed
103
104
105
    This is usually used for nuScenes dataset to utilize previous sweeps.

    Args:
106
107
108
109
110
111
112
        sweeps_num (int, optional): Number of sweeps. Defaults to 10.
        load_dim (int, optional): Dimension number of the loaded points.
            Defaults to 5.
        use_dim (list[int], optional): Which dimension to use.
            Defaults to [0, 1, 2, 4].
        file_client_args (dict, optional): Config dict of file clients,
            refer to
zhangwenwei's avatar
zhangwenwei committed
113
            https://github.com/open-mmlab/mmcv/blob/master/mmcv/fileio/file_client.py
liyinhao's avatar
liyinhao committed
114
            for more details. Defaults to dict(backend='disk').
115
        pad_empty_sweeps (bool, optional): Whether to repeat keyframe when
116
            sweeps is empty. Defaults to False.
117
        remove_close (bool, optional): Whether to remove close points.
118
            Defaults to False.
119
        test_mode (bool, optional): If `test_mode=True`, it will not
120
121
            randomly sample sweeps but select the nearest N frames.
            Defaults to False.
zhangwenwei's avatar
zhangwenwei committed
122
123
124
125
126
    """

    def __init__(self,
                 sweeps_num=10,
                 load_dim=5,
127
128
129
130
131
                 use_dim=[0, 1, 2, 4],
                 file_client_args=dict(backend='disk'),
                 pad_empty_sweeps=False,
                 remove_close=False,
                 test_mode=False):
zhangwenwei's avatar
zhangwenwei committed
132
        self.load_dim = load_dim
zhangwenwei's avatar
zhangwenwei committed
133
        self.sweeps_num = sweeps_num
134
        self.use_dim = use_dim
zhangwenwei's avatar
zhangwenwei committed
135
136
        self.file_client_args = file_client_args.copy()
        self.file_client = None
137
138
139
        self.pad_empty_sweeps = pad_empty_sweeps
        self.remove_close = remove_close
        self.test_mode = test_mode
zhangwenwei's avatar
zhangwenwei committed
140
141

    def _load_points(self, pts_filename):
142
143
144
145
146
147
148
149
        """Private function to load point clouds data.

        Args:
            pts_filename (str): Filename of point clouds data.

        Returns:
            np.ndarray: An array containing point clouds data.
        """
zhangwenwei's avatar
zhangwenwei committed
150
151
152
153
154
155
156
157
158
159
160
161
        if self.file_client is None:
            self.file_client = mmcv.FileClient(**self.file_client_args)
        try:
            pts_bytes = self.file_client.get(pts_filename)
            points = np.frombuffer(pts_bytes, dtype=np.float32)
        except ConnectionError:
            mmcv.check_file_exist(pts_filename)
            if pts_filename.endswith('.npy'):
                points = np.load(pts_filename)
            else:
                points = np.fromfile(pts_filename, dtype=np.float32)
        return points
zhangwenwei's avatar
zhangwenwei committed
162

163
164
165
166
    def _remove_close(self, points, radius=1.0):
        """Removes point too close within a certain radius from origin.

        Args:
167
            points (np.ndarray | :obj:`BasePoints`): Sweep points.
168
            radius (float, optional): Radius below which points are removed.
169
170
171
172
173
                Defaults to 1.0.

        Returns:
            np.ndarray: Points after removing.
        """
174
175
176
177
178
179
180
181
        if isinstance(points, np.ndarray):
            points_numpy = points
        elif isinstance(points, BasePoints):
            points_numpy = points.tensor.numpy()
        else:
            raise NotImplementedError
        x_filt = np.abs(points_numpy[:, 0]) < radius
        y_filt = np.abs(points_numpy[:, 1]) < radius
182
        not_close = np.logical_not(np.logical_and(x_filt, y_filt))
183
        return points[not_close]
184

zhangwenwei's avatar
zhangwenwei committed
185
    def __call__(self, results):
186
187
188
        """Call function to load multi-sweep point clouds from files.

        Args:
189
            results (dict): Result dict containing multi-sweep point cloud
190
191
192
                filenames.

        Returns:
193
            dict: The result dict containing the multi-sweep points data.
194
195
                Added key and value are described below.

196
                - points (np.ndarray | :obj:`BasePoints`): Multi-sweep point
197
                    cloud arrays.
198
        """
zhangwenwei's avatar
zhangwenwei committed
199
        points = results['points']
200
        points.tensor[:, 4] = 0
zhangwenwei's avatar
zhangwenwei committed
201
202
        sweep_points_list = [points]
        ts = results['timestamp']
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
        if self.pad_empty_sweeps and len(results['sweeps']) == 0:
            for i in range(self.sweeps_num):
                if self.remove_close:
                    sweep_points_list.append(self._remove_close(points))
                else:
                    sweep_points_list.append(points)
        else:
            if len(results['sweeps']) <= self.sweeps_num:
                choices = np.arange(len(results['sweeps']))
            elif self.test_mode:
                choices = np.arange(self.sweeps_num)
            else:
                choices = np.random.choice(
                    len(results['sweeps']), self.sweeps_num, replace=False)
            for idx in choices:
                sweep = results['sweeps'][idx]
                points_sweep = self._load_points(sweep['data_path'])
                points_sweep = np.copy(points_sweep).reshape(-1, self.load_dim)
                if self.remove_close:
                    points_sweep = self._remove_close(points_sweep)
                sweep_ts = sweep['timestamp'] / 1e6
                points_sweep[:, :3] = points_sweep[:, :3] @ sweep[
                    'sensor2lidar_rotation'].T
                points_sweep[:, :3] += sweep['sensor2lidar_translation']
                points_sweep[:, 4] = ts - sweep_ts
228
                points_sweep = points.new_point(points_sweep)
229
230
                sweep_points_list.append(points_sweep)

231
232
        points = points.cat(sweep_points_list)
        points = points[:, self.use_dim]
zhangwenwei's avatar
zhangwenwei committed
233
234
235
236
        results['points'] = points
        return results

    def __repr__(self):
237
        """str: Return a string that describes the module."""
zhangwenwei's avatar
zhangwenwei committed
238
        return f'{self.__class__.__name__}(sweeps_num={self.sweeps_num})'
wuyuefeng's avatar
wuyuefeng committed
239
240
241
242
243
244
245
246
247
248


@PIPELINES.register_module()
class PointSegClassMapping(object):
    """Map original semantic class to valid category ids.

    Map valid classes as 0~len(valid_cat_ids)-1 and
    others as len(valid_cat_ids).

    Args:
249
        valid_cat_ids (tuple[int]): A tuple of valid category.
250
251
        max_cat_id (int, optional): The max possible cat_id in input
            segmentation mask. Defaults to 40.
wuyuefeng's avatar
wuyuefeng committed
252
253
    """

254
255
256
257
    def __init__(self, valid_cat_ids, max_cat_id=40):
        assert max_cat_id >= np.max(valid_cat_ids), \
            'max_cat_id should be greater than maximum id in valid_cat_ids'

wuyuefeng's avatar
wuyuefeng committed
258
        self.valid_cat_ids = valid_cat_ids
259
260
261
262
263
264
265
266
        self.max_cat_id = int(max_cat_id)

        # build cat_id to class index mapping
        neg_cls = len(valid_cat_ids)
        self.cat_id2class = np.ones(
            self.max_cat_id + 1, dtype=np.int) * neg_cls
        for cls_idx, cat_id in enumerate(valid_cat_ids):
            self.cat_id2class[cat_id] = cls_idx
wuyuefeng's avatar
wuyuefeng committed
267
268

    def __call__(self, results):
269
270
271
272
273
274
        """Call function to map original semantic class to valid category ids.

        Args:
            results (dict): Result dict containing point semantic masks.

        Returns:
275
            dict: The result dict containing the mapped category ids.
276
277
278
279
                Updated key and value are described below.

                - pts_semantic_mask (np.ndarray): Mapped semantic masks.
        """
wuyuefeng's avatar
wuyuefeng committed
280
281
282
        assert 'pts_semantic_mask' in results
        pts_semantic_mask = results['pts_semantic_mask']

283
        converted_pts_sem_mask = self.cat_id2class[pts_semantic_mask]
wuyuefeng's avatar
wuyuefeng committed
284

285
        results['pts_semantic_mask'] = converted_pts_sem_mask
wuyuefeng's avatar
wuyuefeng committed
286
287
288
        return results

    def __repr__(self):
289
        """str: Return a string that describes the module."""
wuyuefeng's avatar
wuyuefeng committed
290
        repr_str = self.__class__.__name__
291
292
        repr_str += f'(valid_cat_ids={self.valid_cat_ids}, '
        repr_str += f'max_cat_id={self.max_cat_id})'
wuyuefeng's avatar
wuyuefeng committed
293
294
295
296
297
        return repr_str


@PIPELINES.register_module()
class NormalizePointsColor(object):
zhangwenwei's avatar
zhangwenwei committed
298
    """Normalize color of points.
wuyuefeng's avatar
wuyuefeng committed
299
300
301
302
303
304
305
306
307

    Args:
        color_mean (list[float]): Mean color of the point cloud.
    """

    def __init__(self, color_mean):
        self.color_mean = color_mean

    def __call__(self, results):
308
309
310
311
312
313
        """Call function to normalize color of points.

        Args:
            results (dict): Result dict containing point clouds data.

        Returns:
314
            dict: The result dict containing the normalized points.
315
316
                Updated key and value are described below.

317
                - points (:obj:`BasePoints`): Points after color normalization.
318
        """
wuyuefeng's avatar
wuyuefeng committed
319
        points = results['points']
320
321
322
323
324
325
326
        assert points.attribute_dims is not None and \
            'color' in points.attribute_dims.keys(), \
            'Expect points have color attribute'
        if self.color_mean is not None:
            points.color = points.color - \
                points.color.new_tensor(self.color_mean)
        points.color = points.color / 255.0
wuyuefeng's avatar
wuyuefeng committed
327
328
329
330
        results['points'] = points
        return results

    def __repr__(self):
331
        """str: Return a string that describes the module."""
wuyuefeng's avatar
wuyuefeng committed
332
        repr_str = self.__class__.__name__
333
        repr_str += f'(color_mean={self.color_mean})'
wuyuefeng's avatar
wuyuefeng committed
334
335
336
337
338
339
340
        return repr_str


@PIPELINES.register_module()
class LoadPointsFromFile(object):
    """Load Points From File.

341
    Load points from file.
wuyuefeng's avatar
wuyuefeng committed
342
343

    Args:
344
345
346
347
348
        coord_type (str): The type of coordinates of points cloud.
            Available options includes:
            - 'LIDAR': Points in LiDAR coordinates.
            - 'DEPTH': Points in depth coordinates, usually for indoor dataset.
            - 'CAMERA': Points in camera coordinates.
349
        load_dim (int, optional): The dimension of the loaded points.
350
            Defaults to 6.
351
        use_dim (list[int], optional): Which dimensions of the points to use.
liyinhao's avatar
liyinhao committed
352
353
            Defaults to [0, 1, 2]. For KITTI dataset, set use_dim=4
            or use_dim=[0, 1, 2, 3] to use the intensity dimension.
354
355
356
357
358
359
        shift_height (bool, optional): Whether to use shifted height.
            Defaults to False.
        use_color (bool, optional): Whether to use color features.
            Defaults to False.
        file_client_args (dict, optional): Config dict of file clients,
            refer to
wuyuefeng's avatar
wuyuefeng committed
360
            https://github.com/open-mmlab/mmcv/blob/master/mmcv/fileio/file_client.py
liyinhao's avatar
liyinhao committed
361
            for more details. Defaults to dict(backend='disk').
wuyuefeng's avatar
wuyuefeng committed
362
363
364
    """

    def __init__(self,
365
                 coord_type,
wuyuefeng's avatar
wuyuefeng committed
366
367
368
                 load_dim=6,
                 use_dim=[0, 1, 2],
                 shift_height=False,
369
                 use_color=False,
wuyuefeng's avatar
wuyuefeng committed
370
371
                 file_client_args=dict(backend='disk')):
        self.shift_height = shift_height
372
        self.use_color = use_color
wuyuefeng's avatar
wuyuefeng committed
373
374
375
376
        if isinstance(use_dim, int):
            use_dim = list(range(use_dim))
        assert max(use_dim) < load_dim, \
            f'Expect all used dimensions < {load_dim}, got {use_dim}'
377
        assert coord_type in ['CAMERA', 'LIDAR', 'DEPTH']
wuyuefeng's avatar
wuyuefeng committed
378

379
        self.coord_type = coord_type
wuyuefeng's avatar
wuyuefeng committed
380
381
382
383
384
385
        self.load_dim = load_dim
        self.use_dim = use_dim
        self.file_client_args = file_client_args.copy()
        self.file_client = None

    def _load_points(self, pts_filename):
386
387
388
389
390
391
392
393
        """Private function to load point clouds data.

        Args:
            pts_filename (str): Filename of point clouds data.

        Returns:
            np.ndarray: An array containing point clouds data.
        """
wuyuefeng's avatar
wuyuefeng committed
394
395
396
397
398
399
400
401
402
403
404
        if self.file_client is None:
            self.file_client = mmcv.FileClient(**self.file_client_args)
        try:
            pts_bytes = self.file_client.get(pts_filename)
            points = np.frombuffer(pts_bytes, dtype=np.float32)
        except ConnectionError:
            mmcv.check_file_exist(pts_filename)
            if pts_filename.endswith('.npy'):
                points = np.load(pts_filename)
            else:
                points = np.fromfile(pts_filename, dtype=np.float32)
405

wuyuefeng's avatar
wuyuefeng committed
406
407
408
        return points

    def __call__(self, results):
409
410
411
412
413
414
        """Call function to load points data from file.

        Args:
            results (dict): Result dict containing point clouds data.

        Returns:
415
            dict: The result dict containing the point clouds data.
416
417
                Added key and value are described below.

418
                - points (:obj:`BasePoints`): Point clouds data.
419
        """
wuyuefeng's avatar
wuyuefeng committed
420
421
422
423
        pts_filename = results['pts_filename']
        points = self._load_points(pts_filename)
        points = points.reshape(-1, self.load_dim)
        points = points[:, self.use_dim]
424
        attribute_dims = None
wuyuefeng's avatar
wuyuefeng committed
425
426
427
428

        if self.shift_height:
            floor_height = np.percentile(points[:, 2], 0.99)
            height = points[:, 2] - floor_height
429
430
431
            points = np.concatenate(
                [points[:, :3],
                 np.expand_dims(height, 1), points[:, 3:]], 1)
432
433
            attribute_dims = dict(height=3)

434
435
436
437
438
439
440
441
442
443
444
        if self.use_color:
            assert len(self.use_dim) >= 6
            if attribute_dims is None:
                attribute_dims = dict()
            attribute_dims.update(
                dict(color=[
                    points.shape[1] - 3,
                    points.shape[1] - 2,
                    points.shape[1] - 1,
                ]))

445
446
447
        points_class = get_points_type(self.coord_type)
        points = points_class(
            points, points_dim=points.shape[-1], attribute_dims=attribute_dims)
wuyuefeng's avatar
wuyuefeng committed
448
        results['points'] = points
449

wuyuefeng's avatar
wuyuefeng committed
450
451
452
        return results

    def __repr__(self):
453
        """str: Return a string that describes the module."""
liyinhao's avatar
liyinhao committed
454
        repr_str = self.__class__.__name__ + '('
455
456
457
458
459
        repr_str += f'shift_height={self.shift_height}, '
        repr_str += f'use_color={self.use_color}, '
        repr_str += f'file_client_args={self.file_client_args}, '
        repr_str += f'load_dim={self.load_dim}, '
        repr_str += f'use_dim={self.use_dim})'
wuyuefeng's avatar
wuyuefeng committed
460
461
462
        return repr_str


463
464
465
466
467
468
469
470
471
@PIPELINES.register_module()
class LoadPointsFromDict(LoadPointsFromFile):
    """Load Points From Dict."""

    def __call__(self, results):
        assert 'points' in results
        return results


wuyuefeng's avatar
wuyuefeng committed
472
473
474
475
476
477
478
479
480
481
482
483
@PIPELINES.register_module()
class LoadAnnotations3D(LoadAnnotations):
    """Load Annotations3D.

    Load instance mask and semantic mask of points and
    encapsulate the items into related fields.

    Args:
        with_bbox_3d (bool, optional): Whether to load 3D boxes.
            Defaults to True.
        with_label_3d (bool, optional): Whether to load 3D labels.
            Defaults to True.
484
485
        with_attr_label (bool, optional): Whether to load attribute label.
            Defaults to False.
wuyuefeng's avatar
wuyuefeng committed
486
487
488
489
490
491
492
493
494
495
496
497
        with_mask_3d (bool, optional): Whether to load 3D instance masks.
            for points. Defaults to False.
        with_seg_3d (bool, optional): Whether to load 3D semantic masks.
            for points. Defaults to False.
        with_bbox (bool, optional): Whether to load 2D boxes.
            Defaults to False.
        with_label (bool, optional): Whether to load 2D labels.
            Defaults to False.
        with_mask (bool, optional): Whether to load 2D instance masks.
            Defaults to False.
        with_seg (bool, optional): Whether to load 2D semantic masks.
            Defaults to False.
498
499
        with_bbox_depth (bool, optional): Whether to load 2.5D boxes.
            Defaults to False.
wuyuefeng's avatar
wuyuefeng committed
500
501
        poly2mask (bool, optional): Whether to convert polygon annotations
            to bitmasks. Defaults to True.
502
503
        seg_3d_dtype (dtype, optional): Dtype of 3D semantic masks.
            Defaults to int64
wuyuefeng's avatar
wuyuefeng committed
504
505
506
507
508
509
510
511
        file_client_args (dict): Config dict of file clients, refer to
            https://github.com/open-mmlab/mmcv/blob/master/mmcv/fileio/file_client.py
            for more details.
    """

    def __init__(self,
                 with_bbox_3d=True,
                 with_label_3d=True,
512
                 with_attr_label=False,
wuyuefeng's avatar
wuyuefeng committed
513
514
515
516
517
518
                 with_mask_3d=False,
                 with_seg_3d=False,
                 with_bbox=False,
                 with_label=False,
                 with_mask=False,
                 with_seg=False,
519
                 with_bbox_depth=False,
wuyuefeng's avatar
wuyuefeng committed
520
                 poly2mask=True,
521
                 seg_3d_dtype='int',
wuyuefeng's avatar
wuyuefeng committed
522
523
524
525
526
527
528
529
530
                 file_client_args=dict(backend='disk')):
        super().__init__(
            with_bbox,
            with_label,
            with_mask,
            with_seg,
            poly2mask,
            file_client_args=file_client_args)
        self.with_bbox_3d = with_bbox_3d
531
        self.with_bbox_depth = with_bbox_depth
wuyuefeng's avatar
wuyuefeng committed
532
        self.with_label_3d = with_label_3d
533
        self.with_attr_label = with_attr_label
wuyuefeng's avatar
wuyuefeng committed
534
535
        self.with_mask_3d = with_mask_3d
        self.with_seg_3d = with_seg_3d
536
        self.seg_3d_dtype = seg_3d_dtype
wuyuefeng's avatar
wuyuefeng committed
537
538

    def _load_bboxes_3d(self, results):
539
540
541
542
543
544
545
546
        """Private function to load 3D bounding box annotations.

        Args:
            results (dict): Result dict from :obj:`mmdet3d.CustomDataset`.

        Returns:
            dict: The dict containing loaded 3D bounding box annotations.
        """
wuyuefeng's avatar
wuyuefeng committed
547
548
549
550
        results['gt_bboxes_3d'] = results['ann_info']['gt_bboxes_3d']
        results['bbox3d_fields'].append('gt_bboxes_3d')
        return results

551
552
553
554
555
556
557
558
559
560
561
562
563
    def _load_bboxes_depth(self, results):
        """Private function to load 2.5D bounding box annotations.

        Args:
            results (dict): Result dict from :obj:`mmdet3d.CustomDataset`.

        Returns:
            dict: The dict containing loaded 2.5D bounding box annotations.
        """
        results['centers2d'] = results['ann_info']['centers2d']
        results['depths'] = results['ann_info']['depths']
        return results

wuyuefeng's avatar
wuyuefeng committed
564
    def _load_labels_3d(self, results):
565
566
567
568
569
570
571
572
        """Private function to load label annotations.

        Args:
            results (dict): Result dict from :obj:`mmdet3d.CustomDataset`.

        Returns:
            dict: The dict containing loaded label annotations.
        """
wuyuefeng's avatar
wuyuefeng committed
573
574
575
        results['gt_labels_3d'] = results['ann_info']['gt_labels_3d']
        return results

576
577
578
579
580
581
582
583
584
585
586
587
    def _load_attr_labels(self, results):
        """Private function to load label annotations.

        Args:
            results (dict): Result dict from :obj:`mmdet3d.CustomDataset`.

        Returns:
            dict: The dict containing loaded label annotations.
        """
        results['attr_labels'] = results['ann_info']['attr_labels']
        return results

wuyuefeng's avatar
wuyuefeng committed
588
    def _load_masks_3d(self, results):
589
590
591
592
593
594
595
596
        """Private function to load 3D mask annotations.

        Args:
            results (dict): Result dict from :obj:`mmdet3d.CustomDataset`.

        Returns:
            dict: The dict containing loaded 3D mask annotations.
        """
wuyuefeng's avatar
wuyuefeng committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
        pts_instance_mask_path = results['ann_info']['pts_instance_mask_path']

        if self.file_client is None:
            self.file_client = mmcv.FileClient(**self.file_client_args)
        try:
            mask_bytes = self.file_client.get(pts_instance_mask_path)
            pts_instance_mask = np.frombuffer(mask_bytes, dtype=np.int)
        except ConnectionError:
            mmcv.check_file_exist(pts_instance_mask_path)
            pts_instance_mask = np.fromfile(
                pts_instance_mask_path, dtype=np.long)

        results['pts_instance_mask'] = pts_instance_mask
        results['pts_mask_fields'].append('pts_instance_mask')
        return results

    def _load_semantic_seg_3d(self, results):
614
615
616
617
618
619
620
621
        """Private function to load 3D semantic segmentation annotations.

        Args:
            results (dict): Result dict from :obj:`mmdet3d.CustomDataset`.

        Returns:
            dict: The dict containing the semantic segmentation annotations.
        """
wuyuefeng's avatar
wuyuefeng committed
622
623
624
625
626
627
628
        pts_semantic_mask_path = results['ann_info']['pts_semantic_mask_path']

        if self.file_client is None:
            self.file_client = mmcv.FileClient(**self.file_client_args)
        try:
            mask_bytes = self.file_client.get(pts_semantic_mask_path)
            # add .copy() to fix read-only bug
629
630
            pts_semantic_mask = np.frombuffer(
                mask_bytes, dtype=self.seg_3d_dtype).copy()
wuyuefeng's avatar
wuyuefeng committed
631
632
633
634
635
636
637
638
639
640
        except ConnectionError:
            mmcv.check_file_exist(pts_semantic_mask_path)
            pts_semantic_mask = np.fromfile(
                pts_semantic_mask_path, dtype=np.long)

        results['pts_semantic_mask'] = pts_semantic_mask
        results['pts_seg_fields'].append('pts_semantic_mask')
        return results

    def __call__(self, results):
641
642
643
644
645
646
647
648
649
        """Call function to load multiple types annotations.

        Args:
            results (dict): Result dict from :obj:`mmdet3d.CustomDataset`.

        Returns:
            dict: The dict containing loaded 3D bounding box, label, mask and
                semantic segmentation annotations.
        """
wuyuefeng's avatar
wuyuefeng committed
650
651
652
653
654
        results = super().__call__(results)
        if self.with_bbox_3d:
            results = self._load_bboxes_3d(results)
            if results is None:
                return None
655
656
657
658
        if self.with_bbox_depth:
            results = self._load_bboxes_depth(results)
            if results is None:
                return None
wuyuefeng's avatar
wuyuefeng committed
659
660
        if self.with_label_3d:
            results = self._load_labels_3d(results)
661
662
        if self.with_attr_label:
            results = self._load_attr_labels(results)
wuyuefeng's avatar
wuyuefeng committed
663
664
665
666
667
668
669
670
        if self.with_mask_3d:
            results = self._load_masks_3d(results)
        if self.with_seg_3d:
            results = self._load_semantic_seg_3d(results)

        return results

    def __repr__(self):
671
        """str: Return a string that describes the module."""
wuyuefeng's avatar
wuyuefeng committed
672
673
        indent_str = '    '
        repr_str = self.__class__.__name__ + '(\n'
liyinhao's avatar
liyinhao committed
674
675
        repr_str += f'{indent_str}with_bbox_3d={self.with_bbox_3d}, '
        repr_str += f'{indent_str}with_label_3d={self.with_label_3d}, '
676
        repr_str += f'{indent_str}with_attr_label={self.with_attr_label}, '
liyinhao's avatar
liyinhao committed
677
678
679
680
681
682
        repr_str += f'{indent_str}with_mask_3d={self.with_mask_3d}, '
        repr_str += f'{indent_str}with_seg_3d={self.with_seg_3d}, '
        repr_str += f'{indent_str}with_bbox={self.with_bbox}, '
        repr_str += f'{indent_str}with_label={self.with_label}, '
        repr_str += f'{indent_str}with_mask={self.with_mask}, '
        repr_str += f'{indent_str}with_seg={self.with_seg}, '
683
        repr_str += f'{indent_str}with_bbox_depth={self.with_bbox_depth}, '
wuyuefeng's avatar
wuyuefeng committed
684
685
        repr_str += f'{indent_str}poly2mask={self.poly2mask})'
        return repr_str