mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-21 15:15:43 +08:00
merging from main
This commit is contained in:
commit
02b6a460aa
57 files changed
+35038
-744
No files matched your search
@@ -171,6 +171,32 @@ def test_tensor_hash_encoder():
|
||||
assert encoded.shape == (10, out_dim)
|
||||
|
||||
|
||||
def test_kplane_encoder():
|
||||
"""Test K-Planes encoder"""
|
||||
|
||||
out_dim = 8
|
||||
|
||||
encoder = encodings.KPlanesEncoding(
|
||||
resolution=(32, 32, 32, 16),
|
||||
num_components=out_dim,
|
||||
reduce="product",
|
||||
)
|
||||
assert encoder.get_out_dim() == out_dim
|
||||
|
||||
in_tensor = torch.randn((10, 4))
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape == (10, out_dim)
|
||||
|
||||
encoder = encodings.KPlanesEncoding(
|
||||
resolution=(32, 32, 32),
|
||||
num_components=out_dim,
|
||||
reduce="product",
|
||||
)
|
||||
in_tensor = torch.randn((10, 3))
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape == (10, out_dim)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_scaling_and_offset()
|
||||
test_nerf_encoder()
|
||||
|
||||
@@ -40,7 +40,7 @@ def test_sh_renderer():
|
||||
sh_renderer = renderers.SHRenderer()
|
||||
|
||||
rgb = sh_renderer(sh=sh, directions=directions, weights=weights)
|
||||
assert torch.max(rgb) > 0.9
|
||||
assert torch.max(rgb) > 0.7
|
||||
|
||||
|
||||
def test_acc_renderer():
|
||||
|
||||
@@ -57,6 +57,9 @@ def test_load_state_dict():
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.register_parameter("param", nn.Parameter(torch.ones((3,))))
|
||||
self.register_module(
|
||||
"module", Model(*args, **kwargs)
|
||||
) # make sure that non-DDP checkpoint won't be processed as DDP one
|
||||
|
||||
def load_state_dict(self, state_dict, strict=True):
|
||||
"""Mocked load_state_dict"""
|
||||
@@ -64,6 +67,7 @@ def test_load_state_dict():
|
||||
was_called = True
|
||||
assert strict
|
||||
assert "param" in state_dict
|
||||
assert "module.device_indicator_param" in state_dict
|
||||
|
||||
config = VanillaPipelineConfig(
|
||||
datamanager=VanillaDataManagerConfig(
|
||||
@@ -77,3 +81,19 @@ def test_load_state_dict():
|
||||
pipeline.load_pipeline(state_dict, 0)
|
||||
assert was_called
|
||||
assert pipeline.model.param[0].item() == 2
|
||||
|
||||
# preparation for another test
|
||||
state_dict["_model.param"].mul_(2) # pylint: disable=unsubscriptable-object
|
||||
was_called = False
|
||||
# pretends to be a DDP checkpoint
|
||||
ddp_state_dict = {}
|
||||
for key, value in state_dict.items():
|
||||
if key.startswith("_model."):
|
||||
# replace the prefix "_model" with "_model.module"
|
||||
ddp_state_dict["_model.module." + key[len("_model.") :]] = value
|
||||
else:
|
||||
ddp_state_dict[key] = value
|
||||
# load DDP checkpoint
|
||||
pipeline.load_pipeline(ddp_state_dict, 0)
|
||||
assert was_called
|
||||
assert pipeline.model.param[0].item() == 4
|
||||
@@ -12,12 +12,30 @@ from nerfstudio.data.dataparsers.nerfstudio_dataparser import NerfstudioDataPars
|
||||
from nerfstudio.data.utils.colmap_parsing_utils import Camera
|
||||
from nerfstudio.data.utils.colmap_parsing_utils import Image as ColmapImage
|
||||
from nerfstudio.data.utils.colmap_parsing_utils import (
|
||||
qvec2rotmat,
|
||||
write_cameras_binary,
|
||||
write_images_binary,
|
||||
)
|
||||
from scripts.process_data import ProcessImages
|
||||
|
||||
|
||||
def random_quaternion(num_poses: int):
|
||||
"""
|
||||
Generates random rotation quatenion.
|
||||
"""
|
||||
u, v, w = np.random.uniform(size=(3, num_poses))
|
||||
quaternion = np.stack(
|
||||
(
|
||||
np.sqrt(1 - u) * np.sin(2 * np.pi * v),
|
||||
np.sqrt(1 - u) * np.cos(2 * np.pi * v),
|
||||
np.sqrt(u) * np.sin(2 * np.pi * w),
|
||||
np.sqrt(u) * np.cos(2 * np.pi * w),
|
||||
),
|
||||
-1,
|
||||
)
|
||||
return quaternion
|
||||
|
||||
|
||||
def test_process_images_skip_colmap(tmp_path: Path):
|
||||
"""
|
||||
Test ns-process-data images
|
||||
@@ -34,21 +52,21 @@ def test_process_images_skip_colmap(tmp_path: Path):
|
||||
)
|
||||
frames = {}
|
||||
num_frames = 10
|
||||
qvecs = np.random.uniform(size=(num_frames, 4))
|
||||
qvecs = random_quaternion(num_frames)
|
||||
tvecs = np.random.uniform(size=(num_frames, 3))
|
||||
# original_poses = np.concatenate(
|
||||
# (
|
||||
# np.concatenate(
|
||||
# (
|
||||
# np.stack(list(map(qvec2rotmat, qvecs))),
|
||||
# tvecs[:, :, None],
|
||||
# ),
|
||||
# -1,
|
||||
# ),
|
||||
# np.array([[[0, 0, 0, 1]]], dtype=qvecs.dtype).repeat(num_frames, 0),
|
||||
# ),
|
||||
# -2,
|
||||
# )
|
||||
original_poses = np.concatenate(
|
||||
(
|
||||
np.concatenate(
|
||||
(
|
||||
np.stack(list(map(qvec2rotmat, qvecs))),
|
||||
tvecs[:, :, None],
|
||||
),
|
||||
-1,
|
||||
),
|
||||
np.array([[[0, 0, 0, 1]]], dtype=qvecs.dtype).repeat(num_frames, 0),
|
||||
),
|
||||
-2,
|
||||
)
|
||||
for i in range(num_frames):
|
||||
frames[i + 1] = ColmapImage(i + 1, qvecs[i], tvecs[i], 1, f"image_{i}.png", [], [])
|
||||
Image.new("RGB", (width, height)).save(tmp_path / "images" / f"image_{i}.png")
|
||||
@@ -77,4 +95,12 @@ def test_process_images_skip_colmap(tmp_path: Path):
|
||||
outputs = parser.get_dataparser_outputs("train")
|
||||
assert len(outputs.image_filenames) == 9
|
||||
assert torch.is_tensor(outputs.dataparser_transform)
|
||||
# TODO @jkulhanek: Add tests if the loaded poses are the same
|
||||
|
||||
# Test if the original poses can be obtained back
|
||||
gt_poses = original_poses[[0, 1, 2, 3, 4, 5, 6, 7, 9]]
|
||||
dataparser_poses = outputs.transform_poses_to_original_space(outputs.cameras.camera_to_worlds, "opencv").numpy()
|
||||
dataparser_poses = np.concatenate(
|
||||
(dataparser_poses, np.array([[[0, 0, 0, 1]]]).repeat(len(dataparser_poses), 0)), 1
|
||||
)
|
||||
dataparser_poses = np.linalg.inv(dataparser_poses)
|
||||
np.testing.assert_allclose(gt_poses, dataparser_poses, rtol=0, atol=1e-5)
|
||||
Reference in new issue
Block a user