Move util functions out of splatfacto (#3538)

* Move util functions out of splatfacto

Nothing else currently uses some of the SH utils, but it might make sense to get them out of splatfacto.

I also moved the k nearest neighbors to utils since it doesn't depend on the model class.

* fix assert

* fix sh test

* convert to using degrees, not levels
This commit is contained in:
Alexander Kristoffersen authored and GitHub committed 2024-12-05 13:25:55 -05:00
1 parent e8bf472692
commit a8888e7d9e
8 files changed
+207 -181

No files matched your search

+9 -6
View File
@@ -28,8 +28,9 @@ from torch import Tensor, nn
from nerfstudio.field_components.base_field_component import FieldComponent
from nerfstudio.utils.external import TCNN_EXISTS, tcnn
from nerfstudio.utils.math import components_from_spherical_harmonics, expected_sin, generate_polyhedron_basis
from nerfstudio.utils.math import expected_sin, generate_polyhedron_basis
from nerfstudio.utils.printing import print_tcnn_speed_warning
from nerfstudio.utils.spherical_harmonics import MAX_SH_DEGREE, components_from_spherical_harmonics
class Encoding(FieldComponent):
@@ -756,14 +757,16 @@ class SHEncoding(Encoding):
"""Spherical harmonic encoding
Args:
levels: Number of spherical harmonic levels to encode.
levels: Number of spherical harmonic levels to encode. (level = sh degree + 1)
"""
def __init__(self, levels: int = 4, implementation: Literal["tcnn", "torch"] = "torch") -> None:
super().__init__(in_dim=3)
if levels <= 0 or levels > 4:
raise ValueError(f"Spherical harmonic encoding only supports 1 to 4 levels, requested {levels}")
if levels <= 0 or levels > MAX_SH_DEGREE + 1:
raise ValueError(
f"Spherical harmonic encoding only supports 1 to {MAX_SH_DEGREE + 1} levels, requested {levels}"
)
self.levels = levels
@@ -778,7 +781,7 @@ class SHEncoding(Encoding):
)
@classmethod
def get_tcnn_encoding_config(cls, levels) -> dict:
def get_tcnn_encoding_config(cls, levels: int) -> dict:
"""Get the encoding configuration for tcnn if implemented"""
encoding_config = {
"otype": "SphericalHarmonics",
@@ -792,7 +795,7 @@ class SHEncoding(Encoding):
@torch.no_grad()
def pytorch_fwd(self, in_tensor: Float[Tensor, "*bs input_dim"]) -> Float[Tensor, "*bs output_dim"]:
"""Forward pass using pytorch. Significantly slower than TCNN implementation."""
return components_from_spherical_harmonics(levels=self.levels, directions=in_tensor)
return components_from_spherical_harmonics(degree=self.levels - 1, directions=in_tensor)
def forward(self, in_tensor: Float[Tensor, "*bs input_dim"]) -> Float[Tensor, "*bs output_dim"]:
if self.tcnn_encoding is not None: