torchtyping => jaxtyping (#1838)

* Run `ruff --fix`

* Add jaxtyping migration script

* Run migration scripts + ruff + black

* Merge remote-tracking branch 'origin/main' into brent/jaxtyping

* black

* Fix lint

* fix lint

* Cleanup and more specific types

* Fixed annotation errors

* Fix more errors

* Fix annotation

* Delete _torchtyping_to_jaxtyping.py

---------

Co-authored-by: Matt Tancik <mattt4001@gmail.com>
This commit is contained in:
Brent YiandMatt Tancik authored and GitHub committed 2023-05-16 15:57:47 -07:00
1 parent 9b6290ea43
commit 6ba2a18b2b
65 files changed
+597 -531

No files matched your search

+11 -10
View File
@@ -23,7 +23,8 @@ from pathlib import Path
from typing import Any, Dict, List, Literal, Optional, Type
import torch
from torchtyping import TensorType
from jaxtyping import Float
from torch import Tensor
import nerfstudio.configs.base_config as cfg
from nerfstudio.cameras.cameras import Cameras
@@ -39,7 +40,7 @@ class Semantics:
"""filenames to load semantic data"""
classes: List[str]
"""class labels for data"""
colors: torch.Tensor
colors: Tensor
"""color mapping for classes"""
mask_classes: List[str] = field(default_factory=lambda: [])
"""classes to mask out from training for all modalities"""
@@ -54,9 +55,9 @@ class DataparserOutputs:
"""Filenames for the images."""
cameras: Cameras
"""Camera object storing collection of camera information in dataset."""
alpha_color: Optional[TensorType[3]] = None
alpha_color: Optional[Float[Tensor, "3"]] = None
"""Color of dataset background."""
scene_box: SceneBox = SceneBox()
scene_box: SceneBox = SceneBox(aabb=torch.tensor([[-1, -1, -1], [1, 1, 1]]))
"""Scene box of dataset. Used to bound the scene or provide the scene scale depending on model."""
mask_filenames: Optional[List[Path]] = None
"""Filenames for any masks that are required"""
@@ -64,7 +65,7 @@ class DataparserOutputs:
"""Dictionary of any metadata that be required for the given experiment.
Will be processed by the InputDataset to create any additional tensors that may be required.
"""
dataparser_transform: TensorType[3, 4] = torch.eye(4)[:3, :]
dataparser_transform: Float[Tensor, "3 4"] = torch.eye(4)[:3, :]
"""Transform applied by the dataparser."""
dataparser_scale: float = 1.0
"""Scale applied by the dataparser."""
@@ -91,9 +92,9 @@ class DataparserOutputs:
def transform_poses_to_original_space(
self,
poses: TensorType["num_poses", 3, 4],
poses: Float[Tensor, "num_poses 3 4"],
camera_convention: Literal["opengl", "opencv"] = "opencv",
) -> TensorType["num_poses", 3, 4]:
) -> Float[Tensor, "num_poses 3 4"]:
"""
Transforms the poses in the transformed space back to the original world coordinate system.
Args:
@@ -164,11 +165,11 @@ class DataParser:
def transform_poses_to_original_space(
poses: TensorType["num_poses", 3, 4],
applied_transform: TensorType[3, 4],
poses: Float[Tensor, "num_poses 3 4"],
applied_transform: Float[Tensor, "3 4"],
applied_scale: float,
camera_convention: Literal["opengl", "opencv"] = "opencv",
) -> TensorType["num_poses", 3, 4]:
) -> Float[Tensor, "num_poses 3 4"]:
"""
Transforms the poses in the transformed space back to the original world coordinate system.
Args:
+3 -2
View File
@@ -24,9 +24,10 @@ from typing import Dict, List
import numpy as np
import numpy.typing as npt
import torch
from jaxtyping import Float
from PIL import Image
from torch import Tensor
from torch.utils.data import Dataset
from torchtyping import TensorType
from nerfstudio.data.dataparsers.base_dataparser import DataparserOutputs
from nerfstudio.data.utils.data_utils import get_image_mask_tensor_from_path
@@ -73,7 +74,7 @@ class InputDataset(Dataset):
assert image.shape[2] in [3, 4], f"Image shape of {image.shape} is in correct."
return image
def get_image(self, image_idx: int) -> TensorType["image_height", "image_width", "num_channels"]:
def get_image(self, image_idx: int) -> Float[Tensor, "image_height image_width num_channels"]:
"""Returns a 3 channel image.
Args:
+8 -7
View File
@@ -20,7 +20,8 @@ import random
from typing import Dict, Optional, Union
import torch
from torchtyping import TensorType
from jaxtyping import Int
from torch import Tensor
class PixelSampler: # pylint: disable=too-few-public-methods
@@ -50,9 +51,9 @@ class PixelSampler: # pylint: disable=too-few-public-methods
num_images: int,
image_height: int,
image_width: int,
mask: Optional[TensorType] = None,
mask: Optional[Tensor] = None,
device: Union[torch.device, str] = "cpu",
) -> TensorType["batch_size", 3]:
) -> Int[Tensor, "batch_size 3"]:
"""
Naive pixel sampler, uniformly samples across all possible pixels of all possible images.
@@ -218,9 +219,9 @@ class EquirectangularPixelSampler(PixelSampler): # pylint: disable=too-few-publ
num_images: int,
image_height: int,
image_width: int,
mask: Optional[TensorType] = None,
mask: Optional[Tensor] = None,
device: Union[torch.device, str] = "cpu",
) -> TensorType["batch_size", 3]:
) -> Int[Tensor, "batch_size 3"]:
if isinstance(mask, torch.Tensor):
# Note: if there is a mask, sampling reduces back to uniform sampling, which gives more
# sampling weight to the poles of the image than the equators.
@@ -274,9 +275,9 @@ class PatchPixelSampler(PixelSampler): # pylint: disable=too-few-public-methods
num_images: int,
image_height: int,
image_width: int,
mask: Optional[TensorType] = None,
mask: Optional[Tensor] = None,
device: Union[torch.device, str] = "cpu",
) -> TensorType["batch_size", 3]:
) -> Int[Tensor, "batch_size 3"]:
if mask:
# Note: if there is a mask, sampling reduces back to uniform sampling
indices = super().sample_method(batch_size, num_images, image_height, image_width, mask=mask, device=device)
+5 -4
View File
@@ -20,14 +20,15 @@ from dataclasses import dataclass
from typing import Union
import torch
from torchtyping import TensorType
from jaxtyping import Float
from torch import Tensor
@dataclass
class SceneBox:
"""Data to represent the scene box."""
aabb: TensorType[2, 3] = None
aabb: Float[Tensor, "2 3"]
"""aabb: axis-aligned bounding box.
aabb[0] is the minimum (x,y,z) point.
aabb[1] is the maximum (x,y,z) point."""
@@ -53,7 +54,7 @@ class SceneBox:
return SceneBox(aabb=(self.aabb - self.get_center()) * scale_factor)
@staticmethod
def get_normalized_positions(positions: TensorType[..., 3], aabb: TensorType[2, 3]):
def get_normalized_positions(positions: Float[Tensor, "*batch 3"], aabb: Float[Tensor, "2 3"]):
"""Return normalized positions in range [0, 1] based on the aabb axis-aligned bounding box.
Args:
@@ -65,7 +66,7 @@ class SceneBox:
return normalized_positions
@staticmethod
def from_camera_poses(poses: TensorType[..., 3, 4], scale_factor: float) -> "SceneBox":
def from_camera_poses(poses: Float[Tensor, "*batch 3 4"], scale_factor: float) -> "SceneBox":
"""Returns the instance of SceneBox that fully envelopes a set of poses
Args:
@@ -40,7 +40,6 @@ TODO(1480) Delete this file when moving to pycolmap.
#
# Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
import argparse
import collections
import os
import struct