mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-30 12:59:40 +08:00
Computing and predicting normals (#905)
* staring normals PR * code runs but visualization looks bad * Camera and world coordinate conventions (#889) * documented coordinate conventions without a figure for now * another sentence * update frames docs * explain intrinsics and extrinsics * more comments * fix build * Start viewer with more accurate speed calculation (#899) Start viewer with more accurate speed calc * Minor doc updates (#903) * fix conflicts * code runs but visualization looks bad * normals implemented and vis fixed. still debugging quality * minor name change * comment * fix black * lint * predicting normals * adding detach * predicted normals * improved normals prediction performance * cleaning code * cleaning code * cleaning code * adding type to function param * fixing tensorf forward func * reverting tensorf changes * fix style with tensorf field * adding normals check to tensorffield Co-authored-by: Matthew Tancik <tancik@berkeley.edu> Co-authored-by: terrance <terrance.3512@gmail.com>
This commit is contained in:
8 files changed
+204
-17
No files matched your search
@@ -30,6 +30,11 @@ from nerfstudio.field_components.field_heads import FieldHeadNames
|
||||
class Field(nn.Module):
|
||||
"""Base class for fields."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._sample_locations = None
|
||||
self._density_before_activation = None
|
||||
|
||||
def density_fn(self, positions: TensorType["bs":..., 3]) -> TensorType["bs":..., 1]:
|
||||
"""Returns only the density. Used primarily with the density grid.
|
||||
|
||||
@@ -57,6 +62,24 @@ class Field(nn.Module):
|
||||
ray_samples: Samples locations to compute density.
|
||||
"""
|
||||
|
||||
def get_normals(self) -> TensorType[..., 3]:
|
||||
"""Computes and returns a tensor of normals.
|
||||
|
||||
Args:
|
||||
density: Tensor of densities.
|
||||
"""
|
||||
assert self._sample_locations is not None, "Sample locations must be set before calling get_normals."
|
||||
assert self._density_before_activation is not None, "Density must be set before calling get_normals."
|
||||
assert (
|
||||
self._sample_locations.shape[:-1] == self._density_before_activation.shape[:-1]
|
||||
), "Sample locations and density must have the same shape besides the last dimension."
|
||||
|
||||
self._density_before_activation.backward(
|
||||
gradient=torch.ones_like(self._density_before_activation), inputs=self._sample_locations, retain_graph=True
|
||||
)
|
||||
normals = -torch.nn.functional.normalize(self._sample_locations.grad, dim=-1)
|
||||
return normals
|
||||
|
||||
@abstractmethod
|
||||
def get_outputs(
|
||||
self, ray_samples: RaySamples, density_embedding: Optional[TensorType] = None
|
||||
@@ -68,14 +91,23 @@ class Field(nn.Module):
|
||||
density_embedding: Density embeddings to condition on.
|
||||
"""
|
||||
|
||||
def forward(self, ray_samples: RaySamples):
|
||||
def forward(self, ray_samples: RaySamples, compute_normals: bool = False):
|
||||
"""Evaluates the field at points along the ray.
|
||||
|
||||
Args:
|
||||
ray_samples: Samples to evaluate field on.
|
||||
"""
|
||||
density, density_embedding = self.get_density(ray_samples)
|
||||
field_outputs = self.get_outputs(ray_samples, density_embedding=density_embedding)
|
||||
if compute_normals:
|
||||
with torch.enable_grad():
|
||||
density, density_embedding = self.get_density(ray_samples)
|
||||
else:
|
||||
density, density_embedding = self.get_density(ray_samples)
|
||||
|
||||
field_outputs = self.get_outputs(ray_samples, density_embedding=density_embedding)
|
||||
field_outputs[FieldHeadNames.DENSITY] = density # type: ignore
|
||||
|
||||
if compute_normals:
|
||||
with torch.enable_grad():
|
||||
normals = self.get_normals()
|
||||
field_outputs[FieldHeadNames.NORMALS] = normals # type: ignore
|
||||
return field_outputs
|
||||
Reference in new issue
Block a user