test_rasterize_points.py 26.3 KB
Newer Older
1
2
3
4
5
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.


import unittest

6
7
import numpy as np
import torch
Nikhila Ravi's avatar
Nikhila Ravi committed
8
from common_testing import TestCaseMixin, get_random_cuda_device
9
10
from pytorch3d import _C
from pytorch3d.renderer.points.rasterize_points import (
11
    _format_radius,
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    rasterize_points,
    rasterize_points_python,
)
from pytorch3d.structures.pointclouds import Pointclouds


class TestRasterizePoints(TestCaseMixin, unittest.TestCase):
    def test_python_simple_cpu(self):
        self._simple_test_case(
            rasterize_points_python, torch.device("cpu"), bin_size=-1
        )

    def test_naive_simple_cpu(self):
        device = torch.device("cpu")
        self._simple_test_case(rasterize_points, device)

    def test_naive_simple_cuda(self):
Nikhila Ravi's avatar
Nikhila Ravi committed
29
        device = get_random_cuda_device()
30
31
32
33
34
35
36
37
38
39
40
        self._simple_test_case(rasterize_points, device, bin_size=0)

    def test_python_behind_camera(self):
        self._test_behind_camera(
            rasterize_points_python, torch.device("cpu"), bin_size=-1
        )

    def test_cpu_behind_camera(self):
        self._test_behind_camera(rasterize_points, torch.device("cpu"))

    def test_cuda_behind_camera(self):
Nikhila Ravi's avatar
Nikhila Ravi committed
41
42
        device = get_random_cuda_device()
        self._test_behind_camera(rasterize_points, device, bin_size=0)
