Skip to content

Commit 1342964

Browse files
nikhilaravifacebook-github-bot
authored andcommitted
Bug fix for case where aspect ratio is a float
Summary: - Fix the calculation of the non square NDC range when the H and W are not integer multiples. - Add test for this case Reviewed By: gkioxari Differential Revision: D26613213 fbshipit-source-id: df6763cac602e9f1d516b41b432c4d2cfbaa356d
1 parent 0345f86 commit 1342964

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

pytorch3d/csrc/rasterize_points/rasterization_utils.cuh

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
__device__ inline float NonSquareNdcRange(int S1, int S2) {
1111
float range = 2.0f;
1212
if (S1 > S2) {
13-
range = ((S1 / S2) * range);
13+
// First multiply S1 by float range so that division results
14+
// in a float value.
15+
range = (S1 * range) / S2;
1416
}
1517
return range;
1618
}

pytorch3d/csrc/rasterize_points/rasterization_utils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
inline float NonSquareNdcRange(int S1, int S2) {
1111
float range = 2.0f;
1212
if (S1 > S2) {
13-
range = ((S1 / S2) * range);
13+
range = (S1 * range) / S2;
1414
}
1515
return range;
1616
}

pytorch3d/renderer/points/rasterize_points.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import List, Optional, Tuple, Union
44

5+
import numpy as np
56
import torch
67

78
# pyre-fixme[21]: Could not find name `_C` in `pytorch3d`.
@@ -120,15 +121,7 @@ def rasterize_points(
120121
# Binned CPU rasterization not fully implemented
121122
bin_size = 0
122123
else:
123-
# TODO: These heuristics are not well-thought out!
124-
if max_image_size <= 64:
125-
bin_size = 8
126-
elif max_image_size <= 256:
127-
bin_size = 16
128-
elif max_image_size <= 512:
129-
bin_size = 32
130-
elif max_image_size <= 1024:
131-
bin_size = 64
124+
bin_size = int(2 ** max(np.ceil(np.log2(max_image_size)) - 4, 4))
132125

133126
if bin_size != 0:
134127
# There is a limit on the number of points per bin in the cuda kernel.

tests/test_rasterize_rectangle_images.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def _compare_square_with_nonsq(
314314

315315
# Finally check the gradients of the input vertices for
316316
# the square and non square case
317-
self.assertClose(verts_square.grad, grad_tensor.grad, rtol=2e-4)
317+
self.assertClose(verts_square.grad, grad_tensor.grad, rtol=3e-4)
318318

319319
def test_gpu(self):
320320
"""
@@ -323,8 +323,9 @@ def test_gpu(self):
323323
dists, zbuf, bary are all the same for the square
324324
region which is present in both images.
325325
"""
326-
# Test both cases: (W > H), (H > W)
327-
image_sizes = [(64, 128), (128, 64), (128, 256), (256, 128)]
326+
# Test both cases: (W > H), (H > W) as well as the case where
327+
# H and W are not integer multiples of each other (i.e. float aspect ratio)
328+
image_sizes = [(64, 128), (128, 64), (128, 256), (256, 128), (600, 1110)]
328329

329330
devices = ["cuda:0"]
330331
blurs = [0.0, 0.001]
@@ -391,7 +392,7 @@ def test_cpu(self):
391392
"""
392393
# Test both when (W > H) and (H > W).
393394
# Using smaller image sizes here as the Python rasterizer is really slow.
394-
image_sizes = [(32, 64), (64, 32)]
395+
image_sizes = [(32, 64), (64, 32), (60, 110)]
395396
devices = ["cpu"]
396397
blurs = [0.0, 0.001]
397398
batch_sizes = [1]
@@ -646,8 +647,9 @@ def test_gpu(self):
646647
dists, zbuf, idx are all the same for the square
647648
region which is present in both images.
648649
"""
649-
# Test both cases: (W > H), (H > W)
650-
image_sizes = [(64, 128), (128, 64), (128, 256), (256, 128)]
650+
# Test both cases: (W > H), (H > W) as well as the case where
651+
# H and W are not integer multiples of each other (i.e. float aspect ratio)
652+
image_sizes = [(64, 128), (128, 64), (128, 256), (256, 128), (600, 1110)]
651653

652654
devices = ["cuda:0"]
653655
blurs = [5e-2]
@@ -713,7 +715,7 @@ def test_cpu(self):
713715
"""
714716
# Test both when (W > H) and (H > W).
715717
# Using smaller image sizes here as the Python rasterizer is really slow.
716-
image_sizes = [(32, 64), (64, 32)]
718+
image_sizes = [(32, 64), (64, 32), (60, 110)]
717719
devices = ["cpu"]
718720
blurs = [5e-2]
719721
batch_sizes = [1]

0 commit comments

Comments
 (0)