mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-13 19:33:46 +08:00
remove root dir logic from hydra (#587)
This commit is contained in:
1 parent
c871ff7e13
commit
5d26f3d5c4
7 files changed
+20
-85
No files matched your search
@@ -27,7 +27,7 @@ from nerfactory.cameras.cameras import Cameras, CameraType
|
||||
from nerfactory.datamanagers.dataparsers.base import DataParser, DataParserConfig
|
||||
from nerfactory.datamanagers.structs import DatasetInputs, SceneBounds
|
||||
from nerfactory.utils.colors import get_color
|
||||
from nerfactory.utils.io import get_absolute_path, load_from_json
|
||||
from nerfactory.utils.io import load_from_json
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -64,12 +64,11 @@ class Blender(DataParser):
|
||||
else:
|
||||
alpha_color_tensor = None
|
||||
|
||||
abs_dir = get_absolute_path(self.data_directory)
|
||||
meta = load_from_json(abs_dir / f"transforms_{split}.json")
|
||||
meta = load_from_json(self.data_directory / f"transforms_{split}.json")
|
||||
image_filenames = []
|
||||
poses = []
|
||||
for frame in meta["frames"]:
|
||||
fname = abs_dir / Path(frame["file_path"].replace("./", "") + ".png")
|
||||
fname = self.data_directory / Path(frame["file_path"].replace("./", "") + ".png")
|
||||
image_filenames.append(fname)
|
||||
poses.append(np.array(frame["transform_matrix"]))
|
||||
poses = np.array(poses).astype(np.float32)
|
||||
|
||||
@@ -27,7 +27,7 @@ from rich.console import Console
|
||||
from nerfactory.cameras.cameras import Cameras, CameraType
|
||||
from nerfactory.datamanagers.dataparsers.base import DataParser, DataParserConfig
|
||||
from nerfactory.datamanagers.structs import DatasetInputs, SceneBounds, Semantics
|
||||
from nerfactory.utils.io import get_absolute_path, load_from_json
|
||||
from nerfactory.utils.io import load_from_json
|
||||
|
||||
CONSOLE = Console()
|
||||
|
||||
@@ -78,9 +78,7 @@ class Friends(DataParser):
|
||||
|
||||
def _generate_dataset_inputs(self, split="train"): # pylint: disable=unused-argument,too-many-statements
|
||||
|
||||
abs_dir = get_absolute_path(self.config.data_directory)
|
||||
|
||||
cameras_json = load_from_json(abs_dir / "cameras.json")
|
||||
cameras_json = load_from_json(self.config.data_directory / "cameras.json")
|
||||
frames = cameras_json["frames"]
|
||||
bbox = torch.tensor(cameras_json["bbox"])
|
||||
|
||||
@@ -96,7 +94,7 @@ class Friends(DataParser):
|
||||
camera_to_worlds = []
|
||||
for frame in frames:
|
||||
# unpack data
|
||||
image_filename = abs_dir / images_folder / frame["image_name"]
|
||||
image_filename = self.config.data_directory / images_folder / frame["image_name"]
|
||||
intrinsics = torch.tensor(frame["intrinsics"])
|
||||
camtoworld = torch.tensor(frame["camtoworld"])[:3]
|
||||
# append data
|
||||
@@ -152,7 +150,7 @@ class Friends(DataParser):
|
||||
)
|
||||
for image_filename in image_filenames
|
||||
]
|
||||
panoptic_classes = load_from_json(abs_dir / "panoptic_classes.json")
|
||||
panoptic_classes = load_from_json(self.config.data_directory / "panoptic_classes.json")
|
||||
stuff_classes = panoptic_classes["stuff"]
|
||||
stuff_colors = torch.tensor(panoptic_classes["stuff_colors"], dtype=torch.float32) / 255.0
|
||||
thing_classes = panoptic_classes["thing"]
|
||||
|
||||
@@ -28,7 +28,7 @@ from nerfactory.cameras import utils as camera_utils
|
||||
from nerfactory.cameras.cameras import Cameras, CameraType
|
||||
from nerfactory.datamanagers.dataparsers.base import DataParser, DataParserConfig
|
||||
from nerfactory.datamanagers.structs import DatasetInputs, SceneBounds
|
||||
from nerfactory.utils.io import get_absolute_path, load_from_json
|
||||
from nerfactory.utils.io import load_from_json
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -53,14 +53,12 @@ class InstantNGP(DataParser):
|
||||
|
||||
def _generate_dataset_inputs(self, split="train"):
|
||||
|
||||
abs_dir = get_absolute_path(self.config.data_directory)
|
||||
|
||||
meta = load_from_json(abs_dir / "transforms.json")
|
||||
meta = load_from_json(self.config.data_directory / "transforms.json")
|
||||
image_filenames = []
|
||||
poses = []
|
||||
num_skipped_image_filenames = 0
|
||||
for frame in meta["frames"]:
|
||||
fname = abs_dir / Path(frame["file_path"])
|
||||
fname = self.config.data_directory / Path(frame["file_path"])
|
||||
if not fname:
|
||||
num_skipped_image_filenames += 1
|
||||
else:
|
||||
|
||||
@@ -26,7 +26,6 @@ import torch
|
||||
from nerfactory.cameras.cameras import Cameras, CameraType
|
||||
from nerfactory.datamanagers.dataparsers.base import DataParser, DataParserConfig
|
||||
from nerfactory.datamanagers.structs import DatasetInputs, SceneBounds
|
||||
from nerfactory.utils.io import get_absolute_path
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -76,11 +75,10 @@ class Mipnerf360(DataParser):
|
||||
return poses_orig
|
||||
|
||||
def _generate_dataset_inputs(self, split="train"):
|
||||
abs_dir = get_absolute_path(self.config.data_directory)
|
||||
image_dir = "images"
|
||||
if self.config.downscale_factor > 1:
|
||||
image_dir += f"_{self.config.downscale_factor}"
|
||||
image_dir = abs_dir / image_dir
|
||||
image_dir = self.config.data_directory / image_dir
|
||||
if not image_dir.exists():
|
||||
raise ValueError(f"Image directory {image_dir} doesn't exist")
|
||||
|
||||
@@ -94,7 +92,7 @@ class Mipnerf360(DataParser):
|
||||
image_filenames = sorted(image_filenames)
|
||||
num_images = len(image_filenames)
|
||||
|
||||
poses_data = np.load(abs_dir / "poses_bounds.npy")
|
||||
poses_data = np.load(self.config.data_directory / "poses_bounds.npy")
|
||||
poses = poses_data[:, :-2].reshape([-1, 3, 5]).astype(np.float32)
|
||||
bounds = poses_data[:, -2:].transpose([1, 0])
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ from nerfactory.cameras import utils as camera_utils
|
||||
from nerfactory.cameras.cameras import CAMERA_MODEL_TO_TYPE, Cameras, CameraType
|
||||
from nerfactory.datamanagers.dataparsers.base import DataParser, DataParserConfig
|
||||
from nerfactory.datamanagers.structs import DatasetInputs, SceneBounds
|
||||
from nerfactory.utils.io import get_absolute_path, load_from_json
|
||||
from nerfactory.utils.io import load_from_json
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -60,9 +60,8 @@ class Nerfactory(DataParser):
|
||||
|
||||
def _generate_dataset_inputs(self, split="train"):
|
||||
# pylint: disable=too-many-statements
|
||||
abs_dir = get_absolute_path(self.config.data_directory)
|
||||
|
||||
meta = load_from_json(abs_dir / "transforms.json")
|
||||
meta = load_from_json(self.config.data_directory / "transforms.json")
|
||||
image_filenames = []
|
||||
poses = []
|
||||
num_skipped_image_filenames = 0
|
||||
@@ -72,9 +71,9 @@ class Nerfactory(DataParser):
|
||||
else:
|
||||
filepath = Path(frame["file_path"])
|
||||
if self.config.downscale_factor > 1:
|
||||
fname = abs_dir / f"images_{self.config.downscale_factor}" / filepath.name
|
||||
fname = self.config.data_directory / f"images_{self.config.downscale_factor}" / filepath.name
|
||||
else:
|
||||
fname = abs_dir / filepath
|
||||
fname = self.config.data_directory / filepath
|
||||
if not fname:
|
||||
num_skipped_image_filenames += 1
|
||||
else:
|
||||
|
||||
@@ -28,7 +28,7 @@ from nerfactory.cameras.cameras import Cameras, CameraType
|
||||
from nerfactory.datamanagers.dataparsers.base import DataParser, DataParserConfig
|
||||
from nerfactory.datamanagers.structs import DatasetInputs, SceneBounds
|
||||
from nerfactory.utils import poses as pose_utils
|
||||
from nerfactory.utils.io import get_absolute_path, load_from_json
|
||||
from nerfactory.utils.io import load_from_json
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -57,9 +57,7 @@ class Record3D(DataParser):
|
||||
config: Record3DDataParserConfig
|
||||
|
||||
def _generate_dataset_inputs(self, split: str = "train") -> DatasetInputs:
|
||||
abs_dir = get_absolute_path(self.config.data_directory)
|
||||
|
||||
image_dir = abs_dir / "rgb"
|
||||
image_dir = self.config.data_directory / "rgb"
|
||||
|
||||
if not image_dir.exists():
|
||||
raise ValueError(f"Image directory {image_dir} doesn't exist")
|
||||
@@ -73,7 +71,7 @@ class Record3D(DataParser):
|
||||
image_filenames = np.array(image_filenames)
|
||||
num_images = len(image_filenames)
|
||||
|
||||
metadata_path = abs_dir / "metadata.json"
|
||||
metadata_path = self.config.data_directory / "metadata.json"
|
||||
metadata_dict = load_from_json(metadata_path)
|
||||
|
||||
poses_data = np.array(metadata_dict["poses"])
|
||||
|
||||
+1
-56
@@ -16,11 +16,9 @@
|
||||
Input/output utils.
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import pickle
|
||||
from pathlib import Path
|
||||
from typing import Any, Tuple
|
||||
from typing import Any
|
||||
|
||||
|
||||
def load_from_json(filename: Path):
|
||||
@@ -67,56 +65,3 @@ def write_to_pkl(filename: Path, content: Any):
|
||||
assert filename.suffix == ".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.
|
||||
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) -> Path:
|
||||
"""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 Path(project_root)
|
||||
|
||||
|
||||
def get_absolute_path(path, proj_root_func=get_project_root) -> Path:
|
||||
"""
|
||||
Returns the full, absolute path.
|
||||
Relative paths are assumed to start at the repo directory.
|
||||
"""
|
||||
str_absolute_path = str(path)
|
||||
if str_absolute_path == "" or str_absolute_path[0] == "/":
|
||||
return path
|
||||
absolute_path = proj_root_func(str_absolute_path) / path
|
||||
return absolute_path
|
||||
Reference in new issue
Block a user