43

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    def test_python_variable_radius(self):
        self._test_variable_size_radius(
            rasterize_points_python, torch.device("cpu"), bin_size=-1
        )

    def test_cpu_variable_radius(self):
        self._test_variable_size_radius(rasterize_points, torch.device("cpu"))

    def test_cuda_variable_radius(self):
        device = get_random_cuda_device()
        # Naive
        self._test_variable_size_radius(rasterize_points, device, bin_size=0)
        # Coarse to fine
        self._test_variable_size_radius(rasterize_points, device, bin_size=None)

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    def test_cpp_vs_naive_vs_binned(self):
        # Make sure that the backward pass runs for all pathways
        N = 2
        P = 1000
        image_size = 32
        radius = 0.1
        points_per_pixel = 3
        points1 = torch.randn(P, 3, requires_grad=True)
        points2 = torch.randn(int(P / 2), 3, requires_grad=True)
        pointclouds = Pointclouds(points=[points1, points2])
        grad_zbuf = torch.randn(N, image_size, image_size, points_per_pixel)
        grad_dists = torch.randn(N, image_size, image_size, points_per_pixel)

        # Option I: CPU, naive
        idx1, zbuf1, dists1 = rasterize_points(
            pointclouds, image_size, radius, points_per_pixel, bin_size=0
        )
        loss = (zbuf1 * grad_zbuf).sum() + (dists1 * grad_dists).sum()
        loss.backward()
        grad1 = points1.grad.data.clone()

        # Option II: CUDA, naive
        points1_cuda = points1.cuda().detach().clone().requires_grad_(True)
        points2_cuda = points2.cuda().detach().clone().requires_grad_(True)
        pointclouds = Pointclouds(points=[points1_cuda, points2_cuda])
        grad_zbuf = grad_zbuf.cuda()
        grad_dists = grad_dists.cuda()
        idx2, zbuf2, dists2 = rasterize_points(
            pointclouds, image_size, radius, points_per_pixel, bin_size=0
        )
        loss = (zbuf2 * grad_zbuf).sum() + (dists2 * grad_dists).sum()
        loss.backward()
        idx2 = idx2.data.cpu().clone()
        zbuf2 = zbuf2.data.cpu().clone()
        dists2 = dists2.data.cpu().clone()
        grad2 = points1_cuda.grad.data.cpu().clone()

        # Option III: CUDA, binned
        points1_cuda = points1.cuda().detach().clone().requires_grad_(True)
        points2_cuda = points2.cuda().detach().clone().requires_grad_(True)
        pointclouds = Pointclouds(points=[points1_cuda, points2_cuda])
        idx3, zbuf3, dists3 = rasterize_points(
            pointclouds, image_size, radius, points_per_pixel, bin_size=32
        )
        loss = (zbuf3 * grad_zbuf).sum() + (dists3 * grad_dists).sum()
        points1.grad.data.zero_()
        loss.backward()
        idx3 = idx3.data.cpu().clone()
        zbuf3 = zbuf3.data.cpu().clone()
        dists3 = dists3.data.cpu().clone()
        grad3 = points1_cuda.grad.data.cpu().clone()

        # Make sure everything was the same
        idx12_same = (idx1 == idx2).all().item()
        idx13_same = (idx1 == idx3).all().item()
        zbuf12_same = (zbuf1 == zbuf2).all().item()
        zbuf13_same = (zbuf1 == zbuf3).all().item()
        dists12_diff = (dists1 - dists2).abs().max().item()
        dists13_diff = (dists1 - dists3).abs().max().item()
        self.assertTrue(idx12_same)
        self.assertTrue(idx13_same)
        self.assertTrue(zbuf12_same)
        self.assertTrue(zbuf13_same)
        self.assertTrue(dists12_diff < 1e-6)
        self.assertTrue(dists13_diff < 1e-6)

        diff12 = (grad1 - grad2).abs().max().item()
        diff13 = (grad1 - grad3).abs().max().item()
        diff23 = (grad2 - grad3).abs().max().item()
        self.assertTrue(diff12 < 5e-6)
        self.assertTrue(diff13 < 5e-6)
        self.assertTrue(diff23 < 5e-6)

    def test_python_vs_cpu_naive(self):
        torch.manual_seed(231)
        image_size = 32
        radius = 0.1
        points_per_pixel = 3

        # Test a batch of homogeneous point clouds.
        N = 2
        P = 17
        points = torch.randn(N, P, 3, requires_grad=True)
        pointclouds = Pointclouds(points=points)
        args = (pointclouds, image_size, radius, points_per_pixel)
        self._compare_impls(
            rasterize_points_python,
            rasterize_points,
            args,
            args,
            points,
            points,
            compare_grads=True,
        )

        # Test a batch of heterogeneous point clouds.
        P2 = 10
        points1 = torch.randn(P, 3, requires_grad=True)
        points2 = torch.randn(P2, 3)
        pointclouds = Pointclouds(points=[points1, points2])
        args = (pointclouds, image_size, radius, points_per_pixel)
        self._compare_impls(
            rasterize_points_python,
            rasterize_points,
            args,
            args,
            points1,  # check gradients for first element in batch
            points1,
            compare_grads=True,
        )

    def test_cpu_vs_cuda_naive(self):
        torch.manual_seed(231)
        image_size = 64
        radius = 0.1
        points_per_pixel = 5

        # Test homogeneous point cloud batch.
        N = 2
        P = 1000
        bin_size = 0
        points_cpu = torch.rand(N, P, 3, requires_grad=True)
        points_cuda = points_cpu.cuda().detach().requires_grad_(True)
        pointclouds_cpu = Pointclouds(points=points_cpu)
        pointclouds_cuda = Pointclouds(points=points_cuda)
184
185
        args_cpu = (pointclouds_cpu, image_size, radius, points_per_pixel, bin_size)
        args_cuda = (pointclouds_cuda, image_size, radius, points_per_pixel, bin_size)
