| 1 |
from typing import List, Optional, Union |
| 2 |
|
| 3 |
import torch |
| 4 |
from torch import Tensor |
| 5 |
|
| 6 |
import torch_cluster.typing |
| 7 |
|
| 8 |
|
| 9 |
@torch.jit._overload # noqa |
| 10 |
def fps(src, batch, ratio, random_start, batch_size, ptr): # noqa |
| 11 |
# type: (Tensor, Optional[Tensor], Optional[float], bool, Optional[int], Optional[Tensor]) -> Tensor # noqa |
| 12 |
pass # pragma: no cover |
| 13 |
|
| 14 |
|
| 15 |
@torch.jit._overload # noqa |
| 16 |
def fps(src, batch, ratio, random_start, batch_size, ptr): # noqa |
| 17 |
# type: (Tensor, Optional[Tensor], Optional[Tensor], bool, Optional[int], Optional[Tensor]) -> Tensor # noqa |
| 18 |
pass # pragma: no cover |
| 19 |
|
| 20 |
|
| 21 |
@torch.jit._overload # noqa |
| 22 |
def fps(src, batch, ratio, random_start, batch_size, ptr): # noqa |
| 23 |
# type: (Tensor, Optional[Tensor], Optional[float], bool, Optional[int], Optional[List[int]]) -> Tensor # noqa |
| 24 |
pass # pragma: no cover |
| 25 |
|
| 26 |
|
| 27 |
@torch.jit._overload # noqa |
| 28 |
def fps(src, batch, ratio, random_start, batch_size, ptr): # noqa |
| 29 |
# type: (Tensor, Optional[Tensor], Optional[Tensor], bool, Optional[int], Optional[List[int]]) -> Tensor # noqa |
| 30 |
pass # pragma: no cover |
| 31 |
|
| 32 |
|
| 33 |
def fps( # noqa |
| 34 |
src: torch.Tensor, |
| 35 |
batch: Optional[Tensor] = None, |
| 36 |
ratio: Optional[Union[Tensor, float]] = None, |
| 37 |
random_start: bool = True, |
| 38 |
batch_size: Optional[int] = None, |
| 39 |
ptr: Optional[Union[Tensor, List[int]]] = None, |
| 40 |
): |
| 41 |
r""""A sampling algorithm from the `"PointNet++: Deep Hierarchical Feature |
| 42 |
Learning on Point Sets in a Metric Space" |
| 43 |
<https://arxiv.org/abs/1706.02413>`_ paper, which iteratively samples the |
| 44 |
most distant point with regard to the rest points. |
| 45 |
|
| 46 |
Args: |
| 47 |
src (Tensor): Point feature matrix |
| 48 |
:math:`\mathbf{X} \in \mathbb{R}^{N \times F}`. |
| 49 |
batch (LongTensor, optional): Batch vector |
| 50 |
:math:`\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N`, which assigns each |
| 51 |
node to a specific example. (default: :obj:`None`) |
| 52 |
ratio (float or Tensor, optional): Sampling ratio. |
| 53 |
(default: :obj:`0.5`) |
| 54 |
random_start (bool, optional): If set to :obj:`False`, use the first |
| 55 |
node in :math:`\mathbf{X}` as starting node. (default: obj:`True`) |
| 56 |
batch_size (int, optional): The number of examples :math:`B`. |
| 57 |
Automatically calculated if not given. (default: :obj:`None`) |
| 58 |
ptr (torch.Tensor or [int], optional): If given, batch assignment will |
| 59 |
be determined based on boundaries in CSR representation, *e.g.*, |
| 60 |
:obj:`batch=[0,0,1,1,1,2]` translates to :obj:`ptr=[0,2,5,6]`. |
| 61 |
(default: :obj:`None`) |
| 62 |
|
| 63 |
:rtype: :class:`LongTensor` |
| 64 |
|
| 65 |
.. code-block:: python |
| 66 |
|
| 67 |
import torch |
| 68 |
from torch_cluster import fps |
| 69 |
|
| 70 |
src = torch.Tensor([[-1, -1], [-1, 1], [1, -1], [1, 1]]) |
| 71 |
batch = torch.tensor([0, 0, 0, 0]) |
| 72 |
index = fps(src, batch, ratio=0.5) |
| 73 |
""" |
| 74 |
r: Optional[Tensor] = None |
| 75 |
if ratio is None: |
| 76 |
r = torch.tensor(0.5, dtype=src.dtype, device=src.device) |
| 77 |
elif isinstance(ratio, float): |
| 78 |
r = torch.tensor(ratio, dtype=src.dtype, device=src.device) |
| 79 |
else: |
| 80 |
r = ratio |
| 81 |
assert r is not None |
| 82 |
|
| 83 |
if ptr is not None: |
| 84 |
if isinstance(ptr, list) and torch_cluster.typing.WITH_PTR_LIST: |
| 85 |
return torch.ops.torch_cluster.fps_ptr_list( |
| 86 |
src, ptr, r, random_start) |
| 87 |
|
| 88 |
if isinstance(ptr, list): |
| 89 |
return torch.ops.torch_cluster.fps( |
| 90 |
src, torch.tensor(ptr, device=src.device), r, random_start) |
| 91 |
else: |
| 92 |
return torch.ops.torch_cluster.fps(src, ptr, r, random_start) |
| 93 |
|
| 94 |
if batch is not None: |
| 95 |
assert src.size(0) == batch.numel() |
| 96 |
if batch_size is None: |
| 97 |
batch_size = int(batch.max()) + 1 |
| 98 |
|
| 99 |
deg = src.new_zeros(batch_size, dtype=torch.long) |
| 100 |
deg.scatter_add_(0, batch, torch.ones_like(batch)) |
| 101 |
|
| 102 |
ptr_vec = deg.new_zeros(batch_size + 1) |
| 103 |
torch.cumsum(deg, 0, out=ptr_vec[1:]) |
| 104 |
else: |
| 105 |
ptr_vec = torch.tensor([0, src.size(0)], device=src.device) |
| 106 |
|
| 107 |
return torch.ops.torch_cluster.fps(src, ptr_vec, r, random_start) |