sunrgbd_dataset.py 10.9 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
liyinhao's avatar
liyinhao committed
2
import numpy as np
3
from collections import OrderedDict
zhangwenwei's avatar
zhangwenwei committed
4
from os import path as osp
liyinhao's avatar
liyinhao committed
5

6
from mmdet3d.core import show_multi_modality_result, show_result
wuyuefeng's avatar
wuyuefeng committed
7
from mmdet3d.core.bbox import DepthInstance3DBoxes
8
from mmdet.core import eval_map
liyinhao's avatar
liyinhao committed
9
from mmdet.datasets import DATASETS
zhangwenwei's avatar
zhangwenwei committed
10
from .custom_3d import Custom3DDataset
11
from .pipelines import Compose
liyinhao's avatar
liyinhao committed
12
13
14


@DATASETS.register_module()
zhangwenwei's avatar
zhangwenwei committed
15
class SUNRGBDDataset(Custom3DDataset):
zhangwenwei's avatar
zhangwenwei committed
16
    r"""SUNRGBD Dataset.
liyinhao's avatar
liyinhao committed
17

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

zhangwenwei's avatar
zhangwenwei committed
20
21
    See the `download page <http://rgbd.cs.princeton.edu/challenge.html>`_
    for data downloading.
wangtai's avatar
wangtai committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

    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.
        modality (dict, optional): Modality to specify the sensor data used
            as input. Defaults to None.
        box_type_3d (str, optional): Type of 3D box of this dataset.
            Based on the `box_type_3d`, the dataset will encapsulate the box
            to its original format then converted them to `box_type_3d`.
            Defaults to 'Depth' in this dataset. Available options includes

wangtai's avatar
wangtai committed
37
38
39
            - 'LiDAR': Box in LiDAR coordinates.
            - 'Depth': Box in depth coordinates, usually for indoor dataset.
            - 'Camera': Box in camera coordinates.
wangtai's avatar
wangtai committed
40
41
42
43
44
        filter_empty_gt (bool, optional): Whether to filter empty GT.
            Defaults to True.
        test_mode (bool, optional): Whether the dataset is in test mode.
            Defaults to False.
    """
liyinhao's avatar
liyinhao committed
45
46
47
48
    CLASSES = ('bed', 'table', 'sofa', 'chair', 'toilet', 'desk', 'dresser',
               'night_stand', 'bookshelf', 'bathtub')

    def __init__(self,
zhangwenwei's avatar
zhangwenwei committed
49
                 data_root,
liyinhao's avatar
liyinhao committed
50
51
                 ann_file,
                 pipeline=None,
liyinhao's avatar
liyinhao committed
52
                 classes=None,
53
                 modality=dict(use_camera=True, use_lidar=True),
54
                 box_type_3d='Depth',
wuyuefeng's avatar
Votenet  
wuyuefeng committed
55
                 filter_empty_gt=True,
zhangwenwei's avatar
zhangwenwei committed
56
                 test_mode=False):
57
58
59
60
61
62
63
64
65
        super().__init__(
            data_root=data_root,
            ann_file=ann_file,
            pipeline=pipeline,
            classes=classes,
            modality=modality,
            box_type_3d=box_type_3d,
            filter_empty_gt=filter_empty_gt,
            test_mode=test_mode)
66
67
        assert 'use_camera' in self.modality and \
            'use_lidar' in self.modality
68
69
70
71
72
73
74
75
76
        assert self.modality['use_camera'] or self.modality['use_lidar']

    def get_data_info(self, index):
        """Get data info according to the given index.

        Args:
            index (int): Index of the sample data to get.

        Returns:
77
            dict: Data information that will be passed to the data
78
79
80
81
82
                preprocessing pipelines. It includes the following keys:

                - sample_idx (str): Sample index.
                - pts_filename (str, optional): Filename of point clouds.
                - file_name (str, optional): Filename of point clouds.
83
                - img_prefix (str, optional): Prefix of image files.
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
                - img_info (dict, optional): Image info.
                - calib (dict, optional): Camera calibration info.
                - ann_info (dict): Annotation info.
        """
        info = self.data_infos[index]
        sample_idx = info['point_cloud']['lidar_idx']
        assert info['point_cloud']['lidar_idx'] == info['image']['image_idx']
        input_dict = dict(sample_idx=sample_idx)

        if self.modality['use_lidar']:
            pts_filename = osp.join(self.data_root, info['pts_path'])
            input_dict['pts_filename'] = pts_filename
            input_dict['file_name'] = pts_filename

        if self.modality['use_camera']:
99
100
101
            img_filename = osp.join(
                osp.join(self.data_root, 'sunrgbd_trainval'),
                info['image']['image_path'])
102
103
104
            input_dict['img_prefix'] = None
            input_dict['img_info'] = dict(filename=img_filename)
            calib = info['calib']
105
106
107
108
109
110
            rt_mat = calib['Rt']
            # follow Coord3DMode.convert_point
            rt_mat = np.array([[1, 0, 0], [0, 0, -1], [0, 1, 0]
                               ]) @ rt_mat.transpose(1, 0)
            depth2img = calib['K'] @ rt_mat
            input_dict['depth2img'] = depth2img
111
112
113
114
115
116
117

        if not self.test_mode:
            annos = self.get_ann_info(index)
            input_dict['ann_info'] = annos
            if self.filter_empty_gt and len(annos['gt_bboxes_3d']) == 0:
                return None
        return input_dict
liyinhao's avatar
liyinhao committed
118

liyinhao's avatar
liyinhao committed
119
    def get_ann_info(self, index):
120
121
122
123
124
125
        """Get annotation info according to the given index.

        Args:
            index (int): Index of the annotation data to get.

        Returns:
zhangwenwei's avatar
zhangwenwei committed
126
            dict: annotation information consists of the following keys:
127

128
                - gt_bboxes_3d (:obj:`DepthInstance3DBoxes`):
129
                    3D ground truth bboxes
wangtai's avatar
wangtai committed
130
131
132
                - gt_labels_3d (np.ndarray): Labels of ground truths.
                - pts_instance_mask_path (str): Path of instance masks.
                - pts_semantic_mask_path (str): Path of semantic masks.
133
        """
liyinhao's avatar
liyinhao committed
134
        # Use index to get the annos, thus the evalhook could also use this api
liyinhao's avatar
liyinhao committed
135
        info = self.data_infos[index]
liyinhao's avatar
liyinhao committed
136
        if info['annos']['gt_num'] != 0:
liyinhao's avatar
liyinhao committed
137
138
139
            gt_bboxes_3d = info['annos']['gt_boxes_upright_depth'].astype(
                np.float32)  # k, 6
            gt_labels_3d = info['annos']['class'].astype(np.long)
liyinhao's avatar
liyinhao committed
140
        else:
liyinhao's avatar
liyinhao committed
141
            gt_bboxes_3d = np.zeros((0, 7), dtype=np.float32)
liyinhao's avatar
liyinhao committed
142
            gt_labels_3d = np.zeros((0, ), dtype=np.long)
liyinhao's avatar
liyinhao committed
143

wuyuefeng's avatar
wuyuefeng committed
144
145
146
147
        # to target box structure
        gt_bboxes_3d = DepthInstance3DBoxes(
            gt_bboxes_3d, origin=(0.5, 0.5, 0.5)).convert_to(self.box_mode_3d)

liyinhao's avatar
liyinhao committed
148
        anns_results = dict(
liyinhao's avatar
liyinhao committed
149
            gt_bboxes_3d=gt_bboxes_3d, gt_labels_3d=gt_labels_3d)
150
151
152
153
154
155
156
157
158

        if self.modality['use_camera']:
            if info['annos']['gt_num'] != 0:
                gt_bboxes_2d = info['annos']['bbox'].astype(np.float32)
            else:
                gt_bboxes_2d = np.zeros((0, 4), dtype=np.float32)
            anns_results['bboxes'] = gt_bboxes_2d
            anns_results['labels'] = gt_labels_3d

liyinhao's avatar
liyinhao committed
159
        return anns_results
liyinhao's avatar
liyinhao committed
160

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    def _build_default_pipeline(self):
        """Build the default pipeline for this dataset."""
        pipeline = [
            dict(
                type='LoadPointsFromFile',
                coord_type='DEPTH',
                shift_height=False,
                load_dim=6,
                use_dim=[0, 1, 2]),
            dict(
                type='DefaultFormatBundle3D',
                class_names=self.CLASSES,
                with_label=False),
            dict(type='Collect3D', keys=['points'])
        ]
        if self.modality['use_camera']:
            pipeline.insert(0, dict(type='LoadImageFromFile'))
        return Compose(pipeline)

    def show(self, results, out_dir, show=True, pipeline=None):