186
187
188
189
190
191
192
193
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
        self._compare_impls(
            rasterize_points,
            rasterize_points,
            args_cpu,
            args_cuda,
            points_cpu,
            points_cuda,
            compare_grads=True,
        )

    def _compare_impls(
        self,
        fn1,
        fn2,
        args1,
        args2,
        grad_var1=None,
        grad_var2=None,
        compare_grads=False,
    ):
        idx1, zbuf1, dist1 = fn1(*args1)
        torch.manual_seed(231)
        grad_zbuf = torch.randn_like(zbuf1)
        grad_dist = torch.randn_like(dist1)
        loss = (zbuf1 * grad_zbuf).sum() + (dist1 * grad_dist).sum()
        if compare_grads:
            loss.backward()
            grad_points1 = grad_var1.grad.data.clone().cpu()

        idx2, zbuf2, dist2 = fn2(*args2)
        grad_zbuf = grad_zbuf.to(zbuf2)
        grad_dist = grad_dist.to(dist2)
        loss = (zbuf2 * grad_zbuf).sum() + (dist2 * grad_dist).sum()
        if compare_grads:
            # clear points1.grad in case args1 and args2 reused the same tensor
            grad_var1.grad.data.zero_()
            loss.backward()
            grad_points2 = grad_var2.grad.data.clone().cpu()

        self.assertEqual((idx1.cpu() == idx2.cpu()).all().item(), 1)
        self.assertEqual((zbuf1.cpu() == zbuf2.cpu()).all().item(), 1)
        self.assertClose(dist1.cpu(), dist2.cpu())
        if compare_grads:
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
229
            self.assertClose(grad_points1, grad_points2, atol=2e-6)
230

231
232
233
234
235
236
237
    def test_bin_size_error(self):
        points = Pointclouds(points=torch.rand(5, 100, 3))
        image_size = 1024
        bin_size = 16
        with self.assertRaisesRegex(ValueError, "bin_size too small"):
            rasterize_points(points, image_size, 0.0, 2, bin_size=bin_size)

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
    def _test_behind_camera(self, rasterize_points_fn, device, bin_size=None):
        # Test case where all points are behind the camera -- nothing should
        # get rasterized
        N = 2
        P = 32
        xy = torch.randn(N, P, 2)
        z = torch.randn(N, P, 1).abs().mul(-1)  # Make them all negative
        points = torch.cat([xy, z], dim=2).to(device)
        image_size = 16
        points_per_pixel = 3
        radius = 0.2
        idx_expected = torch.full(
            (N, 16, 16, 3), fill_value=-1, dtype=torch.int32, device=device
        )
        zbuf_expected = torch.full(
            (N, 16, 16, 3), fill_value=-1, dtype=torch.float32, device=device
        )
        dists_expected = zbuf_expected.clone()
        pointclouds = Pointclouds(points=points)
        if bin_size == -1:
            # simple python case with no binning
            idx, zbuf, dists = rasterize_points_fn(
                pointclouds, image_size, radius, points_per_pixel
            )
        else:
            idx, zbuf, dists = rasterize_points_fn(
                pointclouds, image_size, radius, points_per_pixel, bin_size
            )
        idx_same = (idx == idx_expected).all().item() == 1
        zbuf_same = (zbuf == zbuf_expected).all().item() == 1

        self.assertTrue(idx_same)
        self.assertTrue(zbuf_same)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
