Type fixes and cleanup (#1937)

This commit is contained in:
Matthew Tancik authored and GitHub committed 2023-05-16 18:47:46 -07:00
1 parent 6ba2a18b2b
commit 8b9574bb5a
7 files changed
+66 -55

No files matched your search

+2 -2
View File
@@ -37,8 +37,8 @@ def get_interpolated_camera_path(cameras: Cameras, steps: int, order_poses: bool
Returns: Returns:
A new set of cameras along a path. A new set of cameras along a path.
""" """
Ks = cameras.get_intrinsics_matrices().cpu().numpy() Ks = cameras.get_intrinsics_matrices()
poses = cameras.camera_to_worlds.cpu().numpy() poses = cameras.camera_to_worlds
poses, Ks = get_interpolated_poses_many(poses, Ks, steps_per_transition=steps, order_poses=order_poses) poses, Ks = get_interpolated_poses_many(poses, Ks, steps_per_transition=steps, order_poses=order_poses)
cameras = Cameras( cameras = Cameras(
+34 -30
View File
@@ -22,13 +22,13 @@ from typing import List, Literal, Optional, Tuple
import numpy as np import numpy as np
import torch import torch
from jaxtyping import Float from jaxtyping import Float
from numpy.typing import ArrayLike from numpy.typing import NDArray
from torch import Tensor from torch import Tensor
_EPS = np.finfo(float).eps * 4.0 _EPS = np.finfo(float).eps * 4.0
def unit_vector(data: ArrayLike, axis: Optional[int] = None) -> np.ndarray: def unit_vector(data: NDArray, axis: Optional[int] = None) -> np.ndarray:
"""Return ndarray normalized by length, i.e. Euclidean norm, along axis. """Return ndarray normalized by length, i.e. Euclidean norm, along axis.
Args: Args:
@@ -47,7 +47,7 @@ def unit_vector(data: ArrayLike, axis: Optional[int] = None) -> np.ndarray:
return data return data
def quaternion_from_matrix(matrix: ArrayLike, isprecise: bool = False) -> np.ndarray: def quaternion_from_matrix(matrix: NDArray, isprecise: bool = False) -> np.ndarray:
"""Return quaternion from rotation matrix. """Return quaternion from rotation matrix.
Args: Args:
@@ -86,14 +86,13 @@ def quaternion_from_matrix(matrix: ArrayLike, isprecise: bool = False) -> np.nda
m21 = M[2, 1] m21 = M[2, 1]
m22 = M[2, 2] m22 = M[2, 2]
# symmetric matrix K # symmetric matrix K
K = np.array( K = [
[ [m00 - m11 - m22, 0.0, 0.0, 0.0],
[m00 - m11 - m22, 0.0, 0.0, 0.0], [m01 + m10, m11 - m00 - m22, 0.0, 0.0],
[m01 + m10, m11 - m00 - m22, 0.0, 0.0], [m02 + m20, m12 + m21, m22 - m00 - m11, 0.0],
[m02 + m20, m12 + m21, m22 - m00 - m11, 0.0], [m21 - m12, m02 - m20, m10 - m01, m00 + m11 + m22],
[m21 - m12, m02 - m20, m10 - m01, m00 + m11 + m22], ]
] K = np.array(K)
)
K /= 3.0 K /= 3.0
# quaternion is eigenvector of K that corresponds to largest eigenvalue # quaternion is eigenvector of K that corresponds to largest eigenvalue
w, V = np.linalg.eigh(K) w, V = np.linalg.eigh(K)
@@ -104,7 +103,7 @@ def quaternion_from_matrix(matrix: ArrayLike, isprecise: bool = False) -> np.nda
def quaternion_slerp( def quaternion_slerp(
quat0: ArrayLike, quat1: ArrayLike, fraction: float, spin: int = 0, shortestpath: bool = True quat0: NDArray, quat1: NDArray, fraction: float, spin: int = 0, shortestpath: bool = True
) -> np.ndarray: ) -> np.ndarray:
"""Return spherical linear interpolation between two quaternions. """Return spherical linear interpolation between two quaternions.
Args: Args:
@@ -139,7 +138,7 @@ def quaternion_slerp(
return q0 return q0
def quaternion_matrix(quaternion: ArrayLike) -> np.ndarray: def quaternion_matrix(quaternion: NDArray) -> np.ndarray:
"""Return homogeneous rotation matrix from quaternion. """Return homogeneous rotation matrix from quaternion.
Args: Args:
@@ -161,7 +160,7 @@ def quaternion_matrix(quaternion: ArrayLike) -> np.ndarray:
) )
def get_interpolated_poses(pose_a: ArrayLike, pose_b: ArrayLike, steps: int = 10) -> List[float]: def get_interpolated_poses(pose_a: NDArray, pose_b: NDArray, steps: int = 10) -> List[float]:
"""Return interpolation of poses with specified number of steps. """Return interpolation of poses with specified number of steps.
Args: Args:
pose_a: first pose pose_a: first pose
@@ -185,7 +184,9 @@ def get_interpolated_poses(pose_a: ArrayLike, pose_b: ArrayLike, steps: int = 10
return poses_ab return poses_ab
def get_interpolated_k(k_a, k_b, steps: int = 10) -> Float[Tensor, "3 4"]: def get_interpolated_k(
k_a: Float[Tensor, "3 3"], k_b: Float[Tensor, "3 3"], steps: int = 10
) -> List[Float[Tensor, "3 4"]]:
""" """
Returns interpolated path between two camera poses with specified number of steps. Returns interpolated path between two camera poses with specified number of steps.
@@ -193,8 +194,11 @@ def get_interpolated_k(k_a, k_b, steps: int = 10) -> Float[Tensor, "3 4"]:
k_a: camera matrix 1 k_a: camera matrix 1
k_b: camera matrix 2 k_b: camera matrix 2
steps: number of steps the interpolated pose path should contain steps: number of steps the interpolated pose path should contain
Returns:
List of interpolated camera poses
""" """
Ks = [] Ks: List[Float[Tensor, "3 3"]] = []
ts = np.linspace(0, 1, steps) ts = np.linspace(0, 1, steps)
for t in ts: for t in ts:
new_k = k_a * (1.0 - t) + k_b * t new_k = k_a * (1.0 - t) + k_b * t
@@ -220,20 +224,20 @@ def get_ordered_poses_and_k(
poses_num = len(poses) poses_num = len(poses)
ordered_poses = np.expand_dims(poses[0], 0) ordered_poses = torch.unsqueeze(poses[0], 0)
ordered_ks = np.expand_dims(Ks[0], 0) ordered_ks = torch.unsqueeze(Ks[0], 0)
# remove the first pose from poses # remove the first pose from poses
poses = np.delete(poses, 0, axis=0) poses = poses[1:]
Ks = np.delete(Ks, 0, axis=0) Ks = Ks[1:]
for _ in range(poses_num - 1): for _ in range(poses_num - 1):
distances = np.linalg.norm(ordered_poses[-1][:, 3] - poses[:, :, 3], axis=1) distances = torch.norm(ordered_poses[-1][:, 3] - poses[:, :, 3], dim=1)
idx = np.argmin(distances) idx = torch.argmin(distances)
ordered_poses = np.concatenate((ordered_poses, np.expand_dims(poses[idx], 0)), axis=0) ordered_poses = torch.cat((ordered_poses, torch.unsqueeze(poses[idx], 0)), dim=0)
ordered_ks = np.concatenate((ordered_ks, np.expand_dims(Ks[idx], 0)), axis=0) ordered_ks = torch.cat((ordered_ks, torch.unsqueeze(Ks[idx], 0)), dim=0)
poses = np.delete(poses, idx, axis=0) poses = torch.cat((poses[0:idx], poses[idx + 1 :]), dim=0)
Ks = np.delete(Ks, idx, axis=0) Ks = torch.cat((Ks[0:idx], Ks[idx + 1 :]), dim=0)
return ordered_poses, ordered_ks return ordered_poses, ordered_ks
@@ -262,14 +266,14 @@ def get_interpolated_poses_many(
poses, Ks = get_ordered_poses_and_k(poses, Ks) poses, Ks = get_ordered_poses_and_k(poses, Ks)
for idx in range(poses.shape[0] - 1): for idx in range(poses.shape[0] - 1):
pose_a = poses[idx] pose_a = poses[idx].cpu().numpy()
pose_b = poses[idx + 1] pose_b = poses[idx + 1].cpu().numpy()
poses_ab = get_interpolated_poses(pose_a, pose_b, steps=steps_per_transition) poses_ab = get_interpolated_poses(pose_a, pose_b, steps=steps_per_transition)
traj += poses_ab traj += poses_ab
k_interp += get_interpolated_k(Ks[idx], Ks[idx + 1], steps=steps_per_transition) k_interp += get_interpolated_k(Ks[idx], Ks[idx + 1], steps=steps_per_transition)
traj = np.stack(traj, axis=0) traj = np.stack(traj, axis=0)
k_interp = np.stack(k_interp, axis=0) k_interp = torch.stack(k_interp, dim=0)
return torch.tensor(traj, dtype=torch.float32), torch.tensor(k_interp, dtype=torch.float32) return torch.tensor(traj, dtype=torch.float32), torch.tensor(k_interp, dtype=torch.float32)
@@ -565,7 +569,7 @@ def auto_orient_and_center_poses(
transform = torch.cat([eigvec, eigvec @ -translation[..., None]], dim=-1) transform = torch.cat([eigvec, eigvec @ -translation[..., None]], dim=-1)
oriented_poses = transform @ poses oriented_poses = transform @ poses
if oriented_poses.mean(axis=0)[2, 1] < 0: if oriented_poses.mean(dim=0)[2, 1] < 0:
oriented_poses[:, 1:3] = -1 * oriented_poses[:, 1:3] oriented_poses[:, 1:3] = -1 * oriented_poses[:, 1:3]
elif method in ("up", "vertical"): elif method in ("up", "vertical"):
up = torch.mean(poses[:, :3, 1], dim=0) up = torch.mean(poses[:, :3, 1], dim=0)
+11 -10
View File
@@ -100,13 +100,11 @@ class Cameras(TensorDataclass):
width: Optional[Union[Shaped[Tensor, "*batch_ws 1"], int]] = None, width: Optional[Union[Shaped[Tensor, "*batch_ws 1"], int]] = None,
height: Optional[Union[Shaped[Tensor, "*batch_hs 1"], int]] = None, height: Optional[Union[Shaped[Tensor, "*batch_hs 1"], int]] = None,
distortion_params: Optional[Float[Tensor, "*batch_dist_params 6"]] = None, distortion_params: Optional[Float[Tensor, "*batch_dist_params 6"]] = None,
camera_type: Optional[ camera_type: Union[
Union[ Int[Tensor, "*batch_cam_types 1"],
Int[Tensor, "*batch_cam_types 1"], int,
int, List[CameraType],
List[CameraType], CameraType,
CameraType,
]
] = CameraType.PERSPECTIVE, ] = CameraType.PERSPECTIVE,
times: Optional[Float[Tensor, "num_cameras"]] = None, times: Optional[Float[Tensor, "num_cameras"]] = None,
metadata: Optional[Dict] = None, metadata: Optional[Dict] = None,
@@ -297,8 +295,8 @@ class Cameras(TensorDataclass):
Grid of image coordinates. Grid of image coordinates.
""" """
if index is None: if index is None:
image_height = torch.max(self.image_height.view(-1)) image_height = torch.max(self.image_height.view(-1)).item()
image_width = torch.max(self.image_width.view(-1)) image_width = torch.max(self.image_width.view(-1)).item()
image_coords = torch.meshgrid(torch.arange(image_height), torch.arange(image_width), indexing="ij") image_coords = torch.meshgrid(torch.arange(image_height), torch.arange(image_width), indexing="ij")
image_coords = torch.stack(image_coords, dim=-1) + pixel_offset # stored as (y, x) coordinates image_coords = torch.stack(image_coords, dim=-1) + pixel_offset # stored as (y, x) coordinates
else: else:
@@ -748,6 +746,9 @@ class Cameras(TensorDataclass):
A JSON representation of the camera A JSON representation of the camera
""" """
flattened = self.flatten() flattened = self.flatten()
times = flattened[camera_idx].times
if times is not None:
times = times.item()
json_ = { json_ = {
"type": "PinholeCamera", "type": "PinholeCamera",
"cx": flattened[camera_idx].cx.item(), "cx": flattened[camera_idx].cx.item(),
@@ -756,7 +757,7 @@ class Cameras(TensorDataclass):
"fy": flattened[camera_idx].fy.item(), "fy": flattened[camera_idx].fy.item(),
"camera_to_world": self.camera_to_worlds[camera_idx].tolist(), "camera_to_world": self.camera_to_worlds[camera_idx].tolist(),
"camera_index": camera_idx, "camera_index": camera_idx,
"times": flattened[camera_idx].times.item() if self.times is not None else None, "times": times,
} }
if image is not None: if image is not None:
image_uint8 = (image * 255).detach().type(torch.uint8) image_uint8 = (image * 255).detach().type(torch.uint8)
@@ -17,7 +17,7 @@ from __future__ import annotations
import math import math
from dataclasses import dataclass, field from dataclasses import dataclass, field
from pathlib import Path, PurePath from pathlib import Path
from typing import Literal, Optional, Type from typing import Literal, Optional, Type
import numpy as np import numpy as np
@@ -109,7 +109,7 @@ class Nerfstudio(DataParser):
distort = [] distort = []
for frame in meta["frames"]: for frame in meta["frames"]:
filepath = PurePath(frame["file_path"]) filepath = Path(frame["file_path"])
fname = self._get_fname(filepath, data_dir) fname = self._get_fname(filepath, data_dir)
if not fname.exists(): if not fname.exists():
num_skipped_image_filenames += 1 num_skipped_image_filenames += 1
@@ -148,7 +148,7 @@ class Nerfstudio(DataParser):
image_filenames.append(fname) image_filenames.append(fname)
poses.append(np.array(frame["transform_matrix"])) poses.append(np.array(frame["transform_matrix"]))
if "mask_path" in frame: if "mask_path" in frame:
mask_filepath = PurePath(frame["mask_path"]) mask_filepath = Path(frame["mask_path"])
mask_fname = self._get_fname( mask_fname = self._get_fname(
mask_filepath, mask_filepath,
data_dir, data_dir,
@@ -157,7 +157,7 @@ class Nerfstudio(DataParser):
mask_filenames.append(mask_fname) mask_filenames.append(mask_fname)
if "depth_file_path" in frame: if "depth_file_path" in frame:
depth_filepath = PurePath(frame["depth_file_path"]) depth_filepath = Path(frame["depth_file_path"])
depth_fname = self._get_fname(depth_filepath, data_dir, downsample_folder_prefix="depths_") depth_fname = self._get_fname(depth_filepath, data_dir, downsample_folder_prefix="depths_")
depth_filenames.append(depth_fname) depth_filenames.append(depth_fname)
@@ -185,7 +185,7 @@ class Nerfstudio(DataParser):
has_split_files_spec = any(f"{split}_filenames" in meta for split in ("train", "val", "test")) has_split_files_spec = any(f"{split}_filenames" in meta for split in ("train", "val", "test"))
if f"{split}_filenames" in meta: if f"{split}_filenames" in meta:
# Validate split first # Validate split first
split_filenames = set(self._get_fname(PurePath(x), data_dir) for x in meta[f"{split}_filenames"]) split_filenames = set(self._get_fname(Path(x), data_dir) for x in meta[f"{split}_filenames"])
unmatched_filenames = split_filenames.difference(image_filenames) unmatched_filenames = split_filenames.difference(image_filenames)
if unmatched_filenames: if unmatched_filenames:
raise RuntimeError(f"Some filenames for split {split} were not found: {unmatched_filenames}.") raise RuntimeError(f"Some filenames for split {split} were not found: {unmatched_filenames}.")
@@ -238,7 +238,9 @@ class Nerfstudio(DataParser):
image_filenames = [image_filenames[i] for i in indices] image_filenames = [image_filenames[i] for i in indices]
mask_filenames = [mask_filenames[i] for i in indices] if len(mask_filenames) > 0 else [] mask_filenames = [mask_filenames[i] for i in indices] if len(mask_filenames) > 0 else []
depth_filenames = [depth_filenames[i] for i in indices] if len(depth_filenames) > 0 else [] depth_filenames = [depth_filenames[i] for i in indices] if len(depth_filenames) > 0 else []
poses = poses[indices]
idx_tensor = torch.tensor(indices, dtype=torch.long)
poses = poses[idx_tensor]
# in x,y,z order # in x,y,z order
# assumes that the scene is centered at the origin # assumes that the scene is centered at the origin
@@ -254,7 +256,6 @@ class Nerfstudio(DataParser):
else: else:
camera_type = CameraType.PERSPECTIVE camera_type = CameraType.PERSPECTIVE
idx_tensor = torch.tensor(indices, dtype=torch.long)
fx = float(meta["fl_x"]) if fx_fixed else torch.tensor(fx, dtype=torch.float32)[idx_tensor] fx = float(meta["fl_x"]) if fx_fixed else torch.tensor(fx, dtype=torch.float32)[idx_tensor]
fy = float(meta["fl_y"]) if fy_fixed else torch.tensor(fy, dtype=torch.float32)[idx_tensor] fy = float(meta["fl_y"]) if fy_fixed else torch.tensor(fy, dtype=torch.float32)[idx_tensor]
cx = float(meta["cx"]) if cx_fixed else torch.tensor(cx, dtype=torch.float32)[idx_tensor] cx = float(meta["cx"]) if cx_fixed else torch.tensor(cx, dtype=torch.float32)[idx_tensor]
@@ -311,7 +312,7 @@ class Nerfstudio(DataParser):
) )
return dataparser_outputs return dataparser_outputs
def _get_fname(self, filepath: PurePath, data_dir: PurePath, downsample_folder_prefix="images_") -> Path: def _get_fname(self, filepath: Path, data_dir: Path, downsample_folder_prefix="images_") -> Path:
"""Get the filename of the image file. """Get the filename of the image file.
downsample_folder_prefix can be used to point to auxiliary image data, e.g. masks downsample_folder_prefix can be used to point to auxiliary image data, e.g. masks
@@ -136,6 +136,7 @@ class TemporalHashMLPDensityField(Field):
else: else:
positions = SceneBox.get_normalized_positions(ray_samples.frustums.get_positions(), self.aabb) positions = SceneBox.get_normalized_positions(ray_samples.frustums.get_positions(), self.aabb)
positions_flat = positions.view(-1, 3) positions_flat = positions.view(-1, 3)
assert ray_samples.times is not None
time_flat = ray_samples.times.reshape(-1, 1) time_flat = ray_samples.times.reshape(-1, 1)
x = self.encoding(positions_flat, time_flat).to(positions) x = self.encoding(positions_flat, time_flat).to(positions)
density_before_activation = self.linear(x).view(*ray_samples.frustums.shape, -1) density_before_activation = self.linear(x).view(*ray_samples.frustums.shape, -1)
@@ -233,7 +234,7 @@ class NerfplayerNerfactoField(Field):
num_levels=num_levels, num_levels=num_levels,
level_dim=features_per_level, level_dim=features_per_level,
log2_hashmap_size=log2_hashmap_size, log2_hashmap_size=log2_hashmap_size,
desired_resolution=1024 * (self.aabb.max() - self.aabb.min()), desired_resolution=int(1024 * (self.aabb.max().item() - self.aabb.min().item())),
) )
self.mlp_base_decode = tcnn.Network( self.mlp_base_decode = tcnn.Network(
n_input_dims=num_levels * features_per_level, n_input_dims=num_levels * features_per_level,
+4 -2
View File
@@ -440,5 +440,7 @@ def entrypoint():
if __name__ == "__main__": if __name__ == "__main__":
entrypoint() entrypoint()
# For sphinx docs
get_parser_fn = lambda: tyro.extras.get_parser(Commands) # noqa def get_parser_fn():
"""Get the parser function for the sphinx docs."""
return tyro.extras.get_parser(Commands) # noqa
+4 -2
View File
@@ -448,5 +448,7 @@ def entrypoint():
if __name__ == "__main__": if __name__ == "__main__":
entrypoint() entrypoint()
# For sphinx docs
get_parser_fn = lambda: tyro.extras.get_parser(Commands) # noqa def get_parser_fn():
"""Get the parser function for the sphinx docs."""
return tyro.extras.get_parser(Commands) # noqa