|
| 1 | +# Copyright (c) Meta Platforms, Inc. and 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 numpy as np |
| 10 | +import torch |
| 11 | +from pytorch3d.structures.pointclouds import Pointclouds |
| 12 | + |
| 13 | +from .common_testing import needs_multigpu, TestCaseMixin |
| 14 | + |
| 15 | + |
| 16 | +class TestPointclouds(TestCaseMixin, unittest.TestCase): |
| 17 | + def setUp(self) -> None: |
| 18 | + np.random.seed(42) |
| 19 | + torch.manual_seed(42) |
| 20 | + |
| 21 | + @staticmethod |
| 22 | + def init_cloud( |
| 23 | + num_clouds: int = 3, |
| 24 | + max_points: int = 100, |
| 25 | + channels: int = 4, |
| 26 | + lists_to_tensors: bool = False, |
| 27 | + with_normals: bool = True, |
| 28 | + with_features: bool = True, |
| 29 | + min_points: int = 0, |
| 30 | + requires_grad: bool = False, |
| 31 | + ): |
| 32 | + """ |
| 33 | + Function to generate a Pointclouds object of N meshes with |
| 34 | + random number of points. |
| 35 | +
|
| 36 | + Args: |
| 37 | + num_clouds: Number of clouds to generate. |
| 38 | + channels: Number of features. |
| 39 | + max_points: Max number of points per cloud. |
| 40 | + lists_to_tensors: Determines whether the generated clouds should be |
| 41 | + constructed from lists (=False) or |
| 42 | + tensors (=True) of points/normals/features. |
| 43 | + with_normals: bool whether to include normals |
| 44 | + with_features: bool whether to include features |
| 45 | + min_points: Min number of points per cloud |
| 46 | +
|
| 47 | + Returns: |
| 48 | + Pointclouds object. |
| 49 | + """ |
| 50 | + device = torch.device("cuda:0") |
| 51 | + p = torch.randint(low=min_points, high=max_points, size=(num_clouds,)) |
| 52 | + if lists_to_tensors: |
| 53 | + p.fill_(p[0]) |
| 54 | + |
| 55 | + points_list = [ |
| 56 | + torch.rand( |
| 57 | + (i, 3), device=device, dtype=torch.float32, requires_grad=requires_grad |
| 58 | + ) |
| 59 | + for i in p |
| 60 | + ] |
| 61 | + normals_list, features_list = None, None |
| 62 | + if with_normals: |
| 63 | + normals_list = [ |
| 64 | + torch.rand( |
| 65 | + (i, 3), |
| 66 | + device=device, |
| 67 | + dtype=torch.float32, |
| 68 | + requires_grad=requires_grad, |
| 69 | + ) |
| 70 | + for i in p |
| 71 | + ] |
| 72 | + if with_features: |
| 73 | + features_list = [ |
| 74 | + torch.rand( |
| 75 | + (i, channels), |
| 76 | + device=device, |
| 77 | + dtype=torch.float32, |
| 78 | + requires_grad=requires_grad, |
| 79 | + ) |
| 80 | + for i in p |
| 81 | + ] |
| 82 | + |
| 83 | + if lists_to_tensors: |
| 84 | + points_list = torch.stack(points_list) |
| 85 | + if with_normals: |
| 86 | + normals_list = torch.stack(normals_list) |
| 87 | + if with_features: |
| 88 | + features_list = torch.stack(features_list) |
| 89 | + |
| 90 | + return Pointclouds(points_list, normals=normals_list, features=features_list) |
| 91 | + |
| 92 | + @needs_multigpu |
| 93 | + def test_to_list(self): |
| 94 | + cloud = self.init_cloud(5, 100, 10) |
| 95 | + device = torch.device("cuda:1") |
| 96 | + |
| 97 | + new_cloud = cloud.to(device) |
| 98 | + self.assertTrue(new_cloud.device == device) |
| 99 | + self.assertTrue(cloud.device == torch.device("cuda:0")) |
| 100 | + for attrib in [ |
| 101 | + "points_padded", |
| 102 | + "points_packed", |
| 103 | + "normals_padded", |
| 104 | + "normals_packed", |
| 105 | + "features_padded", |
| 106 | + "features_packed", |
| 107 | + "num_points_per_cloud", |
| 108 | + "cloud_to_packed_first_idx", |
| 109 | + "padded_to_packed_idx", |
| 110 | + ]: |
| 111 | + self.assertClose( |
| 112 | + getattr(new_cloud, attrib)().cpu(), getattr(cloud, attrib)().cpu() |
| 113 | + ) |
| 114 | + for i in range(len(cloud)): |
| 115 | + self.assertClose( |
| 116 | + cloud.points_list()[i].cpu(), new_cloud.points_list()[i].cpu() |
| 117 | + ) |
| 118 | + self.assertClose( |
| 119 | + cloud.normals_list()[i].cpu(), new_cloud.normals_list()[i].cpu() |
| 120 | + ) |
| 121 | + self.assertClose( |
| 122 | + cloud.features_list()[i].cpu(), new_cloud.features_list()[i].cpu() |
| 123 | + ) |
| 124 | + self.assertTrue(all(cloud.valid.cpu() == new_cloud.valid.cpu())) |
| 125 | + self.assertTrue(cloud.equisized == new_cloud.equisized) |
| 126 | + self.assertTrue(cloud._N == new_cloud._N) |
| 127 | + self.assertTrue(cloud._P == new_cloud._P) |
| 128 | + self.assertTrue(cloud._C == new_cloud._C) |
| 129 | + |
| 130 | + @needs_multigpu |
| 131 | + def test_to_tensor(self): |
| 132 | + cloud = self.init_cloud(5, 100, 10, lists_to_tensors=True) |
| 133 | + device = torch.device("cuda:1") |
| 134 | + |
| 135 | + new_cloud = cloud.to(device) |
| 136 | + self.assertTrue(new_cloud.device == device) |
| 137 | + self.assertTrue(cloud.device == torch.device("cuda:0")) |
| 138 | + for attrib in [ |
| 139 | + "points_padded", |
| 140 | + "points_packed", |
| 141 | + "normals_padded", |
| 142 | + "normals_packed", |
| 143 | + "features_padded", |
| 144 | + "features_packed", |
| 145 | + "num_points_per_cloud", |
| 146 | + "cloud_to_packed_first_idx", |
| 147 | + "padded_to_packed_idx", |
| 148 | + ]: |
| 149 | + self.assertClose( |
| 150 | + getattr(new_cloud, attrib)().cpu(), getattr(cloud, attrib)().cpu() |
| 151 | + ) |
| 152 | + for i in range(len(cloud)): |
| 153 | + self.assertClose( |
| 154 | + cloud.points_list()[i].cpu(), new_cloud.points_list()[i].cpu() |
| 155 | + ) |
| 156 | + self.assertClose( |
| 157 | + cloud.normals_list()[i].cpu(), new_cloud.normals_list()[i].cpu() |
| 158 | + ) |
| 159 | + self.assertClose( |
| 160 | + cloud.features_list()[i].cpu(), new_cloud.features_list()[i].cpu() |
| 161 | + ) |
| 162 | + self.assertTrue(all(cloud.valid.cpu() == new_cloud.valid.cpu())) |
| 163 | + self.assertTrue(cloud.equisized == new_cloud.equisized) |
| 164 | + self.assertTrue(cloud._N == new_cloud._N) |
| 165 | + self.assertTrue(cloud._P == new_cloud._P) |
| 166 | + self.assertTrue(cloud._C == new_cloud._C) |
0 commit comments