Debian Codegraph @

test/test_fps.py

raw symbols SCIP
1 from itertools import product
2
3 import pytest
4 import torch
5 from torch import Tensor
6 from torch_cluster import fps
7 from torch_cluster.testing import devices, grad_dtypes, tensor
8
9
10 @torch.jit.script
11 def fps2(x: Tensor, ratio: Tensor) -> Tensor:
12 return fps(x, None, ratio, False)
13
14
15 @pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
16 def test_fps(dtype, device):
17 x = tensor([
18 [-1, -1],
19 [-1, +1],
20 [+1, +1],
21 [+1, -1],
22 [-2, -2],
23 [-2, +2],
24 [+2, +2],
25 [+2, -2],
26 ], dtype, device)
27 batch = tensor([0, 0, 0, 0, 1, 1, 1, 1], torch.long, device)
28 ptr_list = [0, 4, 8]
29 ptr = torch.tensor(ptr_list, device=device)
30
31 out = fps(x, batch, random_start=False)
32 assert out.tolist() == [0, 2, 4, 6]
33
34 out = fps(x, batch, ratio=0.5, random_start=False)
35 assert out.tolist() == [0, 2, 4, 6]
36
37 ratio = torch.tensor(0.5, device=device)
38 out = fps(x, batch, ratio=ratio, random_start=False)
39 assert out.tolist() == [0, 2, 4, 6]
40
41 out = fps(x, ptr=ptr_list, ratio=0.5, random_start=False)
42 assert out.tolist() == [0, 2, 4, 6]
43
44 out = fps(x, ptr=ptr, ratio=0.5, random_start=False)
45 assert out.tolist() == [0, 2, 4, 6]
46
47 ratio = torch.tensor([0.5, 0.5], device=device)
48 out = fps(x, batch, ratio=ratio, random_start=False)
49 assert out.tolist() == [0, 2, 4, 6]
50
51 out = fps(x, random_start=False)
52 assert out.sort()[0].tolist() == [0, 5, 6, 7]
53
54 out = fps(x, ratio=0.5, random_start=False)
55 assert out.sort()[0].tolist() == [0, 5, 6, 7]
56
57 out = fps(x, ratio=torch.tensor(0.5, device=device), random_start=False)
58 assert out.sort()[0].tolist() == [0, 5, 6, 7]
59
60 out = fps(x, ratio=torch.tensor([0.5], device=device), random_start=False)
61 assert out.sort()[0].tolist() == [0, 5, 6, 7]
62
63 out = fps2(x, torch.tensor([0.5], device=device))
64 assert out.sort()[0].tolist() == [0, 5, 6, 7]
65
66
67 @pytest.mark.parametrize('device', devices)
68 def test_random_fps(device):
69 N = 1024
70 for _ in range(5):
71 pos = torch.randn((2 * N, 3), device=device)
72 batch_1 = torch.zeros(N, dtype=torch.long, device=device)
73 batch_2 = torch.ones(N, dtype=torch.long, device=device)
74 batch = torch.cat([batch_1, batch_2])
75 idx = fps(pos, batch, ratio=0.5)
76 assert idx.min() >= 0 and idx.max() < 2 * N