fps.py 2.66 KB
Newer Older
1
from typing import Optional, Union
rusty1s's avatar
rusty1s committed
2

rusty1s's avatar
update  
rusty1s committed
3
import torch
rusty1s's avatar
rusty1s committed
4
from torch import Tensor
rusty1s's avatar
rusty1s committed
5
6


rusty1s's avatar
rusty1s committed
7
@torch.jit._overload  # noqa
8
9
def fps(src, batch, ratio, random_start, batch_size):  # noqa
    # type: (Tensor, Optional[Tensor], Optional[float], bool, Optional[int]) -> Tensor  # noqa
rusty1s's avatar
rusty1s committed
10
    pass  # pragma: no cover
Duc's avatar
Duc committed
11
12


rusty1s's avatar
rusty1s committed
13
@torch.jit._overload  # noqa
14
15
def fps(src, batch, ratio, random_start, batch_size):  # noqa
    # type: (Tensor, Optional[Tensor], Optional[Tensor], bool, Optional[int]) -> Tensor  # noqa
rusty1s's avatar
rusty1s committed
16
    pass  # pragma: no cover
Duc's avatar
Duc committed
17
18


19
20
21
22
23
24
25
def fps(  # noqa
    src: torch.Tensor,
    batch: Optional[Tensor] = None,
    ratio: Optional[Union[torch.Tensor, float]] = None,
    random_start: bool = True,
    batch_size: Optional[int] = None,
):
rusty1s's avatar
rusty1s committed
26
27
28
29
    r""""A sampling algorithm from the `"PointNet++: Deep Hierarchical Feature
    Learning on Point Sets in a Metric Space"
    <https://arxiv.org/abs/1706.02413>`_ paper, which iteratively samples the
    most distant point with regard to the rest points.
rusty1s's avatar
rusty1s committed
30
31

    Args:
rusty1s's avatar
update  
rusty1s committed
32
        src (Tensor): Point feature matrix
rusty1s's avatar
rusty1s committed
33
34
35
36
            :math:`\mathbf{X} \in \mathbb{R}^{N \times F}`.
        batch (LongTensor, optional): Batch vector
            :math:`\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N`, which assigns each
            node to a specific example. (default: :obj:`None`)
rusty1s's avatar
rusty1s committed
37
38
        ratio (float or Tensor, optional): Sampling ratio.
            (default: :obj:`0.5`)
rusty1s's avatar
rusty1s committed
39
40
        random_start (bool, optional): If set to :obj:`False`, use the first
            node in :math:`\mathbf{X}` as starting node. (default: obj:`True`)
41
42
        batch_size (int, optional): The number of examples :math:`B`.
            Automatically calculated if not given. (default: :obj:`None`)
rusty1s's avatar
rusty1s committed
43

rusty1s's avatar
docs  
rusty1s committed
44
45
    :rtype: :class:`LongTensor`

rusty1s's avatar
update  
rusty1s committed
46
    .. code-block:: python
rusty1s's avatar
rusty1s committed
47
48
49
50

        import torch
        from torch_cluster import fps

rusty1s's avatar
update  
rusty1s committed
51
52
53
        src = torch.Tensor([[-1, -1], [-1, 1], [1, -1], [1, 1]])
        batch = torch.tensor([0, 0, 0, 0])
        index = fps(src, batch, ratio=0.5)
rusty1s's avatar
rusty1s committed
54
55
    """

rusty1s's avatar
rusty1s committed
56
57
58
59
60
61
62
63
    r: Optional[Tensor] = None
    if ratio is None:
        r = torch.tensor(0.5, dtype=src.dtype, device=src.device)
    elif isinstance(ratio, float):
        r = torch.tensor(ratio, dtype=src.dtype, device=src.device)
    else:
        r = ratio
    assert r is not None
64

rusty1s's avatar
update  
rusty1s committed
65
    if batch is not None:
rusty1s's avatar
rusty1s committed
66
        assert src.size(0) == batch.numel()
67
68
        if batch_size is None:
            batch_size = int(batch.max()) + 1
rusty1s's avatar
rusty1s committed
69

rusty1s's avatar
update  
rusty1s committed
70
71
        deg = src.new_zeros(batch_size, dtype=torch.long)
        deg.scatter_add_(0, batch, torch.ones_like(batch))
rusty1s's avatar
typos  
rusty1s committed
72

rusty1s's avatar
rusty1s committed
73
        ptr = deg.new_zeros(batch_size + 1)
rusty1s's avatar
fix  
rusty1s committed
74
        torch.cumsum(deg, 0, out=ptr[1:])
rusty1s's avatar
rusty1s committed
75
76
    else:
        ptr = torch.tensor([0, src.size(0)], device=src.device)
rusty1s's avatar
rusty1s committed
77

rusty1s's avatar
rusty1s committed
78
    return torch.ops.torch_cluster.fps(src, ptr, r, random_start)