Commit 9a737da8 authored by Jeremy Reizenstein's avatar Jeremy Reizenstein Committed by Facebook GitHub Bot
Browse files

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
parent 860b742a
...@@ -16,8 +16,22 @@ from pytorch3d import _C ...@@ -16,8 +16,22 @@ from pytorch3d import _C
# NOTE: All blending function should return an RGBA image per batch element # NOTE: All blending function should return an RGBA image per batch element
# Data class to store blending params with defaults
class BlendParams(NamedTuple): class BlendParams(NamedTuple):
"""
Data class to store blending params with defaults
Members:
sigma (float): Controls the width of the sigmoid function used to
calculate the 2D distance based probability. Determines the
sharpness of the edges of the shape.
Higher => faces have less defined edges.
gamma (float): Controls the scaling of the exponential function used
to set the opacity of the color.
Higher => faces are more transparent.
background_color: RGB values for the background color as a tuple or
as a tensor of three floats.
"""
sigma: float = 1e-4 sigma: float = 1e-4
gamma: float = 1e-4 gamma: float = 1e-4
background_color: Union[torch.Tensor, Sequence[float]] = (1.0, 1.0, 1.0) background_color: Union[torch.Tensor, Sequence[float]] = (1.0, 1.0, 1.0)
......
...@@ -73,12 +73,15 @@ def rasterize_meshes( ...@@ -73,12 +73,15 @@ def rasterize_meshes(
affect the output, but can affect the speed of the forward pass. affect the output, but can affect the speed of the forward pass.
faces_per_bin: Only applicable when using coarse-to-fine rasterization faces_per_bin: Only applicable when using coarse-to-fine rasterization
(bin_size > 0); this is the maximum number of faces allowed within each (bin_size > 0); this is the maximum number of faces allowed within each
bin. If more than this many faces actually fall into a bin, an error bin. This should not affect the output values, but can affect
will be raised. This should not affect the output values, but can affect
the memory usage in the forward pass. the memory usage in the forward pass.
perspective_correct: Bool, Whether to apply perspective correction when computing perspective_correct: Bool, Whether to apply perspective correction when computing
barycentric coordinates for pixels. This should be set to True if a perspective barycentric coordinates for pixels. This should be set to True if a perspective
camera is used. camera is used.
clip_barycentric_coords: Whether, after any perspective correction is applied
but before the depth is calculated (e.g. for z clipping),
to "correct" a location outside the face (i.e. with a negative
barycentric coordinate) to a position on the edge of the face.
cull_backfaces: Bool, Whether to only rasterize mesh faces which are cull_backfaces: Bool, Whether to only rasterize mesh faces which are
visible to the camera. This assumes that vertices of visible to the camera. This assumes that vertices of
front-facing triangles are ordered in an anti-clockwise front-facing triangles are ordered in an anti-clockwise
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
# This source code is licensed under the BSD-style license found in the # This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. # LICENSE file in the root directory of this source tree.
from dataclasses import dataclass
from typing import NamedTuple, Optional, Tuple, Union from typing import NamedTuple, Optional, Tuple, Union
import torch import torch
...@@ -20,46 +21,66 @@ class Fragments(NamedTuple): ...@@ -20,46 +21,66 @@ class Fragments(NamedTuple):
dists: torch.Tensor dists: torch.Tensor
# Class to store the mesh rasterization params with defaults @dataclass
class RasterizationSettings: class RasterizationSettings:
__slots__ = [ """
"image_size", Class to store the mesh rasterization params with defaults
"blur_radius",
"faces_per_pixel", Members:
"bin_size", image_size: Either common height and width or (height, width), in pixels.
"max_faces_per_bin", blur_radius: Float distance in the range [0, 2] used to expand the face
"perspective_correct", bounding boxes for rasterization. Setting blur radius
"clip_barycentric_coords", results in blurred edges around the shape instead of a
"cull_backfaces", hard boundary. Set to 0 for no blur.
"z_clip_value", faces_per_pixel: (int) Number of faces to keep track of per pixel.
"cull_to_frustum", We return the nearest faces_per_pixel faces along the z-axis.
] bin_size: Size of bins to use for coarse-to-fine rasterization. Setting
bin_size=0 uses naive rasterization; setting bin_size=None attempts
def __init__( to set it heuristically based on the shape of the input. This should
self, not affect the output, but can affect the speed of the forward pass.
image_size: Union[int, Tuple[int, int]] = 256, max_faces_per_bin: Only applicable when using coarse-to-fine
blur_radius: float = 0.0, rasterization (bin_size != 0); this is the maximum number of faces
faces_per_pixel: int = 1, allowed within each bin. This should not affect the output values,
bin_size: Optional[int] = None, but can affect the memory usage in the forward pass.
max_faces_per_bin: Optional[int] = None, Setting max_faces_per_bin=None attempts to set with a heuristic.
# set perspective_correct = None so that the perspective_correct: Whether to apply perspective correction when
# value can be inferred correctly from the Camera type computing barycentric coordinates for pixels.
perspective_correct: Optional[bool] = None, None (default) means make correction if the camera uses perspective.
clip_barycentric_coords: Optional[bool] = None, clip_barycentric_coords: Whether, after any perspective correction
cull_backfaces: bool = False, is applied but before the depth is calculated (e.g. for
z_clip_value: Optional[float] = None, z clipping), to "correct" a location outside the face (i.e. with
cull_to_frustum: bool = False, a negative barycentric coordinate) to a position on the edge of the
) -> None: face. None (default) means clip if blur_radius > 0, which is a condition
self.image_size = image_size under which such outside-face-points are likely.
self.blur_radius = blur_radius cull_backfaces: Whether to only rasterize mesh faces which are
self.faces_per_pixel = faces_per_pixel visible to the camera. This assumes that vertices of
self.bin_size = bin_size front-facing triangles are ordered in an anti-clockwise
self.max_faces_per_bin = max_faces_per_bin fashion, and triangles that face away from the camera are
self.perspective_correct = perspective_correct in a clockwise order relative to the current view
self.clip_barycentric_coords = clip_barycentric_coords direction. NOTE: This will only work if the mesh faces are
self.cull_backfaces = cull_backfaces consistently defined with counter-clockwise ordering when
self.z_clip_value = z_clip_value viewed from the outside.
self.cull_to_frustum = cull_to_frustum z_clip_value: if not None, then triangles will be clipped (and possibly
subdivided into smaller triangles) such that z >= z_clip_value.
This avoids camera projections that go to infinity as z->0.
Default is None as clipping affects rasterization speed and
should only be turned on if explicitly needed.
See clip.py for all the extra computation that is required.
cull_to_frustum: Whether to cull triangles outside the view frustum.
Culling involves removing all faces which fall outside view frustum.
Default is False for performance as often not needed.
"""
image_size: Union[int, Tuple[int, int]] = 256
blur_radius: float = 0.0
faces_per_pixel: int = 1
bin_size: Optional[int] = None
max_faces_per_bin: Optional[int] = None
perspective_correct: Optional[bool] = None
clip_barycentric_coords: Optional[bool] = None
cull_backfaces: bool = False
z_clip_value: Optional[float] = None
cull_to_frustum: bool = False
class MeshRasterizer(nn.Module): class MeshRasterizer(nn.Module):
......
...@@ -64,8 +64,7 @@ def rasterize_points( ...@@ -64,8 +64,7 @@ def rasterize_points(
affect the output, but can affect the speed of the forward pass. affect the output, but can affect the speed of the forward pass.
points_per_bin: Only applicable when using coarse-to-fine rasterization points_per_bin: Only applicable when using coarse-to-fine rasterization
(bin_size > 0); this is the maximum number of points allowed within each (bin_size > 0); this is the maximum number of points allowed within each
bin. If more than this many points actually fall into a bin, an error bin. This should not affect the output values, but can affect
will be raised. This should not affect the output values, but can affect
the memory usage in the forward pass. the memory usage in the forward pass.
Returns: Returns:
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
# This source code is licensed under the BSD-style license found in the # This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. # LICENSE file in the root directory of this source tree.
from dataclasses import dataclass
from typing import NamedTuple, Optional, Tuple, Union from typing import NamedTuple, Optional, Tuple, Union
import torch import torch
...@@ -21,29 +21,35 @@ class PointFragments(NamedTuple): ...@@ -21,29 +21,35 @@ class PointFragments(NamedTuple):
dists: torch.Tensor dists: torch.Tensor
# Class to store the point rasterization params with defaults @dataclass
class PointsRasterizationSettings: class PointsRasterizationSettings:
__slots__ = [ """
"image_size", Class to store the point rasterization params with defaults
"radius",
"points_per_pixel", Members:
"bin_size", image_size: Either common height and width or (height, width), in pixels.
"max_points_per_bin", radius: The radius (in NDC units) of each disk to be rasterized.
] This can either be a float in which case the same radius is used
for each point, or a torch.Tensor of shape (N, P) giving a radius
def __init__( per point in the batch.
self, points_per_pixel: (int) Number of points to keep track of per pixel.
image_size: Union[int, Tuple[int, int]] = 256, We return the nearest points_per_pixel points along the z-axis.
radius: Union[float, torch.Tensor] = 0.01, bin_size: Size of bins to use for coarse-to-fine rasterization. Setting
points_per_pixel: int = 8, bin_size=0 uses naive rasterization; setting bin_size=None attempts
bin_size: Optional[int] = None, to set it heuristically based on the shape of the input. This should
max_points_per_bin: Optional[int] = None, not affect the output, but can affect the speed of the forward pass.
) -> None: max_points_per_bin: Only applicable when using coarse-to-fine
self.image_size = image_size rasterization (bin_size != 0); this is the maximum number of points
self.radius = radius allowed within each bin. This should not affect the output values,
self.points_per_pixel = points_per_pixel but can affect the memory usage in the forward pass.
self.bin_size = bin_size Setting max_points_per_bin=None attempts to set with a heuristic.
self.max_points_per_bin = max_points_per_bin """
image_size: Union[int, Tuple[int, int]] = 256
radius: Union[float, torch.Tensor] = 0.01
points_per_pixel: int = 8
bin_size: Optional[int] = None
max_points_per_bin: Optional[int] = None
class PointsRasterizer(nn.Module): class PointsRasterizer(nn.Module):
......
...@@ -5,10 +5,11 @@ ...@@ -5,10 +5,11 @@
# LICENSE file in the root directory of this source tree. # LICENSE file in the root directory of this source tree.
import math import math
from typing import Tuple, Union from typing import Tuple
import torch import torch
DEFAULT_ACOS_BOUND = 1.0 - 1e-4 DEFAULT_ACOS_BOUND = 1.0 - 1e-4
...@@ -27,9 +28,11 @@ def acos_linear_extrapolation( ...@@ -27,9 +28,11 @@ def acos_linear_extrapolation(
if lower_bound <= x <= upper_bound: if lower_bound <= x <= upper_bound:
acos_linear_extrapolation(x) = acos(x) acos_linear_extrapolation(x) = acos(x)
elif x <= lower_bound: # 1st order Taylor approximation elif x <= lower_bound: # 1st order Taylor approximation
acos_linear_extrapolation(x) = acos(lower_bound) + dacos/dx(lower_bound) * (x - lower_bound) acos_linear_extrapolation(x)
= acos(lower_bound) + dacos/dx(lower_bound) * (x - lower_bound)
else: # x >= upper_bound else: # x >= upper_bound
acos_linear_extrapolation(x) = acos(upper_bound) + dacos/dx(upper_bound) * (x - upper_bound) acos_linear_extrapolation(x)
= acos(upper_bound) + dacos/dx(upper_bound) * (x - upper_bound)
``` ```
Args: Args:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment