test_sunrgbd_dataset.py 4.94 KB
Newer Older
liyinhao's avatar
liyinhao committed
1
import numpy as np
liyinhao's avatar
liyinhao committed
2
import torch
liyinhao's avatar
liyinhao committed
3

4
from mmdet3d.datasets import SUNRGBDDataset
liyinhao's avatar
liyinhao committed
5
6
7
8


def test_getitem():
    np.random.seed(0)
liyinhao's avatar
liyinhao committed
9
    root_path = './tests/data/sunrgbd'
liyinhao's avatar
liyinhao committed
10
11
12
13
14
    ann_file = './tests/data/sunrgbd/sunrgbd_infos.pkl'
    class_names = ('bed', 'table', 'sofa', 'chair', 'toilet', 'desk',
                   'dresser', 'night_stand', 'bookshelf', 'bathtub')
    pipelines = [
        dict(
zhangwenwei's avatar
zhangwenwei committed
15
16
            type='LoadPointsFromFile',
            shift_height=True,
liyinhao's avatar
liyinhao committed
17
18
            load_dim=6,
            use_dim=[0, 1, 2]),
zhangwenwei's avatar
zhangwenwei committed
19
        dict(type='LoadAnnotations3D'),
liyinhao's avatar
liyinhao committed
20
        dict(
wuyuefeng's avatar
wuyuefeng committed
21
22
23
24
25
26
27
28
29
            type='RandomFlip3D',
            sync_2d=False,
            flip_ratio_bev_horizontal=0.5,
        ),
        dict(
            type='GlobalRotScaleTrans',
            rot_range=[-0.523599, 0.523599],
            scale_ratio_range=[0.85, 1.15],
            shift_height=True),
liyinhao's avatar
liyinhao committed
30
31
        dict(type='IndoorPointSample', num_points=5),
        dict(type='DefaultFormatBundle3D', class_names=class_names),
zhangwenwei's avatar
zhangwenwei committed
32
        dict(
liyinhao's avatar
liyinhao committed
33
34
35
            type='Collect3D',
            keys=['points', 'gt_bboxes_3d', 'gt_labels_3d'],
            meta_keys=[
wuyuefeng's avatar
wuyuefeng committed
36
37
                'file_name', 'pcd_horizontal_flip', 'sample_idx',
                'pcd_scale_factor', 'pcd_rotation'
liyinhao's avatar
liyinhao committed
38
            ]),
liyinhao's avatar
liyinhao committed
39
40
    ]

41
    sunrgbd_dataset = SUNRGBDDataset(root_path, ann_file, pipelines)
liyinhao's avatar
liyinhao committed
42
43
44
    data = sunrgbd_dataset[0]
    points = data['points']._data
    gt_bboxes_3d = data['gt_bboxes_3d']._data
zhangwenwei's avatar
zhangwenwei committed
45
    gt_labels_3d = data['gt_labels_3d']._data
zhangwenwei's avatar
zhangwenwei committed
46
    file_name = data['img_metas']._data['file_name']
wuyuefeng's avatar
wuyuefeng committed
47
48
49
    pcd_horizontal_flip = data['img_metas']._data['pcd_horizontal_flip']
    pcd_scale_factor = data['img_metas']._data['pcd_scale_factor']
    pcd_rotation = data['img_metas']._data['pcd_rotation']
zhangwenwei's avatar
zhangwenwei committed
50
    sample_idx = data['img_metas']._data['sample_idx']
wuyuefeng's avatar
wuyuefeng committed
51
52
53
54
55
56
57
    pcd_rotation_expected = np.array([[0.99889565, 0.04698427, 0.],
                                      [-0.04698427, 0.99889565, 0.],
                                      [0., 0., 1.]])
    assert file_name == './tests/data/sunrgbd/points/000001.bin'
    assert pcd_horizontal_flip is False
    assert abs(pcd_scale_factor - 0.9770964398016714) < 1e-5
    assert np.allclose(pcd_rotation, pcd_rotation_expected, 1e-3)
liyinhao's avatar
liyinhao committed
58
    assert sample_idx == 1
wuyuefeng's avatar
wuyuefeng committed
59
60
61
62
63
    expected_points = torch.tensor([[-0.9904, 1.2596, 0.1105, 0.0905],
                                    [-0.9948, 1.2758, 0.0437, 0.0238],
                                    [-0.9866, 1.2641, 0.0504, 0.0304],
                                    [-0.9915, 1.2586, 0.1265, 0.1065],
                                    [-0.9890, 1.2561, 0.1216, 0.1017]])
