Fixed e3nn change of basis

This commit is contained in:
Aayush committed 2025-01-19 15:33:20 -08:00
1 parent 0c3a4cdcef
commit 7777fd7b85
4 files changed
+63 -30

No files matched your search

+42 -17
View File
@@ -11,7 +11,7 @@ from nerfstudio.utils.spherical_harmonics import (
@pytest.mark.parametrize("degree", list(range(0, 5)))
def test_spherical_harmonics(degree):
def test_spherical_harmonics_components(degree):
torch.manual_seed(0)
N = 1000000
@@ -23,30 +23,55 @@ def test_spherical_harmonics(degree):
@pytest.mark.parametrize("sh_degree", list(range(0, 4)))
def test_rotate_spherical_harmonics(sh_degree):
def test_spherical_harmonics_rotation(sh_degree):
"""Test if rotating both the view direction and SH coefficients by the same rotation
produces the same color output as the original.
In other words, for any rotation R:
color(dir, coeffs) = color(R @ dir, rotate_sh(R, coeffs))
"""
torch.manual_seed(0)
np.random.seed(0)
N = 1000
num_coeffs = (sh_degree + 1) ** 2
coeffs = torch.rand(1000, 3, num_coeffs)
sh_coeffs = torch.rand(N, 3, num_coeffs)
dirs = torch.rand(N, 3)
dirs = dirs / torch.linalg.norm(dirs, dim=-1, keepdim=True)
y_lm = components_from_spherical_harmonics(sh_degree, dirs)
color_original = (sh_coeffs * y_lm[..., None, :]).sum(dim=-1)
rot_matrix = torch.tensor(ScR.random().as_matrix(), dtype=torch.float32)
rotated_coeffs = rotate_spherical_harmonics(coeffs, rot_matrix)
sh_coeffs_rotated = rotate_spherical_harmonics(rot_matrix, sh_coeffs)
dirs_rotated = (rot_matrix @ dirs.T).T
y_lm_rotated = components_from_spherical_harmonics(sh_degree, dirs_rotated)
color_rotated = (sh_coeffs_rotated * y_lm_rotated[..., None, :]).sum(dim=-1)
# Norm Preservation
norm_original = torch.norm(coeffs, dim=-1)
norm_rotated = torch.norm(rotated_coeffs, dim=-1)
torch.testing.assert_close(color_original, color_rotated, rtol=0, atol=1e-5)
@pytest.mark.parametrize("sh_degree", list(range(0, 4)))
def test_spherical_harmonics_rotation_properties(sh_degree):
"""Test properties of the SH rotation"""
torch.manual_seed(0)
np.random.seed(0)
N = 1000
num_coeffs = (sh_degree + 1) ** 2
sh_coeffs = torch.rand(N, 3, num_coeffs)
rot_matrix = torch.tensor(ScR.random().as_matrix(), dtype=torch.float32)
sh_coeffs_rotated = rotate_spherical_harmonics(rot_matrix, sh_coeffs)
# Norm preserving
norm_original = torch.norm(sh_coeffs, dim=-1)
norm_rotated = torch.norm(sh_coeffs_rotated, dim=-1)
torch.testing.assert_close(norm_original, norm_rotated, rtol=0, atol=1e-5)
# 0th order coeffs are invariant to rotation
torch.testing.assert_close(coeffs[..., 0], rotated_coeffs[..., 0], rtol=0, atol=1e-6)
# 0th degree coeffs are invariant to rotation
torch.testing.assert_close(sh_coeffs[..., 0], sh_coeffs_rotated[..., 0], rtol=0, atol=1e-6)
# 1st order coeffs undergo simple rotation
if sh_degree > 0:
act_1st_order = torch.einsum("ij,...j->...i", rot_matrix, coeffs[..., 1:4])
torch.testing.assert_close(act_1st_order, rotated_coeffs[..., 1:4], rtol=0, atol=1e-5)
# Identity Rotation
# Identity rotation
rot_matrix = torch.eye(3)
rotated_coeffs = rotate_spherical_harmonics(coeffs, rot_matrix)
torch.testing.assert_close(coeffs, rotated_coeffs, rtol=0, atol=1e-6)
sh_coeffs_rotated = rotate_spherical_harmonics(rot_matrix, sh_coeffs)
torch.testing.assert_close(sh_coeffs, sh_coeffs_rotated, rtol=0, atol=1e-6)