"sgl-kernel/vscode:/vscode.git/clone" did not exist on "0d7fe866f97d85f701782949b8579b3bb702f394"
test_sample_points_from_meshes.py 17 KB
Newer Older
1
# Copyright (c) Meta Platforms, Inc. and affiliates.
Patrick Labatut's avatar
Patrick Labatut committed
2
3
4
5
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
facebook-github-bot's avatar
facebook-github-bot committed
6
7
8
9


import unittest

10
import numpy as np
11
import torch
12
13
from common_testing import (
    get_pytorch3d_dir,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
14
    get_random_cuda_device,
15
    get_tests_dir,
16
    TestCaseMixin,
17
)
18
19
from PIL import Image
from pytorch3d.io import load_objs_as_meshes
Georgia Gkioxari's avatar
Georgia Gkioxari committed
20
from pytorch3d.ops import sample_points_from_meshes
21
22
23
24
25
26
27
28
29
30
from pytorch3d.renderer import TexturesVertex
from pytorch3d.renderer.cameras import FoVPerspectiveCameras, look_at_view_transform
from pytorch3d.renderer.mesh.rasterize_meshes import barycentric_coordinates
from pytorch3d.renderer.points import (
    NormWeightedCompositor,
    PointsRasterizationSettings,
    PointsRasterizer,
    PointsRenderer,
)
from pytorch3d.structures import Meshes, Pointclouds
facebook-github-bot's avatar
facebook-github-bot committed
31
32
from pytorch3d.utils.ico_sphere import ico_sphere

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
33

34
35
36
# If DEBUG=True, save out images generated in the tests for debugging.
# All saved images have prefix DEBUG_
DEBUG = False
37
DATA_DIR = get_tests_dir() / "data"
38
39


Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def init_meshes(
    num_meshes: int = 10,
    num_verts: int = 1000,
    num_faces: int = 3000,
    device: str = "cpu",
    add_texture: bool = False,
):
    device = torch.device(device)
    verts_list = []
    faces_list = []
    texts_list = []
    for _ in range(num_meshes):
        verts = torch.rand((num_verts, 3), dtype=torch.float32, device=device)
        faces = torch.randint(
            num_verts, size=(num_faces, 3), dtype=torch.int64, device=device
        )
        texts = torch.rand((num_verts, 3), dtype=torch.float32, device=device)
        verts_list.append(verts)
        faces_list.append(faces)
        texts_list.append(texts)

    # create textures
    textures = None
    if add_texture:
        textures = TexturesVertex(texts_list)
    meshes = Meshes(verts=verts_list, faces=faces_list, textures=textures)

    return meshes


Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
70
class TestSamplePoints(TestCaseMixin, unittest.TestCase):
facebook-github-bot's avatar
facebook-github-bot committed
71
72
73
74
75
76
77
78
79
    def setUp(self) -> None:
        super().setUp()
        torch.manual_seed(1)

    def test_all_empty_meshes(self):
        """
        Check sample_points_from_meshes raises an exception if all meshes are
        invalid.
        """
Nikhila Ravi's avatar
Nikhila Ravi committed
80
        device = get_random_cuda_device()
facebook-github-bot's avatar
facebook-github-bot committed
81
82
        verts1 = torch.tensor([], dtype=torch.float32, device=device)
        faces1 = torch.tensor([], dtype=torch.int64, device=device)
83
        meshes = Meshes(verts=[verts1, verts1, verts1], faces=[faces1, faces1, faces1])
facebook-github-bot's avatar
facebook-github-bot committed
84
        with self.assertRaises(ValueError) as err:
85
            sample_points_from_meshes(meshes, num_samples=100, return_normals=True)
facebook-github-bot's avatar
facebook-github-bot committed
86
87
88
89
90
91
92
93
        self.assertTrue("Meshes are empty." in str(err.exception))

    def test_sampling_output(self):
        """
        Check outputs of sampling are correct for different meshes.
        For an ico_sphere, the sampled vertices should lie on a unit sphere.
        For an empty mesh, the samples and normals should be 0.
        """
Nikhila Ravi's avatar
Nikhila Ravi committed
94
        device = get_random_cuda_device()
