mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-06 07:48:50 +08:00
Serializing JSON code for drawable objects (and some documentation) (#222)
* some comments and starting serializing comments. on plane w/o formatter * fixing pylint and black * removed changes in file * revert file * addressing comments * remove abstract Co-authored-by: Ethan Weber <ethanweber@Ethans-MacBook-Pro.local>
This commit is contained in:
1 parent
44a7fa288c
commit
d13a766b9e
13 files changed
+240
-106
No files matched your search
+114
-24
@@ -15,9 +15,11 @@
|
|||||||
"""
|
"""
|
||||||
Camera Models
|
Camera Models
|
||||||
"""
|
"""
|
||||||
|
import base64
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from typing import Optional, Type
|
from typing import Dict, Optional, Type
|
||||||
|
|
||||||
|
import cv2
|
||||||
import torch
|
import torch
|
||||||
from torch.nn.functional import normalize
|
from torch.nn.functional import normalize
|
||||||
from torchtyping import TensorType
|
from torchtyping import TensorType
|
||||||
@@ -27,12 +29,15 @@ from nerfactory.utils.misc import is_not_none
|
|||||||
|
|
||||||
|
|
||||||
class Camera:
|
class Camera:
|
||||||
"""Base Camera. Intended to be subclassed"""
|
"""Base Camera. Intended to be subclassed.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
camera_to_world: Camera to world transformation
|
||||||
|
camera_index: Camera index
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self, camera_to_world: TensorType[3, 4] = torch.eye(4)[:3], camera_index: Optional[int] = None
|
||||||
camera_to_world: Optional[TensorType[3, 4]] = torch.eye(4)[:3],
|
|
||||||
camera_index: int = None,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
self.camera_to_world = camera_to_world
|
self.camera_to_world = camera_to_world
|
||||||
self.camera_index = camera_index
|
self.camera_index = camera_index
|
||||||
@@ -46,7 +51,7 @@ class Camera:
|
|||||||
def get_num_intrinsics_params(self) -> int:
|
def get_num_intrinsics_params(self) -> int:
|
||||||
"""
|
"""
|
||||||
Returns:
|
Returns:
|
||||||
int: number of optimizable intrinsic parameters
|
number of optimizable intrinsic parameters
|
||||||
"""
|
"""
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -54,21 +59,21 @@ class Camera:
|
|||||||
def get_intrinsics(self) -> torch.Tensor:
|
def get_intrinsics(self) -> torch.Tensor:
|
||||||
"""
|
"""
|
||||||
Returns:
|
Returns:
|
||||||
torch.Tensor: Intrinsics matrix
|
Intrinsics matrix
|
||||||
"""
|
"""
|
||||||
return
|
return
|
||||||
|
|
||||||
def get_camera_to_world(self) -> TensorType[3, 4]:
|
def get_camera_to_world(self) -> TensorType[3, 4]:
|
||||||
"""
|
"""
|
||||||
Returns:
|
Returns:
|
||||||
TensorType[3, 4]: Camera to world transformation
|
Camera to world transformation
|
||||||
"""
|
"""
|
||||||
return self.camera_to_world
|
return self.camera_to_world
|
||||||
|
|
||||||
def get_camera_to_world_h(self) -> TensorType[4, 4]:
|
def get_camera_to_world_h(self) -> TensorType[4, 4]:
|
||||||
"""
|
"""
|
||||||
Returns:
|
Returns:
|
||||||
TensorType[4, 4]: Camera to world transformation with homogeneous coordinates
|
Camera to world transformation with homogeneous coordinates
|
||||||
"""
|
"""
|
||||||
c2w = self.camera_to_world
|
c2w = self.camera_to_world
|
||||||
ones = torch.tensor([0, 0, 0, 1], device=c2w.device)[None]
|
ones = torch.tensor([0, 0, 0, 1], device=c2w.device)[None]
|
||||||
@@ -79,7 +84,7 @@ class Camera:
|
|||||||
def get_image_height(self) -> int:
|
def get_image_height(self) -> int:
|
||||||
"""
|
"""
|
||||||
Returns:
|
Returns:
|
||||||
int: Image height
|
Image height
|
||||||
"""
|
"""
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -87,7 +92,7 @@ class Camera:
|
|||||||
def get_image_width(self) -> int:
|
def get_image_width(self) -> int:
|
||||||
"""
|
"""
|
||||||
Returns:
|
Returns:
|
||||||
int: Image width
|
Image width
|
||||||
"""
|
"""
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -96,7 +101,7 @@ class Camera:
|
|||||||
"""Rescales the camera intrinsics for output resolution.
|
"""Rescales the camera intrinsics for output resolution.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
scaling_factor (float): Scaling factor
|
scaling_factor: Scaling factor
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
None
|
None
|
||||||
@@ -156,9 +161,42 @@ class Camera:
|
|||||||
ray_bundle.set_camera_indices(camera_index=self.camera_index)
|
ray_bundle.set_camera_indices(camera_index=self.camera_index)
|
||||||
return ray_bundle
|
return ray_bundle
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def to_json(self, image: Optional[TensorType["image_height", "image_width", 2]] = None) -> Dict:
|
||||||
|
"""Converts the camera to a json dictionary.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
image: An image that is encoded to a base64 string. Defaults to None.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A JSON representation of the camera
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_json(json_: Dict) -> "Camera": # pylint:disable=no-self-use
|
||||||
|
"""Converts a json dictionary to the camera object.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
json_: The json dictionary.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The camera object instantiated from the json dictionary.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
class PinholeCamera(Camera):
|
class PinholeCamera(Camera):
|
||||||
"""Pinhole camera model."""
|
"""Pinhole camera model.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
fx: Focal length in x direction
|
||||||
|
fy: Focal length in y direction
|
||||||
|
cx: Principal point in x direction
|
||||||
|
cy: Principal point in y direction
|
||||||
|
camera_to_world: Camera to world transformation
|
||||||
|
camera_index: Camera index. Defaults to None.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -166,10 +204,10 @@ class PinholeCamera(Camera):
|
|||||||
cy: float,
|
cy: float,
|
||||||
fx: float,
|
fx: float,
|
||||||
fy: float,
|
fy: float,
|
||||||
camera_to_world: Optional[TensorType[3, 4]] = torch.eye(4)[:3],
|
camera_to_world: TensorType[3, 4] = torch.eye(4)[:3],
|
||||||
camera_index: int = None,
|
camera_index: Optional[int] = None,
|
||||||
):
|
):
|
||||||
super().__init__(camera_to_world, camera_index)
|
super().__init__(camera_to_world=camera_to_world, camera_index=camera_index)
|
||||||
self.cx = cx
|
self.cx = cx
|
||||||
self.cy = cy
|
self.cy = cy
|
||||||
self.fx = fx
|
self.fx = fx
|
||||||
@@ -258,19 +296,48 @@ class PinholeCamera(Camera):
|
|||||||
|
|
||||||
return RayBundle(origins=origins, directions=directions, pixel_area=pixel_area[..., None])
|
return RayBundle(origins=origins, directions=directions, pixel_area=pixel_area[..., None])
|
||||||
|
|
||||||
|
def to_json(self, image: Optional[TensorType["image_height", "image_width", 2]] = None) -> Dict:
|
||||||
|
json_ = {
|
||||||
|
"type": "PinholeCamera",
|
||||||
|
"cx": self.cx,
|
||||||
|
"cy": self.cy,
|
||||||
|
"fx": self.fx,
|
||||||
|
"fy": self.fy,
|
||||||
|
"camera_to_world": self.camera_to_world.tolist(),
|
||||||
|
"camera_index": self.camera_index,
|
||||||
|
}
|
||||||
|
if image:
|
||||||
|
# move image to cpu if not already
|
||||||
|
# TODO: move to numpy
|
||||||
|
data = cv2.imencode(".png", image[:, :, ::-1])[1].tobytes()
|
||||||
|
json_["image"] = str("data:image/png;base64," + base64.b64encode(data).decode("ascii"))
|
||||||
|
return json_
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_json(json_: Dict) -> "PinholeCamera": # pylint:disable=no-self-use
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
class SimplePinholeCamera(PinholeCamera):
|
class SimplePinholeCamera(PinholeCamera):
|
||||||
"""Simple Pinhole Camera model."""
|
"""Simple Pinhole Camera model.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
cx: Principal point x direction
|
||||||
|
cy: Principal point y direction
|
||||||
|
f: Focal length
|
||||||
|
camera_to_world: Camera to world transformation matrix
|
||||||
|
camera_index: Camera index. Defaults to None.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
cx: float,
|
cx: float,
|
||||||
cy: float,
|
cy: float,
|
||||||
f: float,
|
f: float,
|
||||||
camera_to_world: Optional[TensorType[3, 4]] = torch.eye(4)[:3],
|
camera_to_world: TensorType[3, 4] = torch.eye(4)[:3],
|
||||||
camera_index: int = None,
|
camera_index: Optional[int] = None,
|
||||||
):
|
):
|
||||||
super().__init__(cx, cy, f, f, camera_to_world, camera_index)
|
super().__init__(cx, cy, f, f, camera_to_world=camera_to_world, camera_index=camera_index)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def fx_index(cls):
|
def fx_index(cls):
|
||||||
@@ -280,18 +347,32 @@ class SimplePinholeCamera(PinholeCamera):
|
|||||||
def fy_index(cls):
|
def fy_index(cls):
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
|
def to_json(self, image: Optional[TensorType["image_height", "image_width", 2]] = None) -> Dict:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_json(json_: Dict) -> "SimplePinholeCamera": # pylint:disable=no-self-use
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
class EquirectangularCamera(Camera):
|
class EquirectangularCamera(Camera):
|
||||||
"""Equirectangular (360 degree) camera model."""
|
"""Equirectangular (360 degree) camera model.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
height: Height of the image
|
||||||
|
width: Width of the image
|
||||||
|
camera_to_world: Camera to world transformation matrix
|
||||||
|
camera_index: Camera index. Defaults to None.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
height: int,
|
height: int,
|
||||||
width: int,
|
width: int,
|
||||||
camera_to_world: Optional[TensorType[3, 4]] = torch.eye(4)[:3],
|
camera_to_world: TensorType[3, 4] = torch.eye(4)[:3],
|
||||||
camera_index: int = None,
|
camera_index: Optional[int] = None,
|
||||||
):
|
):
|
||||||
super().__init__(camera_to_world, camera_index)
|
super().__init__(camera_to_world=camera_to_world, camera_index=camera_index)
|
||||||
self.height = height
|
self.height = height
|
||||||
self.width = width
|
self.width = width
|
||||||
|
|
||||||
@@ -361,6 +442,13 @@ class EquirectangularCamera(Camera):
|
|||||||
pixel_area = dx * dy
|
pixel_area = dx * dy
|
||||||
return RayBundle(origins=origins, directions=directions, pixel_area=pixel_area[..., None])
|
return RayBundle(origins=origins, directions=directions, pixel_area=pixel_area[..., None])
|
||||||
|
|
||||||
|
def to_json(self, image: Optional[TensorType["image_height", "image_width", 2]] = None) -> Dict:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_json(json_: Dict) -> "SimplePinholeCamera": # pylint:disable=no-self-use
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
def get_intrinsics_from_intrinsics_matrix(intrinsics_matrix: TensorType[3, 3]):
|
def get_intrinsics_from_intrinsics_matrix(intrinsics_matrix: TensorType[3, 3]):
|
||||||
"""
|
"""
|
||||||
@@ -376,6 +464,8 @@ def get_intrinsics_from_intrinsics_matrix(intrinsics_matrix: TensorType[3, 3]):
|
|||||||
|
|
||||||
|
|
||||||
def get_camera_model(num_intrinsics_params: int) -> Type[Camera]:
|
def get_camera_model(num_intrinsics_params: int) -> Type[Camera]:
|
||||||
|
# TODO: we should specify the type in the dataloading pipeline instead of relying on number of intrinsics
|
||||||
|
# to make the choice
|
||||||
"""Returns the camera model given the specified number of intrinsics parameters.
|
"""Returns the camera model given the specified number of intrinsics parameters.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|||||||
@@ -98,9 +98,9 @@ class RaySamples(TensorDataclass):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
frustums: Frustums
|
frustums: Frustums
|
||||||
camera_indices: TensorType["bs":..., 1] = None
|
camera_indices: Optional[TensorType["bs":..., 1]] = None
|
||||||
valid_mask: TensorType["bs":..., 1] = None
|
valid_mask: Optional[TensorType["bs":..., 1]] = None
|
||||||
deltas: TensorType["bs":..., 1] = None
|
deltas: Optional[TensorType["bs":..., 1]] = None
|
||||||
|
|
||||||
def get_weights(self, densities: TensorType[..., "num_samples", 1]) -> TensorType[..., "num_samples", 1]:
|
def get_weights(self, densities: TensorType[..., "num_samples", 1]) -> TensorType[..., "num_samples", 1]:
|
||||||
"""Return weights based on predicted densities
|
"""Return weights based on predicted densities
|
||||||
@@ -115,6 +115,7 @@ class RaySamples(TensorDataclass):
|
|||||||
delta_density = self.deltas * densities
|
delta_density = self.deltas * densities
|
||||||
alphas = 1 - torch.exp(-delta_density)
|
alphas = 1 - torch.exp(-delta_density)
|
||||||
|
|
||||||
|
# TODO: put both versions of the transmittance calculation somewhere
|
||||||
# mip-nerf version of transmittance calculation:
|
# mip-nerf version of transmittance calculation:
|
||||||
transmittance = torch.cumsum(delta_density[..., :-1, :], dim=-2)
|
transmittance = torch.cumsum(delta_density[..., :-1, :], dim=-2)
|
||||||
transmittance = torch.cat(
|
transmittance = torch.cat(
|
||||||
@@ -170,17 +171,6 @@ class RayBundle(TensorDataclass):
|
|||||||
valid_mask: Optional[TensorType["num_rays", 1, bool]] = None
|
valid_mask: Optional[TensorType["num_rays", 1, bool]] = None
|
||||||
num_rays_per_chunk: int = None
|
num_rays_per_chunk: int = None
|
||||||
|
|
||||||
def move_to_device(self, device: torch.device) -> None:
|
|
||||||
"""Move bundle data to a device.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
device (torch.device): Device to move tensors to.
|
|
||||||
"""
|
|
||||||
self.origins = self.origins.to(device)
|
|
||||||
self.directions = self.directions.to(device)
|
|
||||||
if is_not_none(self.camera_indices):
|
|
||||||
self.camera_indices = self.camera_indices.to(device)
|
|
||||||
|
|
||||||
def set_camera_indices(self, camera_index: int) -> None:
|
def set_camera_indices(self, camera_index: int) -> None:
|
||||||
"""Sets all of the the camera indices to a specific camera index.
|
"""Sets all of the the camera indices to a specific camera index.
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ __global__ void kernel_morton3D_invert(
|
|||||||
torch::Tensor morton3D(const torch::Tensor coords){
|
torch::Tensor morton3D(const torch::Tensor coords){
|
||||||
DEVICE_GUARD(coords);
|
DEVICE_GUARD(coords);
|
||||||
CHECK_INPUT(coords);
|
CHECK_INPUT(coords);
|
||||||
|
// NOTE(ethan): why & and not &&?
|
||||||
TORCH_CHECK(coords.ndimension() == 2 & coords.size(1) == 3)
|
TORCH_CHECK(coords.ndimension() == 2 & coords.size(1) == 3)
|
||||||
|
|
||||||
const int N = coords.size(0);
|
const int N = coords.size(0);
|
||||||
@@ -75,7 +76,7 @@ torch::Tensor morton3D(const torch::Tensor coords){
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Convert 3D indices to coords in z-order
|
* @brief Convert indices in z-order to 3D coords
|
||||||
*
|
*
|
||||||
* @param indices Integer indices with shape [N]
|
* @param indices Integer indices with shape [N]
|
||||||
* @return torch::Tensor Integer coords with shape [N, 3]
|
* @return torch::Tensor Integer coords with shape [N, 3]
|
||||||
|
|||||||
+39
-11
@@ -17,7 +17,7 @@ Dataset input structures.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from torchtyping import TensorType
|
from torchtyping import TensorType
|
||||||
@@ -27,7 +27,7 @@ from nerfactory.cameras.rays import RayBundle
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class PointCloud:
|
class PointCloud:
|
||||||
"""_summary_"""
|
"""Dataclass for a point cloud."""
|
||||||
|
|
||||||
xyz: TensorType["num_points", 3] = None
|
xyz: TensorType["num_points", 3] = None
|
||||||
rgb: TensorType["num_points", 3] = None
|
rgb: TensorType["num_points", 3] = None
|
||||||
@@ -35,22 +35,24 @@ class PointCloud:
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Semantics:
|
class Semantics:
|
||||||
"""_summary_"""
|
"""Dataclass for semantic labels."""
|
||||||
|
|
||||||
stuff_classes: List[str] = None
|
stuff_classes: Optional[List[str]] = None
|
||||||
stuff_colors: List[List[int]] = None
|
stuff_colors: Optional[List[str]] = None
|
||||||
stuff_filenames: List[str] = None
|
stuff_filenames: Optional[List[str]] = None
|
||||||
thing_classes: List[str] = None
|
thing_classes: Optional[List[str]] = None
|
||||||
thing_colors: List[List[int]] = None
|
thing_colors: Optional[List[str]] = None
|
||||||
thing_filenames: List[str] = None
|
thing_filenames: Optional[List[str]] = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class SceneBounds:
|
class SceneBounds:
|
||||||
"""Data to represent the scene bounds.
|
"""Data to represent the scene bounds.
|
||||||
|
|
||||||
aabb: axis-aligned bounding box
|
Attributes:
|
||||||
aabb[0] is the minimum (x,y,z) point. aabb[1] is the maximum (x,y,z) point.
|
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
|
aabb: TensorType[2, 3] = None
|
||||||
@@ -71,6 +73,32 @@ class SceneBounds:
|
|||||||
about the origin."""
|
about the origin."""
|
||||||
return SceneBounds(aabb=(self.aabb - self.get_center()) * scale_factor)
|
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
|
@dataclass
|
||||||
class DatasetInputs:
|
class DatasetInputs:
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ logging.getLogger("PIL").setLevel(logging.WARNING)
|
|||||||
|
|
||||||
|
|
||||||
class Trainer:
|
class Trainer:
|
||||||
"""Training class
|
"""Trainer class
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
config (Config): The configuration object.
|
config (Config): The configuration object.
|
||||||
@@ -74,8 +74,9 @@ class Trainer:
|
|||||||
profiler.setup_profiler(config.logging)
|
profiler.setup_profiler(config.logging)
|
||||||
# visualizer variable
|
# visualizer variable
|
||||||
self.visualizer_state = viewer_utils.VisualizerState(config.viewer)
|
self.visualizer_state = viewer_utils.VisualizerState(config.viewer)
|
||||||
|
|
||||||
self.grad_scaler = GradScaler(enabled=self.mixed_precision)
|
self.grad_scaler = GradScaler(enabled=self.mixed_precision)
|
||||||
|
# training callbacks
|
||||||
|
self.callbacks = None
|
||||||
|
|
||||||
def setup(self, test_mode=False):
|
def setup(self, test_mode=False):
|
||||||
"""Setup the Trainer by calling other setup functions.
|
"""Setup the Trainer by calling other setup functions.
|
||||||
@@ -95,7 +96,7 @@ class Trainer:
|
|||||||
self.graph = DDP(self.graph, device_ids=[self.local_rank])
|
self.graph = DDP(self.graph, device_ids=[self.local_rank])
|
||||||
dist.barrier(device_ids=[self.local_rank])
|
dist.barrier(device_ids=[self.local_rank])
|
||||||
|
|
||||||
self.graph.register_callbacks()
|
self.callbacks = self.graph.get_training_callbacks()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_aggregated_loss(cls, loss_dict: Dict[str, torch.Tensor]) -> torch.Tensor:
|
def get_aggregated_loss(cls, loss_dict: Dict[str, torch.Tensor]) -> torch.Tensor:
|
||||||
@@ -203,9 +204,8 @@ class Trainer:
|
|||||||
self.grad_scaler.update()
|
self.grad_scaler.update()
|
||||||
|
|
||||||
self.optimizers.scheduler_step_all(step)
|
self.optimizers.scheduler_step_all(step)
|
||||||
if self.graph.callbacks:
|
for callback in self.callbacks:
|
||||||
for func_ in self.graph.callbacks:
|
callback.after_step(step)
|
||||||
func_.after_step(step)
|
|
||||||
|
|
||||||
# Merging loss and metrics dict into a single output.
|
# Merging loss and metrics dict into a single output.
|
||||||
loss_dict["loss"] = loss
|
loss_dict["loss"] = loss
|
||||||
|
|||||||
@@ -30,10 +30,10 @@ def _create_grid_coords(resolution: int, device: torch.device = "cpu") -> Tensor
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
resolution (int): The 3D resolution of the grid.
|
resolution (int): The 3D resolution of the grid.
|
||||||
device (torch.device): Which device you want the returned tensor to live int.
|
device (torch.device): Which device you want the returned tensor to live on.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
TensorType["n_coords", 3]: All grid coordinates with shape [res * res * res, 3]
|
TensorType["n_coords", 3]: All grid coordinates with shape [res * res * res, 3].
|
||||||
"""
|
"""
|
||||||
arrange = torch.arange(resolution, device=device)
|
arrange = torch.arange(resolution, device=device)
|
||||||
grid_x, grid_y, grid_z = torch.meshgrid([arrange, arrange, arrange], indexing="ij")
|
grid_x, grid_y, grid_z = torch.meshgrid([arrange, arrange, arrange], indexing="ij")
|
||||||
@@ -51,9 +51,9 @@ class DensityGrid(nn.Module):
|
|||||||
on the scale but all shares the same center.
|
on the scale but all shares the same center.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
- `center=0`, `base_scale=3`, `num_cascades=1` would create one grid (lvl=0) covers [-1.5, -1.5, -1.5, 1.5, 1.5, 1.5].
|
- `center=0`, `base_scale=3`, `num_cascades=1` would create one grid (lvl=0) covers the aabb [[-1.5, -1.5, -1.5], [1.5, 1.5, 1.5]].
|
||||||
- `center=0`, `base_scale=3`, `num_cascades=2` would create one grid (lvl=0) covers [-1.5, -1.5, -1.5, 1.5, 1.5, 1.5],
|
- `center=0`, `base_scale=3`, `num_cascades=2` would create one grid (lvl=0) covers the aabb [[-1.5, -1.5, -1.5], [1.5, 1.5, 1.5]],
|
||||||
and one more grid (lvl=1) covers [-3, -3, -3, 3, 3, 3].
|
and one more grid (lvl=1) covers the aabb [[-3, -3, -3], [3, 3, 3]].
|
||||||
|
|
||||||
Normally you can simply set the `center` to the scene center, and `base_scale` to be the scene scale, and use
|
Normally you can simply set the `center` to the scene center, and `base_scale` to be the scene scale, and use
|
||||||
`num_cascades=1` to create a single level density grid that covers the scene.
|
`num_cascades=1` to create a single level density grid that covers the scene.
|
||||||
@@ -68,11 +68,11 @@ class DensityGrid(nn.Module):
|
|||||||
TODO(ruilongli): support `center` and `base_scale` with 3-dim.
|
TODO(ruilongli): support `center` and `base_scale` with 3-dim.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
center (float, optional): Center of all the grids in the world space. Defaults to 0.
|
center (float): Center of all the grids in the world space. Defaults to 0.
|
||||||
base_scale (float, optional): Center of the scale of the base level grid. Defaults to 1.
|
base_scale (float): Center of the scale of the base level grid. Defaults to 1.
|
||||||
num_cascades (int, optional): Number of the cascaded multi-res levels. Defaults to 1.
|
num_cascades (int): Number of the cascaded multi-res levels. Defaults to 1.
|
||||||
resolution (int, optional): Resolution of the grid. Defaults to 128.
|
resolution (int): Resolution of the grid. Defaults to 128.
|
||||||
update_every_num_iters (int, optional): How frequently to update the grid values. Defaults to 16.
|
update_every_num_iters (int): How frequently to update the grid values. Defaults to 16.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@@ -157,11 +157,11 @@ class DensityGrid(nn.Module):
|
|||||||
"""Update the density grid in EMA way.
|
"""Update the density grid in EMA way.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
density_eval_func (Callable): A Callable function that takes in sample positions (N, 3) and
|
density_eval_func: A Callable function that takes in sample positions (N, 3) and
|
||||||
returns densities (N, 1).
|
returns densities (N, 1).
|
||||||
step (int): The current training step. Instant-NGP has a warmup stage where all cells are updated.
|
step: The current training step. Instant-NGP has a warmup stage where all cells are updated.
|
||||||
After certain amount of steps (256), it speeds up by only update sampled cells.
|
After certain amount of steps (256), it speeds up by only update sampled cells.
|
||||||
density_threshold (float): The threshold to prune cells. Recommand to calculate it using this rule:
|
density_threshold (float): The threshold to prune cells. Recommended to calculate it using this rule:
|
||||||
`weight_threshold / dt`. For example for Instant-NGP on Lego, the minimum step size `dt` is
|
`weight_threshold / dt`. For example for Instant-NGP on Lego, the minimum step size `dt` is
|
||||||
`3` (scale of the scene) * `sqrt(3)` / `1024` (number of samples). And if we want to the weight
|
`3` (scale of the scene) * `sqrt(3)` / `1024` (number of samples). And if we want to the weight
|
||||||
threshold to be `0.01`, we will get `0.01 / (3 * sqrt(3) / 1024) ~= 2.` for density threshold.
|
threshold to be `0.01`, we will get `0.01 / (3 * sqrt(3) / 1024) ~= 2.` for density threshold.
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import torch
|
|||||||
from torch.nn.parameter import Parameter
|
from torch.nn.parameter import Parameter
|
||||||
|
|
||||||
from nerfactory.cameras.rays import RaySamples
|
from nerfactory.cameras.rays import RaySamples
|
||||||
|
from nerfactory.data.structs import SceneBounds
|
||||||
from nerfactory.fields.base import Field
|
from nerfactory.fields.base import Field
|
||||||
from nerfactory.fields.modules.encoding import Encoding, HashEncoding, SHEncoding
|
from nerfactory.fields.modules.encoding import Encoding, HashEncoding, SHEncoding
|
||||||
from nerfactory.fields.modules.field_heads import FieldHeadNames
|
from nerfactory.fields.modules.field_heads import FieldHeadNames
|
||||||
@@ -36,13 +37,6 @@ except ImportError:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_normalized_positions(positions, aabb):
|
|
||||||
"""Return normalized positions in range [0, 1] based on the aabb axis-aligned bounding box."""
|
|
||||||
aabb_lengths = aabb[1] - aabb[0]
|
|
||||||
positions = (positions - aabb[0]) / aabb_lengths
|
|
||||||
return positions
|
|
||||||
|
|
||||||
|
|
||||||
def get_normalized_directions(directions):
|
def get_normalized_directions(directions):
|
||||||
"""SH encoding must be in the range [0, 1]"""
|
"""SH encoding must be in the range [0, 1]"""
|
||||||
return (directions + 1.0) / 2.0
|
return (directions + 1.0) / 2.0
|
||||||
@@ -105,7 +99,7 @@ class TCNNInstantNGPField(Field):
|
|||||||
|
|
||||||
def get_density(self, ray_samples: RaySamples):
|
def get_density(self, ray_samples: RaySamples):
|
||||||
"""Computes and returns the densities."""
|
"""Computes and returns the densities."""
|
||||||
positions = get_normalized_positions(ray_samples.frustums.get_positions(), self.aabb)
|
positions = SceneBounds.get_normalized_positions(ray_samples.frustums.get_positions(), self.aabb)
|
||||||
positions_flat = positions.view(-1, 3)
|
positions_flat = positions.view(-1, 3)
|
||||||
h = self.mlp_base(positions_flat).view(*ray_samples.frustums.shape, -1)
|
h = self.mlp_base(positions_flat).view(*ray_samples.frustums.shape, -1)
|
||||||
density_before_activation, base_mlp_out = torch.split(h, [1, self.geo_feat_dim], dim=-1)
|
density_before_activation, base_mlp_out = torch.split(h, [1, self.geo_feat_dim], dim=-1)
|
||||||
@@ -156,7 +150,7 @@ class TorchInstantNGPField(NeRFField):
|
|||||||
|
|
||||||
def get_density(self, ray_samples: RaySamples):
|
def get_density(self, ray_samples: RaySamples):
|
||||||
normalized_ray_samples = ray_samples
|
normalized_ray_samples = ray_samples
|
||||||
normalized_ray_samples.positions = get_normalized_positions(
|
normalized_ray_samples.positions = SceneBounds.get_normalized_positions(
|
||||||
normalized_ray_samples.frustums.get_positions(), self.aabb
|
normalized_ray_samples.frustums.get_positions(), self.aabb
|
||||||
)
|
)
|
||||||
return super().get_density(normalized_ray_samples)
|
return super().get_density(normalized_ray_samples)
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ from nerfactory.cameras.rays import RayBundle
|
|||||||
from nerfactory.data.structs import DatasetInputs, SceneBounds
|
from nerfactory.data.structs import DatasetInputs, SceneBounds
|
||||||
from nerfactory.graphs.modules.ray_generator import RayGenerator
|
from nerfactory.graphs.modules.ray_generator import RayGenerator
|
||||||
from nerfactory.utils import profiler
|
from nerfactory.utils import profiler
|
||||||
|
from nerfactory.utils.callbacks import Callback
|
||||||
from nerfactory.utils.config import GraphConfig
|
from nerfactory.utils.config import GraphConfig
|
||||||
from nerfactory.utils.misc import (
|
from nerfactory.utils.misc import (
|
||||||
get_masked_dict,
|
get_masked_dict,
|
||||||
@@ -58,10 +59,6 @@ class AbstractGraph(nn.Module):
|
|||||||
# to keep track of which device the nn.Module is on
|
# to keep track of which device the nn.Module is on
|
||||||
self.device_indicator_param = nn.Parameter(torch.empty(0))
|
self.device_indicator_param = nn.Parameter(torch.empty(0))
|
||||||
|
|
||||||
def get_device(self):
|
|
||||||
"""Returns the device that the torch parameters are on."""
|
|
||||||
return self.device_indicator_param.device
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device(self):
|
def device(self):
|
||||||
"""Returns the device that the graph is on."""
|
"""Returns the device that the graph is on."""
|
||||||
@@ -73,7 +70,8 @@ class AbstractGraph(nn.Module):
|
|||||||
|
|
||||||
|
|
||||||
class Graph(AbstractGraph):
|
class Graph(AbstractGraph):
|
||||||
"""Where everything (Fields, Optimizers, Samplers, Visualization, etc) is linked together. This should be
|
"""Graph class
|
||||||
|
Where everything (Fields, Optimizers, Samplers, Visualization, etc) is linked together. This should be
|
||||||
subclassed for custom NeRF model.
|
subclassed for custom NeRF model.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -122,9 +120,9 @@ class Graph(AbstractGraph):
|
|||||||
self.vis_outputs = None
|
self.vis_outputs = None
|
||||||
self.default_output_name = None
|
self.default_output_name = None
|
||||||
|
|
||||||
def register_callbacks(self): # pylint:disable=no-self-use
|
def get_training_callbacks(self) -> List[Callback]: # pylint:disable=no-self-use
|
||||||
"""Option to register callback for training functions"""
|
"""Returns a list of callbacks that run functions at the specified training iterations."""
|
||||||
self.callbacks = []
|
return []
|
||||||
|
|
||||||
def populate_density_field(self):
|
def populate_density_field(self):
|
||||||
"""Set the scene density field to use."""
|
"""Set the scene density field to use."""
|
||||||
@@ -235,7 +233,7 @@ class Graph(AbstractGraph):
|
|||||||
|
|
||||||
def get_outputs_for_camera(self, camera: Camera):
|
def get_outputs_for_camera(self, camera: Camera):
|
||||||
"""Get the graph outputs for a Camera."""
|
"""Get the graph outputs for a Camera."""
|
||||||
camera_ray_bundle = camera.get_camera_ray_bundle(device=self.get_device())
|
camera_ray_bundle = camera.get_camera_ray_bundle(device=self.device)
|
||||||
return self.get_outputs_for_camera_ray_bundle(camera_ray_bundle)
|
return self.get_outputs_for_camera_ray_bundle(camera_ray_bundle)
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
|||||||
@@ -36,22 +36,25 @@ from nerfactory.utils.callbacks import Callback
|
|||||||
|
|
||||||
|
|
||||||
class NGPGraph(Graph):
|
class NGPGraph(Graph):
|
||||||
|
"""Instant NGP graph
|
||||||
|
|
||||||
"""Instant NGP graph"""
|
Args:
|
||||||
|
field_implementation (str): one of "torch" or "tcnn", or other fields in 'field_implementation_to_class'
|
||||||
|
kwargs: additional params to pass up to the parent class Graph
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, field_implementation="torch", intrinsics=None, camera_to_world=None, **kwargs) -> None:
|
def __init__(self, field_implementation="torch", **kwargs) -> None:
|
||||||
assert field_implementation in field_implementation_to_class
|
assert field_implementation in field_implementation_to_class
|
||||||
self.field_implementation = field_implementation
|
self.field_implementation = field_implementation
|
||||||
self.field = None
|
self.field = None
|
||||||
super().__init__(intrinsics=intrinsics, camera_to_world=camera_to_world, **kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
def register_callbacks(self) -> None:
|
def get_training_callbacks(self) -> List[Callback]:
|
||||||
"""defining callbacks to run after every training iteration"""
|
|
||||||
assert self.density_field is not None
|
assert self.density_field is not None
|
||||||
self.callbacks = [
|
return [
|
||||||
Callback(
|
Callback(
|
||||||
self.density_field.update_every_num_iters,
|
update_every_num_iters=self.density_field.update_every_num_iters,
|
||||||
self.density_field.update_density_grid,
|
func=self.density_field.update_density_grid,
|
||||||
density_eval_func=self.field.density_fn,
|
density_eval_func=self.field.density_fn,
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
@@ -119,7 +122,7 @@ class NGPGraph(Graph):
|
|||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients):
|
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients):
|
||||||
device = self.get_device()
|
device = self.device
|
||||||
image = batch["image"].to(device)
|
image = batch["image"].to(device)
|
||||||
if "alive_ray_mask" in outputs:
|
if "alive_ray_mask" in outputs:
|
||||||
mask = outputs["alive_ray_mask"]
|
mask = outputs["alive_ray_mask"]
|
||||||
|
|||||||
@@ -82,17 +82,17 @@ class NeRFGraph(Graph):
|
|||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
|
|
||||||
def register_callbacks(self) -> None:
|
def get_training_callbacks(self) -> List[Callback]:
|
||||||
"""defining callbacks to run after every training iteration"""
|
callbacks = []
|
||||||
self.callbacks = []
|
|
||||||
if self.density_field is not None:
|
if self.density_field is not None:
|
||||||
self.callbacks += [
|
callbacks = [
|
||||||
Callback(
|
Callback(
|
||||||
self.density_field.update_every_num_iters,
|
update_every_num_iters=self.density_field.update_every_num_iters,
|
||||||
self.density_field.update_density_grid,
|
func=self.density_field.update_density_grid,
|
||||||
density_eval_func=self.field_coarse.density_fn,
|
density_eval_func=self.field_coarse.density_fn,
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
return callbacks # type: ignore
|
||||||
|
|
||||||
def populate_fields(self):
|
def populate_fields(self):
|
||||||
"""Set the fields."""
|
"""Set the fields."""
|
||||||
|
|||||||
@@ -13,16 +13,26 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Callback functions for training iterations
|
Callback code used for training iterations
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
|
|
||||||
class Callback:
|
class Callback:
|
||||||
"""Callback Object"""
|
"""Callback class used during training.
|
||||||
|
The function 'func' with 'args' and 'kwargs' will be called every 'update_every_num_iters' training iterations,
|
||||||
|
including at iteration 0.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
update_every_num_iters: How often to call the function `func`.
|
||||||
|
func: The function that will be called.
|
||||||
|
args: args for the function 'func'.
|
||||||
|
kwargs: kwargs for the function 'func'.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, update_every_num_iters: int, func: Callable, *args, **kwargs):
|
def __init__(self, update_every_num_iters: int, func: Callable, *args, **kwargs):
|
||||||
|
# TODO(ethan): how do we type args and kwargs?
|
||||||
self.update_every_num_iters = update_every_num_iters
|
self.update_every_num_iters = update_every_num_iters
|
||||||
self.func = func
|
self.func = func
|
||||||
self.args = args
|
self.args = args
|
||||||
|
|||||||
@@ -14,6 +14,11 @@
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
Visualization code for plotly.
|
Visualization code for plotly.
|
||||||
|
The function use prefix conventions in the following way:
|
||||||
|
- 'get_*' functions (e.g., 'get_camera_frustums')
|
||||||
|
return data that can be ploted with plotly
|
||||||
|
- 'vis_*' functions (e.g., 'vis_camera_rays')
|
||||||
|
return 'go.Figure' objects which are the plots. Go Figure! :')
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import List, Union
|
from typing import List, Union
|
||||||
@@ -93,7 +98,7 @@ def get_line_segments_from_lines(
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def visualize_dataset(camera_origins: TensorType["num_cameras", 3], ray_bundle: RayBundle) -> go.FigureWidget:
|
def vis_dataset(camera_origins: TensorType["num_cameras", 3], ray_bundle: RayBundle) -> go.FigureWidget:
|
||||||
"""Visualize a dataset with plotly using our cameras and generated rays.
|
"""Visualize a dataset with plotly using our cameras and generated rays.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -490,3 +495,18 @@ def vis_camera_rays(camera: Camera) -> go.Figure:
|
|||||||
)
|
)
|
||||||
|
|
||||||
return fig
|
return fig
|
||||||
|
|
||||||
|
|
||||||
|
def get_camera_frustums(cameras: List[Camera]):
|
||||||
|
"""Returns the camera frustums for the cameras that we are using.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
cameras: The cameras that we want to plot.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A plotly scatter that can be plotted.
|
||||||
|
"""
|
||||||
|
for camera in cameras:
|
||||||
|
json_ = camera.to_json()
|
||||||
|
print(json_)
|
||||||
|
raise NotImplementedError
|
||||||
@@ -342,7 +342,7 @@ class VisualizerState:
|
|||||||
)
|
)
|
||||||
intrinsics = get_intrinsics_from_intrinsics_matrix(intrinsics_matrix)
|
intrinsics = get_intrinsics_from_intrinsics_matrix(intrinsics_matrix)
|
||||||
camera = get_camera(intrinsics, camera_to_world)
|
camera = get_camera(intrinsics, camera_to_world)
|
||||||
camera_ray_bundle = camera.get_camera_ray_bundle(device=graph.get_device())
|
camera_ray_bundle = camera.get_camera_ray_bundle(device=graph.device)
|
||||||
camera_ray_bundle.num_rays_per_chunk = self.config.num_rays_per_chunk
|
camera_ray_bundle.num_rays_per_chunk = self.config.num_rays_per_chunk
|
||||||
|
|
||||||
graph.eval()
|
graph.eval()
|
||||||
|
|||||||
Reference in new issue
Block a user