mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-29 04:20:10 +08:00
1 parent
62f492f1bc
commit
e13d401b16
97 files changed
+235712
-235721
No files matched your search
@@ -0,0 +1,165 @@
|
||||
# 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.
|
||||
""" Data parser for nerfstudio datasets. """
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import math
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path, PureWindowsPath
|
||||
from typing import Literal, Type
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
from nerfstudio.cameras import camera_utils
|
||||
from nerfstudio.cameras.cameras import CAMERA_MODEL_TO_TYPE, Cameras, CameraType
|
||||
from nerfstudio.data.dataparsers.base_dataparser import (
|
||||
DataParser,
|
||||
DataParserConfig,
|
||||
DataparserOutputs,
|
||||
)
|
||||
from nerfstudio.data.scene_box import SceneBounds
|
||||
from nerfstudio.utils.io import load_from_json
|
||||
|
||||
|
||||
@dataclass
|
||||
class NerfstudioDataParserConfig(DataParserConfig):
|
||||
"""Nerfstudio dataset config"""
|
||||
|
||||
_target: Type = field(default_factory=lambda: Nerfstudio)
|
||||
"""target class to instantiate"""
|
||||
data_directory: Path = Path("data/nerfstudio/poster")
|
||||
"""directory specifying location of data"""
|
||||
scale_factor: float = 1.0
|
||||
"""How much to scale the camera origins by."""
|
||||
downscale_factor: int = 1
|
||||
"""How much to downscale images."""
|
||||
scene_scale: float = 1.0
|
||||
"""How much to scale the region of interest by."""
|
||||
orientation_method: Literal["pca", "up"] = "up"
|
||||
"""The method to use for orientation."""
|
||||
train_split_percentage: float = 0.9
|
||||
"""The percent of images to use for training. The remaining images are for eval.
|
||||
"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class Nerfstudio(DataParser):
|
||||
"""Nerfstudio Dataset"""
|
||||
|
||||
config: NerfstudioDataParserConfig
|
||||
|
||||
def _generate_dataset_inputs(self, split="train"):
|
||||
# pylint: disable=too-many-statements
|
||||
|
||||
meta = load_from_json(self.config.data_directory / "transforms.json")
|
||||
image_filenames = []
|
||||
poses = []
|
||||
num_skipped_image_filenames = 0
|
||||
for frame in meta["frames"]:
|
||||
if "\\" in frame["file_path"]:
|
||||
filepath = PureWindowsPath(frame["file_path"])
|
||||
else:
|
||||
filepath = Path(frame["file_path"])
|
||||
if self.config.downscale_factor > 1:
|
||||
fname = self.config.data_directory / f"images_{self.config.downscale_factor}" / filepath.name
|
||||
else:
|
||||
fname = self.config.data_directory / filepath
|
||||
if not fname:
|
||||
num_skipped_image_filenames += 1
|
||||
else:
|
||||
image_filenames.append(fname)
|
||||
poses.append(np.array(frame["transform_matrix"]))
|
||||
if num_skipped_image_filenames >= 0:
|
||||
logging.info("Skipping %s files in dataset split %s.", num_skipped_image_filenames, split)
|
||||
assert (
|
||||
len(image_filenames) != 0
|
||||
), """
|
||||
No image files found.
|
||||
You should check the file_paths in the transforms.json file to make sure they are correct.
|
||||
"""
|
||||
|
||||
# filter image_filenames and poses based on train/eval split percentage
|
||||
num_images = len(image_filenames)
|
||||
num_train_images = math.ceil(num_images * self.config.train_split_percentage)
|
||||
num_eval_images = num_images - num_train_images
|
||||
i_all = np.arange(num_images)
|
||||
i_train = np.linspace(
|
||||
0, num_images - 1, num_train_images, dtype=int
|
||||
) # equally spaced training images starting and ending at 0 and num_images-1
|
||||
i_eval = np.setdiff1d(i_all, i_train) # eval images are the remaining images
|
||||
assert len(i_eval) == num_eval_images
|
||||
if split == "train":
|
||||
indices = i_train
|
||||
elif split in ["val", "test"]:
|
||||
indices = i_eval
|
||||
else:
|
||||
raise ValueError(f"Unknown dataparser split {split}")
|
||||
|
||||
poses = torch.from_numpy(np.array(poses).astype(np.float32))
|
||||
poses = camera_utils.auto_orient_poses(poses, method=self.config.orientation_method)
|
||||
|
||||
# Scale poses
|
||||
scale_factor = 1.0 / torch.max(torch.abs(poses[:, :3, 3]))
|
||||
poses[:, :3, 3] *= scale_factor * self.config.scale_factor
|
||||
|
||||
# Choose image_filenames and poses based on split, but after auto orient and scaling the poses.
|
||||
image_filenames = [image_filenames[i] for i in indices]
|
||||
poses = poses[indices]
|
||||
|
||||
# in x,y,z order
|
||||
# assumes that the scene is centered at the origin
|
||||
aabb_scale = self.config.scene_scale
|
||||
scene_bounds = SceneBounds(
|
||||
aabb=torch.tensor(
|
||||
[[-aabb_scale, -aabb_scale, -aabb_scale], [aabb_scale, aabb_scale, aabb_scale]], dtype=torch.float32
|
||||
)
|
||||
)
|
||||
|
||||
if "camera_model" in meta:
|
||||
camera_type = CAMERA_MODEL_TO_TYPE[meta["camera_model"]]
|
||||
else:
|
||||
camera_type = CameraType.PERSPECTIVE
|
||||
|
||||
distortion_params = camera_utils.get_distortion_params(
|
||||
k1=float(meta["k1"]) if "k1" in meta else 0.0,
|
||||
k2=float(meta["k2"]) if "k2" in meta else 0.0,
|
||||
k3=float(meta["k3"]) if "k3" in meta else 0.0,
|
||||
k4=float(meta["k4"]) if "k4" in meta else 0.0,
|
||||
p1=float(meta["p1"]) if "p1" in meta else 0.0,
|
||||
p2=float(meta["p2"]) if "p2" in meta else 0.0,
|
||||
)
|
||||
|
||||
cameras = Cameras(
|
||||
fx=float(meta["fl_x"]),
|
||||
fy=float(meta["fl_y"]),
|
||||
cx=float(meta["cx"]),
|
||||
cy=float(meta["cy"]),
|
||||
distortion_params=distortion_params,
|
||||
height=int(meta["h"]),
|
||||
width=int(meta["w"]),
|
||||
camera_to_worlds=poses[:, :3, :4],
|
||||
camera_type=camera_type,
|
||||
)
|
||||
|
||||
cameras.rescale_output_resolution(scaling_factor=1.0 / self.config.downscale_factor)
|
||||
|
||||
dataset_inputs = DataparserOutputs(
|
||||
image_filenames=image_filenames,
|
||||
cameras=cameras,
|
||||
scene_bounds=scene_bounds,
|
||||
)
|
||||
return dataset_inputs
|
||||
Reference in new issue
Block a user