mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-06 07:48:50 +08:00
Occupancy grid (#79)
* occupancy grid in progress * visualizing occupancy grid. currently restructing field and renderer head outputs * black and pylint. broke nerfw and semantic nerf but it's okay * get_project_root
This commit is contained in:
1 parent
5c2d8c2a0b
commit
fb440fedf3
23 files changed
+423
-217
No files matched your search
@@ -2,7 +2,7 @@
|
||||
Collection of render heads
|
||||
"""
|
||||
from enum import Enum
|
||||
from typing import Dict, Optional
|
||||
from typing import Optional
|
||||
|
||||
from torch import nn
|
||||
from torchtyping import TensorType
|
||||
@@ -26,27 +26,23 @@ class FieldHeadNames(Enum):
|
||||
class FieldHead(FieldModule):
|
||||
"""Base field output"""
|
||||
|
||||
def __init__(
|
||||
self, in_dim: int, out_dim: int, field_head_name: FieldHeadNames, activation: Optional[nn.Module] = None
|
||||
) -> None:
|
||||
def __init__(self, in_dim: int, out_dim: int, activation: Optional[nn.Module] = None) -> None:
|
||||
"""
|
||||
Args:
|
||||
in_dim (int): input dimension
|
||||
out_dim (int): output dimension for renderer
|
||||
field_head_name (FieldHeadNames): name of field head
|
||||
activation (Optional[nn.Module]): output head activation
|
||||
"""
|
||||
super().__init__()
|
||||
self.in_dim = in_dim
|
||||
self.out_dim = out_dim
|
||||
self.field_head_name = field_head_name
|
||||
self.activation = activation
|
||||
layers = [nn.Linear(self.in_dim, self.out_dim)]
|
||||
if self.activation:
|
||||
layers.append(self.activation)
|
||||
self.net = nn.Sequential(*layers)
|
||||
|
||||
def forward(self, in_tensor: TensorType[..., "in_dim"]) -> Dict[FieldHeadNames, TensorType]:
|
||||
def forward(self, in_tensor: TensorType[..., "in_dim"]) -> TensorType:
|
||||
"""Process network output for renderer
|
||||
|
||||
Args:
|
||||
@@ -55,22 +51,24 @@ class FieldHead(FieldModule):
|
||||
Returns:
|
||||
TensorType[..., "out_dim"]: Render head output
|
||||
"""
|
||||
if not self.net:
|
||||
raise SystemError("Render head network not initialized. build_nn_modules() should be called.")
|
||||
out_tensor = self.net(in_tensor)
|
||||
return {self.field_head_name: out_tensor}
|
||||
return out_tensor
|
||||
|
||||
|
||||
class DensityFieldHead(FieldHead):
|
||||
"""Density output"""
|
||||
|
||||
def __init__(self, in_dim: int, activation: Optional[nn.Module] = nn.Softplus()) -> None:
|
||||
super().__init__(in_dim, out_dim=1, field_head_name=FieldHeadNames.DENSITY, activation=activation)
|
||||
super().__init__(in_dim, out_dim=1, activation=activation)
|
||||
|
||||
|
||||
class RGBFieldHead(FieldHead):
|
||||
"""RGB output"""
|
||||
|
||||
def __init__(self, in_dim: int, activation: Optional[nn.Module] = nn.Sigmoid()) -> None:
|
||||
super().__init__(in_dim, out_dim=3, field_head_name=FieldHeadNames.RGB, activation=activation)
|
||||
super().__init__(in_dim, out_dim=3, activation=activation)
|
||||
|
||||
|
||||
class SHFieldHead(FieldHead):
|
||||
@@ -86,28 +84,28 @@ class SHFieldHead(FieldHead):
|
||||
activation (Optional[nn.Module], optional): Output activation. Defaults to None.
|
||||
"""
|
||||
out_dim = channels * levels**2
|
||||
super().__init__(in_dim, out_dim=out_dim, field_head_name=FieldHeadNames.SH, activation=activation)
|
||||
super().__init__(in_dim, out_dim=out_dim, activation=activation)
|
||||
|
||||
|
||||
class UncertaintyFieldHead(FieldHead):
|
||||
"""Uncertainty output"""
|
||||
|
||||
def __init__(self, in_dim: int, activation: Optional[nn.Module] = nn.Softplus()) -> None:
|
||||
super().__init__(in_dim, out_dim=1, field_head_name=FieldHeadNames.UNCERTAINTY, activation=activation)
|
||||
super().__init__(in_dim, out_dim=1, activation=activation)
|
||||
|
||||
|
||||
class TransientRGBHead(FieldHead):
|
||||
"""Transient RGB output"""
|
||||
|
||||
def __init__(self, in_dim: int, activation: Optional[nn.Module] = nn.Sigmoid()) -> None:
|
||||
super().__init__(in_dim, out_dim=3, field_head_name=FieldHeadNames.TRANSIENT_RGB, activation=activation)
|
||||
super().__init__(in_dim, out_dim=3, activation=activation)
|
||||
|
||||
|
||||
class TransientDensityHead(FieldHead):
|
||||
"""Transient density output"""
|
||||
|
||||
def __init__(self, in_dim: int, activation: Optional[nn.Module] = nn.Softplus()) -> None:
|
||||
super().__init__(in_dim, out_dim=1, field_head_name=FieldHeadNames.TRANSIENT_DENSITY, activation=activation)
|
||||
super().__init__(in_dim, out_dim=1, activation=activation)
|
||||
|
||||
|
||||
class SemanticStuffHead(FieldHead):
|
||||
@@ -119,4 +117,4 @@ class SemanticStuffHead(FieldHead):
|
||||
in_dim (int): Input dimension
|
||||
num_classes (int): Number of semantic classes
|
||||
"""
|
||||
super().__init__(in_dim, out_dim=num_classes, field_head_name=FieldHeadNames.SEMANTICS_STUFF, activation=None)
|
||||
super().__init__(in_dim, out_dim=num_classes, activation=None)
|
||||
@@ -1,53 +0,0 @@
|
||||
"""
|
||||
Ray generator.
|
||||
"""
|
||||
from torch import nn
|
||||
from torchtyping import TensorType
|
||||
from mattport.structures.rays import RayBundle
|
||||
from mattport.structures.cameras import get_camera_model
|
||||
|
||||
|
||||
class RayGenerator(nn.Module):
|
||||
"""torch.nn Module for generating rays.
|
||||
This class will store the intrinsics and extrinsics parameters of the cameras."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
intrinsics: TensorType["num_cameras", "num_intrinsics_params"],
|
||||
camera_to_world: TensorType["num_cameras", 3, 4],
|
||||
) -> None:
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
intrinsics (TensorType["num_cameras", "num_intrinsics_params"]):
|
||||
The intrinsics parameters.
|
||||
camera_to_world (TensorType["num_cameras", 3, 4]): Camera to world transformation matrix.
|
||||
"""
|
||||
super().__init__()
|
||||
self.num_cameras, self.num_intrinsics_params = intrinsics.shape
|
||||
assert self.num_cameras >= 0
|
||||
self.intrinsics = nn.Parameter(intrinsics, requires_grad=False)
|
||||
self.camera_to_world = nn.Parameter(camera_to_world, requires_grad=False)
|
||||
# TODO(ethan): add learnable parameters that are deltas on the intrinsics and camera_to_world parameters
|
||||
|
||||
def forward(self, ray_indices: TensorType["num_rays", 3]) -> RayBundle:
|
||||
"""Index into the cameras to generate the rays.
|
||||
|
||||
Args:
|
||||
x (_type_): _description_
|
||||
"""
|
||||
c = ray_indices[:, 0] # camera indices
|
||||
y = ray_indices[:, 1] # row indices
|
||||
x = ray_indices[:, 2] # col indices
|
||||
intrinsics = self.intrinsics[c]
|
||||
camera_to_world = self.camera_to_world[c]
|
||||
# NOTE(ethan): we currently assume all images have the same height and width
|
||||
camera_index = 0
|
||||
camera_class = get_camera_model(self.num_intrinsics_params)
|
||||
camera = camera_class(*self.intrinsics[camera_index].tolist())
|
||||
image_coords = camera.get_image_coords().to(ray_indices.device) # NOTE(ethan): I can actually avoid this step
|
||||
coords = image_coords[y, x]
|
||||
|
||||
ray_bundle = camera_class.generate_rays(intrinsics=intrinsics, camera_to_world=camera_to_world, coords=coords)
|
||||
ray_bundle.camera_indices = c
|
||||
return ray_bundle
|
||||
@@ -1,90 +0,0 @@
|
||||
"""
|
||||
Ray generator.
|
||||
"""
|
||||
import torch
|
||||
from torch import nn
|
||||
from torchtyping import TensorType
|
||||
from mattport.nerf.dataset.structs import SceneBounds
|
||||
from mattport.structures.rays import RayBundle
|
||||
|
||||
|
||||
class SceneBoundsCollider(nn.Module):
|
||||
"""Module for setting near and far values for rays."""
|
||||
|
||||
def forward(self, ray_bundle: RayBundle) -> RayBundle:
|
||||
"""To be implemented."""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class AABBBoxCollider(SceneBoundsCollider):
|
||||
"""Module for colliding rays with the scene bounds to compute near and far values."""
|
||||
|
||||
def __init__(self, scene_bounds: SceneBounds) -> None:
|
||||
super().__init__()
|
||||
self.scene_bounds = scene_bounds
|
||||
|
||||
@classmethod
|
||||
def intersect_with_aabb(
|
||||
cls, rays_o: TensorType["num_rays", 3], rays_d: TensorType["num_rays", 3], aabb: TensorType[2, 3]
|
||||
):
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
rays_o (torch.tensor): (num_rays, 3)
|
||||
rays_d (torch.tensor): (num_rays, 3)
|
||||
aabb (torch.tensor): (2, 3) This is [min point (x,y,z), max point (x,y,z)]
|
||||
"""
|
||||
# avoid divide by zero
|
||||
dir_fraction = 1.0 / (rays_d + 1e-6)
|
||||
|
||||
# x
|
||||
t1 = (aabb[0, 0] - rays_o[:, 0:1]) * dir_fraction[:, 0:1]
|
||||
t2 = (aabb[1, 0] - rays_o[:, 0:1]) * dir_fraction[:, 0:1]
|
||||
# y
|
||||
t3 = (aabb[0, 1] - rays_o[:, 1:2]) * dir_fraction[:, 1:2]
|
||||
t4 = (aabb[1, 1] - rays_o[:, 1:2]) * dir_fraction[:, 1:2]
|
||||
# z
|
||||
t5 = (aabb[0, 2] - rays_o[:, 2:3]) * dir_fraction[:, 2:3]
|
||||
t6 = (aabb[1, 2] - rays_o[:, 2:3]) * dir_fraction[:, 2:3]
|
||||
|
||||
nears = torch.max(
|
||||
torch.cat([torch.minimum(t1, t2), torch.minimum(t3, t4), torch.minimum(t5, t6)], dim=1), dim=1
|
||||
).values
|
||||
fars = torch.min(
|
||||
torch.cat([torch.maximum(t1, t2), torch.maximum(t3, t4), torch.maximum(t5, t6)], dim=1), dim=1
|
||||
).values
|
||||
|
||||
# fars < 0: means the ray is behind the camera
|
||||
# nears > fars: means no intersection
|
||||
valid_mask = ((fars > nears).float() * (fars > 0).float()).bool()
|
||||
nears[nears < 0.0] = 0.0
|
||||
nears[~valid_mask] = 0.0
|
||||
fars[~valid_mask] = 0.0
|
||||
return nears, fars, valid_mask
|
||||
|
||||
def forward(self, ray_bundle: RayBundle) -> RayBundle:
|
||||
"""Intersects the rays with the scene bounds and updates the near and far values.
|
||||
Populates nears and fars fields and returns the ray_bundle.
|
||||
"""
|
||||
aabb = self.scene_bounds.aabb
|
||||
nears, fars, valid_mask = self.intersect_with_aabb(ray_bundle.origins, ray_bundle.directions, aabb)
|
||||
ray_bundle.nears = nears
|
||||
ray_bundle.fars = fars
|
||||
ray_bundle.valid_mask = valid_mask
|
||||
return ray_bundle
|
||||
|
||||
|
||||
class NearFarCollider(SceneBoundsCollider):
|
||||
"""Sets the nears and fars with fixed values."""
|
||||
|
||||
def __init__(self, near_plane, far_plane) -> None:
|
||||
self.near_plane = near_plane
|
||||
self.far_plane = far_plane
|
||||
super().__init__()
|
||||
|
||||
def forward(self, ray_bundle: RayBundle) -> RayBundle:
|
||||
ones = torch.ones_like(ray_bundle.origins[:, 0])
|
||||
ray_bundle.nears = ones * self.near_plane
|
||||
ray_bundle.fars = ones * self.far_plane
|
||||
ray_bundle.valid_mask = ones.bool()
|
||||
return ray_bundle
|
||||
Reference in new issue
Block a user