mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-25 02:19:47 +08:00
Restructure tests (#611)
This commit is contained in:
1 parent
afda888591
commit
5f2790246d
7 files changed
No files matched your search
@@ -0,0 +1,14 @@
|
||||
"""
|
||||
Embedding tests
|
||||
"""
|
||||
from nerfstudio.field_components.embedding import Embedding
|
||||
|
||||
|
||||
def test_indexing():
|
||||
"""Test embedding indexing"""
|
||||
in_dim = 100
|
||||
out_dim = 64
|
||||
|
||||
embedding = Embedding(in_dim, out_dim)
|
||||
assert embedding
|
||||
# TODO
|
||||
@@ -0,0 +1,181 @@
|
||||
"""
|
||||
Encoding Tests
|
||||
"""
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from nerfstudio.field_components import encoding
|
||||
|
||||
|
||||
def test_scaling_and_offset():
|
||||
"""Test scaling and offset encoder"""
|
||||
in_dim = 4
|
||||
in_tensor = torch.ones((2, 3, in_dim))
|
||||
|
||||
scaling = 2.0
|
||||
offset = 4.5
|
||||
encoder = encoding.ScalingAndOffset(in_dim=in_dim, scaling=scaling, offset=offset)
|
||||
|
||||
assert encoder.get_out_dim() == in_dim
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape[-1] == in_dim
|
||||
assert in_tensor * 6.5 == pytest.approx(encoded)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
encoding.ScalingAndOffset(in_dim=-1)
|
||||
|
||||
|
||||
def test_nerf_encoder():
|
||||
"""Test NeRF encoder"""
|
||||
in_dim = 4
|
||||
out_dim = 24
|
||||
in_tensor = torch.ones((2, 3, in_dim))
|
||||
|
||||
num_frequencies = 3
|
||||
min_freq_exp = 0
|
||||
max_freq_exp = 3
|
||||
encoder = encoding.NeRFEncoding(
|
||||
in_dim=in_dim, num_frequencies=num_frequencies, min_freq_exp=min_freq_exp, max_freq_exp=max_freq_exp
|
||||
)
|
||||
assert encoder.get_out_dim() == out_dim
|
||||
|
||||
in_tensor = torch.ones((2, 3, in_dim))
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape[-1] == out_dim
|
||||
assert torch.max(encoded) == 1
|
||||
|
||||
in_tensor = torch.zeros((2, 3, in_dim))
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape[-1] == out_dim
|
||||
assert torch.min(encoded) == 0
|
||||
|
||||
# Test integrated posenc
|
||||
covs = torch.ones((2, 3, in_dim, in_dim))
|
||||
encoded = encoder(in_tensor, covs=covs)
|
||||
|
||||
|
||||
def test_rff_encoder():
|
||||
"""Test RFF encoder"""
|
||||
in_dim = 3
|
||||
out_dim = 24
|
||||
in_tensor = torch.ones((2, 3, in_dim))
|
||||
|
||||
num_frequencies = 12
|
||||
scale = 5
|
||||
encoder = encoding.RFFEncoding(in_dim=in_dim, num_frequencies=num_frequencies, scale=scale)
|
||||
assert encoder.get_out_dim() == out_dim
|
||||
|
||||
in_tensor = torch.ones((2, 3, in_dim))
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape[-1] == out_dim
|
||||
|
||||
# Test integrated encoding
|
||||
covs = torch.ones((2, 3, in_dim, in_dim))
|
||||
encoded = encoder(in_tensor, covs=covs)
|
||||
|
||||
|
||||
def test_tensor_vm_encoder():
|
||||
"""Test TensorVM encoder"""
|
||||
|
||||
num_components = 24
|
||||
resolution = 32
|
||||
|
||||
in_dim = 3
|
||||
out_dim = 3 * num_components
|
||||
|
||||
encoder = encoding.TensorVMEncoding(num_components=num_components, resolution=resolution)
|
||||
assert encoder.get_out_dim() == out_dim
|
||||
|
||||
in_tensor = torch.ones((3, in_dim))
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape == (3, out_dim)
|
||||
|
||||
in_tensor = torch.ones((6, 3, in_dim))
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape == (6, 3, out_dim)
|
||||
|
||||
encoder.upsample_grid(resolution=64)
|
||||
encoder.upsample_grid(resolution=128)
|
||||
|
||||
|
||||
def test_tensor_cp_encoder():
|
||||
"""Test TensorCP encoder"""
|
||||
|
||||
num_components = 24
|
||||
resolution = 32
|
||||
|
||||
in_dim = 3
|
||||
out_dim = num_components
|
||||
|
||||
encoder = encoding.TensorCPEncoding(num_components=num_components, resolution=resolution)
|
||||
assert encoder.get_out_dim() == out_dim
|
||||
|
||||
in_tensor = torch.ones((3, in_dim))
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape == (3, out_dim)
|
||||
|
||||
in_tensor = torch.ones((6, 3, in_dim))
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape == (6, 3, out_dim)
|
||||
|
||||
encoder.upsample_grid(resolution=64)
|
||||
|
||||
|
||||
def test_tensor_sh_encoder():
|
||||
"""Test Spherical Harmonic encoder"""
|
||||
|
||||
levels = 4
|
||||
out_dim = levels**2
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
encoder = encoding.SHEncoding(levels=5)
|
||||
|
||||
encoder = encoding.SHEncoding(levels=levels)
|
||||
assert encoder.get_out_dim() == out_dim
|
||||
|
||||
in_tensor = torch.zeros((10, 3))
|
||||
in_tensor[..., 1] = 1
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape == (10, out_dim)
|
||||
|
||||
|
||||
def test_tensor_hash_encoder():
|
||||
"""Test Spherical Harmonic encoder"""
|
||||
|
||||
num_levels = 4
|
||||
features_per_level = 4
|
||||
out_dim = num_levels * features_per_level
|
||||
|
||||
encoder = encoding.HashEncoding(
|
||||
num_levels=num_levels,
|
||||
features_per_level=features_per_level,
|
||||
log2_hashmap_size=5,
|
||||
implementation="torch",
|
||||
)
|
||||
assert encoder.get_out_dim() == out_dim
|
||||
|
||||
in_tensor = torch.zeros((10, 3))
|
||||
in_tensor[..., 1] = 1
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape == (10, out_dim)
|
||||
|
||||
encoder = encoding.HashEncoding(
|
||||
num_levels=num_levels,
|
||||
features_per_level=features_per_level,
|
||||
log2_hashmap_size=5,
|
||||
implementation="tcnn",
|
||||
)
|
||||
in_tensor = torch.zeros((10, 3))
|
||||
in_tensor[..., 1] = 1
|
||||
encoded = encoder(in_tensor)
|
||||
assert encoded.shape == (10, out_dim)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_scaling_and_offset()
|
||||
test_nerf_encoder()
|
||||
test_rff_encoder()
|
||||
test_tensor_vm_encoder()
|
||||
test_tensor_cp_encoder()
|
||||
test_tensor_sh_encoder()
|
||||
test_tensor_hash_encoder()
|
||||
@@ -0,0 +1,72 @@
|
||||
"""
|
||||
Field output tests
|
||||
"""
|
||||
import pytest
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from nerfstudio.field_components.field_heads import (
|
||||
DensityFieldHead,
|
||||
FieldHead,
|
||||
FieldHeadNames,
|
||||
RGBFieldHead,
|
||||
SHFieldHead,
|
||||
)
|
||||
|
||||
|
||||
def test_field_output():
|
||||
"""Test render output"""
|
||||
in_dim = 6
|
||||
out_dim = 4
|
||||
activation = nn.ReLU()
|
||||
render_head = FieldHead(in_dim=in_dim, out_dim=out_dim, field_head_name=FieldHeadNames.RGB, activation=activation)
|
||||
assert render_head.get_out_dim() == out_dim
|
||||
|
||||
x = torch.ones((9, in_dim))
|
||||
render_head(x)
|
||||
|
||||
# Test in_dim not provided at construction
|
||||
render_head = FieldHead(out_dim=out_dim, field_head_name=FieldHeadNames.RGB, activation=activation)
|
||||
with pytest.raises(SystemError):
|
||||
render_head(x)
|
||||
render_head.set_in_dim(in_dim)
|
||||
render_head(x)
|
||||
|
||||
|
||||
def test_density_output():
|
||||
"""Test rgb output"""
|
||||
in_dim = 6
|
||||
density_head = DensityFieldHead(in_dim)
|
||||
assert density_head.get_out_dim() == 1
|
||||
|
||||
x = torch.ones((9, in_dim))
|
||||
density_head(x)
|
||||
|
||||
|
||||
def test_rgb_output():
|
||||
"""Test rgb output"""
|
||||
in_dim = 6
|
||||
rgb_head = RGBFieldHead(in_dim)
|
||||
assert rgb_head.get_out_dim() == 3
|
||||
|
||||
x = torch.ones((9, in_dim))
|
||||
rgb_head(x)
|
||||
|
||||
|
||||
def test_sh_output():
|
||||
"""Test sh output"""
|
||||
in_dim = 6
|
||||
levels = 4
|
||||
channels = 3
|
||||
rgb_head = SHFieldHead(in_dim, levels=levels, channels=channels)
|
||||
assert rgb_head.get_out_dim() == channels * levels**2
|
||||
|
||||
x = torch.ones((9, in_dim))
|
||||
rgb_head(x)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_field_output()
|
||||
test_density_output()
|
||||
test_rgb_output()
|
||||
test_sh_output()
|
||||
@@ -0,0 +1,42 @@
|
||||
"""
|
||||
Test the fields
|
||||
"""
|
||||
import torch
|
||||
|
||||
from nerfstudio.cameras.rays import Frustums, RaySamples
|
||||
from nerfstudio.fields.instant_ngp_field import TCNNInstantNGPField
|
||||
|
||||
|
||||
def test_tcnn_instant_ngp_field():
|
||||
"""Test the tiny-cuda-nn field"""
|
||||
# pylint: disable=import-outside-toplevel
|
||||
# pylint: disable=unused-import
|
||||
try:
|
||||
import tinycudann as tcnn
|
||||
except ImportError as e:
|
||||
# tinycudann module doesn't exist
|
||||
print(e)
|
||||
return
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
aabb_scale = 1.0
|
||||
aabb = torch.tensor(
|
||||
[[-aabb_scale, -aabb_scale, -aabb_scale], [aabb_scale, aabb_scale, aabb_scale]], dtype=torch.float32
|
||||
).to(device)
|
||||
field = TCNNInstantNGPField(aabb)
|
||||
num_rays = 1024
|
||||
num_samples = 256
|
||||
positions = torch.rand((num_rays, num_samples, 3), dtype=torch.float32, device=device)
|
||||
directions = torch.rand_like(positions)
|
||||
frustums = Frustums(
|
||||
origins=positions,
|
||||
directions=directions,
|
||||
starts=torch.zeros((*directions.shape[:-1], 1), device=device),
|
||||
ends=torch.zeros((*directions.shape[:-1], 1), device=device),
|
||||
pixel_area=torch.ones((*directions.shape[:-1], 1), device=device),
|
||||
)
|
||||
ray_samples = RaySamples(frustums=frustums)
|
||||
field.forward(ray_samples)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_tcnn_instant_ngp_field()
|
||||
@@ -0,0 +1,31 @@
|
||||
"""
|
||||
MLP Test
|
||||
"""
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from nerfstudio.field_components import MLP
|
||||
|
||||
|
||||
def test_mlp():
|
||||
"""Test mlp"""
|
||||
in_dim = 6
|
||||
out_dim = 10
|
||||
num_layers = 2
|
||||
layer_width = 32
|
||||
out_activation = nn.ReLU()
|
||||
mlp = MLP(
|
||||
in_dim=in_dim, out_dim=out_dim, num_layers=num_layers, layer_width=layer_width, out_activation=out_activation
|
||||
)
|
||||
assert mlp.get_out_dim() == out_dim
|
||||
|
||||
x = torch.ones((9, in_dim))
|
||||
|
||||
mlp.build_nn_modules()
|
||||
y = mlp(x)
|
||||
|
||||
assert y.shape[-1] == out_dim
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_mlp()
|
||||
Reference in new issue
Block a user