Typecheck workspace (#231)

This commit is contained in:
Matthew Tancik authored and GitHub committed 2022-07-30 15:38:08 -07:00
1 parent db9d83093a
commit 6c45f72c57
19 files changed
+151 -90

No files matched your search

@@ -15,7 +15,7 @@
"""
Code to implement the density grid.
"""
from typing import Callable, List, NoReturn, Tuple
from typing import Callable, List, Tuple, Union
import torch
from torch import nn
@@ -25,7 +25,7 @@ import nerfactory.cuda as nerfactory_cuda
from nerfactory.cameras.rays import RaySamples
def _create_grid_coords(resolution: int, device: torch.device = "cpu") -> TensorType["n_coords", 3]:
def _create_grid_coords(resolution: int, device: Union[torch.device, str] = "cpu") -> TensorType["n_coords", 3]:
"""Create 3D grid coordinates
Args:
@@ -77,6 +77,9 @@ class DensityGrid(nn.Module):
update_every_num_iters (int): How frequently to update the grid values. Defaults to 16.
"""
density_grid: TensorType["num_cascades", "resolution**3"]
mean_density: TensorType[1]
def __init__(
self,
center: float = 0.0,
@@ -155,7 +158,7 @@ class DensityGrid(nn.Module):
step: int,
density_threshold: float = 2, # 0.01 / (SQRT3 / 1024 * 3)
decay: float = 0.95,
) -> NoReturn:
) -> None:
"""Update the density grid in EMA way.
Args:
@@ -197,7 +200,7 @@ class DensityGrid(nn.Module):
# pack to bitfield
self.density_bitfield.data = nerfactory_cuda.packbits(
self.density_grid, min(self.mean_density.item(), density_threshold)
self.density_grid, min(self.mean_density.item(), density_threshold) # type: ignore
)
# TODO(ruilongli): max pooling? https://github.com/NVlabs/instant-ngp/blob/master/src/testbed_nerf.cu#L578
-7
View File
@@ -153,12 +153,5 @@ class TorchInstantNGPField(NeRFField):
)
self.aabb = Parameter(aabb, requires_grad=False)
def get_density(self, ray_samples: RaySamples):
normalized_ray_samples = ray_samples
normalized_ray_samples.positions = SceneBounds.get_normalized_positions(
normalized_ray_samples.frustums.get_positions(), self.aabb
)
return super().get_density(normalized_ray_samples)
field_implementation_to_class = {"tcnn": TCNNInstantNGPField, "torch": TorchInstantNGPField}
+2 -2
View File
@@ -84,7 +84,7 @@ class NeRFField(Field):
self.field_output_density = DensityFieldHead(in_dim=self.mlp_base.get_out_dim())
self.field_heads = nn.ModuleList(field_heads)
for field_head in self.field_heads:
field_head.set_in_dim(self.mlp_head.get_out_dim())
field_head.set_in_dim(self.mlp_head.get_out_dim()) # type: ignore
def get_density(self, ray_samples: RaySamples):
if self.use_integrated_encoding:
@@ -107,6 +107,6 @@ class NeRFField(Field):
outputs = {}
for field_head in self.field_heads:
encoded_dir = self.direction_encoding(ray_samples.frustums.directions)
mlp_out = self.mlp_head(torch.cat([encoded_dir, density_embedding], dim=-1))
mlp_out = self.mlp_head(torch.cat([encoded_dir, density_embedding], dim=-1)) # type: ignore
outputs[field_head.field_head_name] = field_head(mlp_out)
return outputs
+6 -2
View File
@@ -128,11 +128,15 @@ class VanillaNerfWField(Field):
"""
outputs = {}
encoded_dir = self.direction_encoding(ray_samples.frustums.directions)
if ray_samples.camera_indices is None:
raise AttributeError("Camera indices are not provided.")
embedded_appearance = self.embedding_appearance(ray_samples.camera_indices.squeeze())
mlp_head_out = self.mlp_head(torch.cat([density_embedding, encoded_dir, embedded_appearance], dim=-1))
mlp_in = torch.cat([density_embedding, encoded_dir, embedded_appearance], dim=-1) # type: ignore
mlp_head_out = self.mlp_head(mlp_in)
outputs[self.field_head_rgb.field_head_name] = self.field_head_rgb(mlp_head_out) # static rgb
embedded_transient = self.embedding_transient(ray_samples.camera_indices.squeeze())
transient_mlp_out = self.mlp_transient(torch.cat([density_embedding, embedded_transient], dim=-1))
transient_mlp_in = torch.cat([density_embedding, embedded_transient], dim=-1) # type: ignore
transient_mlp_out = self.mlp_transient(transient_mlp_in)
outputs[self.field_head_transient_uncertainty.field_head_name] = self.field_head_transient_uncertainty(
transient_mlp_out
) # uncertainty