Stricter typing, update docstrings (#226)

This commit is contained in:
Matthew Tancik authored and GitHub committed 2022-07-28 11:36:43 -07:00
1 parent 395b03f48c
commit 44a7fa288c
10 files changed
+181 -181

No files matched your search

+12 -12
View File
@@ -43,7 +43,7 @@ def setup_graph(config: GraphConfig, dataset_inputs: DatasetInputs, device: str)
"""Setup the graph. The dataset inputs should be set with the training data.
Args:
dataset_inputs (DatasetInputs): The inputs which will be used to define the camera parameters.
dataset_inputs: The inputs which will be used to define the camera parameters.
"""
graph = instantiate_from_dict_config(DictConfig(config), **dataset_inputs.as_dict())
graph.to(device)
@@ -77,14 +77,14 @@ class Graph(AbstractGraph):
subclassed for custom NeRF model.
Args:
intrinsics (torch.Tensor): Camera intrinsics.
camera_to_world (torch.Tensor): Camera to world transformation.
loss_coefficients (DictConfig): Loss specific weights.
scene_bounds (SceneBounds): Bounds of target scene.
enable_collider (bool): Whether to create a scene collider to filter rays.
collider_config (DictConfig): Configuration of scene collider.
enable_density_field (bool): Whether to create a density field to filter samples.
density_field_config (DictConfig): Configuration of density field.
intrinsics: Camera intrinsics.
camera_to_world: Camera to world transformation.
loss_coefficients: Loss specific weights.
scene_bounds: Bounds of target scene.
enable_collider: Whether to create a scene collider to filter rays.
collider_config: Configuration of scene collider.
enable_density_field: Whether to create a density field to filter samples.
density_field_config: Configuration of density field.
"""
def __init__(
@@ -145,7 +145,7 @@ class Graph(AbstractGraph):
"""Obtain the parameter groups for the optimizers
Returns:
Dict[str, List[Parameter]]: Mapping of different parameter groups
Mapping of different parameter groups
"""
@abstractmethod
@@ -153,10 +153,10 @@ class Graph(AbstractGraph):
"""Takes in a Ray Bundle and returns a dictionary of outputs.
Args:
ray_bundle (RayBundle): Input bundle of rays.
ray_bundle: Input bundle of rays.
Returns:
dict: Outputs of graph. (ie. rendered colors)
Outputs of graph. (ie. rendered colors)
"""
def process_outputs_as_images(self, outputs): # pylint:disable=no-self-use
+3 -4
View File
@@ -27,9 +27,8 @@ class RayGenerator(nn.Module):
This class will store the intrinsics and extrinsics parameters of the cameras.
Args:
intrinsics (TensorType["num_cameras", "num_intrinsics_params"]):
The intrinsics parameters.
camera_to_world (TensorType["num_cameras", 3, 4]): Camera to world transformation matrix.
intrinsics: The intrinsics parameters.
camera_to_world: Camera to world transformation matrix.
"""
def __init__(
@@ -54,7 +53,7 @@ class RayGenerator(nn.Module):
"""Index into the cameras to generate the rays.
Args:
ray_indices (TensorType["num_rays", 3]): Contains camera, row, and col indicies for target rays.
ray_indices: Contains camera, row, and col indicies for target rays.
"""
c = ray_indices[:, 0] # camera indices
y = ray_indices[:, 1] # row indices
+6 -4
View File
@@ -20,7 +20,9 @@ from torchtyping import TensorType
from nerfactory.cameras.rays import RaySamples
def distortion_loss(ray_samples: RaySamples, densities: TensorType[..., "num_samples", 1]) -> TensorType[..., 1]:
def distortion_loss(
ray_samples: RaySamples, densities: TensorType["bs":..., "num_samples", 1]
) -> TensorType["bs":..., 1]:
"""Ray baserd distortion loss proposed in MipNeRF-360.
.. math::
@@ -32,11 +34,11 @@ def distortion_loss(ray_samples: RaySamples, densities: TensorType[..., "num_sam
is the weight at location :math:`u` between bin locations :math:`s_i` and :math:`s_{i+1}`.
Args:
ray_samples (RaySamples): Ray samples to compute loss over
densities (TensorType[..., "num_samples", 1]): Predicted sample densities
ray_samples: Ray samples to compute loss over
densities: Predicted sample densities
Returns:
TensorType[..., 1]: Distortion Loss.
Distortion Loss.
"""
# Compute the weight at each sample location
+39 -46
View File
@@ -69,13 +69,12 @@ class SpacedSampler(Sampler):
) -> None:
"""
Args:
num_samples (int): Number of samples per ray
spacing_fn (Callable): Function that dictates sample spacing (ie `lambda x : x` is uniform).
spacing_fn_inv (Callable): The inverse of spacing_fn.
train_stratified (bool): Use stratified sampling during training. Defults to True
density_field (DensityGrid, optional): Density grid. If provides,
samples below weight_threshold as set as invalid.
weight_threshold (float): Removes samples below threshold weight. Only used if density field is provided.
num_samples: Number of samples per ray
spacing_fn: Function that dictates sample spacing (ie `lambda x : x` is uniform).
spacing_fn_inv: The inverse of spacing_fn.
train_stratified: Use stratified sampling during training. Defults to True
density_field: Density grid. If provides, samples below weight_threshold as set as invalid.
weight_threshold: Removes samples below threshold weight. Only used if density field is provided.
"""
super().__init__(num_samples=num_samples, density_field=density_field, weight_threshold=weight_threshold)
self.train_stratified = train_stratified
@@ -91,11 +90,11 @@ class SpacedSampler(Sampler):
"""Generates position samples accoring to spacing function.
Args:
ray_bundle (RayBundle): Rays to generate samples for
num_samples (Optional[int]): Number of samples per ray
ray_bundle: Rays to generate samples for
num_samples: Number of samples per ray
Returns:
RaySamples: Positions and deltas for samples along a ray
Positions and deltas for samples along a ray
"""
assert ray_bundle is not None
assert ray_bundle.nears is not None
@@ -133,11 +132,10 @@ class UniformSampler(SpacedSampler):
) -> None:
"""
Args:
num_samples (int): Number of samples per ray
train_stratified (bool): Use stratified sampling during training. Defults to True
density_field (DensityGrid, optional): Density grid. If provides,
samples below weight_threshold as set as invalid.
weight_threshold (float): Removes samples below threshold weight. Only used if density field is provided.
num_samples: Number of samples per ray
train_stratified: Use stratified sampling during training. Defults to True
density_field: Density grid. If provides, samples below weight_threshold as set as invalid.
weight_threshold: Removes samples below threshold weight. Only used if density field is provided.
"""
super().__init__(
num_samples=num_samples,
@@ -161,11 +159,10 @@ class LinearDisparitySampler(SpacedSampler):
) -> None:
"""
Args:
num_samples (int): Number of samples per ray
train_stratified (bool): Use stratified sampling during training. Defults to True
density_field (DensityGrid, optional): Density grid. If provides,
samples below weight_threshold as set as invalid.
weight_threshold (float): Removes samples below threshold weight. Only used if density field is provided.
num_samples: Number of samples per ray
train_stratified: Use stratified sampling during training. Defults to True
density_field: Density grid. If provides, samples below weight_threshold as set as invalid.
weight_threshold: Removes samples below threshold weight. Only used if density field is provided.
"""
super().__init__(
num_samples=num_samples,
@@ -189,11 +186,10 @@ class SqrtSampler(SpacedSampler):
) -> None:
"""
Args:
num_samples (int): Number of samples per ray
train_stratified (bool): Use stratified sampling during training. Defults to True
density_field (DensityGrid, optional): Density grid. If provides,
samples below weight_threshold as set as invalid.
weight_threshold (float): Removes samples below threshold weight. Only used if density field is provided.
num_samples: Number of samples per ray
train_stratified: Use stratified sampling during training. Defults to True
density_field: Density grid. If provides, samples below weight_threshold as set as invalid.
weight_threshold: Removes samples below threshold weight. Only used if density field is provided.
"""
super().__init__(
num_samples=num_samples,
@@ -217,11 +213,10 @@ class LogSampler(SpacedSampler):
) -> None:
"""
Args:
num_samples (int): Number of samples per ray
train_stratified (bool): Use stratified sampling during training. Defults to True
density_field (DensityGrid, optional): Density grid. If provides,
samples below weight_threshold as set as invalid.
weight_threshold (float): Removes samples below threshold weight. Only used if density field is provided.
num_samples: Number of samples per ray
train_stratified: Use stratified sampling during training. Defults to True
density_field: Density grid. If provides, samples below weight_threshold as set as invalid.
weight_threshold: Removes samples below threshold weight. Only used if density field is provided.
"""
super().__init__(
num_samples=num_samples,
@@ -247,13 +242,12 @@ class PDFSampler(Sampler):
) -> None:
"""
Args:
num_samples (int): Number of samples per ray
train_stratified: boolean: Randomize location within each bin during training. Defaults to True
num_samples: Number of samples per ray
train_stratified: Randomize location within each bin during training. Defaults to True
include_original: Add original samples to ray. Defaults to True
density_field (DensityGrid, optional): Density grid. If provides,
samples below weight_threshold as set as invalid.
weight_threshold (float): Removes samples below threshold weight. Only used if density field is provided.
histogram_padding (float): Amount to weights prior to computing PDF. Defaults to 0.01.
density_field: Density grid. If provides, samples below weight_threshold as set as invalid.
weight_threshold: Removes samples below threshold weight. Only used if density field is provided.
histogram_padding: Amount to weights prior to computing PDF. Defaults to 0.01.
"""
super().__init__(num_samples=num_samples, density_field=density_field, weight_threshold=weight_threshold)
self.train_stratified = train_stratified
@@ -272,14 +266,14 @@ class PDFSampler(Sampler):
"""Generates position samples given a distribution.
Args:
ray_bundle (RayBundle): Rays to generate samples for
ray_samples (RaySamples): Existing ray samples
weights: (TensorType[..., "num_samples", 1]): Weights for each bin
num_samples (Optional[int]): Number of samples per ray
eps: float: Small value to prevent numerical issues. Defaults to 1e-5
ray_bundle: Rays to generate samples for
ray_samples: Existing ray samples
weights: Weights for each bin
num_samples: Number of samples per ray
eps: Small value to prevent numerical issues. Defaults to 1e-5
Returns:
RaySamples: Positions and deltas for samples along a ray
Positions and deltas for samples along a ray
"""
num_samples = num_samples or self.num_samples
num_bins = num_samples + 1
@@ -367,12 +361,11 @@ class NGPSpacedSampler(Sampler):
TODO(ruilongli): maybe move aabb test to the collision detector?
Args:
ray_bundle (RayBundle): Rays to generate samples for
aabb (TensorType[2, 3]): Bounding box of the scene.
num_samples (Optional[int]): Number of samples per ray
ray_bundle: Rays to generate samples for
aabb: Bounding box of the scene.
num_samples: Number of samples per ray
Returns:
Tuple[RaySamples, TensorType["total_samples", 3], TensorType["total_samples"], TensorType["total_samples"]]:
First return is ray samples in a packed way where only the valid samples are kept.
Second return contains all the information to recover packed samples into unpacked mode for rendering.
The last two returns are t_min and t_max from ray-aabb test.