Files
nerfstudio/nerfstudio/configs/base_config.py
T

179 lines
6.9 KiB
Python

# Copyright 2022 The Nerfstudio 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.
"""Base Configs"""
# pylint: disable=wrong-import-position
from __future__ import annotations
import warnings
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, ForwardRef, Generic, List, Optional, Tuple, Type
from typing_extensions import Literal, TypeVar
# model instances
from nerfstudio.utils import writer
warnings.filterwarnings("ignore", module="torchvision")
# Pretty printing class
class PrintableConfig: # pylint: disable=too-few-public-methods
"""Printable Config defining str function"""
def __str__(self):
lines = [self.__class__.__name__ + ":"]
for key, val in vars(self).items():
if isinstance(val, Tuple):
flattened_val = "["
for item in val:
flattened_val += str(item) + "\n"
flattened_val = flattened_val.rstrip("\n")
val = flattened_val + "]"
lines += f"{key}: {str(val)}".split("\n")
return "\n ".join(lines)
T = TypeVar("T")
# Base instantiate configs
@dataclass
class InstantiateConfig(PrintableConfig, Generic[T]): # pylint: disable=too-few-public-methods
"""Config class for instantiating an the class specified in the _target attribute."""
_target: Optional[Type] = field(repr=False, default=None)
def __post_init__(self):
if self._target is None:
for base in getattr(self, "__orig_bases__", []):
if hasattr(base, "__origin__") and issubclass(base.__origin__, InstantiateConfig):
value = base.__args__[0]
if isinstance(value, ForwardRef):
if value.__forward_evaluated__:
value = value.__forward_value__
elif value.__forward_module__ is None:
value.__forward_module__ = type(self).__module__
value = getattr(value, "_evaluate")(None, None, set())
self._target = value
break
else:
raise RuntimeError(
f"Could not find correct type to instantiate for {type(self)}. "
"Make sure to inherit InstantiateConfig[T], where T is the type to instantiate."
)
def setup(self, **kwargs) -> T:
"""Returns the instantiated object using the config."""
assert self._target is not None
return self._target(self, **kwargs)
# Machine related configs
@dataclass
class MachineConfig(PrintableConfig):
"""Configuration of machine setup"""
seed: int = 42
"""random seed initialization"""
num_gpus: int = 1
"""total number of gpus available for train/eval"""
num_machines: int = 1
"""total number of distributed machines available (for DDP)"""
machine_rank: int = 0
"""current machine's rank (for DDP)"""
dist_url: str = "auto"
"""distributed connection point (for DDP)"""
@dataclass
class LocalWriterConfig(InstantiateConfig[writer.LocalWriter]):
"""Local Writer config"""
enable: bool = False
"""if True enables local logging, else disables"""
stats_to_track: Tuple[writer.EventName, ...] = (
writer.EventName.ITER_TRAIN_TIME,
writer.EventName.TRAIN_RAYS_PER_SEC,
writer.EventName.CURR_TEST_PSNR,
writer.EventName.VIS_RAYS_PER_SEC,
writer.EventName.TEST_RAYS_PER_SEC,
writer.EventName.ETA,
)
"""specifies which stats will be logged/printed to terminal"""
max_log_size: int = 10
"""maximum number of rows to print before wrapping. if 0, will print everything."""
def setup(self, banner_messages: Optional[List[str]] = None, **kwargs) -> Any:
"""Instantiate local writer
Args:
banner_messages: List of strings that always print at the bottom of screen.
"""
assert self._target is not None
return self._target(self, banner_messages=banner_messages, **kwargs)
@dataclass
class LoggingConfig(PrintableConfig):
"""Configuration of loggers and profilers"""
relative_log_dir: Path = Path("./")
"""relative path to save all logged events"""
steps_per_log: int = 10
"""number of steps between logging stats"""
max_buffer_size: int = 20
"""maximum history size to keep for computing running averages of stats.
e.g. if 20, averages will be computed over past 20 occurrences."""
local_writer: LocalWriterConfig = LocalWriterConfig(enable=True)
"""if provided, will print stats locally. if None, will disable printing"""
enable_profiler: bool = True
"""whether to enable profiling code; prints speed of functions at the end of a program.
profiler logs run times of functions and prints at end of training"""
# Viewer related configs
@dataclass
class ViewerConfig(PrintableConfig):
"""Configuration for viewer instantiation"""
relative_log_filename: str = "viewer_log_filename.txt"
"""Filename to use for the log file."""
start_train: bool = True
"""whether to immediately start training upon loading viewer
if False, will just visualize dataset but you can toggle training in viewer"""
zmq_port: Optional[int] = None
"""The zmq port to connect to for communication. If None, find an available port."""
launch_bridge_server: bool = True
"""whether or not to launch the bridge server"""
websocket_port: Optional[int] = 7007
"""the default websocket port to connect to"""
ip_address: str = "127.0.0.1"
"""the ip address where the bridge server is running"""
num_rays_per_chunk: int = 32768
"""number of rays per chunk to render with viewer"""
max_num_display_images: int = 512
"""Maximum number of training images to display in the viewer, to avoid lag. This does not change which images are
actually used in training/evaluation. If -1, display all."""
quit_on_train_completion: bool = False
"""Whether to kill the training job when it has completed. Note this will stop rendering in the viewer."""
skip_openrelay: bool = False
"""Avoid using openrelay to communicate with the viewer. Try disabling if you have trouble
connecting to the viewer"""
codec: Literal["H264", "VP8"] = "VP8"
"""Video codec that viewer will use."""
local: bool = False
"""If running local server instance, avoid using relays to communicate with the viewer."""