update docstrings and random cleanups (#387)

* fixing docstrings and small nits; up to fileds

* finished adding and fixing docstrings
This commit is contained in:
evonneng authored and GitHub committed 2022-09-10 23:09:44 -07:00
1 parent bd5967461d
commit d50c71ac43
52 files changed
+846 -646

No files matched your search

+44 -52
View File
@@ -28,35 +28,32 @@ from nerfactory.utils.tensor_dataclass import TensorDataclass
@dataclass
class Frustums(TensorDataclass):
"""Describes region of space as a frustum.
Attributes:
origins: xyz coordinate for ray origin.
directions: Direction of ray.
starts: Where the frustum starts along a ray.
ends: Where the frustum ends along a ray.
pixel_area: Projected area of pixel a distance 1 away from origin.
"""
"""Describes region of space as a frustum."""
origins: TensorType["bs":..., 3]
"""xyz coordinate for ray origin."""
directions: TensorType["bs":..., 3]
"""Direction of ray."""
starts: TensorType["bs":..., 1]
"""Where the frustum starts along a ray."""
ends: TensorType["bs":..., 1]
"""Where the frustum ends along a ray."""
pixel_area: TensorType["bs":..., 1]
"""Projected area of pixel a distance 1 away from origin."""
def get_positions(self) -> TensorType[..., 3]:
"""Calulates "center" position of frustum. Not weighted by mass.
Returns:
TensorType[..., 3]: xyz positions.
xyz positions.
"""
return self.origins + self.directions * (self.starts + self.ends) / 2
def get_gaussian_blob(self) -> Gaussians:
"""Calculates of guassian approximation of conical frustum.
"""Calculates guassian approximation of conical frustum.
Resturns:
Gaussians: Conical frustums approximated by gaussian distribution.
Conical frustums approximated by gaussian distribution.
"""
# Cone radius is set such that the square pixel_area matches the cone area.
cone_radius = torch.sqrt(self.pixel_area) / 1.7724538509055159 # r = sqrt(pixel_area / pi)
@@ -73,7 +70,7 @@ class Frustums(TensorDataclass):
"""Helper function to generate a placeholder frustum.
Returns:
Frustums: A size 1 frustum with meaningless values.
A size 1 frustum with meaningless values.
"""
return Frustums(
origins=torch.ones((1, 3)).to(device),
@@ -86,30 +83,27 @@ class Frustums(TensorDataclass):
@dataclass
class RaySamples(TensorDataclass):
"""Samples along a ray
Args:
frustums: Frustums along ray.
camera_indices: Camera index.
valid_mask: Rays that are valid.
deltas: "width" of each sample.
ts: sample-camera distance of each sample.
"""
"""Samples along a ray"""
frustums: Frustums
"""Frustums along ray."""
camera_indices: Optional[TensorType["bs":..., 1]] = None
"""Camera index."""
valid_mask: Optional[TensorType["bs":..., 1]] = None
"""Rays that are valid."""
deltas: Optional[TensorType["bs":..., 1]] = None
""""width" of each sample."""
metadata: Optional[Dict[str, TensorType["bs":..., "latent_dims"]]] = None
"""addtional information relevant to generating ray samples"""
def get_weights(self, densities: TensorType[..., "num_samples", 1]) -> TensorType[..., "num_samples", 1]:
"""Return weights based on predicted densities
Args:
densities (TensorType[..., "num_samples", 1]): Predicted densities for samples along ray
densities: Predicted densities for samples along ray
Returns:
TensorType[..., "num_samples", 1]: Weights for each sample
Weights for each sample
"""
delta_density = self.deltas * densities
@@ -134,14 +128,18 @@ class RaySamples(TensorDataclass):
return weights
def set_valid_mask(self, valid_mask: TensorType[..., "num_samples"]) -> None:
"""Sets valid mask"""
"""Sets valid mask
Args:
valid_mask: the mask to use
"""
self.valid_mask = valid_mask
def apply_masks(self) -> "RaySamples":
"""Use valid_mask to mask samples.
Returns:
RaySamples: New set of masked samples.
New set of masked samples.
"""
if self.valid_mask is not None:
return self[self.valid_mask[..., 0]]
@@ -150,36 +148,33 @@ class RaySamples(TensorDataclass):
@dataclass
class RayBundle(TensorDataclass):
"""A bundle of ray parameters.
Args:
origins: Ray origins (XYZ)
directions: Unit ray direction vector
pixel_area: Projected area of pixel a distance 1 away from origin
camera_indices: Camera indices
nears: Distance along ray to start sampling
fars: Rays Distance along ray to stop sampling
valid_mask: Rays that are valid
num_rays_per_chunk: Number of rays per chunk
metadata: Additional metadata or data needed for interpolation, will mimic shape of rays
"""
"""A bundle of ray parameters."""
# TODO(ethan): make sure the sizes with ... are correct
origins: TensorType[..., 3]
"""Ray origins (XYZ)"""
directions: TensorType[..., 3]
"""Unit ray direction vector"""
pixel_area: TensorType[..., 1]
"""Projected area of pixel a distance 1 away from origin"""
camera_indices: Optional[TensorType[..., 1]] = None
"""Camera indices"""
nears: Optional[TensorType[..., 1]] = None
"""Distance along ray to start sampling"""
fars: Optional[TensorType[..., 1]] = None
"""Rays Distance along ray to stop sampling"""
valid_mask: Optional[TensorType[..., 1, bool]] = None
"""Rays that are valid"""
num_rays_per_chunk: int = 128
"""Number of rays per chunk"""
metadata: Optional[Dict[str, TensorType["num_rays", "latent_dims"]]] = None
"""Additional metadata or data needed for interpolation, will mimic shape of rays"""
def set_camera_indices(self, camera_index: int) -> None:
"""Sets all of the the camera indices to a specific camera index.
Args:
camera_index (int): Camera index.
camera_index: Camera index.
"""
self.camera_indices = torch.ones_like(self.origins[..., 0:1]).long() * camera_index
@@ -191,10 +186,10 @@ class RayBundle(TensorDataclass):
"""Returns a RayBundle as a subset of rays.
Args:
num_rays (int): Number of rays in output RayBundle
num_rays: Number of rays in output RayBundle
Returns:
RayBundle: RayBundle with subset of rays.
RayBundle with subset of rays.
"""
assert num_rays <= len(self)
indices = random.sample(range(len(self)), k=num_rays)
@@ -204,11 +199,11 @@ class RayBundle(TensorDataclass):
"""Flattens RayBundle and extracts chunk given start and end indicies.
Args:
start_idx (int): Start index of RayBundle chunk.
end_idx (int): End index of RayBundle chunk.
start_idx: Start index of RayBundle chunk.
end_idx: End index of RayBundle chunk.
Returns:
RayBundle: Flattened RayBundle with end_idx-start_idx rays.
Flattened RayBundle with end_idx-start_idx rays.
"""
return self.flatten()[start_idx:end_idx]
@@ -216,17 +211,14 @@ class RayBundle(TensorDataclass):
def get_ray_samples(
self, bin_starts: TensorType["bs":..., "num_samples", 1], bin_ends: TensorType["bs":..., "num_samples", 1]
) -> RaySamples:
"""Produces samples for each ray by projection points along the ray direction.
Currently samples uniformly.
"""Produces samples for each ray by projection points along the ray direction. Currently samples uniformly.
Args:
bin_starts (TensorType[..., "num_samples", 1]): Distance from origin to start of bin.
bin_ends (TensorType[..., "num_samples", 1]): Distance from origin to end of bin.
bin_starts: Distance from origin to start of bin.
bin_ends: Distance from origin to end of bin.
Returns:
RaySamples: Samples projected along ray.
Samples projected along ray.
"""
device = self.origins.device