271
        self.assertClose(dists, dists_expected)
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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

    def _simple_test_case(self, rasterize_points_fn, device, bin_size=0):
        # Create two pointclouds with different numbers of points.
        # fmt: off
        points1 = torch.tensor(
            [
                [0.0, 0.0,  0.0],  # noqa: E241
                [0.4, 0.0,  0.1],  # noqa: E241
                [0.0, 0.4,  0.2],  # noqa: E241
                [0.0, 0.0, -0.1],  # noqa: E241 Points with negative z should be skippped
            ],
            device=device,
        )
        points2 = torch.tensor(
            [
                [0.0, 0.0,  0.0],  # noqa: E241
                [0.4, 0.0,  0.1],  # noqa: E241
                [0.0, 0.4,  0.2],  # noqa: E241
                [0.0, 0.0, -0.1],  # noqa: E241 Points with negative z should be skippped
                [0.0, 0.0, -0.7],  # noqa: E241 Points with negative z should be skippped
            ],
            device=device,
        )
        # fmt: on
        pointclouds = Pointclouds(points=[points1, points2])

        image_size = 5
        points_per_pixel = 2
        radius = 0.5

        # The expected output values. Note that in the outputs, the world space
        # +Y is up, and the world space +X is left.
        idx1_expected = torch.full(
            (1, 5, 5, 2), fill_value=-1, dtype=torch.int32, device=device
        )
        # fmt: off
        idx1_expected[0, :, :, 0] = torch.tensor([
            [-1, -1,  2, -1, -1],  # noqa: E241
            [-1,  1,  0,  2, -1],  # noqa: E241
            [ 1,  0,  0,  0, -1],  # noqa: E241 E201
            [-1,  1,  0, -1, -1],  # noqa: E241
            [-1, -1, -1, -1, -1],  # noqa: E241
        ], device=device)
        idx1_expected[0, :, :, 1] = torch.tensor([
            [-1, -1, -1, -1, -1],  # noqa: E241
            [-1,  2,  2, -1, -1],  # noqa: E241
            [-1,  1,  1, -1, -1],  # noqa: E241
            [-1, -1, -1, -1, -1],  # noqa: E241
            [-1, -1, -1, -1, -1],  # noqa: E241
        ], device=device)
        # fmt: on

        zbuf1_expected = torch.full(
            (1, 5, 5, 2), fill_value=100, dtype=torch.float32, device=device
        )
        # fmt: off
        zbuf1_expected[0, :, :, 0] = torch.tensor([
            [-1.0, -1.0,  0.2, -1.0, -1.0],  # noqa: E241
            [-1.0,  0.1,  0.0,  0.2, -1.0],  # noqa: E241
            [ 0.1,  0.0,  0.0,  0.0, -1.0],  # noqa: E241 E201
            [-1.0,  0.1,  0.0, -1.0, -1.0],  # noqa: E241
            [-1.0, -1.0, -1.0, -1.0, -1.0]   # noqa: E241
        ], device=device)
        zbuf1_expected[0, :, :, 1] = torch.tensor([
            [-1.0, -1.0, -1.0, -1.0, -1.0],  # noqa: E241
            [-1.0,  0.2,  0.2, -1.0, -1.0],  # noqa: E241
            [-1.0,  0.1,  0.1, -1.0, -1.0],  # noqa: E241
            [-1.0, -1.0, -1.0, -1.0, -1.0],  # noqa: E241
            [-1.0, -1.0, -1.0, -1.0, -1.0],  # noqa: E241
        ], device=device)
        # fmt: on

344
        dists1_expected = torch.zeros((5, 5, 2), dtype=torch.float32, device=device)
345
        # fmt: off
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
346
        dists1_expected[:, :, 0] = torch.tensor([
347
348
349
350
351
352
            [-1.00, -1.00,  0.16, -1.00, -1.00],  # noqa: E241
            [-1.00,  0.16,  0.16,  0.16, -1.00],  # noqa: E241
            [ 0.16,  0.16,  0.00,  0.16, -1.00],  # noqa: E241 E201
            [-1.00,  0.16,  0.16, -1.00, -1.00],  # noqa: E241
            [-1.00, -1.00, -1.00, -1.00, -1.00],  # noqa: E241
        ], device=device)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
