done logic to instantiate graph, test to check instantiation

This commit is contained in:
evonneng committed 2022-04-09 05:12:14 +00:00
1 parent 91f57726ae
commit 62aee54611
6 files changed
+84 -26

No files matched your search

+5 -4
View File
@@ -1,6 +1,6 @@
{
"PositionalEncoder": {
"name": "encoder_0",
"encoder_0": {
"class": "PositionalEncoder",
"inputs": [
"x"
],
@@ -8,8 +8,9 @@
"output_dim": 8
}
},
"MLP": {
"name": "mlp_0",
"mlp_0": {
"class": "MLP",
"inputs": [
"encoder_0", "encoder_0"
],
+27 -19
View File
@@ -1,9 +1,11 @@
"""
The Graph module contains all trainable parameters.
"""
import importlib
from dataclasses import dataclass
from torch import nn
from mattport.nerf.modules.positional_encoder import PositionalEncoder
@dataclass
class GraphInputs:
@@ -21,29 +23,35 @@ class Graph(nn.Module):
def __init__(self, modules_config: dict) -> None:
super().__init__()
# # for key in ...
# temp = graph_config.positional_encoder
# PositionalEncoder(name=temp.name, name=temp.inputs, )
# self.modules = {}
# self.modules["name"] = MLP()
self.modules = {}
self.module_order = self.get_module_order(modules_config)
for module_name, module_dict in modules_config.items():
module = getattr(importlib.import_module("mattport.nerf.modules"), module_dict["class_name"])
if module_dict["class_name"] != "Encoding":
in_dim = 0
for inputs in module_dict["inputs"]:
in_dim += modules_config[inputs]["meta_data"]["out_dim"]
module_dict["meta_data"]["in_dim"] = in_dim
self.modules[module_name] = module(**module_dict["meta_data"])
def get_module_order(self, modules_config: list):
"""Generates a graph to determine the order in which the graph should execute
self.encoder_0 = Encoder()
self.mlp_0 = MLP()
Args:
modules_config (list): _description_
raise NotImplementedError
Returns:
_type_: _description_
"""
# raise NotImplementedError
return modules_config
def forward(self, x):
"""_summary_"""
"""_summary_
Args:
x (_type_): _description_
"""
# raise NotImplementedError
for name in queue: # "encoder_0",
# assert name.output is not None
for name in self.module_order:
x = self.modules[name](x)
# self.mlp_0
+2
View File
@@ -0,0 +1,2 @@
from .encoding import Encoding
from .mlp import MLP
+2 -2
View File
@@ -8,9 +8,9 @@ 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:
def __init__(self, out_dim: int) -> None:
super().__init__()
raise NotImplementedError
# raise NotImplementedError
def encode(self, in_tensor: TensorType[..., "input_dim"]) -> TensorType[..., "output_dim"]:
"""Encodes an input tensor.
+5 -1
View File
@@ -1,4 +1,7 @@
from typing import Optional
from torch import nn
from torchtyping import TensorType
class MLP(nn.Module):
@@ -10,7 +13,7 @@ class MLP(nn.Module):
out_dim: int,
num_layers: int,
layer_width: int,
activation: Optional[nn.Module] = nn.ReLU,
activation: Optional[nn.Module] = None,
out_activation: Optional[nn.Module] = None,
) -> None:
"""Initialize mulilayer perceptron.
@@ -24,6 +27,7 @@ class MLP(nn.Module):
out_activation (Optional[nn.Module], optional): output activation function. Defaults to None.
"""
super().__init__()
self.in_dim = in_dim
layers = []
for i in range(num_layers):
if i == 0:
+43
View File
@@ -0,0 +1,43 @@
import pytest
from mattport.nerf.graph import Graph
def create_basic_graph():
"""Create basic test for creating a graph
Returns:
dict: module definitions for constructing a graph
"""
basic_graph = {
"encoder_0": {"class_name": "Encoding", "inputs": ["x"], "meta_data": {"out_dim": 8}},
"mlp_0": {
"class_name": "MLP",
"inputs": ["encoder_0", "encoder_0"],
"meta_data": {"in_dim": 0, "out_dim": 1, "num_layers": 2, "layer_width": 3},
},
"mlp_1": {
"class_name": "MLP",
"inputs": ["mlp_0"],
"meta_data": {"in_dim": 4, "out_dim": 5, "num_layers": 6, "layer_width": 7},
},
}
return basic_graph
def test_graph_init():
"""test modules dictionary is instantiated properly"""
test_graph = Graph(create_basic_graph())
assert test_graph.modules is not None
assert type(test_graph.modules['encoder_0']).__name__ == 'Encoding'
assert type(test_graph.modules['mlp_0']).__name__ == 'MLP'
assert type(test_graph.modules['mlp_1']).__name__ == 'MLP'
def test_input_dimension():
"""test module input dimension calculation"""
test_graph = Graph(create_basic_graph())
assert not hasattr(test_graph.modules['encoder_0'], 'in_dim')
assert test_graph.modules['mlp_0'].in_dim == 16
assert test_graph.modules['mlp_1'].in_dim == 1