Restructure into field modules

This commit is contained in:
Matt Tancik committed 2022-04-15 14:27:43 -07:00
1 parent 28a66dac72
commit 5e24cbf6fe
13 files changed
+21 -23

No files matched your search

@@ -0,0 +1,65 @@
"""
Encoding Tests
"""
import pytest
import torch
from mattport.nerf.field_modules 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.encode(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.encode(in_tensor)
assert encoded.shape[-1] == out_dim
assert torch.max(encoded) == 1
in_tensor = torch.zeros((2, 3, in_dim))
encoded = encoder.encode(in_tensor)
assert encoded.shape[-1] == out_dim
assert torch.min(encoded) == 0
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.encode(in_tensor)
assert encoded.shape[-1] == out_dim
@@ -0,0 +1,56 @@
"""
Render Head Tests
"""
import pytest
import torch
from torch import nn
from mattport.nerf.field_modules import render_heads
def test_render_head():
"""Test rgb render head"""
in_dim = 6
out_dim = 4
activation = nn.ReLU()
render_head = render_heads.RenderHead(in_dim=in_dim, out_dim=out_dim, activation=activation)
assert render_head.get_out_dim() == out_dim
x = torch.ones((9, in_dim))
with pytest.raises(SystemError):
render_head(x)
render_head.build_nn_modules()
y = render_head(x)
assert y.shape[-1] == out_dim
def test_density_head():
"""Test rgb render head"""
in_dim = 6
density_head = render_heads.DensityHead(in_dim)
assert density_head.get_out_dim() == 1
x = torch.ones((9, in_dim))
with pytest.raises(SystemError):
density_head(x)
density_head.build_nn_modules()
y = density_head(x)
assert y.shape[-1] == 1
def test_rgb_head():
"""Test rgb render head"""
in_dim = 6
rgb_head = render_heads.RGBHead(in_dim)
assert rgb_head.get_out_dim() == 3
x = torch.ones((9, in_dim))
with pytest.raises(SystemError):
rgb_head(x)
rgb_head.build_nn_modules()
y = rgb_head(x)
assert y.shape[-1] == 3