353
        dists1_expected[:, :, 1] = torch.tensor([
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
            [-1.00, -1.00, -1.00, -1.00, -1.00],  # noqa: E241
            [-1.00,  0.16,  0.00, -1.00, -1.00],  # noqa: E241
            [-1.00,  0.00,  0.16, -1.00, -1.00],  # noqa: E241
            [-1.00, -1.00, -1.00, -1.00, -1.00],  # noqa: E241
            [-1.00, -1.00, -1.00, -1.00, -1.00],  # noqa: E241
        ], device=device)
        # fmt: on

        if bin_size == -1:
            # simple python case with no binning
            idx, zbuf, dists = rasterize_points_fn(
                pointclouds, image_size, radius, points_per_pixel
            )
        else:
            idx, zbuf, dists = rasterize_points_fn(
                pointclouds, image_size, radius, points_per_pixel, bin_size
            )

        # check first point cloud
        idx_same = (idx[0, ...] == idx1_expected).all().item() == 1
        if idx_same == 0:
            print(idx[0, :, :, 0])
            print(idx[0, :, :, 1])
        zbuf_same = (zbuf[0, ...] == zbuf1_expected).all().item() == 1
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
378
        self.assertClose(dists[0, ...], dists1_expected)
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
        self.assertTrue(idx_same)
        self.assertTrue(zbuf_same)

        # Check second point cloud - the indices in idx refer to points in the
        # pointclouds.points_packed() tensor. In the second point cloud,
        # two points are behind the screen - the expected indices are the same
        # the first pointcloud but offset by the number of points in the
        # first pointcloud.
        num_points_per_cloud = pointclouds.num_points_per_cloud()
        idx1_expected[idx1_expected >= 0] += num_points_per_cloud[0]

        idx_same = (idx[1, ...] == idx1_expected).all().item() == 1
        zbuf_same = (zbuf[1, ...] == zbuf1_expected).all().item() == 1
        self.assertTrue(idx_same)
        self.assertTrue(zbuf_same)
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
394
        self.assertClose(dists[1, ...], dists1_expected)
395
396
397
398
399

    def test_coarse_cpu(self):
        return self._test_coarse_rasterize(torch.device("cpu"))

    def test_coarse_cuda(self):
Nikhila Ravi's avatar
Nikhila Ravi committed
400
401
        device = get_random_cuda_device()
        return self._test_coarse_rasterize(device)
402
403
404
405
406

    def test_compare_coarse_cpu_vs_cuda(self):
        torch.manual_seed(231)
        N = 3
        max_P = 1000
407
        image_size = (64, 64)
408
409
410
411
412
413
414
415
416
417
418
419
420
421
        radius = 0.1
        bin_size = 16
        max_points_per_bin = 500

        # create heterogeneous point clouds
        points = []
        for _ in range(N):
            p = np.random.choice(max_P)
            points.append(torch.randn(p, 3))

        pointclouds = Pointclouds(points=points)
        points_packed = pointclouds.points_packed()
        cloud_to_packed_first_idx = pointclouds.cloud_to_packed_first_idx()
        num_points_per_cloud = pointclouds.num_points_per_cloud()
422
423

        radius = torch.full((points_packed.shape[0],), fill_value=radius)
424
425
426
427
428
429
430
431
432
433
434
        args = (
            points_packed,
            cloud_to_packed_first_idx,
            num_points_per_cloud,
            image_size,
            radius,
            bin_size,
            max_points_per_bin,
        )
        bp_cpu = _C._rasterize_points_coarse(*args)

Nikhila Ravi's avatar
Nikhila Ravi committed
435
436
        device = get_random_cuda_device()
        pointclouds_cuda = pointclouds.to(device)
437
438
439
        points_packed = pointclouds_cuda.points_packed()
        cloud_to_packed_first_idx = pointclouds_cuda.cloud_to_packed_first_idx()
        num_points_per_cloud = pointclouds_cuda.num_points_per_cloud()
