scannet_dataset.py 5.1 KB
Newer Older
1
import numpy as np
zhangwenwei's avatar
zhangwenwei committed
2
from os import path as osp
3

liyinhao's avatar
liyinhao committed
4
from mmdet3d.core import show_result
wuyuefeng's avatar
wuyuefeng committed
5
from mmdet3d.core.bbox import DepthInstance3DBoxes
6
from mmdet.datasets import DATASETS
zhangwenwei's avatar
zhangwenwei committed
7
from .custom_3d import Custom3DDataset
8
9
10


@DATASETS.register_module()
zhangwenwei's avatar
zhangwenwei committed
11
class ScanNetDataset(Custom3DDataset):
wangtai's avatar
wangtai committed
12
    r"""ScanNet Dataset.
13

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

zhangwenwei's avatar
zhangwenwei committed
16
17
    Please refer to the `github repo <https://github.com/ScanNet/ScanNet>`_
    for data downloading.
wangtai's avatar
wangtai committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

    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
33
34
35
            - 'LiDAR': Box in LiDAR coordinates.
            - 'Depth': Box in depth coordinates, usually for indoor dataset.
            - 'Camera': Box in camera coordinates.
wangtai's avatar
wangtai committed
36
37
38
39
40
        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.
    """
41
42
43
44
45
46
    CLASSES = ('cabinet', 'bed', 'chair', 'sofa', 'table', 'door', 'window',
               'bookshelf', 'picture', 'counter', 'desk', 'curtain',
               'refrigerator', 'showercurtrain', 'toilet', 'sink', 'bathtub',
               'garbagebin')

    def __init__(self,
zhangwenwei's avatar
zhangwenwei committed
47
                 data_root,
48
49
                 ann_file,
                 pipeline=None,
liyinhao's avatar
liyinhao committed
50
                 classes=None,
liyinhao's avatar
liyinhao committed
51
                 modality=None,
52
                 box_type_3d='Depth',
wuyuefeng's avatar
Votenet  
wuyuefeng committed
53
                 filter_empty_gt=True,
zhangwenwei's avatar
zhangwenwei committed
54
                 test_mode=False):
55
56
57
58
59
60
61
62
63
        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)
64

liyinhao's avatar
liyinhao committed
65
    def get_ann_info(self, index):
66
67
68
69
70
71
        """Get annotation info according to the given index.

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

        Returns:
zhangwenwei's avatar
zhangwenwei committed
72
            dict: annotation information consists of the following keys:
73

zhangwenwei's avatar
zhangwenwei committed
74
                - gt_bboxes_3d (:obj:`DepthInstance3DBoxes`): \
75
                    3D ground truth bboxes
wangtai's avatar
wangtai committed
76
77
78
                - 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.
79
        """
80
        # Use index to get the annos, thus the evalhook could also use this api
liyinhao's avatar
liyinhao committed
81
        info = self.data_infos[index]
82
        if info['annos']['gt_num'] != 0:
liyinhao's avatar
liyinhao committed
83
84
85
            gt_bboxes_3d = info['annos']['gt_boxes_upright_depth'].astype(
                np.float32)  # k, 6
            gt_labels_3d = info['annos']['class'].astype(np.long)
86
        else:
liyinhao's avatar
liyinhao committed
87
            gt_bboxes_3d = np.zeros((0, 6), dtype=np.float32)
liyinhao's avatar
liyinhao committed
88
            gt_labels_3d = np.zeros((0, ), dtype=np.long)
wuyuefeng's avatar
wuyuefeng committed
89
90
91
92
93
94
95
96

        # to target box structure
        gt_bboxes_3d = DepthInstance3DBoxes(
            gt_bboxes_3d,
            box_dim=gt_bboxes_3d.shape[-1],
            with_yaw=False,
            origin=(0.5, 0.5, 0.5)).convert_to(self.box_mode_3d)

zhangwenwei's avatar
zhangwenwei committed
97
        pts_instance_mask_path = osp.join(self.data_root,
liyinhao's avatar
liyinhao committed
98
                                          info['pts_instance_mask_path'])
zhangwenwei's avatar
zhangwenwei committed
99
        pts_semantic_mask_path = osp.join(self.data_root,
liyinhao's avatar
liyinhao committed
100
                                          info['pts_semantic_mask_path'])
101
102
103

        anns_results = dict(
            gt_bboxes_3d=gt_bboxes_3d,
zhangwenwei's avatar
zhangwenwei committed
104
            gt_labels_3d=gt_labels_3d,
105
106
107
            pts_instance_mask_path=pts_instance_mask_path,
            pts_semantic_mask_path=pts_semantic_mask_path)
        return anns_results
liyinhao's avatar
liyinhao committed
108
109

    def show(self, results, out_dir):
110
111
112
113
114
115
        """Results visualization.

        Args:
            results (list[dict]): List of bounding boxes results.
            out_dir (str): Output directory of visualization result.
        """
liyinhao's avatar
liyinhao committed
116
117
118
119
120
121
122
123
124
125
126
127
128
        assert out_dir is not None, 'Expect out_dir, got none.'
        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]
            points = np.fromfile(
                osp.join(self.data_root, pts_path),
                dtype=np.float32).reshape(-1, 6)
            gt_bboxes = np.pad(data_info['annos']['gt_boxes_upright_depth'],
                               ((0, 0), (0, 1)), 'constant')
            pred_bboxes = result['boxes_3d'].tensor.numpy()
            pred_bboxes[..., 2] += pred_bboxes[..., 5] / 2
            show_result(points, gt_bboxes, pred_bboxes, out_dir, file_name)