mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-07 00:09:14 +08:00
Merge pull request #16 from ethanweber/tancik/render_head_module
Update render_heads to use Module class
This commit is contained in:
4 files changed
+76
-19
No files matched your search
@@ -6,22 +6,31 @@ from typing import Optional
|
|||||||
from torch import nn
|
from torch import nn
|
||||||
from torchtyping import TensorType
|
from torchtyping import TensorType
|
||||||
|
|
||||||
|
from mattport.nerf.modules.module import Module
|
||||||
|
|
||||||
class RenderHead(nn.Module):
|
|
||||||
|
class RenderHead(Module):
|
||||||
"""Base Render head"""
|
"""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:
|
Args:
|
||||||
in_dim (int): input dimension
|
in_dim (int): input dimension
|
||||||
out_dim (int): output dimension for renderer
|
out_dim (int): output dimension for renderer
|
||||||
activation (Optional[nn.Module]): output head activation
|
activation (Optional[nn.Module]): output head activation
|
||||||
"""
|
"""
|
||||||
super().__init__()
|
super().__init__()
|
||||||
layers = [nn.Linear(in_dim, out_dim)]
|
self.in_dim = in_dim
|
||||||
if activation:
|
self.out_dim = out_dim
|
||||||
layers.append(activation)
|
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)
|
self.net = nn.Sequential(*layers)
|
||||||
|
|
||||||
def forward(self, in_tensor: TensorType[..., "in_dim"]) -> TensorType[..., "out_dim"]:
|
def forward(self, in_tensor: TensorType[..., "in_dim"]) -> TensorType[..., "out_dim"]:
|
||||||
@@ -33,19 +42,20 @@ class RenderHead(nn.Module):
|
|||||||
Returns:
|
Returns:
|
||||||
TensorType[..., "out_dim"]: Render head output
|
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)
|
return self.net(in_tensor)
|
||||||
|
|
||||||
|
|
||||||
class DensityHead(RenderHead):
|
class DensityHead(RenderHead):
|
||||||
"""Density head"""
|
"""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)
|
super().__init__(in_dim, 1, activation)
|
||||||
|
|
||||||
|
|
||||||
class RGBHead(RenderHead):
|
class RGBHead(RenderHead):
|
||||||
"""RGB head"""
|
"""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)
|
super().__init__(in_dim, 3, activation)
|
||||||
@@ -7,7 +7,7 @@ import torch
|
|||||||
from torch import nn
|
from torch import nn
|
||||||
from torchtyping import TensorType
|
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):
|
class Renderer(nn.Module):
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
import pytest
|
|
||||||
|
|
||||||
|
|
||||||
def test_positional_encoding():
|
|
||||||
assert True
|
|
||||||
|
|
||||||
|
|
||||||
def test_lower_case():
|
|
||||||
assert "MATTPORT".lower() == "mattport"
|
|
||||||
Reference in new issue
Block a user