facebook-github-bot's avatar
facebook-github-bot committed
95
96
97

        # Unit simplex.
        verts_pyramid = torch.tensor(
98
            [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
facebook-github-bot's avatar
facebook-github-bot committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
            dtype=torch.float32,
            device=device,
        )
        faces_pyramid = torch.tensor(
            [[0, 1, 2], [0, 2, 3], [0, 1, 3], [1, 2, 3]],
            dtype=torch.int64,
            device=device,
        )
        sphere_mesh = ico_sphere(9, device)
        verts_sphere, faces_sphere = sphere_mesh.get_mesh_verts_faces(0)
        verts_empty = torch.tensor([], dtype=torch.float32, device=device)
        faces_empty = torch.tensor([], dtype=torch.int64, device=device)
        num_samples = 10
        meshes = Meshes(
            verts=[verts_empty, verts_sphere, verts_pyramid],
            faces=[faces_empty, faces_sphere, faces_pyramid],
        )
        samples, normals = sample_points_from_meshes(
            meshes, num_samples=num_samples, return_normals=True
        )
        samples = samples.cpu()
        normals = normals.cpu()

        self.assertEqual(samples.shape, (3, num_samples, 3))
        self.assertEqual(normals.shape, (3, num_samples, 3))

        # Empty meshes: should have all zeros for samples and normals.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
126
127
        self.assertClose(samples[0, :], torch.zeros((num_samples, 3)))
        self.assertClose(normals[0, :], torch.zeros((num_samples, 3)))
facebook-github-bot's avatar
facebook-github-bot committed
128
129
130

        # Sphere: points should have radius 1.
        x, y, z = samples[1, :].unbind(1)
131
        radius = torch.sqrt(x**2 + y**2 + z**2)
facebook-github-bot's avatar
facebook-github-bot committed
132

Nikhila Ravi's avatar
Nikhila Ravi committed
133
        self.assertClose(radius, torch.ones(num_samples))
facebook-github-bot's avatar
facebook-github-bot committed
134
135
136
137
138

        # Pyramid: points shoudl lie on one of the faces.
        pyramid_verts = samples[2, :]
        pyramid_normals = normals[2, :]

139
140
        self.assertClose(pyramid_verts.lt(1).float(), torch.ones_like(pyramid_verts))
        self.assertClose((pyramid_verts >= 0).float(), torch.ones_like(pyramid_verts))
facebook-github-bot's avatar
facebook-github-bot committed
141
142
143
144
145
146
147

        # Face 1: z = 0,  x + y <= 1, normals = (0, 0, 1).
        face_1_idxs = pyramid_verts[:, 2] == 0
        face_1_verts, face_1_normals = (
            pyramid_verts[face_1_idxs, :],
            pyramid_normals[face_1_idxs, :],
        )
148
        self.assertTrue(torch.all((face_1_verts[:, 0] + face_1_verts[:, 1]) <= 1))
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
149
150
        self.assertClose(
            face_1_normals,
151
            torch.tensor([0, 0, 1], dtype=torch.float32).expand(face_1_normals.size()),
facebook-github-bot's avatar
facebook-github-bot committed
152
153
154
155
156
157
158
159
        )

        # Face 2: x = 0,  z + y <= 1, normals = (1, 0, 0).
        face_2_idxs = pyramid_verts[:, 0] == 0
        face_2_verts, face_2_normals = (
            pyramid_verts[face_2_idxs, :],
            pyramid_normals[face_2_idxs, :],
        )
160
        self.assertTrue(torch.all((face_2_verts[:, 1] + face_2_verts[:, 2]) <= 1))
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
161
162
        self.assertClose(
            face_2_normals,
163
            torch.tensor([1, 0, 0], dtype=torch.float32).expand(face_2_normals.size()),
facebook-github-bot's avatar
facebook-github-bot committed
164
165
166
167
168
169
170
171
        )

        # Face 3: y = 0, x + z <= 1, normals = (0, -1, 0).
        face_3_idxs = pyramid_verts[:, 1] == 0
        face_3_verts, face_3_normals = (
            pyramid_verts[face_3_idxs, :],
            pyramid_normals[face_3_idxs, :],
        )
172
        self.assertTrue(torch.all((face_3_verts[:, 0] + face_3_verts[:, 2]) <= 1))
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
173
174
        self.assertClose(
            face_3_normals,
175
            torch.tensor([0, -1, 0], dtype=torch.float32).expand(face_3_normals.size()),
facebook-github-bot's avatar
facebook-github-bot committed
176
177
178
179
180
181
182
183
        )

        # Face 4: x + y + z = 1, normals = (1, 1, 1)/sqrt(3).
        face_4_idxs = pyramid_verts.gt(0).all(1)
        face_4_verts, face_4_normals = (
            pyramid_verts[face_4_idxs, :],
            pyramid_normals[face_4_idxs, :],
        )
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
184
185
186
187
188
189
190
        self.assertClose(face_4_verts.sum(1), torch.ones(face_4_verts.size(0)))
        self.assertClose(
            face_4_normals,
            (
                torch.tensor([1, 1, 1], dtype=torch.float32)
                / torch.sqrt(torch.tensor(3, dtype=torch.float32))
            ).expand(face_4_normals.size()),
facebook-github-bot's avatar
facebook-github-bot committed
191
192
        )

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
193
    def test_multinomial(self):
facebook-github-bot's avatar
facebook-github-bot committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
        """
        Confirm that torch.multinomial does not sample elements which have
        zero probability.
        """
        freqs = torch.cuda.FloatTensor(
            [
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.03178183361887932,
                0.027680952101945877,
                0.033176131546497345,
                0.046052902936935425,
                0.07742464542388916,
                0.11543981730937958,
                0.14148041605949402,
                0.15784293413162231,
                0.13180233538150787,
                0.08271478116512299,
                0.049702685326337814,
                0.027557924389839172,
                0.018125897273421288,
                0.011851548217236996,
                0.010252203792333603,
                0.007422595750540495,
                0.005372154992073774,
                0.0045109698548913,
                0.0036087757907807827,
                0.0035267581697553396,
                0.0018864056328311563,
                0.0024605290964245796,
                0.0022964938543736935,
                0.0018453967059031129,
                0.0010662291897460818,
                0.0009842115687206388,
                0.00045109697384759784,
                0.0007791675161570311,
                0.00020504408166743815,
                0.00020504408166743815,
                0.00020504408166743815,
                0.00012302644609007984,
                0.0,
                0.00012302644609007984,
                4.100881778867915e-05,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ]
        )

        sample = []
        for _ in range(1000):
            torch.cuda.get_rng_state()
            sample = torch.multinomial(freqs, 1000, True)
            if freqs[sample].min() == 0:
                sample_idx = (freqs[sample] == 0).nonzero()[0][0]
                sampled = sample[sample_idx]
                print(
                    "%s th element of last sample was %s, which has probability %s"
                    % (sample_idx, sampled, freqs[sampled])
                )
                return False
        return True

    def test_multinomial_weights(self):
        """
        Confirm that torch.multinomial does not sample elements which have
        zero probability using a real example of input from a training run.
        """
272
        weights = torch.load(get_tests_dir() / "weights.pt")
facebook-github-bot's avatar
facebook-github-bot committed
273
274
275
276
277
278
279
280
281
282
        S = 4096
        num_trials = 100
        for _ in range(0, num_trials):
            weights[weights < 0] = 0.0
            samples = weights.multinomial(S, replacement=True)
            sampled_weights = weights[samples]
            assert sampled_weights.min() > 0
            if sampled_weights.min() <= 0:
                return False
        return True
Georgia Gkioxari's avatar
Georgia Gkioxari committed
283

284
285
286
287
288
    def test_verts_nan(self):
        num_verts = 30
        num_faces = 50
        for device in ["cpu", "cuda:0"]:
            for invalid in ["nan", "inf"]:
289
                verts = torch.rand((num_verts, 3), dtype=torch.float32, device=device)
Georgia Gkioxari's avatar
Georgia Gkioxari committed
290
                # randomly assign an invalid type
291
292
                verts[torch.randperm(num_verts)[:10]] = float(invalid)
                faces = torch.randint(
293
                    num_verts, size=(num_faces, 3), dtype=torch.int64, device=device
294
295
296
                )
                meshes = Meshes(verts=[verts], faces=[faces])

297
                with self.assertRaisesRegex(ValueError, "Meshes contain nan or inf."):
298
299
300
                    sample_points_from_meshes(
                        meshes, num_samples=100, return_normals=True
                    )
facebook-github-bot's avatar
facebook-github-bot committed
301

302
303
304
    def test_outputs(self):

        for add_texture in (True, False):
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
305
            meshes = init_meshes(device=torch.device("cuda:0"), add_texture=add_texture)
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
            out1 = sample_points_from_meshes(meshes, num_samples=100)
            self.assertTrue(torch.is_tensor(out1))

            out2 = sample_points_from_meshes(
                meshes, num_samples=100, return_normals=True
            )
            self.assertTrue(isinstance(out2, tuple) and len(out2) == 2)

            if add_texture:
                out3 = sample_points_from_meshes(
                    meshes, num_samples=100, return_textures=True
                )
                self.assertTrue(isinstance(out3, tuple) and len(out3) == 2)

                out4 = sample_points_from_meshes(
                    meshes, num_samples=100, return_normals=True, return_textures=True
                )
                self.assertTrue(isinstance(out4, tuple) and len(out4) == 3)
            else:
                with self.assertRaisesRegex(
                    ValueError, "Meshes do not contain textures."
                ):
                    sample_points_from_meshes(
                        meshes, num_samples=100, return_textures=True
                    )

                with self.assertRaisesRegex(
                    ValueError, "Meshes do not contain textures."
                ):
                    sample_points_from_meshes(
                        meshes,
                        num_samples=100,
                        return_normals=True,
                        return_textures=True,
                    )

    def test_texture_sampling(self):
        device = torch.device("cuda:0")
        batch_size = 6
        # verts
        verts = torch.rand((batch_size, 6, 3), device=device, dtype=torch.float32)
        verts[:, :3, 2] = 1.0
        verts[:, 3:, 2] = -1.0
        # textures
        texts = torch.rand((batch_size, 6, 3), device=device, dtype=torch.float32)
        # faces
        faces = torch.tensor([[0, 1, 2], [3, 4, 5]], device=device, dtype=torch.int64)
        faces = faces.view(1, 2, 3).expand(batch_size, -1, -1)

        meshes = Meshes(verts=verts, faces=faces, textures=TexturesVertex(texts))

        num_samples = 24
        samples, normals, textures = sample_points_from_meshes(
            meshes, num_samples=num_samples, return_normals=True, return_textures=True
        )

        textures_naive = torch.zeros(
            (batch_size, num_samples, 3), dtype=torch.float32, device=device
        )
        for n in range(batch_size):
            for i in range(num_samples):
                p = samples[n, i]
                if p[2] > 0.0:  # sampled from 1st face
                    v0, v1, v2 = verts[n, 0, :2], verts[n, 1, :2], verts[n, 2, :2]
                    w0, w1, w2 = barycentric_coordinates(p[:2], v0, v1, v2)
                    t0, t1, t2 = texts[n, 0], texts[n, 1], texts[n, 2]
                else:  # sampled from 2nd face
                    v0, v1, v2 = verts[n, 3, :2], verts[n, 4, :2], verts[n, 5, :2]
                    w0, w1, w2 = barycentric_coordinates(p[:2], v0, v1, v2)
                    t0, t1, t2 = texts[n, 3], texts[n, 4], texts[n, 5]

                tt = w0 * t0 + w1 * t1 + w2 * t2
                textures_naive[n, i] = tt

        self.assertClose(textures, textures_naive)

    def test_texture_sampling_cow(self):
        # test texture sampling for the cow example by converting
        # the cow mesh and its texture uv to a pointcloud with texture

        device = torch.device("cuda:0")
387
        obj_dir = get_pytorch3d_dir() / "docs/tutorials/data"
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
        obj_filename = obj_dir / "cow_mesh/cow.obj"

        for text_type in ("uv", "atlas"):
            # Load mesh + texture
            if text_type == "uv":
                mesh = load_objs_as_meshes(
                    [obj_filename], device=device, load_textures=True, texture_wrap=None
                )
            elif text_type == "atlas":
                mesh = load_objs_as_meshes(
                    [obj_filename],
                    device=device,
                    load_textures=True,
                    create_texture_atlas=True,
                    texture_atlas_size=8,
                    texture_wrap=None,
                )

            points, normals, textures = sample_points_from_meshes(
                mesh, num_samples=50000, return_normals=True, return_textures=True
            )
            pointclouds = Pointclouds(points, normals=normals, features=textures)

            for pos in ("front", "back"):
                # Init rasterizer settings
                if pos == "back":
                    azim = 0.0
                elif pos == "front":
                    azim = 180
                R, T = look_at_view_transform(2.7, 0, azim)
                cameras = FoVPerspectiveCameras(device=device, R=R, T=T)

                raster_settings = PointsRasterizationSettings(
                    image_size=512, radius=1e-2, points_per_pixel=1
                )

                rasterizer = PointsRasterizer(
                    cameras=cameras, raster_settings=raster_settings
                )
                compositor = NormWeightedCompositor()
                renderer = PointsRenderer(rasterizer=rasterizer, compositor=compositor)
                images = renderer(pointclouds)

                rgb = images[0, ..., :3].squeeze().cpu()
                if DEBUG:
                    filename = "DEBUG_cow_mesh_to_pointcloud_%s_%s.png" % (
                        text_type,
                        pos,
                    )
                    Image.fromarray((rgb.numpy() * 255).astype(np.uint8)).save(
                        DATA_DIR / filename
                    )

facebook-github-bot's avatar
facebook-github-bot committed
441
442
443
444
445
446
447
448
449
450
451
    @staticmethod
    def sample_points_with_init(
        num_meshes: int,
        num_verts: int,
        num_faces: int,
        num_samples: int,
        device: str = "cpu",
    ):
        verts_list = []
        faces_list = []
        for _ in range(num_meshes):
452
            verts = torch.rand((num_verts, 3), dtype=torch.float32, device=device)
facebook-github-bot's avatar
facebook-github-bot committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
            faces = torch.randint(
                num_verts, size=(num_faces, 3), dtype=torch.int64, device=device
            )
            verts_list.append(verts)
            faces_list.append(faces)
        meshes = Meshes(verts_list, faces_list)
        torch.cuda.synchronize()

        def sample_points():
            sample_points_from_meshes(
                meshes, num_samples=num_samples, return_normals=True
            )
            torch.cuda.synchronize()

        return sample_points