440
        radius = radius.to(device)
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
        args = (
            points_packed,
            cloud_to_packed_first_idx,
            num_points_per_cloud,
            image_size,
            radius,
            bin_size,
            max_points_per_bin,
        )
        bp_cuda = _C._rasterize_points_coarse(*args)

        # Bin points might not be the same: CUDA version might write them in
        # any order. But if we sort the non-(-1) elements of the CUDA output
        # then they should be the same.
        for n in range(N):
            for by in range(bp_cpu.shape[1]):
                for bx in range(bp_cpu.shape[2]):
                    K = (bp_cpu[n, by, bx] != -1).sum().item()
                    idxs_cpu = bp_cpu[n, by, bx].tolist()
                    idxs_cuda = bp_cuda[n, by, bx].tolist()
                    idxs_cuda[:K] = sorted(idxs_cuda[:K])
                    self.assertEqual(idxs_cpu, idxs_cuda)

    def _test_coarse_rasterize(self, device):
        #
        #
Nikhila Ravi's avatar
Nikhila Ravi committed
467
468
469
470
471
472
473
474
475
476
477
478
479
480
        #           |2                  (4)
        #           |
        #           |
        #           |
        #           |1
        #           |
        #           |    (1)
        #        (2)|
        # _________(5)___(0)_______________
        # -1        |           1         2
        #           |
        #           |            (3)
        #           |
        #           |-1
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
        #
        # Locations of the points are shown by o. The screen bounding box
        # is between [-1, 1] in both the x and y directions.
        #
        # These points are interesting because:
        # (0) Falls into two bins;
        # (1) and (2) fall into one bin;
        # (3) is out-of-bounds, but its disk is in-bounds;
        # (4) is out-of-bounds, and its entire disk is also out-of-bounds
        # (5) has a negative z-value, so it should be skipped
        # fmt: off
        points = torch.tensor(
            [
                [ 0.5,  0.0,  0.0],  # noqa: E241, E201
                [ 0.5,  0.5,  0.1],  # noqa: E241, E201
                [-0.3,  0.4,  0.0],  # noqa: E241
                [ 1.1, -0.5,  0.2],  # noqa: E241, E201
                [ 2.0,  2.0,  0.3],  # noqa: E241, E201
                [ 0.0,  0.0, -0.1],  # noqa: E241, E201
            ],
            device=device
        )
        # fmt: on
504
        image_size = (16, 16)
505
506
507
508
509
510
511
512
513
        radius = 0.2
        bin_size = 8
        max_points_per_bin = 5

        bin_points_expected = -1 * torch.ones(
            1, 2, 2, 5, dtype=torch.int32, device=device
        )
        # Note that the order is only deterministic here for CUDA if all points
        # fit in one chunk. This will the the case for this small example, but
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
514
        # to properly exercise coordinated writes among multiple chunks we need
515
        # to use a bigger test case.
Nikhila Ravi's avatar
Nikhila Ravi committed
516
517
518
        bin_points_expected[0, 0, 1, :2] = torch.tensor([0, 3])
        bin_points_expected[0, 1, 0, 0] = torch.tensor([2])
        bin_points_expected[0, 1, 1, :2] = torch.tensor([0, 1])
519
520

        pointclouds = Pointclouds(points=[points])
521
        radius = torch.full((points.shape[0],), fill_value=radius, device=device)
522
523
524
525
526
527
528
529
530
531
532
        args = (
            pointclouds.points_packed(),
            pointclouds.cloud_to_packed_first_idx(),
            pointclouds.num_points_per_cloud(),
            image_size,
            radius,
            bin_size,
            max_points_per_bin,
        )
        bin_points = _C._rasterize_points_coarse(*args)
        bin_points_same = (bin_points == bin_points_expected).all()
Nikhila Ravi's avatar
Nikhila Ravi committed
533

