mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-01 05:20:32 +08:00
Adios mattport
This commit is contained in:
1 parent
9d2b34ab60
commit
b26408eb7a
86 files changed
+176
-176
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
Code to handle loading blender datasets.
|
||||
"""
|
||||
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
import imageio
|
||||
import numpy as np
|
||||
import torch
|
||||
from torchtyping import TensorType
|
||||
|
||||
from radiance.nerf.dataset.structs import DatasetInputs, SceneBounds
|
||||
from radiance.utils.io import load_from_json
|
||||
|
||||
|
||||
def load_blender_data(
|
||||
basedir: str, downscale_factor: int = 1, alpha_color: Optional[TensorType[3]] = None, split: str = "train"
|
||||
) -> DatasetInputs:
|
||||
"""Processes the a blender dataset directory.
|
||||
Some of this code comes from https://github.com/yenchenlin/nerf-pytorch/blob/master/load_blender.py#L37.
|
||||
|
||||
Args:
|
||||
data_directory (str): Location of data
|
||||
downscale_factor (int, optional): How much to downscale images. Defaults to 1.0.
|
||||
alpha_color (TensorType[3], optional): Sets transparent regions to specified color, otherwise black.
|
||||
split (str, optional): Which dataset split to generate.
|
||||
|
||||
Returns:
|
||||
DatasetInputs
|
||||
"""
|
||||
# pylint: disable=unused-argument
|
||||
# pylint: disable-msg=too-many-locals
|
||||
meta = load_from_json(os.path.join(basedir, f"transforms_{split}.json"))
|
||||
image_filenames = []
|
||||
poses = []
|
||||
for frame in meta["frames"]:
|
||||
fname = os.path.join(basedir, frame["file_path"].replace("./", "") + ".png")
|
||||
image_filenames.append(fname)
|
||||
poses.append(np.array(frame["transform_matrix"]))
|
||||
poses = np.array(poses).astype(np.float32)
|
||||
|
||||
img_0 = imageio.imread(image_filenames[0])
|
||||
image_height, image_width = img_0.shape[:2]
|
||||
camera_angle_x = float(meta["camera_angle_x"])
|
||||
focal_length = 0.5 * image_width / np.tan(0.5 * camera_angle_x)
|
||||
|
||||
cx = image_width / 2.0
|
||||
cy = image_height / 2.0
|
||||
camera_to_world = torch.from_numpy(poses[:, :3]) # camera to world transform
|
||||
num_cameras = len(image_filenames)
|
||||
num_intrinsics_params = 3
|
||||
intrinsics = torch.ones((num_cameras, num_intrinsics_params), dtype=torch.float32)
|
||||
intrinsics *= torch.tensor([cx, cy, focal_length])
|
||||
|
||||
# in x,y,z order
|
||||
scene_bounds = SceneBounds(aabb=torch.tensor([[-2, -2, -2], [2, 2, 2]], dtype=torch.float32))
|
||||
|
||||
dataset_inputs = DatasetInputs(
|
||||
image_filenames=image_filenames,
|
||||
downscale_factor=downscale_factor,
|
||||
alpha_color=alpha_color,
|
||||
intrinsics=intrinsics * 1.0 / downscale_factor, # downscaling the intrinsics here
|
||||
camera_to_world=camera_to_world,
|
||||
scene_bounds=scene_bounds,
|
||||
)
|
||||
|
||||
return dataset_inputs
|
||||
@@ -0,0 +1,138 @@
|
||||
"""
|
||||
Code to handle loading friends datasets.
|
||||
"""
|
||||
|
||||
import os
|
||||
import numpy as np
|
||||
|
||||
import torch
|
||||
|
||||
from radiance.nerf.dataset.colmap_utils import read_cameras_binary, read_images_binary, read_pointsTD_binary
|
||||
from radiance.nerf.dataset.structs import DatasetInputs, PointCloud, SceneBounds, Semantics
|
||||
from radiance.utils import profiler
|
||||
from radiance.utils.io import load_from_json
|
||||
|
||||
|
||||
def get_aabb_and_transform(basedir):
|
||||
"""Returns the aabb and pointcloud transform from the threejs.json file."""
|
||||
filename = os.path.join(basedir, "threejs.json")
|
||||
assert os.path.exists(filename)
|
||||
data = load_from_json(filename)
|
||||
|
||||
# point cloud transformation
|
||||
transposed_point_cloud_transform = np.array(data["object"]["children"][0]["matrix"]).reshape(4, 4).T
|
||||
assert transposed_point_cloud_transform[3, 3] == 1.0
|
||||
|
||||
# bbox transformation
|
||||
bbox_transform = np.array(data["object"]["children"][1]["matrix"]).reshape(4, 4).T
|
||||
w, h, d = data["geometries"][1]["width"], data["geometries"][1]["height"], data["geometries"][1]["depth"]
|
||||
temp = np.array([w, h, d]) / 2.0
|
||||
bbox = np.array([-temp, temp])
|
||||
bbox = np.concatenate([bbox, np.ones_like(bbox[:, 0:1])], axis=1)
|
||||
bbox = (bbox_transform @ bbox.T).T[:, 0:3]
|
||||
|
||||
aabb = bbox # rename to aabb because it's an axis-aligned bounding box
|
||||
return torch.from_numpy(aabb).float(), torch.from_numpy(transposed_point_cloud_transform).float()
|
||||
|
||||
|
||||
@profiler.time_function
|
||||
def load_friends_data(basedir, downscale_factor=1, split="train", include_semantics=True, include_point_cloud=False):
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
basedir (_type_): _description_
|
||||
downscale_factor (float, optional): _description_. Defaults to 1.0.
|
||||
split (str, optional): _description_. Defaults to "train".
|
||||
include_point_cloud (bool): whether or not to include the point cloud
|
||||
"""
|
||||
# pylint: disable=unused-argument
|
||||
|
||||
# --- image filenames ----
|
||||
images_data = read_images_binary(os.path.join(basedir, "colmap", "images.bin"))
|
||||
# `image_path` is only the end of the filename, including the extension e.g., `.jpg`
|
||||
image_paths = sorted(os.listdir(os.path.join(basedir, "images")))
|
||||
|
||||
image_path_to_image_id = {}
|
||||
image_id_to_image_path = {}
|
||||
for v in images_data.values():
|
||||
image_path_to_image_id[v.name] = v.id
|
||||
image_id_to_image_path[v.id] = v.name
|
||||
# TODO: handle the splits differently
|
||||
image_filenames = [os.path.join(basedir, "images", image_path) for image_path in image_paths]
|
||||
|
||||
# -- set the bounding box ---
|
||||
aabb, transposed_point_cloud_transform = get_aabb_and_transform(basedir)
|
||||
scene_bounds = SceneBounds(aabb=aabb)
|
||||
|
||||
# --- intrinsics ---
|
||||
cameras_data = read_cameras_binary(os.path.join(basedir, "colmap", "cameras.bin"))
|
||||
intrinsics = []
|
||||
for image_path in image_paths:
|
||||
cam = cameras_data[image_path_to_image_id[image_path]]
|
||||
assert len(cam.params) == 3
|
||||
focal_length = cam.params[0] # f (fx and fy)
|
||||
cx = cam.params[1] # cx
|
||||
cy = cam.params[2] # cy
|
||||
intrinsics.append([cx, cy, focal_length])
|
||||
intrinsics = torch.tensor(intrinsics).float()
|
||||
|
||||
# --- camera_to_world (extrinsics) ---
|
||||
camera_to_world = []
|
||||
bottom_row = np.array([0, 0, 0, 1.0]).reshape(1, 4)
|
||||
for image_path in image_paths:
|
||||
image_data = images_data[image_path_to_image_id[image_path]]
|
||||
rot = image_data.qvec2rotmat()
|
||||
trans = image_data.tvec.reshape(3, 1)
|
||||
c2w = np.concatenate([np.concatenate([rot, trans], 1), bottom_row], 0)
|
||||
camera_to_world.append(c2w)
|
||||
camera_to_world = torch.tensor(np.array(camera_to_world)).float()
|
||||
camera_to_world = torch.inverse(camera_to_world)
|
||||
camera_to_world[..., 1:3] *= -1
|
||||
camera_to_world = transposed_point_cloud_transform @ camera_to_world
|
||||
camera_to_world = camera_to_world[:, :3]
|
||||
|
||||
# --- masks to mask out things (e.g., people) ---
|
||||
|
||||
# --- semantics ---
|
||||
semantics = Semantics()
|
||||
if include_semantics:
|
||||
thing_filenames = [
|
||||
image_filename.replace("/images/", "/segmentations/thing/").replace(".jpg", ".png")
|
||||
for image_filename in image_filenames
|
||||
]
|
||||
stuff_filenames = [
|
||||
image_filename.replace("/images/", "/segmentations/stuff/").replace(".jpg", ".png")
|
||||
for image_filename in image_filenames
|
||||
]
|
||||
panoptic_classes = load_from_json(os.path.join(basedir, "panoptic_classes.json"))
|
||||
stuff_classes = panoptic_classes["stuff"]
|
||||
thing_classes = panoptic_classes["thing"]
|
||||
semantics = Semantics(
|
||||
stuff_classes=stuff_classes,
|
||||
stuff_filenames=stuff_filenames,
|
||||
thing_classes=thing_classes,
|
||||
thing_filenames=thing_filenames,
|
||||
)
|
||||
|
||||
# Possibly include the sparse point cloud from COLMAP in the dataset inputs.
|
||||
# NOTE(ethan): this will be common across the different splits.
|
||||
point_cloud = PointCloud()
|
||||
if include_point_cloud:
|
||||
points_3d = read_pointsTD_binary(os.path.join(basedir, "colmap", "points3D.bin"))
|
||||
xyz = torch.tensor(np.array([p_value.xyz for p_id, p_value in points_3d.items()])).float()
|
||||
rgb = torch.tensor(np.array([p_value.rgb for p_id, p_value in points_3d.items()])).float()
|
||||
xyz_h = torch.cat([xyz, torch.ones_like(xyz[..., :1])], -1)
|
||||
xyz = (xyz_h @ transposed_point_cloud_transform.T)[..., :3]
|
||||
point_cloud.xyz = xyz
|
||||
point_cloud.rgb = rgb
|
||||
|
||||
dataset_inputs = DatasetInputs(
|
||||
image_filenames=image_filenames,
|
||||
downscale_factor=downscale_factor,
|
||||
intrinsics=intrinsics / downscale_factor,
|
||||
camera_to_world=camera_to_world,
|
||||
semantics=semantics,
|
||||
point_cloud=point_cloud,
|
||||
scene_bounds=scene_bounds,
|
||||
)
|
||||
return dataset_inputs
|
||||
@@ -0,0 +1,80 @@
|
||||
"""
|
||||
Code for loading instant-ngp dataset formats.
|
||||
# TODO
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
import imageio
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
from radiance.nerf.dataset.structs import DatasetInputs, SceneBounds
|
||||
from radiance.utils.io import load_from_json
|
||||
|
||||
|
||||
def load_instant_ngp_data(
|
||||
basedir: str, downscale_factor: int = 1, split: str = "train", camera_translation_scalar=0.33
|
||||
) -> DatasetInputs:
|
||||
"""Processes the a blender dataset directory.
|
||||
Some of this code comes from https://github.com/yenchenlin/nerf-pytorch/blob/master/load_blender.py#L37.
|
||||
|
||||
Args:
|
||||
data_directory (str): Location of data
|
||||
downscale_factor (int, optional): How much to downscale images. Defaults to 1.0.
|
||||
split (str, optional): Which dataset split to generate.
|
||||
|
||||
Returns:
|
||||
DatasetInputs
|
||||
"""
|
||||
# pylint: disable=unused-argument
|
||||
# pylint: disable-msg=too-many-locals
|
||||
meta = load_from_json(os.path.join(basedir, "transforms.json"))
|
||||
image_filenames = []
|
||||
poses = []
|
||||
num_skipped_image_filenames = 0
|
||||
for frame in meta["frames"]:
|
||||
fname = os.path.join(basedir, frame["file_path"])
|
||||
if not os.path.exists(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)
|
||||
poses = np.array(poses).astype(np.float32)
|
||||
poses[:3, 3] *= camera_translation_scalar
|
||||
|
||||
img_0 = imageio.imread(image_filenames[0])
|
||||
image_height, image_width = img_0.shape[:2]
|
||||
camera_angle_x = float(meta["camera_angle_x"])
|
||||
focal_length = 0.5 * image_width / np.tan(0.5 * camera_angle_x)
|
||||
|
||||
cx = image_width / 2.0
|
||||
cy = image_height / 2.0
|
||||
camera_to_world = torch.from_numpy(poses[:, :3]) # camera to world transform
|
||||
num_cameras = len(image_filenames)
|
||||
num_intrinsics_params = 3
|
||||
intrinsics = torch.ones((num_cameras, num_intrinsics_params), dtype=torch.float32)
|
||||
intrinsics *= torch.tensor([cx, cy, focal_length])
|
||||
|
||||
# in x,y,z order
|
||||
# assumes that the scene is centered at the origin
|
||||
aabb_scale = meta["aabb_scale"]
|
||||
scene_bounds = SceneBounds(
|
||||
aabb=torch.tensor(
|
||||
[[-aabb_scale, -aabb_scale, -aabb_scale], [aabb_scale, aabb_scale, aabb_scale]], dtype=torch.float32
|
||||
)
|
||||
)
|
||||
|
||||
# TODO(ethan): add alpha background color
|
||||
dataset_inputs = DatasetInputs(
|
||||
image_filenames=image_filenames,
|
||||
downscale_factor=downscale_factor,
|
||||
intrinsics=intrinsics * 1.0 / downscale_factor, # downscaling the intrinsics here
|
||||
camera_to_world=camera_to_world,
|
||||
scene_bounds=scene_bounds,
|
||||
)
|
||||
|
||||
return dataset_inputs
|
||||
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
Code for loading the LLFF data.
|
||||
"""
|
||||
|
||||
|
||||
def load_llff_data(basedir, downscale_factor=1, split="train", include_point_cloud=False):
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
basedir (_type_): _description_
|
||||
downscale_factor (float, optional): _description_. Defaults to 1.0.
|
||||
split (str, optional): _description_. Defaults to "train".
|
||||
include_point_cloud (bool): whether or not to include the point cloud
|
||||
"""
|
||||
# pylint: disable=unused-argument
|
||||
raise NotImplementedError
|
||||
Reference in new issue
Block a user