test_bbox.py 1.84 KB
Newer Older
1
import numpy as np
2
import pytest
3
4
5
import torch


limm's avatar
limm committed
6
7
8
@pytest.mark.skipif(
    not torch.cuda.is_available(), reason='requires CUDA support')
class TestBBox(object):
9

limm's avatar
limm committed
10
    def _test_bbox_overlaps(self, dtype=torch.float):
11
12
13

        from mmcv.ops import bbox_overlaps
        b1 = torch.tensor([[1.0, 1.0, 3.0, 4.0], [2.0, 2.0, 3.0, 4.0],
limm's avatar
limm committed
14
                           [7.0, 7.0, 8.0, 8.0]]).cuda().type(dtype)
15
        b2 = torch.tensor([[0.0, 2.0, 2.0, 5.0], [2.0, 1.0, 3.0,
limm's avatar
limm committed
16
                                                  3.0]]).cuda().type(dtype)
17
18
19
20
21
        should_output = np.array([[0.33333334, 0.5], [0.2, 0.5], [0.0, 0.0]])
        out = bbox_overlaps(b1, b2, offset=1)
        assert np.allclose(out.cpu().numpy(), should_output, 1e-2)

        b1 = torch.tensor([[1.0, 1.0, 3.0, 4.0], [2.0, 2.0, 3.0,
limm's avatar
limm committed
22
                                                  4.0]]).cuda().type(dtype)
23
        b2 = torch.tensor([[0.0, 2.0, 2.0, 5.0], [2.0, 1.0, 3.0,
limm's avatar
limm committed
24
                                                  3.0]]).cuda().type(dtype)
25
26
27
28
        should_output = np.array([0.33333334, 0.5])
        out = bbox_overlaps(b1, b2, aligned=True, offset=1)
        assert np.allclose(out.cpu().numpy(), should_output, 1e-2)

limm's avatar
limm committed
29
30
        b1 = torch.tensor([[0.0, 0.0, 3.0, 3.0]]).cuda().type(dtype)
        b1 = torch.tensor([[0.0, 0.0, 3.0, 3.0]]).cuda().type(dtype)
31
32
        b2 = torch.tensor([[4.0, 0.0, 5.0, 3.0], [3.0, 0.0, 4.0, 3.0],
                           [2.0, 0.0, 3.0, 3.0], [1.0, 0.0, 2.0,
limm's avatar
limm committed
33
                                                  3.0]]).cuda().type(dtype)
34
35
36
37
        should_output = np.array([0, 0.2, 0.5, 0.5])
        out = bbox_overlaps(b1, b2, offset=1)
        assert np.allclose(out.cpu().numpy(), should_output, 1e-2)

limm's avatar
limm committed
38
39
    def test_bbox_overlaps_float(self):
        self._test_bbox_overlaps(torch.float)
40

limm's avatar
limm committed
41
42
    def test_bbox_overlaps_half(self):
        self._test_bbox_overlaps(torch.half)