three_nn.py 1.31 KB
Newer Older
1
# Copyright (c) OpenMMLab. All rights reserved.
2
3
from typing import Tuple

wuyuefeng's avatar
wuyuefeng committed
4
5
6
7
8
9
10
11
12
13
14
import torch
from torch.autograd import Function

from . import interpolate_ext


class ThreeNN(Function):

    @staticmethod
    def forward(ctx, target: torch.Tensor,
                source: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
zhangwenwei's avatar
zhangwenwei committed
15
16
        """Find the top-3 nearest neighbors of the target set from the source
        set.
wuyuefeng's avatar
wuyuefeng committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

        Args:
            target (Tensor): shape (B, N, 3), points set that needs to
                find the nearest neighbors.
            source (Tensor): shape (B, M, 3), points set that is used
                to find the nearest neighbors of points in target set.

        Returns:
            Tensor: shape (B, N, 3), L2 distance of each point in target
                set to their corresponding nearest neighbors.
        """
        assert target.is_contiguous()
        assert source.is_contiguous()

        B, N, _ = target.size()
        m = source.size(1)
        dist2 = torch.cuda.FloatTensor(B, N, 3)
        idx = torch.cuda.IntTensor(B, N, 3)

        interpolate_ext.three_nn_wrapper(B, N, m, target, source, dist2, idx)
37
38
39

        ctx.mark_non_differentiable(idx)

wuyuefeng's avatar
wuyuefeng committed
40
41
42
43
44
45
46
47
        return torch.sqrt(dist2), idx

    @staticmethod
    def backward(ctx, a=None, b=None):
        return None, None


three_nn = ThreeNN.apply