More typing fixes (#1941)

* Fix more types

* Fix tests

* Minor cleaning

* Address comments

* Resolve linting errors

* Re-ignore pymeshlab type

* Add extra assert / error message to mipnerf model
This commit is contained in:
Brent Yi authored and GitHub committed 2023-05-17 07:17:40 -07:00
1 parent 8b9574bb5a
commit 814db32ec4
53 files changed
+386 -205

No files matched your search

+2 -4
View File
@@ -24,7 +24,7 @@ from torch import Tensor
from nerfstudio.utils import colors
Colormaps = Literal["default", "turbo", "viridis", "magma", "inferno", "cividis"]
Colormaps = Literal["default", "turbo", "viridis", "magma", "inferno", "cividis", "pca"]
@dataclass(frozen=True)
@@ -103,15 +103,13 @@ def apply_float_colormap(image: Float[Tensor, "*bs 1"], colormap: Colormaps = "v
if colormap == "default":
colormap = "turbo"
colormap = matplotlib.colormaps[colormap]
colormap = torch.tensor(colormap.colors).to(image.device) # type: ignore
image = torch.nan_to_num(image, 0)
image_long = (image * 255).long()
image_long_min = torch.min(image_long)
image_long_max = torch.max(image_long)
assert image_long_min >= 0, f"the min value is {image_long_min}"
assert image_long_max <= 255, f"the max value is {image_long_max}"
return colormap[image_long[..., 0]]
return torch.tensor(matplotlib.colormaps[colormap].colors, device=image.device)[image_long[..., 0]]
def apply_depth_colormap(
+3 -1
View File
@@ -25,6 +25,7 @@ from typing import Literal, Optional, Tuple
import torch
import yaml
from nerfstudio.data.datamanagers.base_datamanager import VanillaDataManagerConfig
from nerfstudio.engine.trainer import TrainerConfig
from nerfstudio.pipelines.base_pipeline import Pipeline
from nerfstudio.utils.rich_utils import CONSOLE
@@ -92,7 +93,8 @@ def eval_setup(
# load checkpoints from wherever they were saved
# TODO: expose the ability to choose an arbitrary checkpoint
config.load_dir = config.get_checkpoint_dir()
config.pipeline.datamanager.eval_image_indices = None
if isinstance(config.pipeline.datamanager, VanillaDataManagerConfig):
config.pipeline.datamanager.eval_image_indices = None
# setup pipeline (which includes the DataManager)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
+4 -2
View File
@@ -258,7 +258,7 @@ def masked_reduction(
input_tensor: Float[Tensor, "1 32 mult"],
mask: Bool[Tensor, "1 32 mult"],
reduction_type: Literal["image", "batch"],
):
) -> Tensor:
"""
Whether to consolidate the input_tensor across the batch or across the image
Args:
@@ -271,7 +271,9 @@ def masked_reduction(
if reduction_type == "batch":
# avoid division by 0 (if sum(M) = sum(sum(mask)) = 0: sum(image_loss) = 0)
divisor = torch.sum(mask)
input_tensor = 0 if divisor == 0 else torch.sum(input_tensor) / divisor
if divisor == 0:
return torch.tensor(0, device=input_tensor.device)
input_tensor = torch.sum(input_tensor) / divisor
elif reduction_type == "image":
# avoid division by 0 (if M = sum(mask) = 0: image_loss = 0)
valid = mask.nonzero()
+22 -15
View File
@@ -82,7 +82,7 @@ def get_line_segments_from_lines(
marker_color = colors[idx]
thiscolor = colors[idx]
data.append(
go.Scatter3d(
go.Scatter3d( # type: ignore
x=line[:, 0],
y=line[:, 1],
z=line[:, 2],
@@ -100,7 +100,10 @@ def get_line_segments_from_lines(
return data
def vis_dataset(camera_origins: Float[Tensor, "num_cameras 3"], ray_bundle: RayBundle) -> go.FigureWidget:
def vis_dataset(
camera_origins: Float[Tensor, "num_cameras 3"],
ray_bundle: RayBundle,
) -> go.FigureWidget: # type: ignore
"""Visualize a dataset with plotly using our cameras and generated rays.
Args:
@@ -117,7 +120,7 @@ def vis_dataset(camera_origins: Float[Tensor, "num_cameras 3"], ray_bundle: RayB
data = []
data += [
go.Scatter3d(
go.Scatter3d( # type: ignore
x=camera_origins[::skip, 0],
y=camera_origins[::skip, 1],
z=camera_origins[::skip, 2],
@@ -144,7 +147,7 @@ def vis_dataset(camera_origins: Float[Tensor, "num_cameras 3"], ray_bundle: RayB
camera=dict(up=dict(x=0, y=0, z=1), center=dict(x=0, y=0, z=0), eye=dict(x=1.25, y=1.25, z=1.25)),
),
)
fig = go.Figure(data=data, layout=layout)
fig = go.Figure(data=data, layout=layout) # type: ignore
return fig
@@ -166,7 +169,11 @@ def get_random_color(colormap: Optional[List[str]] = None, idx: Optional[int] =
def get_sphere(
radius: float, center: Float[Tensor, "3"] = None, color: str = "black", opacity: float = 1.0, resolution: int = 32
radius: float,
center: Optional[Float[Tensor, "3"]] = None,
color: str = "black",
opacity: float = 1.0,
resolution: int = 32,
) -> go.Mesh3d: # type: ignore
"""Returns a sphere object for plotting with plotly.
@@ -193,7 +200,7 @@ def get_sphere(
if center is not None:
pts += center
return go.Mesh3d(
return go.Mesh3d( # type: ignore
{
"x": pts[:, :, 0].flatten(),
"y": pts[:, :, 1].flatten(),
@@ -207,7 +214,7 @@ def get_sphere(
def get_cube(
side_length: Union[float, torch.Tensor],
center: Float[Tensor, "3"] = None,
center: Optional[Float[Tensor, "3"]] = None,
color: str = "black",
opacity: float = 1.0,
) -> go.Mesh3d: # type: ignore
@@ -241,7 +248,7 @@ def get_cube(
pts[1] = np.add(pts[1], center[1])
pts[2] = np.add(pts[2], center[2])
return go.Mesh3d(
return go.Mesh3d( # type: ignore
{
"x": pts[0],
"y": pts[1],
@@ -299,7 +306,7 @@ def get_gaussian_ellipsiod(
pts += mean
return go.Mesh3d(
return go.Mesh3d( # type: ignore
{
"x": pts[:, :, 0].flatten(),
"y": pts[:, :, 1].flatten(),
@@ -328,7 +335,7 @@ def get_gaussian_ellipsoids_list(
"""
data = []
vis_means = go.Scatter3d(
vis_means = go.Scatter3d( # type: ignore
x=gaussians.mean[:, 0],
y=gaussians.mean[:, 1],
z=gaussians.mean[:, 2],
@@ -394,7 +401,7 @@ def get_frustum_mesh(
pts = torch.einsum("kj,ij->ki", pts, rotation)
pts += frustum.origins
return go.Mesh3d(
return go.Mesh3d( # type: ignore
x=pts[..., 0],
y=pts[..., 1],
z=pts[..., 2],
@@ -448,7 +455,7 @@ def get_frustum_points(
frustum = frustum.flatten()
pts = frustum.get_positions()
return go.Scatter3d(
return go.Scatter3d( # type: ignore
x=pts[..., 0],
y=pts[..., 1],
z=pts[..., 2],
@@ -483,7 +490,7 @@ def get_ray_bundle_lines(
lines = torch.empty((origins.shape[0] * 2, 3))
lines[0::2] = origins
lines[1::2] = origins + directions * length
return go.Scatter3d(
return go.Scatter3d( # type: ignore
x=lines[..., 0],
y=lines[..., 1],
z=lines[..., 2],
@@ -522,8 +529,8 @@ def vis_camera_rays(cameras: Cameras) -> go.Figure: # type: ignore
colors[0::2] = coords
colors[1::2] = coords
fig = go.Figure(
data=go.Scatter3d(
fig = go.Figure( # type: ignore
data=go.Scatter3d( # type: ignore
x=lines[:, 0],
y=lines[:, 2],
z=lines[:, 1],
+38 -2
View File
@@ -24,7 +24,18 @@ import typing
from collections import deque
from contextlib import ContextDecorator, contextmanager
from pathlib import Path
from typing import Callable, Dict, List, Optional, Tuple, Union
from typing import (
Any,
Callable,
ContextManager,
Dict,
List,
Optional,
Tuple,
TypeVar,
Union,
overload,
)
from torch.profiler import ProfilerActivity, profile, record_function
@@ -41,7 +52,32 @@ PROFILER = []
PYTORCH_PROFILER = None
class time_function(ContextDecorator): # pylint: disable=invalid-name
CallableT = TypeVar("CallableT", bound=Callable)
@overload
def time_function(name_or_func: CallableT) -> CallableT:
...
@overload
def time_function(name_or_func: str) -> ContextManager[Any]:
...
def time_function(name_or_func: Union[CallableT, str]) -> Union[CallableT, ContextManager[Any]]:
"""Profile a function or block of code. Can be used either to create a context or to wrap a function.
Args:
name_or_func: Either the name of a context or function to profile.
Returns:
A wrapped function or context to use in a `with` statement.
"""
return _TimeFunction(name_or_func)
class _TimeFunction(ContextDecorator): # pylint: disable=invalid-name
"""Decorator/Context manager: time a function call or a block of code"""
def __init__(self, name: Union[str, Callable]):
+2 -1
View File
@@ -22,6 +22,7 @@ from rich.progress import (
BarColumn,
Progress,
ProgressColumn,
Task,
TaskProgressColumn,
TextColumn,
TimeRemainingColumn,
@@ -38,7 +39,7 @@ class ItersPerSecColumn(ProgressColumn):
super().__init__()
self.suffix = suffix
def render(self, task: "Task") -> Text:
def render(self, task: Task) -> Text:
"""Show data transfer speed."""
speed = task.finished_speed or task.speed
if speed is None:
+12 -9
View File
@@ -76,23 +76,23 @@ class TensorDataclass:
isinstance(v, int) and v > 1
), f"Custom dimensions must be an integer greater than 1, since 1 is the default, received {k}: {v}"
if not dataclasses.is_dataclass(self):
# Shim to prevent pyright from narrowing `self` to DataclassInstance.
self_dc = self
if not dataclasses.is_dataclass(self_dc):
raise TypeError("TensorDataclass must be a dataclass")
batch_shapes = self._get_dict_batch_shapes(
{f.name: self.__getattribute__(f.name) for f in dataclasses.fields(self)}
)
batch_shapes = self._get_dict_batch_shapes({f.name: getattr(self, f.name) for f in dataclasses.fields(self_dc)})
if len(batch_shapes) == 0:
raise ValueError("TensorDataclass must have at least one tensor")
batch_shape = torch.broadcast_shapes(*batch_shapes)
broadcasted_fields = self._broadcast_dict_fields(
{f.name: self.__getattribute__(f.name) for f in dataclasses.fields(self)}, batch_shape
{f.name: getattr(self, f.name) for f in dataclasses.fields(self_dc)}, batch_shape
)
for f, v in broadcasted_fields.items():
self.__setattr__(f, v)
object.__setattr__(self, f, v)
self.__setattr__("_shape", batch_shape)
object.__setattr__(self, "_shape", batch_shape)
def _get_dict_batch_shapes(self, dict_: Dict) -> List:
"""Returns batch shapes of all tensors in a dictionary
@@ -289,14 +289,17 @@ class TensorDataclass:
A new TensorDataclass with the same data but with a new shape.
"""
self_dc = self
assert dataclasses.is_dataclass(self_dc)
new_fields = self._apply_fn_to_dict(
{f.name: self.__getattribute__(f.name) for f in dataclasses.fields(self)},
{f.name: getattr(self, f.name) for f in dataclasses.fields(self_dc)},
fn,
dataclass_fn,
custom_tensor_dims_fn,
)
return dataclasses.replace(self, **new_fields)
return dataclasses.replace(self_dc, **new_fields)
def _apply_fn_to_dict(
self,