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

from mmcv.ops import chamfer_distance
limm's avatar
limm committed
7
from mmcv.utils import IS_CUDA_AVAILABLE, IS_NPU_AVAILABLE
8
9


limm's avatar
limm committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def chamfer_distance_forward_groundtruth(xyz1, xyz2, dtype):
    bs, ns, ss = xyz1.shape
    dist1 = np.zeros((bs, ns)).astype(torch_to_np_type(dtype))
    dist2 = np.zeros((bs, ns)).astype(torch_to_np_type(dtype))
    idx1 = np.zeros((bs, ns)).astype('int32')
    idx2 = np.zeros((bs, ns)).astype('int32')
    for b1 in range(bs):
        for n1 in range(ns):
            x1, y1 = xyz1[b1][n1]
            dist1[b1][n1] = 10000000
            for n2 in range(ns):
                x2, y2 = xyz2[b1][n2]
                dst = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)
                if dist1[b1][n1] > dst:
                    dist1[b1][n1] = dst
                    idx1[b1][n1] = n2
    for b1 in range(bs):
        for n1 in range(ns):
            x1, y1 = xyz2[b1][n1]
            dist2[b1][n1] = 10000000
            for n2 in range(ns):
                x2, y2 = xyz1[b1][n2]
                dst = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)
                if dist2[b1][n1] > dst:
                    dist2[b1][n1] = dst
                    idx2[b1][n1] = n2
    return [dist1, dist2, idx1, idx2]
37
38


limm's avatar
limm committed
39
40
41
42
43
def torch_to_np_type(dtype):
    if dtype == torch.half:
        return np.float16
    elif dtype == torch.float32:
        return np.float32
44
45


limm's avatar
limm committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@pytest.mark.parametrize('device', [
    pytest.param(
        'cuda',
        marks=pytest.mark.skipif(
            not IS_CUDA_AVAILABLE, reason='requires CUDA support')),
    pytest.param(
        'npu',
        marks=pytest.mark.skipif(
            not IS_NPU_AVAILABLE, reason='requires NPU support'))
])
@pytest.mark.parametrize('dtype', [torch.half, torch.float32])
@pytest.mark.parametrize('shape', [(2, 600, 2), (2, 600, 2)])
def test_chamfer_distance_npu_dynamic_shape(dtype, device, shape):
    bs = shape[0]
    ns = shape[1]
    xyz1 = np.random.uniform(-10.0, 10.0,
                             (bs, ns, 2)).astype(torch_to_np_type(dtype))
    xyz2 = np.random.uniform(-10.0, 10.0,
                             (bs, ns, 2)).astype(torch_to_np_type(dtype))
    xyz1_npu = torch.tensor(xyz1, dtype=dtype).to(device)
    xyz2_npu = torch.tensor(xyz2, dtype=dtype).to(device)
    expected_output = chamfer_distance_forward_groundtruth(xyz1, xyz2, dtype)
    output = chamfer_distance(xyz1_npu, xyz2_npu)
    assert np.allclose(output[0].cpu().numpy(), expected_output[0], 1e-3, 1e-4)
    assert np.allclose(output[1].cpu().numpy(), expected_output[1], 1e-3, 1e-4)
    assert np.allclose(output[2].cpu().numpy(), expected_output[2], 1e-3, 1e-4)
    assert np.allclose(output[3].cpu().numpy(), expected_output[3], 1e-3, 1e-4)