mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-06 15:59:16 +08:00
Tiny-cuda-nn and fixed logic for instant-ngp (#102)
* correcting implementation for instant ngp * instant ngp with tcnn * black and pylint * merged master
This commit is contained in:
1 parent
8cc4c063df
commit
7d8beaaed4
17 files changed
+477
-43
No files matched your search
@@ -31,15 +31,15 @@ class CollateIterDataset(torch.utils.data.IterableDataset):
|
||||
dataset: torch.utils.data.Dataset,
|
||||
collate_fn: Callable,
|
||||
num_samples_to_collate: int,
|
||||
num_times_to_repeat: int = 0,
|
||||
num_times_to_repeat_images: int = 0,
|
||||
):
|
||||
super().__init__()
|
||||
self.dataset = dataset
|
||||
self.collate_fn = collate_fn
|
||||
self.num_samples_to_collate = num_samples_to_collate
|
||||
self.num_times_to_repeat = num_times_to_repeat
|
||||
self.num_times_to_repeat_images = num_times_to_repeat_images
|
||||
|
||||
self.num_repeated = self.num_times_to_repeat # starting value
|
||||
self.num_repeated = self.num_times_to_repeat_images # starting value
|
||||
self.cached_batch_list = None
|
||||
|
||||
def __getitem__(self, idx):
|
||||
@@ -54,12 +54,12 @@ class CollateIterDataset(torch.utils.data.IterableDataset):
|
||||
|
||||
def __iter__(self):
|
||||
while True:
|
||||
if self.num_repeated >= self.num_times_to_repeat:
|
||||
if self.num_repeated >= self.num_times_to_repeat_images:
|
||||
# trigger a reset
|
||||
self.num_repeated = 0
|
||||
batch_list = self.get_batch_list()
|
||||
# possibly save a cached item
|
||||
self.cached_batch_list = batch_list if self.num_times_to_repeat != 0 else None
|
||||
self.cached_batch_list = batch_list if self.num_times_to_repeat_images != 0 else None
|
||||
else:
|
||||
batch_list = self.cached_batch_list
|
||||
self.num_repeated += 1
|
||||
|
||||
@@ -2,3 +2,79 @@
|
||||
Code for loading instant-ngp dataset formats.
|
||||
# TODO
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
import imageio
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
from mattport.nerf.dataset.structs import DatasetInputs, SceneBounds
|
||||
from mattport.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
|
||||
@@ -8,6 +8,7 @@ from omegaconf import ListConfig
|
||||
|
||||
from mattport.nerf.dataset.format.blender import load_blender_data
|
||||
from mattport.nerf.dataset.format.friends import load_friends_data
|
||||
from mattport.nerf.dataset.format.instant_ngp import load_instant_ngp_data
|
||||
from mattport.nerf.dataset.structs import DatasetInputs
|
||||
from mattport.structures.colors import get_color
|
||||
from mattport.utils.io import get_absolute_path
|
||||
@@ -57,6 +58,12 @@ def get_dataset_inputs(
|
||||
split="train",
|
||||
include_point_cloud=False,
|
||||
)
|
||||
elif dataset_format == "instant_ngp":
|
||||
dataset_inputs = load_instant_ngp_data(
|
||||
get_absolute_path(data_directory),
|
||||
downscale_factor=downscale_factor,
|
||||
split="train",
|
||||
)
|
||||
else:
|
||||
raise NotImplementedError(f"{dataset_format} is not a valid dataset type")
|
||||
|
||||
|
||||
Reference in new issue
Block a user