Remove redundant data (#103)

RaySamples contained fields for bin_starts and bin_ends. These are also stored in the frustums. This PR removes them from the RaySamples.
This commit is contained in:
Matthew Tancik authored and GitHub committed 2022-07-07 18:07:34 -07:00
1 parent be45d5c9bb
commit 74280110ce
12 files changed
+145682 -145693

No files matched your search

+12 -18
View File
@@ -34,15 +34,15 @@ class Frustums(TensorDataclass):
Args:
origins (TensorType[..., 3]): xyz coordinate for ray origin.
directions (TensorType[..., 3]): Direction of ray.
frustum_starts (TensorType[..., num_samples, 1]): Where the frustum starts along a ray.
frustum_ends (TensorType[..., num_samples, 1]): Where the frustum ends along a ray.
starts (TensorType[..., num_samples, 1]): Where the frustum starts along a ray.
ends (TensorType[..., num_samples, 1]): Where the frustum 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]
starts: TensorType[..., 1]
ends: TensorType[..., 1]
pixel_area: TensorType[..., 1]
def get_positions(self) -> TensorType[..., 3]:
@@ -51,7 +51,7 @@ class Frustums(TensorDataclass):
Returns:
TensorType[..., 3]: xyz positions.
"""
return self.origins + self.directions * (self.frustum_starts + self.frustum_ends) / 2
return self.origins + self.directions * (self.starts + self.ends) / 2
def get_gaussian_blob(self) -> Gaussians:
"""Calculates of guassian approximation of conical frustum.
@@ -64,8 +64,8 @@ class Frustums(TensorDataclass):
return conical_frustum_to_gaussian(
origins=self.origins,
directions=self.directions,
starts=self.frustum_starts,
ends=self.frustum_ends,
starts=self.starts,
ends=self.ends,
radius=cone_radius,
)
@@ -79,8 +79,8 @@ class Frustums(TensorDataclass):
return Frustums(
origins=torch.ones((1, 3)).to(device),
directions=torch.ones((1, 3)).to(device),
frustum_starts=torch.ones((1, 1)).to(device),
frustum_ends=torch.ones((1, 1)).to(device),
starts=torch.ones((1, 1)).to(device),
ends=torch.ones((1, 1)).to(device),
pixel_area=torch.ones((1, 1)).to(device),
)
@@ -93,16 +93,12 @@ class RaySamples(TensorDataclass):
frustums (Frustums): Frustums along ray.
camera_indices (TensorType[..., 1]): Camera index.
valid_mask (TensorType[..., 1]): Rays that are valid.
bin_starts (TensorType[..., 1]): frustum starts along ray.
bin_ends (TensorType[..., 1]): frustum ends along ray.
deltas )TensorType[..., 1]): "width" of each sample.
deltas (TensorType[..., 1]): "width" of each sample.
"""
frustums: Frustums
camera_indices: TensorType[..., 1] = None
valid_mask: TensorType[..., 1] = None
bin_starts: TensorType[..., 1] = None
bin_ends: TensorType[..., 1] = None
deltas: TensorType[..., 1] = None
def get_weights(self, densities: TensorType[..., "num_samples", 1]) -> TensorType[..., "num_samples", 1]:
@@ -250,8 +246,8 @@ class RayBundle(TensorDataclass):
frustums = Frustums(
origins=self.origins[:, None, :], # [N_rays, 1, 3]
directions=self.directions[:, None, :], # [N_rays, 1, 3]
frustum_starts=bin_starts, # [N_rays, N_samples, 1]
frustum_ends=bin_ends, # [N_rays, N_samples, 1]
starts=bin_starts, # [N_rays, N_samples, 1]
ends=bin_ends, # [N_rays, N_samples, 1]
pixel_area=self.pixel_area[:, None, :], # [N_rays, 1, 1]
).to(device)
@@ -259,8 +255,6 @@ class RayBundle(TensorDataclass):
frustums=frustums,
camera_indices=camera_indices, # [N_rays, 1, 1]
valid_mask=valid_mask, # [N_rays, N_samples, 1]
bin_starts=bin_starts, # [N_rays, N_samples, 1]
bin_ends=bin_ends, # [N_rays, N_samples, 1]
deltas=deltas, # [N_rays, N_samples, 1]
)
+2 -2
View File
@@ -38,8 +38,8 @@ class Field(nn.Module):
frustums=Frustums(
origins=positions,
directions=torch.ones_like(positions),
frustum_starts=torch.zeros_like((positions[..., :1])),
frustum_ends=torch.zeros_like((positions[..., :1])),
starts=torch.zeros_like((positions[..., :1])),
ends=torch.zeros_like((positions[..., :1])),
pixel_area=torch.ones_like((positions[..., :1])),
)
)
+2 -2
View File
@@ -102,8 +102,8 @@ class NGPGraph(Graph):
field_outputs_pdf = self.field.forward(ray_samples_pdf)
# Hacky treatment of bins as points to allow us to merge uniform and pdf.
ts_uniform = (ray_samples_uniform.bin_starts + ray_samples_uniform.bin_ends) / 2.0
ts_pdf = (ray_samples_pdf.bin_starts + ray_samples_pdf.bin_ends) / 2.0
ts_uniform = (ray_samples_uniform.frustums.starts + ray_samples_uniform.frustums.ends) / 2.0
ts_pdf = (ray_samples_pdf.frustums.starts + ray_samples_pdf.frustums.ends) / 2.0
ts, indices = torch.sort(torch.cat([ts_uniform, ts_pdf], -2), -2)
bin_starts = ts
bin_ends = torch.cat([ts[..., 1:, :], ts[..., -1:, :]], dim=-2)
+3 -3
View File
@@ -30,7 +30,7 @@ from pyrad.fields.modules.field_heads import FieldHeadNames
from pyrad.fields.modules.spatial_distortions import SceneContraction
from pyrad.fields.nerf_field import NeRFField
from pyrad.graphs.base import Graph
from pyrad.graphs.modules.ray_losses import interval_loss
from pyrad.graphs.modules.ray_losses import distortion_loss
from pyrad.optimizers.loss import MSELoss
from pyrad.graphs.modules.ray_sampler import PDFSampler, UniformSampler
from pyrad.renderers.renderers import AccumulationRenderer, DepthRenderer, RGBRenderer
@@ -112,7 +112,7 @@ class MipNerf360Graph(Graph):
)
accumulation_coarse = self.renderer_accumulation(weights_coarse)
depth_coarse = self.renderer_depth(weights_coarse, ray_samples_uniform)
ray_loss_coarse = interval_loss(ray_samples_uniform, field_outputs_coarse[FieldHeadNames.DENSITY])
ray_loss_coarse = distortion_loss(ray_samples_uniform, field_outputs_coarse[FieldHeadNames.DENSITY])
# pdf sampling
ray_samples_pdf = self.sampler_pdf(ray_bundle, ray_samples_uniform, weights_coarse)
@@ -126,7 +126,7 @@ class MipNerf360Graph(Graph):
)
accumulation_fine = self.renderer_accumulation(weights_fine)
depth_fine = self.renderer_depth(weights_fine, ray_samples_pdf)
ray_loss_fine = interval_loss(ray_samples_pdf, field_outputs_fine[FieldHeadNames.DENSITY])
ray_loss_fine = distortion_loss(ray_samples_pdf, field_outputs_fine[FieldHeadNames.DENSITY])
outputs = {
"rgb_coarse": rgb_coarse,
+5 -5
View File
@@ -20,8 +20,8 @@ from torchtyping import TensorType
from pyrad.cameras.rays import RaySamples
def interval_loss(ray_samples: RaySamples, densities: TensorType[..., "num_samples", 1]) -> TensorType[..., 1]:
"""Interval loss origially proposed in MipNeRF-360.
def distortion_loss(ray_samples: RaySamples, densities: TensorType[..., "num_samples", 1]) -> TensorType[..., 1]:
"""Ray baserd distortion loss proposed in MipNeRF-360.
.. math::
@@ -36,18 +36,18 @@ def interval_loss(ray_samples: RaySamples, densities: TensorType[..., "num_sampl
densities (TensorType[..., "num_samples", 1]): Predicted sample densities
Returns:
TensorType[..., 1]: Interval Loss.
TensorType[..., 1]: Distortion Loss.
"""
# Compute the weight at each sample location
weights = ray_samples.get_weights(densities)
midpoints = (ray_samples.bin_starts + ray_samples.bin_ends) / 2.0 # (..., num_samples, 1)
midpoints = (ray_samples.frustums.starts + ray_samples.frustums.ends) / 2.0 # (..., num_samples, 1)
loss = (
weights * weights[..., None, :, 0] * torch.abs(midpoints - midpoints[..., None, :, 0])
) # (..., num_samples, num_samples)
loss = torch.sum(loss, dim=(-1, -2))[..., None] # (..., num_samples)
loss = loss + 1 / 3.0 * torch.sum(weights**2 * (ray_samples.bin_ends - ray_samples.bin_starts), dim=-2)
loss = loss + 1 / 3.0 * torch.sum(weights**2 * (ray_samples.frustums.ends - ray_samples.frustums.starts), dim=-2)
return loss
+3 -3
View File
@@ -337,9 +337,9 @@ class PDFSampler(Sampler):
# Force bins to not have a gap between them. Kinda hacky, should reconsider.
existing_bins = torch.cat(
[
ray_samples.bin_starts[..., :1, 0],
(ray_samples.bin_starts[..., 1:, 0] + ray_samples.bin_ends[..., :-1, 0]) / 2.0,
ray_samples.bin_ends[..., -1:, 0],
ray_samples.frustums.starts[..., :1, 0],
(ray_samples.frustums.starts[..., 1:, 0] + ray_samples.frustums.ends[..., :-1, 0]) / 2.0,
ray_samples.frustums.ends[..., -1:, 0],
],
axis=-1,
)
+1 -1
View File
@@ -190,7 +190,7 @@ class DepthRenderer(nn.Module):
if self.method == "expected":
eps = 1e-10
steps = (ray_samples.bin_starts + ray_samples.bin_ends) / 2
steps = (ray_samples.frustums.starts + ray_samples.frustums.ends) / 2
depth = torch.sum(weights * steps, dim=-2) / (torch.sum(weights, -2) + eps)
depth = torch.clip(depth, steps[..., 0, :], steps[..., -1, :])