mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-01 21:39:06 +08:00
some initial module structuring
This commit is contained in:
1 parent
8a53684131
commit
91f57726ae
7 files changed
+67
-18
No files matched your search
@@ -0,0 +1,64 @@
|
||||
"""
|
||||
Encoding functions
|
||||
"""
|
||||
from torch import nn
|
||||
from torchtyping import TensorType
|
||||
|
||||
|
||||
class Encoding(nn.Module):
|
||||
"""Encode an input tensor. Intended to be subclassed"""
|
||||
|
||||
def __init__(self, name: str, inputs: list, meta_data: dict) -> None:
|
||||
super().__init__()
|
||||
raise NotImplementedError
|
||||
|
||||
def encode(self, in_tensor: TensorType[..., "input_dim"]) -> TensorType[..., "output_dim"]:
|
||||
"""Encodes an input tensor.
|
||||
|
||||
Args:
|
||||
x (TensorType[..., "input_dim"]): Input tensor to be encoded
|
||||
|
||||
Returns:
|
||||
TensorType[..., "output_dim"]: A encoded input tensor
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def forward(self, in_tensor: TensorType[..., "input_dim"]) -> TensorType[..., "output_dim"]:
|
||||
"""Call forward"""
|
||||
return self.encode(in_tensor)
|
||||
|
||||
|
||||
class RFFEncoding(Encoding):
|
||||
"""Random Fourier Feature encoding"""
|
||||
|
||||
def encode(self, in_tensor: TensorType[..., "input_dim"]) -> TensorType[..., "output_dim"]:
|
||||
"""Encodes an input tensor.
|
||||
|
||||
Args:
|
||||
x (TensorType[..., "input_dim"]): Input tensor to be encoded
|
||||
|
||||
Returns:
|
||||
TensorType[..., "output_dim"]: A encoded input tensor
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class HashEncoding(Encoding):
|
||||
"""Hash encoding"""
|
||||
|
||||
def encode(self, in_tensor: TensorType[..., "input_dim"]) -> TensorType[..., "output_dim"]:
|
||||
"""Encodes an input tensor.
|
||||
|
||||
Args:
|
||||
x (TensorType[..., "input_dim"]): Input tensor to be encoded
|
||||
|
||||
Returns:
|
||||
TensorType[..., "output_dim"]: A encoded input tensor
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
encodings_dict = {
|
||||
"RFF": RFFEncoding,
|
||||
"hash": HashEncoding,
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
from torch import nn
|
||||
|
||||
|
||||
class MLP(nn.Module):
|
||||
"""Multilayer perceptron"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
in_dim: int,
|
||||
out_dim: int,
|
||||
num_layers: int,
|
||||
layer_width: int,
|
||||
activation: Optional[nn.Module] = nn.ReLU,
|
||||
out_activation: Optional[nn.Module] = None,
|
||||
) -> None:
|
||||
"""Initialize mulilayer perceptron.
|
||||
|
||||
Args:
|
||||
in_dim (int): Input layer dimension
|
||||
out_dim (int): Ouput layer dimension
|
||||
num_layers (int): Number of network layers
|
||||
layer_width (int): Width of each MLP layer
|
||||
activation (Optional[nn.Module], optional): intermediate layer activation function. Defaults to nn.ReLU.
|
||||
out_activation (Optional[nn.Module], optional): output activation function. Defaults to None.
|
||||
"""
|
||||
super().__init__()
|
||||
layers = []
|
||||
for i in range(num_layers):
|
||||
if i == 0:
|
||||
layers.append(nn.Linear(in_dim, layer_width))
|
||||
else:
|
||||
layers.append(nn.Linear(layer_width, layer_width))
|
||||
if activation is not None:
|
||||
layers.append(activation)
|
||||
layers.append(nn.Linear(layer_width, out_dim))
|
||||
if out_activation is not None:
|
||||
layers.append(out_activation)
|
||||
|
||||
self.net = nn.Sequential(*layers)
|
||||
|
||||
def forward(self, in_tensor: TensorType[..., "in_dim"]) -> TensorType[..., "out_dim"]:
|
||||
"""Process input with a multilayer perceptron.
|
||||
|
||||
Args:
|
||||
in_tensor (TensorType[..., "in_dim]): Network input
|
||||
|
||||
Returns:
|
||||
TensorType[..., "out_dim"]: Network output
|
||||
"""
|
||||
|
||||
return self.net(in_tensor)
|
||||
Reference in new issue
Block a user