mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-04 06:49:13 +08:00
Initial ray generation and dataset code (#12)
* ray generator and cameras structure progress * test renames * added pass * conflicts * fixing some black errors * reformated files * merged master and .gitignore * fixing linting * removing old folder * removed return False * pylint fixes * update * updates * update * update * update * update * update * update * update * update * update * update
This commit is contained in:
1 parent
1995afda57
commit
0b7dfcdc5d
21 files changed
+652
-139
No files matched your search
@@ -0,0 +1,94 @@
|
||||
"""
|
||||
The dataset baseclass.
|
||||
"""
|
||||
|
||||
from abc import abstractmethod
|
||||
import torch
|
||||
import imageio
|
||||
|
||||
from mattport.structures.cameras import Camera
|
||||
|
||||
|
||||
class Dataset(torch.utils.data.Dataset):
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
torch (_type_): _description_
|
||||
"""
|
||||
|
||||
def __init__(self, data_directory: str, dataset_type: str, scene: str):
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
data_directory (str): _description_
|
||||
type (str): _description_
|
||||
scene (str): _description_
|
||||
"""
|
||||
super().__init__()
|
||||
self.data_directory = data_directory
|
||||
self.dataset_type = dataset_type
|
||||
self.scene = scene
|
||||
|
||||
# data to populate
|
||||
self.image_filenames = []
|
||||
self.cameras = []
|
||||
|
||||
def __len__(self):
|
||||
return len(self.image_filenames)
|
||||
|
||||
def get_image(self, idx):
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
idx (_type_): _description_
|
||||
|
||||
Raises:
|
||||
NotImplementedError: _description_
|
||||
"""
|
||||
image_filename = self.image_filenames[idx]
|
||||
image = imageio.imread(image_filename)
|
||||
return image
|
||||
|
||||
@abstractmethod
|
||||
def get_camera(self, idx) -> Camera:
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
idx (_type_): _description_
|
||||
|
||||
Raises:
|
||||
NotImplementedError: _description_
|
||||
|
||||
Returns:
|
||||
Camera: _description_
|
||||
"""
|
||||
return self.cameras[idx]
|
||||
|
||||
def __getitem__(self, idx):
|
||||
"""Returns the rays for the camera.
|
||||
|
||||
Args:
|
||||
idx (_type_): _description_
|
||||
|
||||
Raises:
|
||||
NotImplementedError: _description_
|
||||
"""
|
||||
# raise NotImplementedError
|
||||
|
||||
# TODO(ethan): we should have a dataset which always takes rays from the same camera
|
||||
# or, it could take rays from multiple cameras
|
||||
|
||||
camera = self.cameras[idx]
|
||||
rays = camera.generate_all_rays() # (H, W, 6)
|
||||
camera_indices = torch.ones_like(rays[:, :, 0:1]) * idx
|
||||
rgbs = self.get_image(idx) / 255.0
|
||||
|
||||
return {
|
||||
"rays": rays,
|
||||
"rgbs": rgbs,
|
||||
"camera_indices": camera_indices
|
||||
# question: do we want to have more information here?
|
||||
}
|
||||
|
||||
|
||||
# question... what do I want the dataset to return?
|
||||
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
For loading the blender dataset format.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import imageio
|
||||
import numpy as np
|
||||
from mattport.utils.io import load_from_json
|
||||
|
||||
|
||||
def load_blender_data(basedir, half_res=False, testskip=1):
|
||||
"""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:
|
||||
basedir (_type_): _description_
|
||||
half_res (bool, optional): _description_. Defaults to False.
|
||||
testskip (int, optional): _description_. Defaults to 1.
|
||||
|
||||
Returns:
|
||||
_type_: _description_
|
||||
"""
|
||||
# pylint: disable=unused-argument
|
||||
# pylint: disable-msg=too-many-locals
|
||||
splits = ["train", "val", "test"]
|
||||
metas = {}
|
||||
for split in splits:
|
||||
metas[split] = load_from_json(os.path.join(basedir, f"transforms_{split}.json"))
|
||||
|
||||
image_filenames = []
|
||||
all_poses = []
|
||||
for split in splits:
|
||||
meta = metas[split]
|
||||
poses = []
|
||||
if split == "train" or testskip == 0:
|
||||
skip = 1
|
||||
else:
|
||||
skip = testskip
|
||||
|
||||
for frame in meta["frames"][::skip]:
|
||||
fname = os.path.join(basedir, frame["file_path"] + ".png")
|
||||
image_filenames.append(fname)
|
||||
poses.append(np.array(frame["transform_matrix"]))
|
||||
poses = np.array(poses).astype(np.float32)
|
||||
all_poses.append(poses)
|
||||
|
||||
poses = np.concatenate(all_poses, 0)
|
||||
|
||||
img_0 = imageio.imread(image_filenames[0])
|
||||
H, W = img_0.shape[:2]
|
||||
camera_angle_x = float(meta["camera_angle_x"])
|
||||
focal = 0.5 * W / np.tan(0.5 * camera_angle_x)
|
||||
|
||||
return image_filenames, poses, focal, H, W
|
||||
@@ -4,7 +4,7 @@ Builds the dataset
|
||||
import importlib
|
||||
import logging
|
||||
|
||||
from mattport.nerf.dataset.dataset import Dataset
|
||||
from mattport.nerf.dataset.base import Dataset
|
||||
|
||||
|
||||
def build_dataset(config: dict) -> Dataset:
|
||||
@@ -16,11 +16,9 @@ def build_dataset(config: dict) -> Dataset:
|
||||
"""
|
||||
|
||||
# TODO(ethan): better handle this with configs
|
||||
module = importlib.import_module("mattport.nerf.dataset.base")
|
||||
dataset = module.Dataset(
|
||||
data_directory=config.data_directory,
|
||||
dataset_type=config.dataset_type,
|
||||
scene=config.scene,
|
||||
module = importlib.import_module("mattport.nerf.dataset.blender_dataset")
|
||||
dataset = module.BlenderDataset(
|
||||
data_directory=config.data_directory, dataset_type=config.dataset_type, scene=config.scene
|
||||
)
|
||||
logging.info(dataset)
|
||||
return dataset
|
||||
@@ -1,62 +0,0 @@
|
||||
"""
|
||||
The dataset baseclass.
|
||||
"""
|
||||
|
||||
from abc import abstractmethod
|
||||
import torch
|
||||
|
||||
from mattport.nerf.cameras import Camera
|
||||
|
||||
|
||||
class Dataset(torch.utils.data.Dataset):
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
torch (_type_): _description_
|
||||
"""
|
||||
|
||||
def __init__(self, data_directory: str, dataset_type: str, scene: str):
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
data_directory (str): _description_
|
||||
type (str): _description_
|
||||
scene (str): _description_
|
||||
"""
|
||||
super().__init__()
|
||||
self.data_directory = data_directory
|
||||
self.dataset_type = dataset_type
|
||||
self.scene = scene
|
||||
|
||||
def __len__(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_image(self, idx):
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
idx (_type_): _description_
|
||||
|
||||
Raises:
|
||||
NotImplementedError: _description_
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def get_camera(self, idx) -> Camera:
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
idx (_type_): _description_
|
||||
|
||||
Raises:
|
||||
NotImplementedError: _description_
|
||||
|
||||
Returns:
|
||||
Camera: _description_
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def __getitem__(self, idx):
|
||||
raise NotImplementedError
|
||||
Reference in new issue
Block a user