Skip to content

Commit 9a737da

Browse files
bottlerfacebook-github-bot
authored andcommitted
More renderer parameter descriptions
Summary: Copy some descriptions of renderer parameters to more places so they are easier to find. Also a couple of small corrections, and make RasterizationSettings a dataclass. Reviewed By: nikhilaravi, patricklabatut Differential Revision: D30899822 fbshipit-source-id: 805cf366acb7d51cb308fa574deff0657c199673
1 parent 860b742 commit 9a737da

File tree

6 files changed

+116
-70
lines changed

6 files changed

+116
-70
lines changed

pytorch3d/renderer/blending.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,22 @@
1616
# NOTE: All blending function should return an RGBA image per batch element
1717

1818

19-
# Data class to store blending params with defaults
2019
class BlendParams(NamedTuple):
20+
"""
21+
Data class to store blending params with defaults
22+
23+
Members:
24+
sigma (float): Controls the width of the sigmoid function used to
25+
calculate the 2D distance based probability. Determines the
26+
sharpness of the edges of the shape.
27+
Higher => faces have less defined edges.
28+
gamma (float): Controls the scaling of the exponential function used
29+
to set the opacity of the color.
30+
Higher => faces are more transparent.
31+
background_color: RGB values for the background color as a tuple or
32+
as a tensor of three floats.
33+
"""
34+
2135
sigma: float = 1e-4
2236
gamma: float = 1e-4
2337
background_color: Union[torch.Tensor, Sequence[float]] = (1.0, 1.0, 1.0)

