mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-06 07:48:50 +08:00
dataset_inputs -> dataparser_outputs (#627)
This commit is contained in:
1 parent
ef5384afb3
commit
3efd3c3298
17 files changed
+52
-179
No files matched your search
@@ -49,7 +49,7 @@ CAMERA_MODEL_TO_TYPE = {
|
||||
|
||||
|
||||
class Cameras:
|
||||
"""Dataset inputs for the image dataset and the ray generator.
|
||||
"""Dataparser outputs for the image dataset and the ray generator.
|
||||
|
||||
Note: currently only supports cameras with the same principal points and types. The reason we type
|
||||
the focal lengths, principal points, and image sizes as tensors is to allow for batched cameras
|
||||
|
||||
@@ -284,9 +284,9 @@ class VanillaDataManager(DataManager): # pylint: disable=abstract-method
|
||||
self.local_rank = local_rank
|
||||
self.sampler = None
|
||||
|
||||
self.train_dataset = InputDataset(config.dataparser.setup().get_dataset_inputs(split="train"))
|
||||
self.train_dataset = InputDataset(config.dataparser.setup().get_dataparser_outputs(split="train"))
|
||||
self.eval_dataset = InputDataset(
|
||||
config.dataparser.setup().get_dataset_inputs(split="val" if not test_mode else "test")
|
||||
config.dataparser.setup().get_dataparser_outputs(split="val" if not test_mode else "test")
|
||||
)
|
||||
super().__init__()
|
||||
|
||||
@@ -310,10 +310,10 @@ class VanillaDataManager(DataManager): # pylint: disable=abstract-method
|
||||
self.iter_train_image_dataloader = iter(self.train_image_dataloader)
|
||||
self.train_pixel_sampler = PixelSampler(self.config.train_num_rays_per_batch)
|
||||
self.train_camera_optimizer = self.config.train_camera_optimizer.setup(
|
||||
num_cameras=self.train_dataset.dataset_inputs.cameras.size, device=self.device
|
||||
num_cameras=self.train_dataset.dataparser_outputs.cameras.size, device=self.device
|
||||
)
|
||||
self.train_ray_generator = RayGenerator(
|
||||
self.train_dataset.dataset_inputs.cameras.to(self.device),
|
||||
self.train_dataset.dataparser_outputs.cameras.to(self.device),
|
||||
self.train_camera_optimizer,
|
||||
)
|
||||
|
||||
@@ -337,7 +337,7 @@ class VanillaDataManager(DataManager): # pylint: disable=abstract-method
|
||||
self.iter_eval_image_dataloader = iter(self.eval_image_dataloader)
|
||||
self.eval_pixel_sampler = PixelSampler(self.config.eval_num_rays_per_batch)
|
||||
self.eval_ray_generator = RayGenerator(
|
||||
self.eval_dataset.dataset_inputs.cameras.to(self.device),
|
||||
self.eval_dataset.dataparser_outputs.cameras.to(self.device),
|
||||
self.train_camera_optimizer, # should be shared between train and eval.
|
||||
)
|
||||
# for loading full images
|
||||
|
||||
@@ -50,7 +50,7 @@ class Semantics:
|
||||
|
||||
@dataclass
|
||||
class DataparserOutputs:
|
||||
"""Dataset inputs for the image dataset and the ray generator."""
|
||||
"""Dataparser outputs for the image dataset and the ray generator."""
|
||||
|
||||
image_filenames: List[Path]
|
||||
"""Filenames for the images."""
|
||||
@@ -64,7 +64,7 @@ class DataparserOutputs:
|
||||
additional_inputs: Dict[str, Any] = to_immutable_dict({})
|
||||
"""Dictionary of additional dataset information (e.g. semantics/point clouds/masks).
|
||||
{input_name:
|
||||
... {"func": function to process additional dataset inputs,
|
||||
... {"func": function to process additional dataparser outputs,
|
||||
... "kwargs": dictionary of data to pass into "func"}
|
||||
}
|
||||
"""
|
||||
@@ -97,24 +97,24 @@ class DataParser:
|
||||
self.config = config
|
||||
|
||||
@abstractmethod
|
||||
def _generate_dataset_inputs(self, split: str = "train") -> DataparserOutputs:
|
||||
"""Abstract method that returns the dataset inputs for the given split.
|
||||
def _generate_dataparser_outputs(self, split: str = "train") -> DataparserOutputs:
|
||||
"""Abstract method that returns the dataparser outputs for the given split.
|
||||
|
||||
Args:
|
||||
split: Which dataset split to generate (train/test).
|
||||
|
||||
Returns:
|
||||
DatasetInputs containing data for the specified dataset and split
|
||||
DataparserOutputs containing data for the specified dataset and split
|
||||
"""
|
||||
|
||||
def get_dataset_inputs(self, split: str = "train") -> DataparserOutputs:
|
||||
"""Returns the dataset inputs for the given split.
|
||||
def get_dataparser_outputs(self, split: str = "train") -> DataparserOutputs:
|
||||
"""Returns the dataparser outputs for the given split.
|
||||
|
||||
Args:
|
||||
split: Which dataset split to generate (train/test).
|
||||
|
||||
Returns:
|
||||
DatasetInputs containing data for the specified dataset and split
|
||||
DataparserOutputs containing data for the specified dataset and split
|
||||
"""
|
||||
dataset_inputs = self._generate_dataset_inputs(split)
|
||||
return dataset_inputs
|
||||
dataparser_outputs = self._generate_dataparser_outputs(split)
|
||||
return dataparser_outputs
|
||||
@@ -62,7 +62,7 @@ class Blender(DataParser):
|
||||
self.scale_factor: float = config.scale_factor
|
||||
self.alpha_color = config.alpha_color
|
||||
|
||||
def _generate_dataset_inputs(self, split="train"):
|
||||
def _generate_dataparser_outputs(self, split="train"):
|
||||
if self.alpha_color is not None:
|
||||
alpha_color_tensor = get_color(self.alpha_color)
|
||||
else:
|
||||
@@ -99,11 +99,11 @@ class Blender(DataParser):
|
||||
camera_type=CameraType.PERSPECTIVE,
|
||||
)
|
||||
|
||||
dataset_inputs = DataparserOutputs(
|
||||
dataparser_outputs = DataparserOutputs(
|
||||
image_filenames=image_filenames,
|
||||
cameras=cameras,
|
||||
alpha_color=alpha_color_tensor,
|
||||
scene_box=scene_box,
|
||||
)
|
||||
|
||||
return dataset_inputs
|
||||
return dataparser_outputs
|
||||
@@ -81,7 +81,7 @@ class Friends(DataParser):
|
||||
|
||||
config: FriendsDataParserConfig
|
||||
|
||||
def _generate_dataset_inputs(self, split="train"): # pylint: disable=unused-argument,too-many-statements
|
||||
def _generate_dataparser_outputs(self, split="train"): # pylint: disable=unused-argument,too-many-statements
|
||||
|
||||
cameras_json = load_from_json(self.config.data_directory / "cameras.json")
|
||||
frames = cameras_json["frames"]
|
||||
@@ -182,10 +182,10 @@ class Friends(DataParser):
|
||||
)
|
||||
cameras.rescale_output_resolution(scaling_factor=1.0 / self.config.downscale_factor)
|
||||
|
||||
dataset_inputs = DataparserOutputs(
|
||||
dataparser_outputs = DataparserOutputs(
|
||||
image_filenames=image_filenames,
|
||||
cameras=cameras,
|
||||
scene_box=scene_box,
|
||||
additional_inputs={"semantics": {"func": get_semantics_and_masks, "kwargs": {"semantics": semantics}}},
|
||||
)
|
||||
return dataset_inputs
|
||||
return dataparser_outputs
|
||||
@@ -55,7 +55,7 @@ class InstantNGP(DataParser):
|
||||
|
||||
config: InstantNGPDataParserConfig
|
||||
|
||||
def _generate_dataset_inputs(self, split="train"):
|
||||
def _generate_dataparser_outputs(self, split="train"):
|
||||
|
||||
meta = load_from_json(self.config.data_directory / "transforms.json")
|
||||
image_filenames = []
|
||||
@@ -107,10 +107,10 @@ class InstantNGP(DataParser):
|
||||
)
|
||||
|
||||
# TODO(ethan): add alpha background color
|
||||
dataset_inputs = DataparserOutputs(
|
||||
dataparser_outputs = DataparserOutputs(
|
||||
image_filenames=image_filenames,
|
||||
cameras=cameras,
|
||||
scene_box=scene_box,
|
||||
)
|
||||
|
||||
return dataset_inputs
|
||||
return dataparser_outputs
|
||||
@@ -78,7 +78,7 @@ class Mipnerf360(DataParser):
|
||||
poses_orig[:, :3, :4] = poses[:, :3, :4]
|
||||
return poses_orig
|
||||
|
||||
def _generate_dataset_inputs(self, split="train"):
|
||||
def _generate_dataparser_outputs(self, split="train"):
|
||||
image_dir = "images"
|
||||
if self.config.downscale_factor > 1:
|
||||
image_dir += f"_{self.config.downscale_factor}"
|
||||
@@ -155,10 +155,10 @@ class Mipnerf360(DataParser):
|
||||
|
||||
cameras.rescale_output_resolution(scaling_factor=1.0 / self.config.downscale_factor)
|
||||
|
||||
dataset_inputs = DataparserOutputs(
|
||||
dataparser_outputs = DataparserOutputs(
|
||||
image_filenames=image_filenames,
|
||||
cameras=cameras,
|
||||
scene_box=scene_box,
|
||||
)
|
||||
|
||||
return dataset_inputs
|
||||
return dataparser_outputs
|
||||
@@ -62,7 +62,7 @@ class Nerfstudio(DataParser):
|
||||
|
||||
config: NerfstudioDataParserConfig
|
||||
|
||||
def _generate_dataset_inputs(self, split="train"):
|
||||
def _generate_dataparser_outputs(self, split="train"):
|
||||
# pylint: disable=too-many-statements
|
||||
|
||||
meta = load_from_json(self.config.data_directory / "transforms.json")
|
||||
@@ -157,9 +157,9 @@ class Nerfstudio(DataParser):
|
||||
|
||||
cameras.rescale_output_resolution(scaling_factor=1.0 / self.config.downscale_factor)
|
||||
|
||||
dataset_inputs = DataparserOutputs(
|
||||
dataparser_outputs = DataparserOutputs(
|
||||
image_filenames=image_filenames,
|
||||
cameras=cameras,
|
||||
scene_box=scene_box,
|
||||
)
|
||||
return dataset_inputs
|
||||
return dataparser_outputs
|
||||
@@ -60,7 +60,7 @@ class Record3D(DataParser):
|
||||
|
||||
config: Record3DDataParserConfig
|
||||
|
||||
def _generate_dataset_inputs(self, split: str = "train") -> DataparserOutputs:
|
||||
def _generate_dataparser_outputs(self, split: str = "train") -> DataparserOutputs:
|
||||
image_dir = self.config.data_directory / "rgb"
|
||||
|
||||
if not image_dir.exists():
|
||||
@@ -144,10 +144,10 @@ class Record3D(DataParser):
|
||||
camera_type=CameraType.PERSPECTIVE,
|
||||
)
|
||||
|
||||
dataset_inputs = DataparserOutputs(
|
||||
dataparser_outputs = DataparserOutputs(
|
||||
image_filenames=image_filenames,
|
||||
cameras=cameras,
|
||||
scene_box=scene_box,
|
||||
)
|
||||
|
||||
return dataset_inputs
|
||||
return dataparser_outputs
|
||||
@@ -116,7 +116,7 @@ class EvalDataloader(DataLoader):
|
||||
**kwargs,
|
||||
):
|
||||
self.input_dataset = input_dataset
|
||||
self.cameras = input_dataset.dataset_inputs.cameras.to(device)
|
||||
self.cameras = input_dataset.dataparser_outputs.cameras.to(device)
|
||||
self.device = device
|
||||
self.kwargs = kwargs
|
||||
super().__init__(dataset=input_dataset)
|
||||
|
||||
@@ -33,15 +33,15 @@ class InputDataset(Dataset):
|
||||
"""Dataset that returns images.
|
||||
|
||||
Args:
|
||||
dataset_inputs: description of where and how to read input images.
|
||||
dataparser_outputs: description of where and how to read input images.
|
||||
"""
|
||||
|
||||
def __init__(self, dataset_inputs: DataparserOutputs):
|
||||
def __init__(self, dataparser_outputs: DataparserOutputs):
|
||||
super().__init__()
|
||||
self.dataset_inputs = dataset_inputs
|
||||
self.dataparser_outputs = dataparser_outputs
|
||||
|
||||
def __len__(self):
|
||||
return len(self.dataset_inputs.image_filenames)
|
||||
return len(self.dataparser_outputs.image_filenames)
|
||||
|
||||
def get_numpy_image(self, image_idx: int) -> npt.NDArray[np.uint8]:
|
||||
"""Returns the image of shape (H, W, 3 or 4).
|
||||
@@ -49,7 +49,7 @@ class InputDataset(Dataset):
|
||||
Args:
|
||||
image_idx: The image index in the dataset.
|
||||
"""
|
||||
image_filename = self.dataset_inputs.image_filenames[image_idx]
|
||||
image_filename = self.dataparser_outputs.image_filenames[image_idx]
|
||||
pil_image = Image.open(image_filename)
|
||||
image = np.array(pil_image, dtype="uint8") # shape is (h, w, 3 or 4)
|
||||
assert len(image.shape) == 3
|
||||
@@ -64,9 +64,9 @@ class InputDataset(Dataset):
|
||||
image_idx: The image index in the dataset.
|
||||
"""
|
||||
image = torch.from_numpy(self.get_numpy_image(image_idx).astype("float32") / 255.0)
|
||||
if self.dataset_inputs.alpha_color is not None and image.shape[-1] == 4:
|
||||
if self.dataparser_outputs.alpha_color is not None and image.shape[-1] == 4:
|
||||
assert image.shape[-1] == 4
|
||||
image = image[:, :, :3] * image[:, :, -1:] + self.dataset_inputs.alpha_color * (1.0 - image[:, :, -1:])
|
||||
image = image[:, :, :3] * image[:, :, -1:] + self.dataparser_outputs.alpha_color * (1.0 - image[:, :, -1:])
|
||||
else:
|
||||
image = image[:, :, :3]
|
||||
return image
|
||||
@@ -80,7 +80,7 @@ class InputDataset(Dataset):
|
||||
image = self.get_image(image_idx)
|
||||
data = {"image_idx": image_idx}
|
||||
data["image"] = image
|
||||
for _, data_func_dict in self.dataset_inputs.additional_inputs.items():
|
||||
for _, data_func_dict in self.dataparser_outputs.additional_inputs.items():
|
||||
assert "func" in data_func_dict, "Missing function to process data: specify `func` in `additional_inputs`"
|
||||
func = data_func_dict["func"]
|
||||
assert "kwargs" in data_func_dict, "No data to process: specify `kwargs` in `additional_inputs`"
|
||||
|
||||
@@ -218,7 +218,7 @@ class VanillaPipeline(Pipeline):
|
||||
# TODO(ethan): get rid of scene_bounds from the model
|
||||
assert self.datamanager.train_dataset is not None, "Missing input dataset"
|
||||
self._model = config.model.setup(
|
||||
scene_box=self.datamanager.train_dataset.dataset_inputs.scene_box,
|
||||
scene_box=self.datamanager.train_dataset.dataparser_outputs.scene_box,
|
||||
num_train_data=len(self.datamanager.train_dataset),
|
||||
)
|
||||
self.model.to(device)
|
||||
|
||||
@@ -272,11 +272,11 @@ class ViewerState:
|
||||
for idx in image_indices:
|
||||
image = dataset[idx]["image"]
|
||||
bgr = image[..., [2, 1, 0]]
|
||||
camera_json = dataset.dataset_inputs.cameras.to_json(camera_idx=idx, image=bgr, max_size=100)
|
||||
camera_json = dataset.dataparser_outputs.cameras.to_json(camera_idx=idx, image=bgr, max_size=100)
|
||||
self.vis[f"sceneState/cameras/{idx:06d}"].write(camera_json)
|
||||
|
||||
# draw the scene box (i.e., the bounding box)
|
||||
json_ = dataset.dataset_inputs.scene_box.to_json()
|
||||
json_ = dataset.dataparser_outputs.scene_box.to_json()
|
||||
self.vis["sceneState/sceneBox"].write(json_)
|
||||
|
||||
# set the initial state whether to train or not
|
||||
|
||||
Reference in new issue
Block a user