wuyuefeng's avatar
wuyuefeng committed
64
    expected_gt_bboxes_3d = torch.tensor(
wuyuefeng's avatar
wuyuefeng committed
65
66
67
        [[0.8308, 4.1168, -1.2035, 2.2493, 1.8444, 1.9245, 1.6486],
         [2.3002, 4.8149, -1.2442, 0.5718, 0.8629, 0.9510, 1.6030],
         [-1.1477, 1.8090, -1.1725, 0.6965, 1.5273, 2.0563, 0.0552]])
liyinhao's avatar
liyinhao committed
68
    expected_gt_labels = np.array([0, 7, 6])
69
    original_classes = sunrgbd_dataset.CLASSES
liyinhao's avatar
liyinhao committed
70

wuyuefeng's avatar
wuyuefeng committed
71
72
    assert torch.allclose(points, expected_points, 1e-2)
    assert torch.allclose(gt_bboxes_3d.tensor, expected_gt_bboxes_3d, 1e-3)
zhangwenwei's avatar
zhangwenwei committed
73
    assert np.all(gt_labels_3d.numpy() == expected_gt_labels)
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    assert original_classes == class_names

    SUNRGBD_dataset = SUNRGBDDataset(
        root_path, ann_file, pipeline=None, classes=['bed', 'table'])
    assert SUNRGBD_dataset.CLASSES != original_classes
    assert SUNRGBD_dataset.CLASSES == ['bed', 'table']

    SUNRGBD_dataset = SUNRGBDDataset(
        root_path, ann_file, pipeline=None, classes=('bed', 'table'))
    assert SUNRGBD_dataset.CLASSES != original_classes
    assert SUNRGBD_dataset.CLASSES == ('bed', 'table')

    import tempfile
    tmp_file = tempfile.NamedTemporaryFile()
    with open(tmp_file.name, 'w') as f:
        f.write('bed\ntable\n')

    SUNRGBD_dataset = SUNRGBDDataset(
        root_path, ann_file, pipeline=None, classes=tmp_file.name)
    assert SUNRGBD_dataset.CLASSES != original_classes
    assert SUNRGBD_dataset.CLASSES == ['bed', 'table']
liyinhao's avatar
liyinhao committed
95
96
97


def test_evaluate():
wuyuefeng's avatar
wuyuefeng committed
98
    from mmdet3d.core.bbox.structures import DepthInstance3DBoxes
liyinhao's avatar
liyinhao committed
99
100
    root_path = './tests/data/sunrgbd'
    ann_file = './tests/data/sunrgbd/sunrgbd_infos.pkl'
101
    sunrgbd_dataset = SUNRGBDDataset(root_path, ann_file)
liyinhao's avatar
liyinhao committed
102
103
    results = []
    pred_boxes = dict()
wuyuefeng's avatar
wuyuefeng committed
104
105
106
107
108
109
110
    pred_boxes['boxes_3d'] = DepthInstance3DBoxes(
        torch.tensor(
            [[1.0473, 4.1687, -1.2317, 2.3021, 1.8876, 1.9696, 1.6956],
             [2.5831, 4.8117, -1.2733, 0.5852, 0.8832, 0.9733, 1.6500],
             [-1.0864, 1.9045, -1.2000, 0.7128, 1.5631, 2.1045, 0.1022]]))
    pred_boxes['labels_3d'] = torch.tensor([0, 7, 6])
    pred_boxes['scores_3d'] = torch.tensor([0.5, 1.0, 1.0])
liyinhao's avatar
liyinhao committed
111
    results.append(pred_boxes)
liyinhao's avatar
liyinhao committed
112
    metric = [0.25, 0.5]
liyinhao's avatar
liyinhao committed
113
    ap_dict = sunrgbd_dataset.evaluate(results, metric)
liyinhao's avatar
liyinhao committed
114
115
116
    bed_precision_25 = ap_dict['bed_AP_0.25']
    dresser_precision_25 = ap_dict['dresser_AP_0.25']
    night_stand_precision_25 = ap_dict['night_stand_AP_0.25']
liyinhao's avatar
liyinhao committed
117
118
119
    assert abs(bed_precision_25 - 1) < 0.01
    assert abs(dresser_precision_25 - 1) < 0.01
    assert abs(night_stand_precision_25 - 1) < 0.01