test_iou3d.py 4.6 KB
Newer Older
limm's avatar
limm committed
1
# Copyright (c) OpenMMLab. All rights reserved.
2
3
4
5
import numpy as np
import pytest
import torch

limm's avatar
limm committed
6
from mmcv.ops import boxes_iou3d, boxes_overlap_bev, nms3d, nms3d_normal
7
8


limm's avatar
limm committed
9
10
@pytest.mark.skipif(
    not torch.cuda.is_available(), reason='requires CUDA support')
limm's avatar
limm committed
11
12
13
14
15
16
17
18
19
20
21
22
def test_boxes_overlap_bev():
    np_boxes1 = np.asarray([[1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.0],
                            [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.0],
                            [3.0, 3.0, 3.0, 3.0, 2.0, 2.0, 0.0]],
                           dtype=np.float32)
    np_boxes2 = np.asarray([[1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.0],
                            [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, np.pi / 2],
                            [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, np.pi / 4]],
                           dtype=np.float32)
    np_expect_overlaps = np.asarray(
        [[4.0, 4.0, (8 + 8 * 2**0.5) /
          (3 + 2 * 2**0.5)], [1.0, 1.0, 1.0], [0.0, 0.0, 0.0]],
23
        dtype=np.float32)
limm's avatar
limm committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

    boxes1 = torch.from_numpy(np_boxes1).cuda()
    boxes2 = torch.from_numpy(np_boxes2).cuda()

    # test for 3 boxes
    overlaps = boxes_overlap_bev(boxes1, boxes2)
    assert np.allclose(overlaps.cpu().numpy(), np_expect_overlaps, atol=1e-4)

    # test for many boxes
    boxes2 = boxes2.repeat_interleave(555, 0)

    overlaps = boxes_overlap_bev(boxes1, boxes2)
    assert np.allclose(
        overlaps.cpu().numpy(), np_expect_overlaps.repeat(555, 1), atol=1e-4)


@pytest.mark.skipif(
    not torch.cuda.is_available(), reason='requires CUDA support')
def test_boxes_iou3d():
    np_boxes1 = np.asarray([[1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.0],
                            [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.0],
                            [3.0, 3.0, 3.0, 3.0, 2.0, 2.0, 0.0]],
                           dtype=np.float32)
    np_boxes2 = np.asarray([[1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.0],
                            [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, np.pi / 2],
                            [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, np.pi / 4]],
                           dtype=np.float32)
51
    np_expect_ious = np.asarray(
limm's avatar
limm committed
52
53
        [[1.0, 1.0, 1.0 / 2**0.5], [1.0 / 15, 1.0 / 15, 1.0 / 15],
         [0.0, 0.0, 0.0]],
54
        dtype=np.float32)
55

limm's avatar
limm committed
56
57
    boxes1 = torch.from_numpy(np_boxes1).cuda()
    boxes2 = torch.from_numpy(np_boxes2).cuda()
58

limm's avatar
limm committed
59
    ious = boxes_iou3d(boxes1, boxes2)
60
61
62
    assert np.allclose(ious.cpu().numpy(), np_expect_ious, atol=1e-4)


limm's avatar
limm committed
63
64
@pytest.mark.skipif(
    not torch.cuda.is_available(), reason='requires CUDA support')
limm's avatar
limm committed
65
66
67
68
69
70
71
72
73
def test_nms3d():
    # test for 5 boxes
    np_boxes = np.asarray([[1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.0],
                           [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.0],
                           [3.0, 3.0, 3.0, 3.0, 2.0, 2.0, 0.3],
                           [3.0, 3.0, 3.0, 3.0, 2.0, 2.0, 0.0],
                           [3.0, 3.2, 3.2, 3.0, 2.0, 2.0, 0.3]],
                          dtype=np.float32)
    np_scores = np.array([0.6, 0.9, 0.1, 0.2, 0.15], dtype=np.float32)
74
75
76
    np_inds = np.array([1, 0, 3])
    boxes = torch.from_numpy(np_boxes)
    scores = torch.from_numpy(np_scores)
limm's avatar
limm committed
77
    inds = nms3d(boxes.cuda(), scores.cuda(), iou_threshold=0.3)
78

79
    assert np.allclose(inds.cpu().numpy(), np_inds)
80

limm's avatar
limm committed
81
82
83
84
85
86
87
88
89
90
    # test for many boxes
    np.random.seed(42)
    np_boxes = np.random.rand(555, 7).astype(np.float32)
    np_scores = np.random.rand(555).astype(np.float32)
    boxes = torch.from_numpy(np_boxes)
    scores = torch.from_numpy(np_scores)
    inds = nms3d(boxes.cuda(), scores.cuda(), iou_threshold=0.3)

    assert len(inds.cpu().numpy()) == 176

91

limm's avatar
limm committed
92
93
@pytest.mark.skipif(
    not torch.cuda.is_available(), reason='requires CUDA support')
limm's avatar
limm committed
94
95
96
97
98
99
100
101
102
def test_nms3d_normal():
    # test for 5 boxes
    np_boxes = np.asarray([[1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.0],
                           [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.0],
                           [3.0, 3.0, 3.0, 3.0, 2.0, 2.0, 0.3],
                           [3.0, 3.0, 3.0, 3.0, 2.0, 2.0, 0.0],
                           [3.0, 3.2, 3.2, 3.0, 2.0, 2.0, 0.3]],
                          dtype=np.float32)
    np_scores = np.array([0.6, 0.9, 0.1, 0.2, 0.15], dtype=np.float32)
103
    np_inds = np.array([1, 0, 3])
104
105
    boxes = torch.from_numpy(np_boxes)
    scores = torch.from_numpy(np_scores)
limm's avatar
limm committed
106
    inds = nms3d_normal(boxes.cuda(), scores.cuda(), iou_threshold=0.3)
107

108
    assert np.allclose(inds.cpu().numpy(), np_inds)
limm's avatar
limm committed
109
110
111
112
113
114
115
116
117
118

    # test for many boxes
    np.random.seed(42)
    np_boxes = np.random.rand(555, 7).astype(np.float32)
    np_scores = np.random.rand(555).astype(np.float32)
    boxes = torch.from_numpy(np_boxes)
    scores = torch.from_numpy(np_scores)
    inds = nms3d_normal(boxes.cuda(), scores.cuda(), iou_threshold=0.3)

    assert len(inds.cpu().numpy()) == 148