mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-02 22:08:41 +08:00
Switch from point samples to frustum samples
This commit is contained in:
1 parent
609bf32d22
commit
1b20028ea3
17 files changed
+255
-102
No files matched your search
@@ -10,7 +10,7 @@ import torch
|
||||
from torchtyping import TensorType
|
||||
from pyrad.nerf.field_modules.field_heads import FieldHeadNames
|
||||
|
||||
from pyrad.structures.rays import PointSamples
|
||||
from pyrad.structures.rays import Frustums, PointSamples
|
||||
from pyrad.utils.misc import is_not_none
|
||||
|
||||
|
||||
@@ -19,7 +19,16 @@ class Field(nn.Module):
|
||||
|
||||
def density_fn(self, positions):
|
||||
"""Returns only the density. Used primarily with the occupancy grid."""
|
||||
point_samples = PointSamples(positions=positions)
|
||||
# Need to figure out a better way to descibe positions with a ray.
|
||||
point_samples = PointSamples(
|
||||
frustums=Frustums(
|
||||
origins=positions,
|
||||
directions=torch.ones_like(positions),
|
||||
frustum_starts=torch.zeros((*positions.shape[:-1], 1)),
|
||||
frustum_ends=torch.zeros((*positions.shape[:-1], 1)),
|
||||
pixel_area=torch.ones((*positions.shape[:-1], 1)),
|
||||
)
|
||||
)
|
||||
density, _ = self.get_density(point_samples)
|
||||
return density
|
||||
|
||||
@@ -55,9 +64,7 @@ class Field(nn.Module):
|
||||
if is_not_none(valid_mask):
|
||||
# Hacky handling of empty masks. Tests on a single ray but doesn't use results
|
||||
if not valid_mask.any():
|
||||
point_samples = PointSamples(
|
||||
positions=point_samples.positions[0, :], directions=point_samples.directions[0, :]
|
||||
)
|
||||
point_samples = PointSamples(frustums=Frustums.get_mock_frustum())
|
||||
else:
|
||||
point_samples = point_samples.apply_masks()
|
||||
density_masked, density_embedding_masked = self.get_density(point_samples)
|
||||
|
||||
@@ -63,7 +63,7 @@ class NeRFField(Field):
|
||||
|
||||
def get_density(self, point_samples: PointSamples):
|
||||
"""Computes and returns the densities."""
|
||||
encoded_xyz = self.position_encoding(point_samples.positions)
|
||||
encoded_xyz = self.position_encoding(point_samples.frustums.get_positions())
|
||||
base_mlp_out = self.mlp_base(encoded_xyz)
|
||||
density = self.field_output_density(base_mlp_out)
|
||||
return density, base_mlp_out
|
||||
@@ -74,7 +74,7 @@ class NeRFField(Field):
|
||||
"""Computes and returns the outputs."""
|
||||
outputs = {}
|
||||
for field_head in self.field_heads:
|
||||
encoded_dir = self.direction_encoding(point_samples.directions)
|
||||
encoded_dir = self.direction_encoding(point_samples.frustums.directions)
|
||||
mlp_out = self.mlp_head(torch.cat([encoded_dir, density_embedding], dim=-1))
|
||||
outputs[field_head.field_head_name] = field_head(mlp_out)
|
||||
return outputs
|
||||
@@ -81,7 +81,7 @@ class VanillaNerfWField(Field):
|
||||
|
||||
def get_density(self, point_samples: PointSamples):
|
||||
"""Computes and returns the densities."""
|
||||
encoded_xyz = self.position_encoding(point_samples.positions)
|
||||
encoded_xyz = self.position_encoding(point_samples.frustums.positions())
|
||||
base_mlp_out = self.mlp_base(encoded_xyz)
|
||||
density = self.field_head_density(base_mlp_out)
|
||||
return density, base_mlp_out
|
||||
|
||||
@@ -79,7 +79,7 @@ class NGPGraph(Graph):
|
||||
ray_samples_pdf = self.sampler_pdf(ray_bundle, ray_samples_uniform, weights_uniform)
|
||||
field_outputs_pdf = self.field.forward(ray_samples_pdf.to_point_samples())
|
||||
|
||||
ts, indices = torch.sort(torch.cat([ray_samples_uniform.ts, ray_samples_pdf.ts], -1), -1)
|
||||
ts, indices = torch.sort(torch.cat([ray_samples_uniform.bins, ray_samples_pdf.bins], -1), -1)
|
||||
ray_samples = ray_bundle.get_ray_samples(ts)
|
||||
field_outputs = {}
|
||||
for fo_name, _ in field_outputs_pdf.items():
|
||||
@@ -96,11 +96,11 @@ class NGPGraph(Graph):
|
||||
weights=weights,
|
||||
)
|
||||
accumulation = self.renderer_accumulation(weights)
|
||||
depth = self.renderer_depth(weights, ray_samples.ts)
|
||||
depth = self.renderer_depth(weights, ray_samples.bins)
|
||||
|
||||
densities_occupancy_grid = self.occupancy_grid.get_densities(ray_samples.positions)
|
||||
weights_occupancy_grid = ray_samples.get_weights(densities_occupancy_grid)
|
||||
depth_occupancy_grid = self.renderer_depth(weights_occupancy_grid, ray_samples.ts)
|
||||
depth_occupancy_grid = self.renderer_depth(weights_occupancy_grid, ray_samples.bins)
|
||||
|
||||
outputs = {
|
||||
"rgb": rgb,
|
||||
|
||||
@@ -97,7 +97,7 @@ class NerfWGraph(Graph):
|
||||
rgb=field_outputs_coarse[FieldHeadNames.RGB],
|
||||
weights=weights_coarse,
|
||||
)
|
||||
depth_coarse = self.renderer_depth(weights_coarse, ray_samples_uniform.ts)
|
||||
depth_coarse = self.renderer_depth(weights_coarse, ray_samples_uniform.bins)
|
||||
|
||||
# pdf sampling
|
||||
ray_samples_pdf = self.sampler_pdf(ray_bundle, ray_samples_uniform, weights_coarse)
|
||||
@@ -131,8 +131,8 @@ class NerfWGraph(Graph):
|
||||
density_transient = field_outputs_fine[FieldHeadNames.TRANSIENT_DENSITY]
|
||||
|
||||
# depth
|
||||
depth_fine = self.renderer_depth(weights_fine, ray_samples_pdf.ts)
|
||||
depth_fine_static = self.renderer_depth(weights_fine_static, ray_samples_pdf.ts)
|
||||
depth_fine = self.renderer_depth(weights_fine, ray_samples_pdf.bins)
|
||||
depth_fine_static = self.renderer_depth(weights_fine_static, ray_samples_pdf.bins)
|
||||
|
||||
# uncertainty
|
||||
uncertainty = self.renderer_uncertainty(field_outputs_fine[FieldHeadNames.UNCERTAINTY], weights_fine_transient)
|
||||
|
||||
@@ -121,7 +121,7 @@ class SemanticNerfGraph(NeRFGraph):
|
||||
weights=weights_coarse,
|
||||
)
|
||||
accumulation_coarse = self.renderer_accumulation(weights_coarse)
|
||||
depth_coarse = self.renderer_depth(weights_coarse, ray_samples_uniform.ts)
|
||||
depth_coarse = self.renderer_depth(weights_coarse, ray_samples_uniform.bins)
|
||||
|
||||
# pdf sampling
|
||||
ray_samples_pdf = self.sampler_pdf(ray_bundle, ray_samples_uniform, weights_coarse)
|
||||
@@ -134,7 +134,7 @@ class SemanticNerfGraph(NeRFGraph):
|
||||
weights=weights_fine,
|
||||
)
|
||||
accumulation_fine = self.renderer_accumulation(weights_fine)
|
||||
depth_fine = self.renderer_depth(weights_fine, ray_samples_pdf.ts)
|
||||
depth_fine = self.renderer_depth(weights_fine, ray_samples_pdf.bins)
|
||||
semantic_fine = self.renderer_semantic(field_outputs_fine[FieldHeadNames.SEMANTICS], weights=weights_fine)
|
||||
|
||||
outputs = {
|
||||
|
||||
@@ -91,7 +91,7 @@ class NeRFGraph(Graph):
|
||||
weights=weights_coarse,
|
||||
)
|
||||
accumulation_coarse = self.renderer_accumulation(weights_coarse)
|
||||
depth_coarse = self.renderer_depth(weights_coarse, ray_samples_uniform.ts)
|
||||
depth_coarse = self.renderer_depth(weights_coarse, ray_samples_uniform.bins)
|
||||
|
||||
# pdf sampling
|
||||
ray_samples_pdf = self.sampler_pdf(ray_bundle, ray_samples_uniform, weights_coarse)
|
||||
@@ -104,7 +104,7 @@ class NeRFGraph(Graph):
|
||||
weights=weights_fine,
|
||||
)
|
||||
accumulation_fine = self.renderer_accumulation(weights_fine)
|
||||
depth_fine = self.renderer_depth(weights_fine, ray_samples_pdf.ts)
|
||||
depth_fine = self.renderer_depth(weights_fine, ray_samples_pdf.bins)
|
||||
|
||||
outputs = {
|
||||
"rgb_coarse": rgb_coarse,
|
||||
|
||||
+16
-13
@@ -32,7 +32,7 @@ class Sampler(nn.Module):
|
||||
"""Generate ray samples with optional occupancy filtering"""
|
||||
ray_samples = self.generate_ray_samples(*args, **kwargs)
|
||||
if self.occupancy_field is not None:
|
||||
densities = self.occupancy_field.get_densities(ray_samples.positions)
|
||||
densities = self.occupancy_field.get_densities(ray_samples.frustums.get_positions())
|
||||
weights = ray_samples.get_weights(densities)
|
||||
|
||||
valid_mask = weights >= self.weight_threshold
|
||||
@@ -83,18 +83,19 @@ class UniformSampler(Sampler):
|
||||
num_samples = num_samples or self.num_samples
|
||||
num_rays = ray_bundle.origins.shape[0]
|
||||
|
||||
bins = torch.linspace(0.0, 1.0, num_samples + 1).to(ray_bundle.origins.device) # shape (num_samples+1,)
|
||||
bins = torch.linspace(0.0, 1.0, num_samples + 1).to(ray_bundle.origins.device) # shape (num_samples+1)
|
||||
bins = ray_bundle.nears[:, None] + bins[None, :] * (
|
||||
ray_bundle.fars[:, None] - ray_bundle.nears[:, None]
|
||||
) # shape (num_rays, num_samples+1)
|
||||
|
||||
if self.train_stratified and self.training:
|
||||
t_rand = torch.rand((num_rays, num_samples), dtype=bins.dtype, device=bins.device)
|
||||
ts = bins[:, :-1] + t_rand * (bins[:, 1:] - bins[:, :-1]) # shape (num_rays, num_samples)
|
||||
else:
|
||||
ts = (bins[:, 1:] + bins[:, :-1]) / 2
|
||||
t_rand = torch.rand((num_rays, num_samples + 1), dtype=bins.dtype, device=bins.device)
|
||||
bin_centers = (bins[..., 1:] + bins[..., :-1]) / 2.0
|
||||
bin_upper = torch.cat([bin_centers, bins[..., -1:]], -1)
|
||||
bin_lower = torch.cat([bins[..., :1], bin_centers], -1)
|
||||
bins = bin_lower + (bin_upper - bin_lower) * t_rand
|
||||
|
||||
ray_samples = ray_bundle.get_ray_samples(ts)
|
||||
ray_samples = ray_bundle.get_ray_samples(bins)
|
||||
|
||||
return ray_samples
|
||||
|
||||
@@ -171,21 +172,23 @@ class PDFSampler(Sampler):
|
||||
indicies_g = torch.stack([below, above], -1)
|
||||
matched_shape = (indicies_g.shape[0], indicies_g.shape[1], cdf.shape[-1])
|
||||
cdf_g = torch.gather(cdf.unsqueeze(1).expand(matched_shape), dim=2, index=indicies_g)
|
||||
bins_g = torch.gather(ray_samples.ts.unsqueeze(1).expand(matched_shape), dim=2, index=indicies_g)
|
||||
# TODO tancik (Fix bin samples)
|
||||
steps = (ray_samples.bins[:, :-1] + ray_samples.bins[:, 1:]) / 2
|
||||
bins_g = torch.gather(steps.unsqueeze(1).expand(matched_shape), dim=2, index=indicies_g)
|
||||
|
||||
denom = cdf_g[..., 1] - cdf_g[..., 0]
|
||||
denom = torch.where(denom < eps, torch.ones_like(denom), denom)
|
||||
t = (u - cdf_g[..., 0]) / denom
|
||||
ts = bins_g[..., 0] + t * (bins_g[..., 1] - bins_g[..., 0])
|
||||
bins = bins_g[..., 0] + t * (bins_g[..., 1] - bins_g[..., 0])
|
||||
|
||||
if self.include_original:
|
||||
ts, _ = torch.sort(torch.cat([ray_samples.ts, ts], -1), -1)
|
||||
bins, _ = torch.sort(torch.cat([ray_samples.bins, bins], -1), -1)
|
||||
else:
|
||||
ts, _ = torch.sort(ts, -1)
|
||||
bins, _ = torch.sort(bins, -1)
|
||||
|
||||
# Stop gradients
|
||||
ts = ts.detach()
|
||||
bins = bins.detach()
|
||||
|
||||
ray_samples = ray_bundle.get_ray_samples(ts)
|
||||
ray_samples = ray_bundle.get_ray_samples(bins)
|
||||
|
||||
return ray_samples
|
||||
@@ -152,13 +152,13 @@ class DepthRenderer(nn.Module):
|
||||
self.method = method
|
||||
|
||||
def forward(
|
||||
self, weights: TensorType[..., "num_samples"], ts: TensorType[..., "num_samples"]
|
||||
self, weights: TensorType[..., "num_samples"], bins: TensorType[..., "num_samples+1"]
|
||||
) -> TensorType[..., 1]:
|
||||
"""Composite samples along ray and calculate disparities.
|
||||
|
||||
Args:
|
||||
weights (TensorType[..., "num_samples"]): Weights for each sample
|
||||
ts (TensorType[..., "num_samples"]): Sample locations along rays
|
||||
bins (TensorType[..., "num_samples+1"]): Bins along ray
|
||||
|
||||
Returns:
|
||||
TensorType[..., 1]: Outputs of depth values.
|
||||
@@ -166,9 +166,10 @@ class DepthRenderer(nn.Module):
|
||||
|
||||
if self.method == "expected":
|
||||
eps = 1e-10
|
||||
depth = torch.sum(weights * ts, dim=-1) / (torch.sum(weights, -1) + eps)
|
||||
steps = (bins[..., :-1] + bins[..., 1:]) / 2
|
||||
depth = torch.sum(weights * steps, dim=-1) / (torch.sum(weights, -1) + eps)
|
||||
|
||||
depth = torch.clip(depth, ts[..., 0], ts[..., -1])
|
||||
depth = torch.clip(depth, steps[..., 0], steps[..., -1])
|
||||
|
||||
return depth[..., None]
|
||||
|
||||
|
||||
+57
-20
@@ -83,15 +83,15 @@ class Camera:
|
||||
@abstractmethod
|
||||
def generate_rays(
|
||||
cls,
|
||||
intrinsics: TensorType["num_rays", "num_intrinsics_params"],
|
||||
camera_to_world: TensorType["num_rays", 3, 4],
|
||||
coords: TensorType["image_height", "image_width", 2],
|
||||
intrinsics: TensorType[..., "num_intrinsics_params"],
|
||||
camera_to_world: TensorType[..., 3, 4],
|
||||
coords: TensorType[..., 2],
|
||||
) -> RayBundle:
|
||||
"""
|
||||
Args:
|
||||
intrinsics (TensorType["num_rays", "num_intrinsics_params"]): Camera intrinsics
|
||||
camera_to_world (TensorType["num_rays", 3, 4]): Camera to world transformation matrix
|
||||
coords (TensorType["image_height", "image_width", 2]): Image grid coordinates
|
||||
intrinsics (TensorType[..., "num_intrinsics_params"]): Camera intrinsics
|
||||
camera_to_world (TensorType[..., 3, 4]): Camera to world transformation matrix
|
||||
coords (TensorType[..., 2]): Image grid coordinates
|
||||
|
||||
Returns:
|
||||
RayBundle: A bundle of rays for each grid coordinate.
|
||||
@@ -193,7 +193,17 @@ class PinholeCamera(Camera):
|
||||
) # (..., 1, 3) * (..., 3, 3) -> (..., 3)
|
||||
directions = normalize(directions, dim=-1)
|
||||
origins = camera_to_world[..., :3, 3] # (..., 3)
|
||||
return RayBundle(origins=origins, directions=directions)
|
||||
|
||||
## Calculate pixel area directly
|
||||
dirx_min = normalize(torch.cat([(x - cx - 0.5) / fx, -(y - cy) / fy, -torch.ones_like(x)], -1), dim=-1)
|
||||
dirx_max = normalize(torch.cat([(x - cx + 0.5) / fx, -(y - cy) / fy, -torch.ones_like(x)], -1), dim=-1)
|
||||
diry_min = normalize(torch.cat([(x - cx) / fx, -(y - cy - 0.5) / fy, -torch.ones_like(x)], -1), dim=-1)
|
||||
diry_max = normalize(torch.cat([(x - cx) / fx, -(y - cy + 0.5) / fy, -torch.ones_like(x)], -1), dim=-1)
|
||||
dx = torch.sqrt(torch.sum((dirx_max - dirx_min) ** 2, dim=-1))
|
||||
dy = torch.sqrt(torch.sum((diry_max - diry_min) ** 2, dim=-1))
|
||||
pixel_area = dx * dy
|
||||
|
||||
return RayBundle(origins=origins, directions=directions, pixel_area=pixel_area[..., None])
|
||||
|
||||
|
||||
class SimplePinholeCamera(PinholeCamera):
|
||||
@@ -234,25 +244,52 @@ class EquirectangularCamera(Camera):
|
||||
@classmethod
|
||||
def generate_rays(
|
||||
cls,
|
||||
intrinsics: TensorType["num_rays", 2],
|
||||
camera_to_world: TensorType["num_rays", 3, 4],
|
||||
coords: TensorType["num_rays", 2],
|
||||
intrinsics: TensorType[..., 2],
|
||||
camera_to_world: TensorType[..., 3, 4],
|
||||
coords: TensorType[..., 2],
|
||||
) -> RayBundle:
|
||||
|
||||
phi = coords[:, 0:1] # (num_rays, 1)
|
||||
theta = coords[:, 1:2] # (num_rays, 1)
|
||||
y = coords[..., 0:1] # (num_rays, 1)
|
||||
x = coords[..., 1:2] # (num_rays, 1)
|
||||
|
||||
height = intrinsics[:, 0:1]
|
||||
width = intrinsics[:, 1:2]
|
||||
height = intrinsics[..., 0:1]
|
||||
width = intrinsics[..., 1:2]
|
||||
|
||||
phi = phi / height * torch.pi
|
||||
theta = -theta / width * 2 * torch.pi
|
||||
phi = y / height * torch.pi
|
||||
theta = -x / width * 2 * torch.pi
|
||||
|
||||
directions = torch.stack(
|
||||
[torch.cos(theta) * torch.sin(phi), torch.sin(theta) * torch.sin(phi), torch.cos(phi)], dim=-1
|
||||
d_phi_min = (y + 0.5) / height * torch.pi
|
||||
d_phi_max = (y - 0.5) / height * torch.pi
|
||||
d_theta_min = -(x - 0.5) / width * 2 * torch.pi
|
||||
d_theta_max = -(x + 0.5) / width * 2 * torch.pi
|
||||
|
||||
s_phi = torch.sin(phi)
|
||||
c_phi = torch.cos(phi)
|
||||
s_theta = torch.sin(theta)
|
||||
c_theta = torch.cos(theta)
|
||||
|
||||
directions = torch.cat([c_theta * s_phi, s_theta * s_phi, c_phi], dim=-1)
|
||||
origins = camera_to_world[..., :3, 3] # (num_rays, 3)
|
||||
|
||||
## Calculate area directly
|
||||
dirx_min = normalize(
|
||||
torch.cat([torch.cos(d_theta_min) * s_phi, torch.sin(d_theta_min) * s_phi, c_phi], -1), dim=-1
|
||||
)
|
||||
origins = camera_to_world[:, :3, 3] # (num_rays, 3)
|
||||
return RayBundle(origins=origins, directions=directions)
|
||||
dirx_max = normalize(
|
||||
torch.cat([torch.cos(d_theta_max) * s_phi, torch.sin(d_theta_max) * s_phi, c_phi], -1), dim=-1
|
||||
)
|
||||
diry_min = normalize(
|
||||
torch.cat([c_theta * torch.sin(d_phi_min), s_theta * torch.sin(d_phi_min), torch.cos(d_phi_min)], -1),
|
||||
dim=-1,
|
||||
)
|
||||
diry_max = normalize(
|
||||
torch.cat([c_theta * torch.sin(d_phi_max), s_theta * torch.sin(d_phi_max), torch.cos(d_phi_max)], -1),
|
||||
dim=-1,
|
||||
)
|
||||
dx = torch.sqrt(torch.sum((dirx_max - dirx_min) ** 2, dim=-1))
|
||||
dy = torch.sqrt(torch.sum((diry_max - diry_min) ** 2, dim=-1))
|
||||
pixel_area = dx * dy
|
||||
return RayBundle(origins=origins, directions=directions, pixel_area=pixel_area)
|
||||
|
||||
|
||||
def get_camera_model(num_intrinsics_params: int) -> Type[Camera]:
|
||||
|
||||
+58
-31
@@ -13,12 +13,40 @@ from pyrad.utils.misc import is_not_none
|
||||
|
||||
@dataclass
|
||||
class Frustums:
|
||||
"""Describes region of space as a frustum.
|
||||
|
||||
Args:
|
||||
origins (TensorType[..., 3]): xyz coordinate for ray origin.
|
||||
directions (TensorType[..., 3]): Direction of ray.
|
||||
frustum_starts (TensorType[..., 1]): Where the frustum starts along a ray.
|
||||
frustum_ends (TensorType[..., 3]): Where the frustim ends along a ray.
|
||||
pixel_area (TensorType[..., 1]): Projected area of pixel a distance 1 away from origin.
|
||||
"""
|
||||
|
||||
origins: TensorType[..., 3]
|
||||
directions: TensorType[..., 3]
|
||||
frustum_starts: TensorType[..., 1]
|
||||
frustum_ends: TensorType[..., 1]
|
||||
radius: TensorType[..., 1]
|
||||
pixel_area: TensorType[..., 1]
|
||||
|
||||
def get_positions(self) -> TensorType[..., 3]:
|
||||
"""Returns "center" position of frustum. Not weighted by mass."""
|
||||
return self.origins + self.directions * (self.frustum_starts + self.frustum_ends) / 2
|
||||
|
||||
@classmethod
|
||||
def get_mock_frustum(cls) -> "Frustums":
|
||||
"""Helper function to generate a placeholder frustum.
|
||||
|
||||
Returns:
|
||||
Frustums: A size 1 frustum with meaningless values.
|
||||
"""
|
||||
return Frustums(
|
||||
origins=torch.ones((1, 3)),
|
||||
directions=torch.ones((1, 3)),
|
||||
frustum_starts=torch.ones((1, 1)),
|
||||
frustum_ends=torch.ones((1, 1)) + 1,
|
||||
pixel_area=torch.ones((1, 1)),
|
||||
)
|
||||
|
||||
def apply_masks(self, mask: TensorType) -> "Frustums":
|
||||
"""Use valid_mask to mask samples.
|
||||
@@ -34,7 +62,7 @@ class Frustums:
|
||||
directions=self.directions[mask],
|
||||
frustum_starts=self.frustum_starts[mask],
|
||||
frustum_ends=self.frustum_ends[mask],
|
||||
radius=self.radius[mask],
|
||||
pixel_area=self.pixel_area[mask],
|
||||
)
|
||||
|
||||
|
||||
@@ -43,10 +71,10 @@ class PointSamples:
|
||||
"""Samples in space.
|
||||
|
||||
Args:
|
||||
positions (TensorType[..., 3]): XYZ locations
|
||||
directions (TensorType[..., 3]): Unit direction vector
|
||||
camera_indices (TensorType[..., 1]): Camera index
|
||||
valid_mask (TensorType[...]): Rays that are valid
|
||||
frustims (Frustums): Frustums along ray.
|
||||
directions (TensorType[..., 3]): Unit direction vector.
|
||||
camera_indices (TensorType[..., 1]): Camera index.
|
||||
valid_mask (TensorType[...]): Rays that are valid.
|
||||
"""
|
||||
|
||||
frustums: Frustums
|
||||
@@ -79,18 +107,17 @@ class RaySamples:
|
||||
"""Samples along a ray
|
||||
|
||||
Args:
|
||||
positions (TensorType[..., 3]): XYZ locations
|
||||
directions (TensorType[..., 3]): Unit direction vector
|
||||
camera_indices (TensorType[..., 1]): Camera index
|
||||
valid_mask (TensorType[...]): Rays that are valid
|
||||
ts (TensorType[..., 1]): "time steps", distances along ray
|
||||
deltas )TensorType[..., 1]): "width" of each sample
|
||||
frustums (Frustums): Frustums along ray.
|
||||
camera_indices (TensorType[..., 1]): Camera index.
|
||||
valid_mask (TensorType[...]): Rays that are valid.
|
||||
bins (TensorType[..., 1]): frustum bins along ray.
|
||||
deltas )TensorType[..., 1]): "width" of each sample.
|
||||
"""
|
||||
|
||||
frustums: TensorType[..., 3]
|
||||
camera_indices: TensorType[..., 1] = None
|
||||
valid_mask: TensorType[...] = None
|
||||
ts: TensorType[..., 1] = None
|
||||
bins: TensorType[..., 1] = None
|
||||
deltas: TensorType[..., 1] = None
|
||||
|
||||
def to_point_samples(self) -> PointSamples:
|
||||
@@ -144,6 +171,7 @@ class RayBundle:
|
||||
Args:
|
||||
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 (TensorType[..., 1]): Camera indices
|
||||
nears (TensorType[..., 1]): Distance along ray to start sampling
|
||||
fars (TensorType[..., 1]): Rays Distance along ray to stop sampling
|
||||
@@ -152,7 +180,7 @@ class RayBundle:
|
||||
|
||||
origins: TensorType["num_rays", 3]
|
||||
directions: TensorType["num_rays", 3]
|
||||
radius: TensorType["num_rays", 1]
|
||||
pixel_area: TensorType["num_rays", 1]
|
||||
camera_indices: Optional[TensorType["num_rays", 1]] = None
|
||||
nears: Optional[TensorType["num_rays"]] = None
|
||||
fars: Optional[TensorType["num_rays"]] = None
|
||||
@@ -195,7 +223,7 @@ class RayBundle:
|
||||
return RayBundle(
|
||||
origins=self.origins[indices],
|
||||
directions=self.directions[indices],
|
||||
radius=self.radius[indices],
|
||||
pixel_area=self.pixel_area[indices],
|
||||
camera_indices=self.camera_indices[indices],
|
||||
)
|
||||
|
||||
@@ -211,7 +239,7 @@ class RayBundle:
|
||||
return RayBundle(
|
||||
origins=self.origins[valid_mask],
|
||||
directions=self.directions[valid_mask],
|
||||
radius=self.radius[valid_mask],
|
||||
pixel_area=self.pixel_area[valid_mask],
|
||||
camera_indices=self.camera_indices[valid_mask] if is_not_none(self.camera_indices) else None,
|
||||
nears=self.nears[valid_mask] if is_not_none(self.nears) else None,
|
||||
fars=self.fars[valid_mask] if is_not_none(self.fars) else None,
|
||||
@@ -235,46 +263,45 @@ class RayBundle:
|
||||
return RayBundle(
|
||||
origins=self.origins.view(-1, 3)[start_idx:end_idx],
|
||||
directions=self.directions.view(-1, 3)[start_idx:end_idx],
|
||||
radius=self.radius.view(-1, 1)[start_idx:end_idx],
|
||||
pixel_area=self.pixel_area.view(-1, 1)[start_idx:end_idx],
|
||||
camera_indices=camera_indices,
|
||||
)
|
||||
|
||||
def get_ray_samples(self, ts: TensorType[..., "num_samples"]) -> RaySamples:
|
||||
def get_ray_samples(self, bins: TensorType["num_rays", "num_samples+1"]) -> RaySamples:
|
||||
"""Produces samples for each ray by projection points along the ray direction.
|
||||
|
||||
|
||||
Args:
|
||||
ts (TensorType[..., "num_samples"]): Distance from origin of sample points.
|
||||
bins (TensorType["num_rays", "num_samples+1"]): Distance from origin of sample points.
|
||||
|
||||
Returns:
|
||||
RaySamples: Samples projected along ray.
|
||||
"""
|
||||
positions = self.origins[:, None] + ts[:, :, None] * self.directions[:, None]
|
||||
num_samples = bins.shape[-1] - 1
|
||||
|
||||
valid_mask = torch.ones_like(ts, dtype=torch.bool)
|
||||
valid_mask = torch.ones((bins.shape[0], num_samples), dtype=torch.bool, device=bins.device)
|
||||
|
||||
dists = ts[..., 1:] - ts[..., :-1]
|
||||
dists = torch.cat([dists, dists[..., -1:]], -1) # [N_rays, N_samples]
|
||||
deltas = dists * torch.norm(self.directions[..., None, :], dim=-1)
|
||||
dists = bins[:, 1:] - bins[:, :-1] # [N_rays, N_samples]
|
||||
deltas = dists * torch.norm(self.directions[:, None, :], dim=-1)
|
||||
|
||||
if is_not_none(self.camera_indices):
|
||||
camera_indices = self.camera_indices.unsqueeze(1).repeat(1, positions.shape[1])
|
||||
camera_indices = self.camera_indices.unsqueeze(1).repeat(1, num_samples)
|
||||
else:
|
||||
camera_indices = None
|
||||
|
||||
frustums = Frustums(
|
||||
origins=self.origins,
|
||||
directions=self.directions.unsqueeze(1).repeat(1, positions.shape[1], 1),
|
||||
frustum_starts=ts[:, :-1, None],
|
||||
frustum_ends=ts[:, :-1, None],
|
||||
radius=self.radius,
|
||||
origins=self.origins.unsqueeze(1).repeat(1, num_samples, 1),
|
||||
directions=self.directions.unsqueeze(1).repeat(1, num_samples, 1),
|
||||
frustum_starts=bins[:, :-1, None],
|
||||
frustum_ends=bins[:, 1:, None],
|
||||
pixel_area=self.pixel_area.unsqueeze(1).repeat(1, num_samples, 1),
|
||||
)
|
||||
|
||||
ray_samples = RaySamples(
|
||||
frustums=frustums,
|
||||
camera_indices=camera_indices,
|
||||
valid_mask=valid_mask,
|
||||
ts=ts,
|
||||
bins=bins,
|
||||
deltas=deltas,
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user