mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-11 02:24:38 +08:00
* Model class done, pipeline class still needs more visualization stuffs * Renaming the BaseDataContainer to DataloaderOutputs and adding documentation * Pushing more documentation, removing some trivial functions * Updating dict typing * Removing config dicts for collider * Passing tests * Pushing shell new dataloader * Saving work, need to fix NGP model's get_loss_dict and Pipeline * V1 of the ngp pipeline should be done. Need train script and debugging * Updating with latest instant-ngp code * Syntax updates from latest ngp graph code * Linter, removing unneeded code in new NGP graph code * Style * Should be good now. Linter is happy and the code is verbatum current instant-ngp * Everything done except novel view testing code * Fixing some nitpicks * Debugging dataloader * merged master * WOP. integrating pipeline with configs and notebook demo * removing model folder * a lot of name changing from Graph to Model * in progress... changing trainer to use pipeline... broken right now * pipeline code runs * switched to val dataset * makes run_eval.py work * updated configs for pipeline change * fixing test_run_train script * fixing black and linting * fix docs * small updates to trainer Co-authored-by: Jake Austin <jake-austin@berkeley.edu>
152 lines
4.9 KiB
Python
152 lines
4.9 KiB
Python
# Copyright 2022 The Plenoptix Team. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
"""
|
|
Miscellaneous helper code.
|
|
"""
|
|
|
|
import hashlib
|
|
import json
|
|
from math import floor, log
|
|
from pydoc import locate
|
|
from typing import Any, Callable, Dict, Union
|
|
|
|
import torch
|
|
from omegaconf import DictConfig
|
|
|
|
|
|
class DotDict(dict):
|
|
"""
|
|
dot.notation access to dictionary attributes
|
|
"""
|
|
|
|
def __getattr__(self, attr):
|
|
return self[attr]
|
|
|
|
__setattr__ = dict.__setitem__ # type: ignore
|
|
__delattr__ = dict.__delitem__ # type: ignore
|
|
|
|
|
|
def get_dict_to_torch(stuff, device: Union[torch.device, str] = "cpu"):
|
|
"""Set everything in the dict to the specified torch device."""
|
|
if isinstance(stuff, dict):
|
|
for k, v in stuff.items():
|
|
stuff[k] = get_dict_to_torch(v, device)
|
|
return stuff
|
|
if isinstance(stuff, torch.Tensor):
|
|
return stuff.to(device)
|
|
return stuff
|
|
|
|
|
|
def get_dict_to_cpu(stuff):
|
|
"""Set everything in the dict to CPU."""
|
|
if isinstance(stuff, dict):
|
|
for k, v in stuff.items():
|
|
stuff[k] = get_dict_to_cpu(v)
|
|
return stuff
|
|
if isinstance(stuff, torch.Tensor):
|
|
return stuff.detach().cpu()
|
|
return stuff
|
|
|
|
|
|
def is_not_none(var):
|
|
"""Return True if the variable var is None."""
|
|
return not isinstance(var, type(None))
|
|
|
|
|
|
def get_masked_dict(d, mask):
|
|
"""Return a masked dictionary.
|
|
TODO(ethan): add more asserts/checks so this doesn't have unpredictable behavior."""
|
|
masked_dict = {}
|
|
for key, value in d.items():
|
|
masked_dict[key] = value[mask]
|
|
return masked_dict
|
|
|
|
|
|
def instantiate_from_dict_config(dict_config: Any, **kwargs):
|
|
"""Our version of hydra's instantiate function.
|
|
|
|
Args:
|
|
dict_config: DictConfig object to instantiate. It can be a dataclass or a dict but must have a `_target_` field.
|
|
"""
|
|
dict_config_kwargs = {k: v for k, v in dict_config.items() if k != "_target_"}
|
|
uninstantiated_class = locate(dict_config._target_) # pylint: disable=protected-access
|
|
assert (
|
|
uninstantiated_class is not None
|
|
), f"Could not find class {dict_config._target_}" # pylint: disable=protected-access
|
|
all_kwargs = dict_config_kwargs
|
|
all_kwargs.update(kwargs)
|
|
instantiated_class = uninstantiated_class(**all_kwargs) # type: ignore
|
|
return instantiated_class
|
|
|
|
|
|
def get_hash_str_from_dict(dictionary: Dict[str, Any]) -> str:
|
|
"""MD5 hash of a dictionary. Based on
|
|
https://www.doc.ic.ac.uk/~nuric/coding/how-to-hash-a-dictionary-in-python.html"""
|
|
dhash = hashlib.md5()
|
|
encoded = json.dumps(dictionary, sort_keys=True).encode()
|
|
dhash.update(encoded)
|
|
return dhash.hexdigest()
|
|
|
|
|
|
class IterableWrapper: # pylint: disable=too-few-public-methods
|
|
"""A helper that will allow an instance of a class to return multiple kinds of iterables bound
|
|
to different functions of that class.
|
|
|
|
To use this, pass in the instance of the class you want to have multiple kinds of iterables for,
|
|
and pass in a method belonging to that class that you want to be the __next__() method of the
|
|
iterable. The resulting instantiated object will be an iterable that will use the passed in
|
|
class method as the __next__() method.
|
|
|
|
Args:
|
|
instance (object): instance class we are wrapping
|
|
new_next (callable): function that will be called instead as the __next__()
|
|
|
|
|
|
Attributes:
|
|
instance (object): instance class we are wrapping
|
|
new_next (callable): function that will be called instead as the __next__()
|
|
"""
|
|
|
|
def __init__(self, instance: object, new_next: Callable):
|
|
self.instance = instance
|
|
self.new_next = new_next
|
|
|
|
def __next__(self):
|
|
return self.new_next(self.instance)
|
|
|
|
|
|
def human_format(num):
|
|
"""Format a number in a more human readable way"""
|
|
units = ["", "K", "M", "B", "T", "P"]
|
|
k = 1000.0
|
|
magnitude = int(floor(log(num, k)))
|
|
return f"{(num / k**magnitude):.2f} {units[magnitude]}"
|
|
|
|
|
|
def scale_dict(dictionary: Dict[Any, Any], coefficients: DictConfig) -> Dict[Any, Any]:
|
|
"""Scale a dictionary in-place given a coefficients dictionary.
|
|
|
|
Args:
|
|
dictionary: input dict to be scaled.
|
|
coefficients: scalar dict config for holding coefficients.
|
|
|
|
Returns:
|
|
Input dict scaled by coefficients.
|
|
"""
|
|
for key in dictionary:
|
|
if key in coefficients:
|
|
dictionary[key] *= coefficients[key]
|
|
return dictionary
|