|
| 1 | +# Copyright (c) Facebook, Inc. and its affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import unittest |
| 8 | + |
| 9 | +import torch |
| 10 | +from common_testing import TestCaseMixin, get_random_cuda_device |
| 11 | +from pytorch3d.ops.sample_farthest_points import sample_farthest_points_naive |
| 12 | +from pytorch3d.ops.utils import masked_gather |
| 13 | + |
| 14 | + |
| 15 | +class TestFPS(TestCaseMixin, unittest.TestCase): |
| 16 | + def test_simple(self): |
| 17 | + device = get_random_cuda_device() |
| 18 | + # fmt: off |
| 19 | + points = torch.tensor( |
| 20 | + [ |
| 21 | + [ |
| 22 | + [-1.0, -1.0], # noqa: E241, E201 |
| 23 | + [-1.3, 1.1], # noqa: E241, E201 |
| 24 | + [ 0.2, -1.1], # noqa: E241, E201 |
| 25 | + [ 0.0, 0.0], # noqa: E241, E201 |
| 26 | + [ 1.3, 1.3], # noqa: E241, E201 |
| 27 | + [ 1.0, 0.5], # noqa: E241, E201 |
| 28 | + [-1.3, 0.2], # noqa: E241, E201 |
| 29 | + [ 1.5, -0.5], # noqa: E241, E201 |
| 30 | + ], |
| 31 | + [ |
| 32 | + [-2.2, -2.4], # noqa: E241, E201 |
| 33 | + [-2.1, 2.0], # noqa: E241, E201 |
| 34 | + [ 2.2, 2.1], # noqa: E241, E201 |
| 35 | + [ 2.1, -2.4], # noqa: E241, E201 |
| 36 | + [ 0.4, -1.0], # noqa: E241, E201 |
| 37 | + [ 0.3, 0.3], # noqa: E241, E201 |
| 38 | + [ 1.2, 0.5], # noqa: E241, E201 |
| 39 | + [ 4.5, 4.5], # noqa: E241, E201 |
| 40 | + ], |
| 41 | + ], |
| 42 | + dtype=torch.float32, |
| 43 | + device=device, |
| 44 | + ) |
| 45 | + # fmt: on |
| 46 | + expected_inds = torch.tensor([[0, 4], [0, 7]], dtype=torch.int64, device=device) |
| 47 | + out_points, out_inds = sample_farthest_points_naive(points, K=2) |
| 48 | + self.assertClose(out_inds, expected_inds) |
| 49 | + |
| 50 | + # Gather the points |
| 51 | + expected_inds = expected_inds[..., None].expand(-1, -1, points.shape[-1]) |
| 52 | + self.assertClose(out_points, points.gather(dim=1, index=expected_inds)) |
| 53 | + |
| 54 | + # Different number of points sampled for each pointcloud in the batch |
| 55 | + expected_inds = torch.tensor( |
| 56 | + [[0, 4, 1], [0, 7, -1]], dtype=torch.int64, device=device |
| 57 | + ) |
| 58 | + out_points, out_inds = sample_farthest_points_naive(points, K=[3, 2]) |
| 59 | + self.assertClose(out_inds, expected_inds) |
| 60 | + |
| 61 | + # Gather the points |
| 62 | + expected_points = masked_gather(points, expected_inds) |
| 63 | + self.assertClose(out_points, expected_points) |
| 64 | + |
| 65 | + def test_random_heterogeneous(self): |
| 66 | + device = get_random_cuda_device() |
| 67 | + N, P, D, K = 5, 40, 5, 8 |
| 68 | + points = torch.randn((N, P, D), device=device) |
| 69 | + out_points, out_idxs = sample_farthest_points_naive(points, K=K) |
| 70 | + self.assertTrue(out_idxs.min() >= 0) |
| 71 | + for n in range(N): |
| 72 | + self.assertEqual(out_idxs[n].ne(-1).sum(), K) |
| 73 | + |
| 74 | + lengths = torch.randint(low=1, high=P, size=(N,), device=device) |
| 75 | + out_points, out_idxs = sample_farthest_points_naive(points, lengths, K=50) |
| 76 | + |
| 77 | + for n in range(N): |
| 78 | + # Check that for heterogeneous batches, the max number of |
| 79 | + # selected points is less than the length |
| 80 | + self.assertTrue(out_idxs[n].ne(-1).sum() <= lengths[n]) |
| 81 | + self.assertTrue(out_idxs[n].max() <= lengths[n]) |
| 82 | + |
| 83 | + # Check there are no duplicate indices |
| 84 | + val_mask = out_idxs[n].ne(-1) |
| 85 | + vals, counts = torch.unique(out_idxs[n][val_mask], return_counts=True) |
| 86 | + self.assertTrue(counts.le(1).all()) |
| 87 | + |
| 88 | + def test_errors(self): |
| 89 | + device = get_random_cuda_device() |
| 90 | + N, P, D, K = 5, 40, 5, 8 |
| 91 | + points = torch.randn((N, P, D), device=device) |
| 92 | + wrong_batch_dim = torch.randint(low=1, high=K, size=(K,), device=device) |
| 93 | + |
| 94 | + # K has diferent batch dimension to points |
| 95 | + with self.assertRaisesRegex(ValueError, "K and points must have"): |
| 96 | + sample_farthest_points_naive(points, K=wrong_batch_dim) |
| 97 | + |
| 98 | + # lengths has diferent batch dimension to points |
| 99 | + with self.assertRaisesRegex(ValueError, "points and lengths must have"): |
| 100 | + sample_farthest_points_naive(points, lengths=wrong_batch_dim, K=K) |
| 101 | + |
| 102 | + def test_random_start(self): |
| 103 | + device = get_random_cuda_device() |
| 104 | + N, P, D, K = 5, 40, 5, 8 |
| 105 | + points = torch.randn((N, P, D), device=device) |
| 106 | + out_points, out_idxs = sample_farthest_points_naive( |
| 107 | + points, K=K, random_start_point=True |
| 108 | + ) |
| 109 | + # Check the first index is not 0 for all batch elements |
| 110 | + # when random_start_point = True |
| 111 | + self.assertTrue(out_idxs[:, 0].sum() > 0) |
0 commit comments