534
        self.assertTrue(bin_points_same.item() == 1)
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562

    def _test_variable_size_radius(self, rasterize_points_fn, device, bin_size=0):
        # Two points
        points = torch.tensor(
            [[0.5, 0.5, 0.3], [0.5, -0.5, -0.1], [0.0, 0.0, 0.3]],
            dtype=torch.float32,
            device=device,
        )
        image_size = 16
        points_per_pixel = 1
        radius = torch.tensor([0.1, 0.0, 0.2], dtype=torch.float32, device=device)
        pointclouds = Pointclouds(points=[points])
        if bin_size == -1:
            # simple python case with no binning
            idx, zbuf, dists = rasterize_points_fn(
                pointclouds, image_size, radius, points_per_pixel
            )
        else:
            idx, zbuf, dists = rasterize_points_fn(
                pointclouds, image_size, radius, points_per_pixel, bin_size
            )

        idx_expected = torch.zeros(
            (1, image_size, image_size, 1), dtype=torch.int64, device=device
        )
        # fmt: off
        idx_expected[0, ..., 0] = torch.tensor(
            [
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
                [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1,  0,  0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1,  0,  0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1, -1,  2,  2, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1,  2,  2,  2,  2, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1,  2,  2,  2,  2, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1, -1,  2,  2, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],  # noqa: E241
                [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]   # noqa: E241
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
            ],
            dtype=torch.int64,
            device=device
        )
        # fmt: on
        zbuf_expected = torch.full(
            idx_expected.shape, fill_value=-1, dtype=torch.float32, device=device
        )
        zbuf_expected[idx_expected == 0] = 0.3
        zbuf_expected[idx_expected == 2] = 0.3

        dists_expected = torch.full(
            idx_expected.shape, fill_value=-1, dtype=torch.float32, device=device
        )

        # fmt: off
        dists_expected[0, ..., 0] = torch.Tensor(
            [
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
                [-1., -1., -1.,    -1.,     -1., -1.,    -1.,    -1.,    -1.,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1.,    -1.,    -1.,    -1.,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1.,    -1.,    -1.,     -1.,   -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1., 0.0078, 0.0078,  -1.,    -1.,    -1.,    -1.,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1., 0.0078, 0.0078,  -1.,    -1.,    -1.,    -1.,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1.,    -1.,    -1.,    -1.,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1.,    -1., 0.0391, 0.0391,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1., 0.0391, 0.0078, 0.0078, 0.0391, -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1., 0.0391, 0.0078, 0.0078, 0.0391, -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1.,    -1., 0.0391, 0.0391,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1.,    -1.,    -1.,    -1.,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1.,    -1.,    -1.,    -1.,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1.,    -1.,    -1.,    -1.,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1.,    -1.,    -1.,    -1.,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1.,    -1.,    -1.,    -1.,    -1., -1., -1., -1., -1., -1., -1.],  # noqa: E241, B950
                [-1., -1., -1.,    -1.,     -1., -1.,    -1.,    -1.,    -1.,    -1., -1., -1., -1., -1., -1., -1.]   # noqa: E241, B950
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
            ]
        )
        # fmt: on

        # Check the distances for a point are less than the squared radius
        # for that point.
        self.assertTrue((dists[idx == 0] < radius[0] ** 2).all())
        self.assertTrue((dists[idx == 2] < radius[2] ** 2).all())

        # Check all values are correct.
        idx_same = (idx == idx_expected).all().item() == 1
        zbuf_same = (zbuf == zbuf_expected).all().item() == 1

        self.assertTrue(idx_same)
        self.assertTrue(zbuf_same)
        self.assertClose(dists, dists_expected, atol=4e-5)

    def test_radius_format_failure(self):
        N = 20
        P_max = 15
        points_list = []
        for _ in range(N):
            p = torch.randint(low=1, high=P_max, size=(1,))[0]
            points_list.append(torch.randn((p, 3)))

        points = Pointclouds(points=points_list)

        # Incorrect shape
        with self.assertRaisesRegex(ValueError, "radius must be of shape"):
            _format_radius([0, 1, 2], points)

        # Incorrect type
        with self.assertRaisesRegex(ValueError, "float, list, tuple or tensor"):
            _format_radius({0: [0, 1, 2]}, points)