mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-25 18:39:55 +08:00
Depth supervision for Nerfacto (#1173)
* Implement depth supervision * Fixes * Change normalize dtype * refactor * Change depth weight * Small refactor * Change docstring * Removu unused import * Add assumption to depth comment * Implement URF loss * Remove default depth loss type param * URF fixes * Fix depth loss scale * Visualize gt depths * Fixes & documentation * Documentation and formatting * Fix formatting problems * Fix linter problems * Fix tests * Fix tests
This commit is contained in:
1 parent
fa6f279751
commit
3da877050d
12 files changed
+460
-4
No files matched your search
@@ -15,6 +15,7 @@
|
||||
"""
|
||||
Collection of Losses.
|
||||
"""
|
||||
from enum import Enum
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
@@ -29,6 +30,16 @@ LOSSES = {"L1": L1Loss, "MSE": MSELoss}
|
||||
|
||||
EPS = 1.0e-7
|
||||
|
||||
# Sigma scale factor from Urban Radiance Fields (Rematas et al., 2022)
|
||||
URF_SIGMA_SCALE_FACTOR = 3.0
|
||||
|
||||
|
||||
class DephtLossType(Enum):
|
||||
"""Types of depth losses for depth supervision."""
|
||||
|
||||
DS_NERF = 1
|
||||
URF = 2
|
||||
|
||||
|
||||
def outer(
|
||||
t0_starts: TensorType[..., "num_samples_0"],
|
||||
@@ -194,3 +205,100 @@ def pred_normal_loss(
|
||||
):
|
||||
"""Loss between normals calculated from density and normals from prediction network."""
|
||||
return (weights[..., 0] * (1.0 - torch.sum(normals * pred_normals, dim=-1))).sum(dim=-1)
|
||||
|
||||
|
||||
def ds_nerf_depth_loss(
|
||||
weights: TensorType[..., "num_samples", 1],
|
||||
termination_depth: TensorType[..., 1],
|
||||
steps: TensorType[..., "num_samples", 1],
|
||||
lengths: TensorType[..., "num_samples", 1],
|
||||
sigma: TensorType[0],
|
||||
) -> TensorType[..., 1]:
|
||||
"""Depth loss from Depth-supervised NeRF (Deng et al., 2022).
|
||||
|
||||
Args:
|
||||
weights: Weights predicted for each sample.
|
||||
termination_depth: Ground truth depth of rays.
|
||||
steps: Sampling distances along rays.
|
||||
lengths: Distances between steps.
|
||||
sigma: Uncertainty around depth values.
|
||||
Returns:
|
||||
Depth loss scalar.
|
||||
"""
|
||||
loss = -torch.log(weights + EPS) * torch.exp(-((steps - termination_depth[:, None]) ** 2) / (2 * sigma)) * lengths
|
||||
return torch.mean(loss.sum(-2))
|
||||
|
||||
|
||||
def urban_radiance_field_depth_loss(
|
||||
weights: TensorType[..., "num_samples", 1],
|
||||
termination_depth: TensorType[..., 1],
|
||||
predicted_depth: TensorType[..., 1],
|
||||
steps: TensorType[..., "num_samples", 1],
|
||||
sigma: TensorType[0],
|
||||
) -> TensorType[..., 1]:
|
||||
"""Lidar losses from Urban Radiance Fields (Rematas et al., 2022).
|
||||
|
||||
Args:
|
||||
weights: Weights predicted for each sample.
|
||||
termination_depth: Ground truth depth of rays.
|
||||
predicted_depth: Depth prediction from the network.
|
||||
steps: Sampling distances along rays.
|
||||
sigma: Uncertainty around depth values.
|
||||
Returns:
|
||||
Depth loss scalar.
|
||||
"""
|
||||
# Expected depth loss
|
||||
expected_depth_loss = (termination_depth - predicted_depth) ** 2
|
||||
|
||||
# Line of sight losses
|
||||
target_distribution = torch.distributions.normal.Normal(0.0, sigma / URF_SIGMA_SCALE_FACTOR)
|
||||
termination_depth = termination_depth[:, None]
|
||||
line_of_sight_loss_near_mask = torch.logical_and(
|
||||
steps <= termination_depth + sigma, steps >= termination_depth - sigma
|
||||
)
|
||||
line_of_sight_loss_near = (weights - torch.exp(target_distribution.log_prob(steps - termination_depth))) ** 2
|
||||
line_of_sight_loss_near = (line_of_sight_loss_near_mask * line_of_sight_loss_near).sum(-2)
|
||||
line_of_sight_loss_empty_mask = steps < termination_depth - sigma
|
||||
line_of_sight_loss_empty = (line_of_sight_loss_empty_mask * weights**2).sum(-2)
|
||||
line_of_sight_loss = line_of_sight_loss_near + line_of_sight_loss_empty
|
||||
|
||||
return torch.mean(expected_depth_loss + line_of_sight_loss)
|
||||
|
||||
|
||||
def depth_loss(
|
||||
weights: TensorType[..., "num_samples", 1],
|
||||
ray_samples: RaySamples,
|
||||
termination_depth: TensorType[..., 1],
|
||||
predicted_depth: TensorType[..., 1],
|
||||
sigma: TensorType[0],
|
||||
directions_norm: TensorType[..., 1],
|
||||
is_euclidean: bool,
|
||||
depth_loss_type: DephtLossType,
|
||||
) -> TensorType[0]:
|
||||
"""Implementation of depth losses.
|
||||
|
||||
Args:
|
||||
weights: Weights predicted for each sample.
|
||||
ray_samples: Samples along rays corresponding to weights.
|
||||
termination_depth: Ground truth depth of rays.
|
||||
predicted_depth: Depth prediction from the network.
|
||||
sigma: Uncertainty around depth value.
|
||||
directions_norm: Norms of ray direction vectors in the camera frame.
|
||||
is_euclidean: Whether ground truth depths correponds to normalized direction vectors.
|
||||
depth_loss_type: Type of depth loss to apply.
|
||||
|
||||
Returns:
|
||||
Depth loss scalar.
|
||||
"""
|
||||
if not is_euclidean:
|
||||
termination_depth = termination_depth / directions_norm
|
||||
steps = (ray_samples.frustums.starts + ray_samples.frustums.ends) / 2
|
||||
|
||||
if depth_loss_type == DephtLossType.DS_NERF:
|
||||
lengths = ray_samples.frustums.ends - ray_samples.frustums.starts
|
||||
return ds_nerf_depth_loss(weights, termination_depth, steps, lengths, sigma)
|
||||
|
||||
if depth_loss_type == DephtLossType.URF:
|
||||
return urban_radiance_field_depth_loss(weights, termination_depth, predicted_depth, steps, sigma)
|
||||
|
||||
raise NotImplementedError("Provided depth loss type not implemented.")
|
||||
Reference in new issue
Block a user