mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-05 15:28:49 +08:00
updated logic for calculating inputs dynamically; untested code
This commit is contained in:
1 parent
1a0273671c
commit
f89bb8f7be
1 file changed
+40
-16
+40
-16
@@ -21,7 +21,9 @@ class Node:
|
|||||||
|
|
||||||
name: str
|
name: str
|
||||||
children: dict
|
children: dict
|
||||||
visited: Optional[bool] = False
|
parents: dict
|
||||||
|
visited_order: Optional[bool] = False
|
||||||
|
visited_in_dim: Optional[bool] = False
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash(self.name)
|
return hash(self.name)
|
||||||
@@ -33,18 +35,39 @@ class Graph(nn.Module):
|
|||||||
def __init__(self, modules_config: dict) -> None:
|
def __init__(self, modules_config: dict) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.modules_config = modules_config
|
self.modules_config = modules_config
|
||||||
# calculate input dimensions based on module dependencies
|
# create graph and get ordering
|
||||||
self.modules = {}
|
self.roots = self.construct_graph()
|
||||||
|
self.module_order = self.get_module_order()
|
||||||
|
|
||||||
|
# initialize graph with known input dimensions; set default in_dim to 0
|
||||||
for module_name, module_dict in modules_config.items():
|
for module_name, module_dict in modules_config.items():
|
||||||
module = getattr(importlib.import_module("mattport.nerf.modules"), module_dict["class_name"])
|
module = getattr(importlib.import_module("mattport.nerf.modules"), module_dict["class_name"])
|
||||||
if module_dict["class_name"] != "Encoding":
|
if "in_dim" not in module_dict["meta_data"]:
|
||||||
in_dim = 0
|
module_dict["meta_data"]["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"])
|
self.modules[module_name] = module(**module_dict["meta_data"])
|
||||||
# generate dependency ordering for module calls
|
|
||||||
self.module_order = self.get_module_order()
|
# calculate input dimensions based on module dependencies
|
||||||
|
for root in self.roots:
|
||||||
|
self.set_in_dim(root)
|
||||||
|
|
||||||
|
def set_in_dim(self, curr_node: "Node") -> None:
|
||||||
|
"""Dynamically calculates and sets the input dimensions of the modules based on dependency graph
|
||||||
|
|
||||||
|
Args:
|
||||||
|
curr_node (Node): pointer to current node in process
|
||||||
|
"""
|
||||||
|
curr_node.visited_in_dim = True
|
||||||
|
|
||||||
|
if len(curr_node.parents) > 0:
|
||||||
|
in_dim = 0
|
||||||
|
for parent_name in curr_node.parents.keys():
|
||||||
|
in_dim += self.modules[parent_name].get_out_dim()
|
||||||
|
self.modules[curr_node.name].set_in_dim()
|
||||||
|
self.modules_config[curr_node.name]["meta_data"]["in_dim"] = in_dim
|
||||||
|
|
||||||
|
for child_node in curr_node.children.values():
|
||||||
|
if not child_node.visited_order:
|
||||||
|
self.set_in_dim(child_node)
|
||||||
|
|
||||||
def construct_graph(self) -> dict:
|
def construct_graph(self) -> dict:
|
||||||
"""Constructs a dependency graph given the module configuration
|
"""Constructs a dependency graph given the module configuration
|
||||||
@@ -59,17 +82,18 @@ class Graph(nn.Module):
|
|||||||
roots = set()
|
roots = set()
|
||||||
for module_name, module_dict in self.modules_config.items():
|
for module_name, module_dict in self.modules_config.items():
|
||||||
if not module_name in processed_modules:
|
if not module_name in processed_modules:
|
||||||
curr_module = Node(name=module_name, children={})
|
curr_module = Node(name=module_name, children={}, parents={})
|
||||||
processed_modules[module_name] = curr_module
|
processed_modules[module_name] = curr_module
|
||||||
else:
|
else:
|
||||||
curr_module = processed_modules[module_name]
|
curr_module = processed_modules[module_name]
|
||||||
inputs = module_dict["inputs"]
|
inputs = module_dict["inputs"]
|
||||||
for input_module in inputs:
|
for input_module in inputs:
|
||||||
if not input_module in processed_modules:
|
if not input_module in processed_modules:
|
||||||
parent_module = Node(name=input_module, children={module_name: curr_module})
|
parent_module = Node(name=input_module, children={module_name: curr_module}, parents={})
|
||||||
processed_modules[input_module] = parent_module
|
processed_modules[input_module] = parent_module
|
||||||
else:
|
else:
|
||||||
processed_modules[input_module].children[module_name] = curr_module
|
processed_modules[input_module].children[module_name] = curr_module
|
||||||
|
curr_module.parents[input_module] = parent_module
|
||||||
if input_module == "x":
|
if input_module == "x":
|
||||||
roots.add(curr_module)
|
roots.add(curr_module)
|
||||||
return roots
|
return roots
|
||||||
@@ -81,9 +105,9 @@ class Graph(nn.Module):
|
|||||||
curr_node (Node): pointer to current node in process
|
curr_node (Node): pointer to current node in process
|
||||||
ordering_stack (list): cumulative ordering of graph nodes
|
ordering_stack (list): cumulative ordering of graph nodes
|
||||||
"""
|
"""
|
||||||
curr_node.visited = True
|
curr_node.visited_order = True
|
||||||
for child_node in curr_node.children.values():
|
for child_node in curr_node.children.values():
|
||||||
if not child_node.visited:
|
if not child_node.visited_order:
|
||||||
self.topological_sort(child_node, ordering_stack)
|
self.topological_sort(child_node, ordering_stack)
|
||||||
ordering_stack.append(curr_node.name)
|
ordering_stack.append(curr_node.name)
|
||||||
|
|
||||||
@@ -96,10 +120,10 @@ class Graph(nn.Module):
|
|||||||
Returns:
|
Returns:
|
||||||
list: ordering of the module names that should be executed
|
list: ordering of the module names that should be executed
|
||||||
"""
|
"""
|
||||||
roots = self.construct_graph()
|
roots = self.roots
|
||||||
ordering_stack = []
|
ordering_stack = []
|
||||||
for root in roots:
|
for root in roots:
|
||||||
if not root.visited:
|
if not root.visited_order:
|
||||||
self.topological_sort(root, ordering_stack)
|
self.topological_sort(root, ordering_stack)
|
||||||
|
|
||||||
return ordering_stack[::-1]
|
return ordering_stack[::-1]
|
||||||
|
|||||||
Reference in new issue
Block a user