pytorch3d/renderer/mesh/rasterize_meshes.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,15 @@ def rasterize_meshes(
7373
affect the output, but can affect the speed of the forward pass.
7474
faces_per_bin: Only applicable when using coarse-to-fine rasterization
7575
(bin_size > 0); this is the maximum number of faces allowed within each
76-
bin. If more than this many faces actually fall into a bin, an error
77-
will be raised. This should not affect the output values, but can affect
76+
bin. This should not affect the output values, but can affect
7877
the memory usage in the forward pass.
7978
perspective_correct: Bool, Whether to apply perspective correction when computing
8079
barycentric coordinates for pixels. This should be set to True if a perspective
8180
camera is used.
81+
clip_barycentric_coords: Whether, after any perspective correction is applied
82+
but before the depth is calculated (e.g. for z clipping),
83+
to "correct" a location outside the face (i.e. with a negative
84+
barycentric coordinate) to a position on the edge of the face.
8285
cull_backfaces: Bool, Whether to only rasterize mesh faces which are
8386
visible to the camera. This assumes that vertices of
8487
front-facing triangles are ordered in an anti-clockwise

pytorch3d/renderer/mesh/rasterizer.py

+60-39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
from dataclasses import dataclass
78
from typing import NamedTuple, Optional, Tuple, Union
89

910
import torch
@@ -20,46 +21,66 @@ class Fragments(NamedTuple):
2021
dists: torch.Tensor
2122

2223

23-
# Class to store the mesh rasterization params with defaults
24+
@dataclass
2425
class RasterizationSettings:
25-
__slots__ = [
26-
"image_size",
27-
"blur_radius",
28-
"faces_per_pixel",
29-
"bin_size",
30-
"max_faces_per_bin",
31-
"perspective_correct",
32-
"clip_barycentric_coords",
33-
"cull_backfaces",
34-
"z_clip_value",
35-
"cull_to_frustum",
36-
]
37-
38-
def __init__(
39-
self,
40-
image_size: Union[int, Tuple[int, int]] = 256,
41-
blur_radius: float = 0.0,
42-
faces_per_pixel: int = 1,
43-
bin_size: Optional[int] = None,
44-
max_faces_per_bin: Optional[int] = None,
45-
# set perspective_correct = None so that the
46-
# value can be inferred correctly from the Camera type
47-
perspective_correct: Optional[bool] = None,
48-
clip_barycentric_coords: Optional[bool] = None,
49-
cull_backfaces: bool = False,
50-
z_clip_value: Optional[float] = None,
51-
cull_to_frustum: bool = False,
52-
) -> None:
53-
self.image_size = image_size
54-
self.blur_radius = blur_radius
55-
self.faces_per_pixel = faces_per_pixel
56-
self.bin_size = bin_size
57-
self.max_faces_per_bin = max_faces_per_bin
58-
self.perspective_correct = perspective_correct
59-
self.clip_barycentric_coords = clip_barycentric_coords
60-
self.cull_backfaces = cull_backfaces
61-
self.z_clip_value = z_clip_value
62-
self.cull_to_frustum = cull_to_frustum
26+
"""
27+
Class to store the mesh rasterization params with defaults
28+
29+
Members:
30+
image_size: Either common height and width or (height, width), in pixels.
31+
blur_radius: Float distance in the range [0, 2] used to expand the face
32+
bounding boxes for rasterization. Setting blur radius
33+
results in blurred edges around the shape instead of a
34+
hard boundary. Set to 0 for no blur.
35+
faces_per_pixel: (int) Number of faces to keep track of per pixel.
36+
We return the nearest faces_per_pixel faces along the z-axis.
37+
bin_size: Size of bins to use for coarse-to-fine rasterization. Setting
38+
bin_size=0 uses naive rasterization; setting bin_size=None attempts
39+
to set it heuristically based on the shape of the input. This should
40+
not affect the output, but can affect the speed of the forward pass.
41+
max_faces_per_bin: Only applicable when using coarse-to-fine
42+
rasterization (bin_size != 0); this is the maximum number of faces
43+
allowed within each bin. This should not affect the output values,
44+
but can affect the memory usage in the forward pass.
45+
Setting max_faces_per_bin=None attempts to set with a heuristic.
46+
perspective_correct: Whether to apply perspective correction when
47+
computing barycentric coordinates for pixels.
48+
None (default) means make correction if the camera uses perspective.
49+
clip_barycentric_coords: Whether, after any perspective correction
50+
is applied but before the depth is calculated (e.g. for
51+
z clipping), to "correct" a location outside the face (i.e. with
52+
a negative barycentric coordinate) to a position on the edge of the
53+
face. None (default) means clip if blur_radius > 0, which is a condition
54+
under which such outside-face-points are likely.
55+
cull_backfaces: Whether to only rasterize mesh faces which are
56+
visible to the camera. This assumes that vertices of
57+
front-facing triangles are ordered in an anti-clockwise
58+
fashion, and triangles that face away from the camera are
59+
in a clockwise order relative to the current view
60+
direction. NOTE: This will only work if the mesh faces are
61+
consistently defined with counter-clockwise ordering when
62+
viewed from the outside.
63+
z_clip_value: if not None, then triangles will be clipped (and possibly
64+
subdivided into smaller triangles) such that z >= z_clip_value.
65+
This avoids camera projections that go to infinity as z->0.
66+
Default is None as clipping affects rasterization speed and
67+
should only be turned on if explicitly needed.
68+
See clip.py for all the extra computation that is required.
69+
cull_to_frustum: Whether to cull triangles outside the view frustum.
70+
Culling involves removing all faces which fall outside view frustum.
71+
Default is False for performance as often not needed.
72+
"""
73+
74+
image_size: Union[int, Tuple[int, int]] = 256
75+
blur_radius: float = 0.0
76+
faces_per_pixel: int = 1
77+
bin_size: Optional[int] = None
78+
max_faces_per_bin: Optional[int] = None
79+
perspective_correct: Optional[bool] = None
80+
clip_barycentric_coords: Optional[bool] = None
81+
cull_backfaces: bool = False
82+
z_clip_value: Optional[float] = None
83+
cull_to_frustum: bool = False
6384

6485

6586
class MeshRasterizer(nn.Module):

pytorch3d/renderer/points/rasterize_points.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ def rasterize_points(
6464
affect the output, but can affect the speed of the forward pass.
6565
points_per_bin: Only applicable when using coarse-to-fine rasterization
6666
(bin_size > 0); this is the maximum number of points allowed within each
67-
bin. If more than this many points actually fall into a bin, an error
68-
will be raised. This should not affect the output values, but can affect
67+
bin. This should not affect the output values, but can affect
6968
the memory usage in the forward pass.
7069
7170
Returns:

pytorch3d/renderer/points/rasterizer.py

+29-23
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# This source code is licensed under the BSD-style license found in the
66
# LICENSE file in the root directory of this source tree.
77

8-
8+
from dataclasses import dataclass
99
from typing import NamedTuple, Optional, Tuple, Union
1010

1111
import torch
@@ -21,29 +21,35 @@ class PointFragments(NamedTuple):
2121
dists: torch.Tensor
2222

2323

24-
# Class to store the point rasterization params with defaults
24+
@dataclass
2525
class PointsRasterizationSettings:
26-
__slots__ = [
27-
"image_size",
28-
"radius",
29-
"points_per_pixel",
30-
"bin_size",
31-
"max_points_per_bin",
32-
]
33-
34-
def __init__(
35-
self,
36-
image_size: Union[int, Tuple[int, int]] = 256,
37-
radius: Union[float, torch.Tensor] = 0.01,
38-
points_per_pixel: int = 8,
39-
bin_size: Optional[int] = None,
40-
max_points_per_bin: Optional[int] = None,
41-
) -> None:
42-
self.image_size = image_size
43-
self.radius = radius
44-
self.points_per_pixel = points_per_pixel
45-
self.bin_size = bin_size
46-
self.max_points_per_bin = max_points_per_bin
26+
"""
27+
Class to store the point rasterization params with defaults
28+
29+
Members:
30+
image_size: Either common height and width or (height, width), in pixels.
31+
radius: The radius (in NDC units) of each disk to be rasterized.
32+
This can either be a float in which case the same radius is used
33+
for each point, or a torch.Tensor of shape (N, P) giving a radius
34+
per point in the batch.
35+
points_per_pixel: (int) Number of points to keep track of per pixel.
36+
We return the nearest points_per_pixel points along the z-axis.
37+
bin_size: Size of bins to use for coarse-to-fine rasterization. Setting
38+
bin_size=0 uses naive rasterization; setting bin_size=None attempts
39+
to set it heuristically based on the shape of the input. This should
40+
not affect the output, but can affect the speed of the forward pass.
41+
max_points_per_bin: Only applicable when using coarse-to-fine
42+
rasterization (bin_size != 0); this is the maximum number of points
43+
allowed within each bin. This should not affect the output values,
44+
but can affect the memory usage in the forward pass.
45+
Setting max_points_per_bin=None attempts to set with a heuristic.
46+
"""
47+
48+
image_size: Union[int, Tuple[int, int]] = 256
49+
radius: Union[float, torch.Tensor] = 0.01
50+
points_per_pixel: int = 8
51+
bin_size: Optional[int] = None
52+
max_points_per_bin: Optional[int] = None
4753

4854

4955
class PointsRasterizer(nn.Module):

pytorch3d/transforms/math.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
# LICENSE file in the root directory of this source tree.
66

77
import math
8-
from typing import Tuple, Union
8+
from typing import Tuple
99

1010
import torch
1111

12+
1213
DEFAULT_ACOS_BOUND = 1.0 - 1e-4
1314

1415

@@ -27,9 +28,11 @@ def acos_linear_extrapolation(
2728
if lower_bound <= x <= upper_bound:
2829
acos_linear_extrapolation(x) = acos(x)
2930
elif x <= lower_bound: # 1st order Taylor approximation
30-
acos_linear_extrapolation(x) = acos(lower_bound) + dacos/dx(lower_bound) * (x - lower_bound)
31+
acos_linear_extrapolation(x)
32+
= acos(lower_bound) + dacos/dx(lower_bound) * (x - lower_bound)
3133
else: # x >= upper_bound
32-
acos_linear_extrapolation(x) = acos(upper_bound) + dacos/dx(upper_bound) * (x - upper_bound)
34+
acos_linear_extrapolation(x)
35+
= acos(upper_bound) + dacos/dx(upper_bound) * (x - upper_bound)
3336
```
3437
3538
Args:

0 commit comments

Comments
 (0)