4
0
mirror of https://github.com/nerfstudio-project/nerfstudio.git synced 2026-11-11 02:24:38 +08:00
nerfstudio/nerfactory/dataloaders/structs.py
Ethan Weber 0b042be956
Pipeline v2 (#258)
* Model class done, pipeline class still needs more visualization stuffs

* Renaming the BaseDataContainer to DataloaderOutputs and adding documentation

* Pushing more documentation, removing some trivial functions

* Updating dict typing

* Removing config dicts for collider

* Passing tests

* Pushing shell new dataloader

* Saving work, need to fix NGP model's get_loss_dict and Pipeline

* V1 of the ngp pipeline should be done. Need train script and debugging

* Updating with latest instant-ngp code

* Syntax updates from latest ngp graph code

* Linter, removing unneeded code in new NGP graph code

* Style

* Should be good now. Linter is happy and the code is verbatum current instant-ngp

* Everything done except novel view testing code

* Fixing some nitpicks

* Debugging dataloader

* merged master

* WOP. integrating pipeline with configs and notebook demo

* removing model folder

* a lot of name changing from Graph to Model

* in progress... changing trainer to use pipeline... broken right now

* pipeline code runs

* switched to val dataset

* makes run_eval.py work

* updated configs for pipeline change

* fixing test_run_train script

* fixing black and linting

* fix docs

* small updates to trainer

Co-authored-by: Jake Austin <jake-austin@berkeley.edu>
2022-08-10 14:59:45 -07:00

178 lines
5.6 KiB
Python

# 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.
"""
Dataset input structures.
"""
from dataclasses import dataclass
from typing import Dict, List, Optional, Union
import torch
from torchtyping import TensorType
from nerfactory.cameras.rays import RayBundle
@dataclass
class PointCloud:
"""Dataclass for a point cloud."""
xyz: TensorType["num_points", 3] = None
rgb: TensorType["num_points", 3] = None
@dataclass
class Semantics:
"""Dataclass for semantic labels."""
stuff_filenames: List[str]
stuff_classes: List[str]
thing_filenames: List[str]
thing_classes: List[str]
stuff_colors: torch.Tensor
thing_colors: torch.Tensor
@dataclass
class SceneBounds:
"""Data to represent the scene bounds.
Attributes:
aabb: axis-aligned bounding box.
aabb[0] is the minimum (x,y,z) point.
aabb[1] is the maximum (x,y,z) point.
"""
aabb: TensorType[2, 3] = None
def get_diagonal_length(self):
"""Returns the longest diagonal length."""
diff = self.aabb[1] - self.aabb[0]
length = torch.sqrt((diff**2).sum() + 1e-20)
return length
def get_center(self):
"""Returns the center of the box."""
diff = self.aabb[1] - self.aabb[0]
return self.aabb[0] + diff / 2.0
def get_centered_and_scaled_scene_bounds(self, scale_factor: Union[float, torch.Tensor] = 1.0):
"""Returns a new box that has been shifted and rescaled to be centered
about the origin."""
return SceneBounds(aabb=(self.aabb - self.get_center()) * scale_factor)
@staticmethod
def get_normalized_positions(positions: TensorType[..., 3], aabb: TensorType[2, 3]):
"""Return normalized positions in range [0, 1] based on the aabb axis-aligned bounding box.
Args:
positions: the xyz positions
aabb: the axis-aligned bounding box
Returns:
positions that are normalized into the range [0, 1].
"""
aabb_lengths = aabb[1] - aabb[0]
normalized_positions = (positions - aabb[0]) / aabb_lengths
return normalized_positions
def to_json(self) -> Dict:
"""Returns a json object from the Python object."""
return {"type": "aabb", "min_point": self.aabb[0].tolist(), "max_point": self.aabb[1].tolist()}
@staticmethod
def from_json(json_: Dict) -> "SceneBounds":
"""Returns the an instance of SceneBounds from a json dictionary."""
assert json_["type"] == "aabb"
aabb = torch.tensor([json_[0], json_[1]])
return SceneBounds(aabb=aabb)
@dataclass
class DatasetInputs:
"""Dataset inputs for the image dataset and the ray generator.
Args:
image_filenames: Filenames for the images.
...
"""
image_filenames: List[str]
intrinsics: torch.Tensor
camera_to_world: torch.Tensor
downscale_factor: int = 1
mask_filenames: Optional[List[str]] = None
depth_filenames: Optional[List[str]] = None
scene_bounds: SceneBounds = SceneBounds()
semantics: Optional[Semantics] = None
point_cloud: PointCloud = PointCloud()
alpha_color: Optional[TensorType[3]] = None
def check_inputs(self):
"""Check the inputs to make sure everything is okay."""
assert self.intrinsics.dtype == torch.float32
assert self.camera_to_world.dtype == torch.float32
def save_to_folder_name(self, data_directory: str):
"""Save the dataset inputs."""
raise NotImplementedError
def load_from_folder_name(self, data_directory: str):
"""Load the saved dataset inputs."""
raise NotImplementedError
def as_dict(self) -> dict:
"""Returns the dataclass as a dictionary."""
return vars(self)
@dataclass
class DataloaderOutputs:
"""A container for data that is not specific to any dataset. It should be everything
needed by the renderer and loss calculation. Different datasets and models will probably
need to subclass from this differently, since different Field modules will require different
data.
Args:
rays (RayBundle): The rays for the image.
ground_truth_pixels (TensorType["num_pixels", 3]): The ground truth pixels for the image.
"""
rays: RayBundle # Raybundle and the cameras will be merged into one thing in a later PR
cameras: torch.Tensor
ground_truth_pixels: Optional[TensorType["num_pixels", 3]] = None
def check_inputs(self, expected_attr: Dict):
"""This will check to make sure that the expected attributes exist.
This will exist in any subclassed version of this"""
for attr in expected_attr:
assert hasattr(self, attr)
@dataclass
class ModelOutputs:
"""A container for outputs from the model.
This will be passed into the loss function along with the base data container. It should be
subclassed for when your model outputs different things.
Args:
rays (RayBundle): The rays for the image.
ground_truth_pixels (TensorType["num_pixels", 3]): The ground truth pixels for the image.
"""
rendered_pixels: Optional[TensorType["num_pixels", 3]] = None