test_fps.py 1.13 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
from itertools import product

import pytest
import torch
rusty1s's avatar
rusty1s committed
5
from torch_cluster import fps
rusty1s's avatar
rusty1s committed
6

rusty1s's avatar
rusty1s committed
7
from .utils import grad_dtypes, devices, tensor
rusty1s's avatar
rusty1s committed
8
9


rusty1s's avatar
rusty1s committed
10
@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
rusty1s's avatar
rusty1s committed
11
def test_fps(dtype, device):
rusty1s's avatar
rusty1s committed
12
13
14
15
16
17
18
19
20
21
    x = tensor([
        [-1, -1],
        [-1, +1],
        [+1, +1],
        [+1, -1],
        [-2, -2],
        [-2, +2],
        [+2, +2],
        [+2, -2],
    ], dtype, device)
rusty1s's avatar
rusty1s committed
22
23
    batch = tensor([0, 0, 0, 0, 1, 1, 1, 1], torch.long, device)

24
    out = fps(x, batch, ratio=torch.tensor(0.5), random_start=False)
rusty1s's avatar
rusty1s committed
25
    assert out.tolist() == [0, 2, 4, 6]
rusty1s's avatar
rusty1s committed
26

27
    out = fps(x, ratio=torch.tensor(0.5), random_start=False)
rusty1s's avatar
rusty1s committed
28
    assert out.sort()[0].tolist() == [0, 5, 6, 7]
rusty1s's avatar
rusty1s committed
29
30
31
32
33
34
35
36
37
38


@pytest.mark.parametrize('device', devices)
def test_random_fps(device):
    N = 1024
    for _ in range(5):
        pos = torch.randn((2 * N, 3), device=device)
        batch_1 = torch.zeros(N, dtype=torch.long, device=device)
        batch_2 = torch.ones(N, dtype=torch.long, device=device)
        batch = torch.cat([batch_1, batch_2])
39
        idx = fps(pos, batch, ratio=torch.tensor(0.5))
rusty1s's avatar
rusty1s committed
40
        assert idx.min() >= 0 and idx.max() < 2 * N