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:
Michal Pandy authored and GitHub committed 2023-01-02 10:13:38 +01:00
1 parent fa6f279751
commit 3da877050d
12 files changed
+460 -4

No files matched your search

+15
View File
@@ -230,6 +230,21 @@ def normalize(x) -> TensorType[...]:
return x / torch.linalg.norm(x)
def normalize_with_norm(x: torch.Tensor, dim: int) -> Tuple[torch.Tensor, torch.Tensor]:
"""Normalize tensor along axis and return normalized value with norms.
Args:
x: tensor to normalize.
dim: axis along which to normalize.
Returns:
Tuple of normalized tensor and corresponding norm.
"""
norm = torch.maximum(torch.linalg.vector_norm(x, dim=dim, keepdims=True), torch.tensor([_EPS]).to(x))
return x / norm, norm
def viewmatrix(lookat, up, pos) -> TensorType[...]:
"""Returns a camera transformation matrix.
+2 -2
View File
@@ -24,7 +24,6 @@ from typing import Dict, List, Optional, Tuple, Union
import cv2
import torch
import torchvision
from torch.nn.functional import normalize
from torchtyping import TensorType
import nerfstudio.utils.poses as pose_utils
@@ -666,7 +665,7 @@ class Cameras(TensorDataclass):
directions_stack = torch.sum(
directions_stack[..., None, :] * rotation, dim=-1
) # (..., 1, 3) * (..., 3, 3) -> (..., 3)
directions_stack = normalize(directions_stack, dim=-1)
directions_stack, directions_norm = camera_utils.normalize_with_norm(directions_stack, -1)
assert directions_stack.shape == (3,) + num_rays_shape + (3,)
origins = c2w[..., :3, 3] # (..., 3)
@@ -691,6 +690,7 @@ class Cameras(TensorDataclass):
pixel_area=pixel_area,
camera_indices=camera_indices,
times=times,
metadata={"directions_norm": directions_norm[0].detach()},
)
def to_json(