4
0
mirror of https://github.com/nerfstudio-project/nerfstudio.git synced 2026-11-11 02:24:38 +08:00
nerfstudio/nerfactory/utils/io.py
Ethan Weber 0b042be956
Pipeline v2 (#258)
* 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>
2022-08-10 14:59:45 -07:00

146 lines
4.4 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.
"""
Input/output utils.
"""
import json
import logging
import os
import pickle
from typing import Any, Tuple
def load_from_json(filename: str):
"""Load a dictionary from a JSON filename.
Args:
filename: The filename to load from.
"""
assert filename.endswith(".json")
with open(filename, encoding="UTF-8") as file:
return json.load(file)
def write_to_json(filename: str, content: dict):
"""Write data to a JSON file.
Args:
filename: The filename to write to.
content: The dictionary data to write.
"""
assert filename.endswith(".json")
with open(filename, "w", encoding="UTF-8") as file:
json.dump(content, file)
def load_from_pkl(filename: str):
"""Load from a pickle file.
Args:
filename: The filename to load from.
"""
assert filename.endswith(".pkl")
with open(filename, "rb") as f:
return pickle.load(f)
def write_to_pkl(filename: str, content: Any):
"""Write to a pickle file.
Args:
filename (str): The filename to write to.
content (Any): The data to write.
"""
assert filename.endswith(".pkl")
with open(filename, "wb") as f:
pickle.dump(content, f)
def get_git_root(path: str, dirs: Tuple[str] = (".git",), default: str = "") -> str:
"""Find the root of the git repository.
'https://stackoverflow.com/questions/22081209/find-the-root-of-the-git-repository-where-the-file-lives'
Args:
path: The path to start from.
dirs: The directories to look for. Defaults to (".git",).
default: The default value to return if no git root is found.
Returns:
The root of the git repository. Returns None if not found.
"""
prev, test = None, os.path.abspath(path)
while prev != test:
if any(os.path.isdir(os.path.join(test, directory)) for directory in dirs):
return test
prev, test = test, os.path.abspath(os.path.join(test, os.pardir))
logging.info("Couldn't find git root")
return default
def get_project_root(path: str) -> str:
"""Return the project root directory from an environment variable.
# TODO: handle this better and report user error
Args:
path: The path to start from.
Returns:
The project root directory.
"""
project_root = os.getenv("PROJECT_ROOT")
if project_root is None:
logging.info(
"Please set PROJECT_ROOT to the root directory of this repo. "
"Going to try calling get_git_root(path) instead."
)
project_root = get_git_root(path)
return project_root
def get_absolute_path(path, proj_root_func=get_project_root):
"""
Returns the full, absolute path.
Relative paths are assumed to start at the repo directory.
"""
if path == "":
return ""
absolute_path = path
if absolute_path[0] != "/":
absolute_path = os.path.join(proj_root_func(path), absolute_path)
return absolute_path
def make_dir(filename_or_folder: str) -> str:
"""Make the directory for either the filename or folder.
Note that filename_or_folder currently needs to end in / for it to be recognized as a folder.
Args:
filename_or_folder (str): The filename or folder to make.
Returns:
The filename_or_folder, which is the input.
"""
if filename_or_folder[-1] != "/" and filename_or_folder.find(".") < 0:
folder = os.path.dirname(filename_or_folder + "/")
else:
folder = os.path.dirname(filename_or_folder)
if not os.path.exists(folder):
try:
os.makedirs(folder)
except Exception as e: # pylint: disable=broad-except
print(f"Couldn't create folder: {folder}. Maybe due to a parallel process?")
print(e)
return filename_or_folder