181
182
183
184
185
        """Results visualization.

        Args:
            results (list[dict]): List of bounding boxes results.
            out_dir (str): Output directory of visualization result.
186
            show (bool): Visualize the results online.
187
188
            pipeline (list[dict], optional): raw data loading for showing.
                Default: None.
189
        """
liyinhao's avatar
liyinhao committed
190
        assert out_dir is not None, 'Expect out_dir, got none.'
191
        pipeline = self._get_pipeline(pipeline)
liyinhao's avatar
liyinhao committed
192
193
194
195
        for i, result in enumerate(results):
            data_info = self.data_infos[i]
            pts_path = data_info['pts_path']
            file_name = osp.split(pts_path)[-1].split('.')[0]
196
197
            points, img_metas, img = self._extract_data(
                i, pipeline, ['points', 'img_metas', 'img'])
198
199
            # scale colors to [0, 255]
            points = points.numpy()
liyinhao's avatar
liyinhao committed
200
            points[:, 3:] *= 255
201
202

            gt_bboxes = self.get_ann_info(i)['gt_bboxes_3d'].tensor.numpy()
liyinhao's avatar
liyinhao committed
203
            pred_bboxes = result['boxes_3d'].tensor.numpy()
204
205
206
207
            show_result(points, gt_bboxes.copy(), pred_bboxes.copy(), out_dir,
                        file_name, show)

            # multi-modality visualization
208
            if self.modality['use_camera']:
209
210
211
                img = img.numpy()
                # need to transpose channel to first dim
                img = img.transpose(1, 2, 0)
212
213
214
215
216
217
218
219
                pred_bboxes = DepthInstance3DBoxes(
                    pred_bboxes, origin=(0.5, 0.5, 0))
                gt_bboxes = DepthInstance3DBoxes(
                    gt_bboxes, origin=(0.5, 0.5, 0))
                show_multi_modality_result(
                    img,
                    gt_bboxes,
                    pred_bboxes,
220
                    None,
221
222
                    out_dir,
                    file_name,
223
                    box_mode='depth',
224
                    img_metas=img_metas,
225
                    show=show)
226
227
228
229
230
231
232
233

    def evaluate(self,
                 results,
                 metric=None,
                 iou_thr=(0.25, 0.5),
                 iou_thr_2d=(0.5, ),
                 logger=None,
                 show=False,
234
235
236
237
238
                 out_dir=None,
                 pipeline=None):
        """Evaluate.

        Evaluation in indoor protocol.
239

240
241
        Args:
            results (list[dict]): List of results.
242
243
244
245
246
247
248
            metric (str | list[str], optional): Metrics to be evaluated.
                Default: None.
            iou_thr (list[float], optional): AP IoU thresholds for 3D
                evaluation. Default: (0.25, 0.5).
            iou_thr_2d (list[float], optional): AP IoU thresholds for 2D
                evaluation. Default: (0.5, ).
            show (bool, optional): Whether to visualize.
249
                Default: False.
250
            out_dir (str, optional): Path to save the visualization results.
251
252
253
254
255
256
257
                Default: None.
            pipeline (list[dict], optional): raw data loading for showing.
                Default: None.

        Returns:
            dict: Evaluation results.
        """
258
259
260
        # evaluate 3D detection performance
        if isinstance(results[0], dict):
            return super().evaluate(results, metric, iou_thr, logger, show,
261
                                    out_dir, pipeline)
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
        # evaluate 2D detection performance
        else:
            eval_results = OrderedDict()
            annotations = [self.get_ann_info(i) for i in range(len(self))]
            iou_thr_2d = (iou_thr_2d) if isinstance(iou_thr_2d,
                                                    float) else iou_thr_2d
            for iou_thr_2d_single in iou_thr_2d:
                mean_ap, _ = eval_map(
                    results,
                    annotations,
                    scale_ranges=None,
                    iou_thr=iou_thr_2d_single,
                    dataset=self.CLASSES,
                    logger=logger)
                eval_results['mAP_' + str(iou_thr_2d_single)] = mean_ap
            return eval_results