mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-19 22:25:44 +08:00
merging main
This commit is contained in:
commit
80cdb73406
221 files changed
+50645
-2513
No files matched your search
@@ -0,0 +1,107 @@
|
||||
# pylint: disable=all
|
||||
from copy import copy
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
import yaml
|
||||
|
||||
from nerfstudio.cameras.cameras import Cameras
|
||||
from nerfstudio.configs.base_config import InstantiateConfig
|
||||
from nerfstudio.data.datamanagers.base_datamanager import (
|
||||
DataManager,
|
||||
DataparserOutputs,
|
||||
VanillaDataManager,
|
||||
VanillaDataManagerConfig,
|
||||
)
|
||||
from nerfstudio.data.datasets.base_dataset import InputDataset
|
||||
from nerfstudio.data.datasets.depth_dataset import DepthDataset
|
||||
|
||||
|
||||
class DummyDataParser:
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def __getattr__(self, __name: str) -> Any:
|
||||
if __name.startswith("_"):
|
||||
return object.__getattribute__(self, __name)
|
||||
return None
|
||||
|
||||
def get_dataparser_outputs(self, *args, **kwargs):
|
||||
return DataparserOutputs(
|
||||
[],
|
||||
Cameras(
|
||||
torch.ones((0, 3, 4)),
|
||||
torch.ones((0, 1)),
|
||||
torch.ones((0, 1)),
|
||||
torch.ones((0, 1)),
|
||||
torch.ones((0, 1)),
|
||||
10,
|
||||
10,
|
||||
),
|
||||
metadata={"depth_filenames": [], "depth_unit_scale_factor": 1.0},
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def config():
|
||||
config = VanillaDataManagerConfig()
|
||||
setattr(config, "dataparser", InstantiateConfig(_target=DummyDataParser))
|
||||
setattr(config.dataparser, "data", None)
|
||||
return config
|
||||
|
||||
|
||||
def test_data_manager_type_inference(config):
|
||||
# Mock for a faster test
|
||||
|
||||
assert VanillaDataManager(config).dataset_type is InputDataset
|
||||
assert VanillaDataManager[DepthDataset](config).dataset_type is DepthDataset
|
||||
|
||||
class tmp(VanillaDataManager):
|
||||
pass
|
||||
|
||||
assert tmp(config).dataset_type is InputDataset
|
||||
|
||||
class tmp2(VanillaDataManager[DepthDataset]):
|
||||
pass
|
||||
|
||||
assert tmp2(config).dataset_type is DepthDataset
|
||||
|
||||
|
||||
def test_data_manager_type_can_be_serialized(config):
|
||||
# Mock for a faster test
|
||||
|
||||
assert VanillaDataManager(config).dataset_type is InputDataset
|
||||
obj = yaml.load(yaml.dump(VanillaDataManager(config)), Loader=yaml.UnsafeLoader)
|
||||
assert obj.dataset_type is InputDataset
|
||||
assert isinstance(obj, VanillaDataManager)
|
||||
|
||||
assert VanillaDataManager[DepthDataset](config).dataset_type is DepthDataset
|
||||
obj = yaml.load(yaml.dump(VanillaDataManager[DepthDataset](config)), Loader=yaml.UnsafeLoader)
|
||||
assert obj.dataset_type is DepthDataset
|
||||
assert isinstance(obj, VanillaDataManager)
|
||||
|
||||
class tmp(VanillaDataManager):
|
||||
pass
|
||||
|
||||
try:
|
||||
globals()["tmp"] = tmp
|
||||
assert tmp(config).dataset_type is InputDataset
|
||||
obj = yaml.load(yaml.dump(tmp(config)), Loader=yaml.UnsafeLoader)
|
||||
assert obj.dataset_type is InputDataset
|
||||
assert isinstance(obj, tmp)
|
||||
finally:
|
||||
globals().pop("tmp")
|
||||
|
||||
class tmp2(VanillaDataManager[DepthDataset]):
|
||||
pass
|
||||
|
||||
try:
|
||||
globals()["tmp2"] = tmp2
|
||||
|
||||
assert tmp2(config).dataset_type is DepthDataset
|
||||
obj = yaml.load(yaml.dump(tmp2(config)), Loader=yaml.UnsafeLoader)
|
||||
assert obj.dataset_type is DepthDataset
|
||||
assert isinstance(obj, tmp2)
|
||||
finally:
|
||||
globals().pop("tmp2")
|
||||
@@ -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()
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
Test losses
|
||||
"""
|
||||
|
||||
import torch
|
||||
|
||||
from nerfstudio.model_components.losses import tv_loss
|
||||
|
||||
|
||||
def test_tv_loss():
|
||||
"""Test total variation loss"""
|
||||
|
||||
grids = torch.ones([3, 64, 128, 128])
|
||||
assert tv_loss(grids).item() == 0
|
||||
|
||||
grids = torch.zeros([1, 10, 100, 100])
|
||||
for i in range(100):
|
||||
for j in range(100):
|
||||
grid_location = (i + j) % 2
|
||||
for k in range(10):
|
||||
grids[0, k, i, j] = grid_location
|
||||
|
||||
# TV_row = 1, TV_col = 1. Totol tv should be 2 * (TV_row + TV_col)
|
||||
assert tv_loss(grids).item() == 4.0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_tv_loss()
|
||||
@@ -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
|
||||
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
Tests for the nerfstudio.plugins.registry module.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
|
||||
from nerfstudio.engine.trainer import TrainerConfig
|
||||
@@ -49,3 +50,23 @@ def test_discover_methods():
|
||||
finally:
|
||||
# Revert mock
|
||||
registry.entry_points = entry_points_backup
|
||||
|
||||
|
||||
def test_discover_methods_from_environment_variable():
|
||||
"""Tests if a custom method from env variable gets properly registered using the discover_methods method"""
|
||||
old_env = None
|
||||
try:
|
||||
old_env = os.environ.get("NERFSTUDIO_METHOD_CONFIGS", None)
|
||||
os.environ["NERFSTUDIO_METHOD_CONFIGS"] = "test-method-env=test_registry:TestConfig"
|
||||
|
||||
# Discover plugins
|
||||
methods, _ = registry.discover_methods()
|
||||
assert "test-method-env" in methods
|
||||
config = methods["test-method-env"]
|
||||
assert isinstance(config, TrainerConfig)
|
||||
finally:
|
||||
# Revert mock
|
||||
if old_env is not None:
|
||||
os.environ["NERFSTUDIO_METHOD_CONFIGS"] = old_env
|
||||
else:
|
||||
del os.environ["NERFSTUDIO_METHOD_CONFIGS"]
|
||||
@@ -12,10 +12,37 @@ 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 (
|
||||
<<<<<<< HEAD
|
||||
write_cameras_binary,
|
||||
write_images_binary,
|
||||
)
|
||||
from scripts.process_data import ProcessImages
|
||||
=======
|
||||
qvec2rotmat,
|
||||
write_cameras_binary,
|
||||
write_images_binary,
|
||||
)
|
||||
from nerfstudio.process_data.images_to_nerfstudio_dataset import (
|
||||
ImagesToNerfstudioDataset,
|
||||
)
|
||||
|
||||
|
||||
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
|
||||
>>>>>>> b1ddb9e695b789fae47e0f760a5c7f59f25af0bb
|
||||
|
||||
|
||||
def test_process_images_skip_colmap(tmp_path: Path):
|
||||
@@ -34,6 +61,7 @@ def test_process_images_skip_colmap(tmp_path: Path):
|
||||
)
|
||||
frames = {}
|
||||
num_frames = 10
|
||||
<<<<<<< HEAD
|
||||
qvecs = np.random.uniform(size=(num_frames, 4))
|
||||
tvecs = np.random.uniform(size=(num_frames, 3))
|
||||
# original_poses = np.concatenate(
|
||||
@@ -49,6 +77,23 @@ def test_process_images_skip_colmap(tmp_path: Path):
|
||||
# ),
|
||||
# -2,
|
||||
# )
|
||||
=======
|
||||
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,
|
||||
)
|
||||
>>>>>>> b1ddb9e695b789fae47e0f760a5c7f59f25af0bb
|
||||
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")
|
||||
@@ -61,8 +106,15 @@ def test_process_images_skip_colmap(tmp_path: Path):
|
||||
(tmp_path / "mocked_bin" / "colmap").touch(mode=0o777)
|
||||
(tmp_path / "mocked_bin" / "ffmpeg").touch(mode=0o777)
|
||||
|
||||
<<<<<<< HEAD
|
||||
# Run ProcessImages
|
||||
cmd = ProcessImages(tmp_path / "images", tmp_path / "nerfstudio", colmap_model_path=sparse_path, skip_colmap=True)
|
||||
=======
|
||||
# Convert images into a NerfStudio dataset
|
||||
cmd = ImagesToNerfstudioDataset(
|
||||
data=tmp_path / "images", output_dir=tmp_path / "nerfstudio", colmap_model_path=sparse_path, skip_colmap=True
|
||||
)
|
||||
>>>>>>> b1ddb9e695b789fae47e0f760a5c7f59f25af0bb
|
||||
cmd.main()
|
||||
os.environ["PATH"] = old_path
|
||||
|
||||
@@ -77,4 +129,16 @@ 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)
|
||||
<<<<<<< HEAD
|
||||
# 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)
|
||||
>>>>>>> b1ddb9e695b789fae47e0f760a5c7f59f25af0bb
|
||||
+7
-6
@@ -13,7 +13,7 @@ from nerfstudio.configs.method_configs import method_configs
|
||||
from nerfstudio.data.dataparsers.blender_dataparser import BlenderDataParserConfig
|
||||
from nerfstudio.data.dataparsers.minimal_dataparser import MinimalDataParserConfig
|
||||
from nerfstudio.engine.trainer import TrainerConfig
|
||||
from scripts.train import train_loop
|
||||
from nerfstudio.scripts.train import train_loop
|
||||
|
||||
BLACKLIST = [
|
||||
"base",
|
||||
@@ -21,13 +21,18 @@ BLACKLIST = [
|
||||
"instant-ngp",
|
||||
"instant-ngp-bounded",
|
||||
"nerfacto",
|
||||
"nerfacto-big",
|
||||
"volinga",
|
||||
"phototourism",
|
||||
"depth-nerfacto",
|
||||
"nerfplayer-ngp",
|
||||
"nerfplayer-nerfacto",
|
||||
"neus",
|
||||
<<<<<<< HEAD
|
||||
"dreamfusion",
|
||||
=======
|
||||
"neus-facto",
|
||||
>>>>>>> b1ddb9e695b789fae47e0f760a5c7f59f25af0bb
|
||||
]
|
||||
|
||||
|
||||
@@ -50,11 +55,7 @@ def set_reduced_config(config: TrainerConfig):
|
||||
if hasattr(config.pipeline.model, "num_importance_samples"):
|
||||
config.pipeline.model.num_importance_samples = 4
|
||||
# remove viewer
|
||||
config.viewer.enable = False
|
||||
|
||||
# model specific config settings
|
||||
if config.method_name == "instant-ngp":
|
||||
config.pipeline.model.field_implementation = "torch"
|
||||
config.viewer.quit_on_train_completion = True
|
||||
|
||||
return config
|
||||
|
||||
|
||||
@@ -168,7 +168,11 @@ def test_equall_nerfacc():
|
||||
# time2 = time.time()
|
||||
|
||||
# time3 = time.time()
|
||||
t_min_nerfacc, t_max_nerfacc = nerfacc.ray_aabb_intersect(origins, directions, aabb)
|
||||
t_min_nerfacc, t_max_nerfacc, _ = nerfacc.ray_aabb_intersect(
|
||||
origins, directions, aabb[None, :], near_plane=0, far_plane=1e10, miss_value=1e10
|
||||
)
|
||||
t_max_nerfacc = t_max_nerfacc.squeeze(-1)
|
||||
t_min_nerfacc = t_min_nerfacc.squeeze(-1)
|
||||
# time4 = time.time()
|
||||
|
||||
# print("pytorch ", time2-time1)
|
||||
|
||||
Reference in new issue
Block a user