test_iou3d.py 2.03 KB
Newer Older
1
2
3
4
import numpy as np
import pytest
import torch

limm's avatar
limm committed
5
from mmcv.ops import boxes_iou_bev, nms_bev, nms_normal_bev
6
7


limm's avatar
limm committed
8
9
10
11
12
13
14
15
16
17
@pytest.mark.skipif(
    not torch.cuda.is_available(), reason='requires CUDA support')
def test_boxes_iou_bev():
    np_boxes1 = np.asarray(
        [[1.0, 1.0, 3.0, 4.0, 0.5], [2.0, 2.0, 3.0, 4.0, 0.6],
         [7.0, 7.0, 8.0, 8.0, 0.4]],
        dtype=np.float32)
    np_boxes2 = np.asarray(
        [[0.0, 2.0, 2.0, 5.0, 0.3], [2.0, 1.0, 3.0, 3.0, 0.5],
         [5.0, 5.0, 6.0, 7.0, 0.4]],
18
19
        dtype=np.float32)
    np_expect_ious = np.asarray(
limm's avatar
limm committed
20
21
        [[0.2621, 0.2948, 0.0000], [0.0549, 0.1587, 0.0000],
         [0.0000, 0.0000, 0.0000]],
22
        dtype=np.float32)
23

limm's avatar
limm committed
24
25
    boxes1 = torch.from_numpy(np_boxes1).cuda()
    boxes2 = torch.from_numpy(np_boxes2).cuda()
26

limm's avatar
limm committed
27
    ious = boxes_iou_bev(boxes1, boxes2)
28
29
30
    assert np.allclose(ious.cpu().numpy(), np_expect_ious, atol=1e-4)


limm's avatar
limm committed
31
32
33
34
35
36
37
38
@pytest.mark.skipif(
    not torch.cuda.is_available(), reason='requires CUDA support')
def test_nms_bev():
    np_boxes = np.array(
        [[6.0, 3.0, 8.0, 7.0, 2.0], [3.0, 6.0, 9.0, 11.0, 1.0],
         [3.0, 7.0, 10.0, 12.0, 1.0], [1.0, 4.0, 13.0, 7.0, 3.0]],
        dtype=np.float32)
    np_scores = np.array([0.6, 0.9, 0.7, 0.2], dtype=np.float32)
39
40
41
    np_inds = np.array([1, 0, 3])
    boxes = torch.from_numpy(np_boxes)
    scores = torch.from_numpy(np_scores)
limm's avatar
limm committed
42
    inds = nms_bev(boxes.cuda(), scores.cuda(), thresh=0.3)
43

44
    assert np.allclose(inds.cpu().numpy(), np_inds)
45

46

limm's avatar
limm committed
47
48
49
50
51
52
53
54
@pytest.mark.skipif(
    not torch.cuda.is_available(), reason='requires CUDA support')
def test_nms_normal_bev():
    np_boxes = np.array(
        [[6.0, 3.0, 8.0, 7.0, 2.0], [3.0, 6.0, 9.0, 11.0, 1.0],
         [3.0, 7.0, 10.0, 12.0, 1.0], [1.0, 4.0, 13.0, 7.0, 3.0]],
        dtype=np.float32)
    np_scores = np.array([0.6, 0.9, 0.7, 0.2], dtype=np.float32)
55
    np_inds = np.array([1, 0, 3])
56
57
    boxes = torch.from_numpy(np_boxes)
    scores = torch.from_numpy(np_scores)
limm's avatar
limm committed
58
    inds = nms_normal_bev(boxes.cuda(), scores.cuda(), thresh=0.3)
59

60
    assert np.allclose(inds.cpu().numpy(), np_inds)