fps.py 2.17 KB
Newer Older
rusty1s's avatar
update  
rusty1s committed
1
from typing import Optional
Duc's avatar
Duc committed
2
from torch import Tensor
rusty1s's avatar
update  
rusty1s committed
3
import torch
rusty1s's avatar
rusty1s committed
4
5


Duc's avatar
Duc committed
6
7
8
9
10
11
12
13
14
15
16
17
18
@torch.jit._overload
def fps(src, batch=None, ratio=None, random_start=True):
    # type: (Tensor, Optional[Tensor], Optional[int], bool) -> Tensor
    pass


@torch.jit._overload
def fps(src, batch=None, ratio=None, random_start=True):
    # type: (Tensor, Optional[Tensor], Optional[Tensor], bool) -> Tensor
    pass


def fps(src: torch.Tensor, batch=None, ratio=None, random_start=True):
rusty1s's avatar
rusty1s committed
19
20
21
22
    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
23
24

    Args:
rusty1s's avatar
update  
rusty1s committed
25
        src (Tensor): Point feature matrix
rusty1s's avatar
rusty1s committed
26
27
28
29
            :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`)
30
        ratio (Tensor, optional): Sampling ratio. (default: :obj:`0.5`)
rusty1s's avatar
rusty1s committed
31
32
        random_start (bool, optional): If set to :obj:`False`, use the first
            node in :math:`\mathbf{X}` as starting node. (default: obj:`True`)
rusty1s's avatar
rusty1s committed
33

rusty1s's avatar
docs  
rusty1s committed
34
35
    :rtype: :class:`LongTensor`

rusty1s's avatar
update  
rusty1s committed
36
    .. code-block:: python
rusty1s's avatar
rusty1s committed
37
38
39
40

        import torch
        from torch_cluster import fps

rusty1s's avatar
update  
rusty1s committed
41
42
43
        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
44
45
    """

Duc's avatar
Duc committed
46
47
48
49
    if not isinstance(ratio, Tensor):
        ratio = torch.tensor(ratio)

    assert len(ratio.shape) < 2, f'ratio should be a scalar or a vector, received a tensor rank {len(ratio.shape)}'
50
51
    ratio = ratio.to(src.device)

rusty1s's avatar
update  
rusty1s committed
52
    if batch is not None:
rusty1s's avatar
rusty1s committed
53
        assert src.size(0) == batch.numel()
rusty1s's avatar
update  
rusty1s committed
54
        batch_size = int(batch.max()) + 1
rusty1s's avatar
rusty1s committed
55

rusty1s's avatar
update  
rusty1s committed
56
57
        deg = src.new_zeros(batch_size, dtype=torch.long)
        deg.scatter_add_(0, batch, torch.ones_like(batch))
rusty1s's avatar
typos  
rusty1s committed
58

rusty1s's avatar
rusty1s committed
59
        ptr = deg.new_zeros(batch_size + 1)
rusty1s's avatar
fix  
rusty1s committed
60
        torch.cumsum(deg, 0, out=ptr[1:])
rusty1s's avatar
rusty1s committed
61
62
    else:
        ptr = torch.tensor([0, src.size(0)], device=src.device)
rusty1s's avatar
rusty1s committed
63

rusty1s's avatar
update  
rusty1s committed
64
    return torch.ops.torch_cluster.fps(src, ptr, ratio, random_start)