diff --git a/mattport/nerf/render_heads.py b/mattport/nerf/modules/render_heads.py similarity index 65% rename from mattport/nerf/render_heads.py rename to mattport/nerf/modules/render_heads.py index be509d80b..1c2b5f114 100644 --- a/mattport/nerf/render_heads.py +++ b/mattport/nerf/modules/render_heads.py @@ -6,22 +6,31 @@ from typing import Optional from torch import nn from torchtyping import TensorType +from mattport.nerf.modules.module import Module -class RenderHead(nn.Module): + +class RenderHead(Module): """Base Render head""" - def __init__(self, in_dim: int, out_dim: int, activation: Optional[nn.Module]) -> None: + def __init__(self, in_dim: int, out_dim: int, activation: Optional[nn.Module] = None) -> None: """ - Args: in_dim (int): input dimension out_dim (int): output dimension for renderer activation (Optional[nn.Module]): output head activation """ super().__init__() - layers = [nn.Linear(in_dim, out_dim)] - if activation: - layers.append(activation) + self.in_dim = in_dim + self.out_dim = out_dim + self.activation = activation + + self.net = None + + def build_nn_modules(self) -> None: + layers = [nn.Linear(self.in_dim, self.out_dim)] + if self.activation: + layers.append(self.activation) + print(layers) self.net = nn.Sequential(*layers) def forward(self, in_tensor: TensorType[..., "in_dim"]) -> TensorType[..., "out_dim"]: @@ -33,19 +42,20 @@ class RenderHead(nn.Module): Returns: TensorType[..., "out_dim"]: Render head output """ - + if not self.net: + raise SystemError("Render head network not initialized. build_nn_modules() should be called.") return self.net(in_tensor) class DensityHead(RenderHead): """Density head""" - def __init__(self, in_dim: int, activation: Optional[nn.Module] = nn.Softplus) -> None: + def __init__(self, in_dim: int, activation: Optional[nn.Module] = nn.Softplus()) -> None: super().__init__(in_dim, 1, activation) class RGBHead(RenderHead): """RGB head""" - def __init__(self, in_dim: int, activation: Optional[nn.Module] = nn.Sigmoid) -> None: + def __init__(self, in_dim: int, activation: Optional[nn.Module] = nn.Sigmoid()) -> None: super().__init__(in_dim, 3, activation) diff --git a/mattport/nerf/renderers.py b/mattport/nerf/renderers.py index e7b3c4a46..fa9f83ee3 100644 --- a/mattport/nerf/renderers.py +++ b/mattport/nerf/renderers.py @@ -7,7 +7,7 @@ import torch from torch import nn from torchtyping import TensorType -from mattport.nerf import render_heads as rh +from mattport.nerf.modules import render_heads as rh class Renderer(nn.Module): diff --git a/tests/nerf/modules/test_render_heads.py b/tests/nerf/modules/test_render_heads.py new file mode 100644 index 000000000..b22c48499 --- /dev/null +++ b/tests/nerf/modules/test_render_heads.py @@ -0,0 +1,56 @@ +""" +Render Head Tests +""" +import pytest +import torch +from torch import nn + +from mattport.nerf.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 diff --git a/tests/nerf/test_encoding.py b/tests/nerf/test_encoding.py deleted file mode 100644 index 99f26096c..000000000 --- a/tests/nerf/test_encoding.py +++ /dev/null @@ -1,9 +0,0 @@ -import pytest - - -def test_positional_encoding(): - assert True - - -def test_lower_case(): - assert "MATTPORT".lower() == "mattport"