mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-04 14:58:55 +08:00
Add norm argument to contraction (#557)
This commit is contained in:
1 parent
d6139f25d4
commit
a53f180a33
4 files changed
+74718
-643
No files matched your search
@@ -14,7 +14,7 @@
|
||||
|
||||
"""Space distortions."""
|
||||
|
||||
from typing import Union
|
||||
from typing import Optional, Union
|
||||
|
||||
import torch
|
||||
from functorch import jacrev, vmap
|
||||
@@ -40,7 +40,7 @@ class SpatialDistortion(nn.Module):
|
||||
|
||||
|
||||
class SceneContraction(SpatialDistortion):
|
||||
"""Contract unbounded space into a sphere of radius 2. This contraction was proposed in MipNeRF-360.
|
||||
"""Contract unbounded space using the contraction was proposed in MipNeRF-360.
|
||||
We use the following contraction equation:
|
||||
|
||||
.. math::
|
||||
@@ -49,11 +49,23 @@ class SceneContraction(SpatialDistortion):
|
||||
x & ||x|| \\leq 1 \\\\
|
||||
(2 - \\frac{1}{||x||})(\\frac{x}{||x||}) & ||x|| > 1
|
||||
\\end{cases}
|
||||
|
||||
If the order is not specified, we use the Frobenius norm, this will contract the space to a sphere of
|
||||
radius 1. If the order is L_inf (order=float("inf")), we will contract the space to a cube of side length 2.
|
||||
If using voxel based encodings such as the Hash encoder, we recommend using the L_inf norm.
|
||||
|
||||
Args:
|
||||
order: Order of the norm. Default to the Frobenius norm. Must be set to None for Gaussians.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, order: Optional[Union[float, int]] = None) -> None:
|
||||
super().__init__()
|
||||
self.order = order
|
||||
|
||||
def forward(self, positions):
|
||||
def contract(x):
|
||||
mag = x.norm(dim=-1)
|
||||
mag = torch.linalg.norm(x, ord=self.order, dim=-1)
|
||||
mask = mag >= 1
|
||||
x[mask] = (2 - (1 / mag[mask][..., None])) * (x[mask] / mag[mask][..., None])
|
||||
|
||||
@@ -62,7 +74,9 @@ class SceneContraction(SpatialDistortion):
|
||||
if isinstance(positions, Gaussians):
|
||||
means = contract(positions.mean.clone())
|
||||
|
||||
contract = lambda x: (2 - (1 / x.norm(dim=-1, keepdim=True))) * (x / x.norm(dim=-1, keepdim=True))
|
||||
contract = lambda x: (2 - (1 / torch.linalg.norm(x, ord=self.order, dim=-1, keepdim=True))) * (
|
||||
x / torch.linalg.norm(x, ord=self.order, dim=-1, keepdim=True)
|
||||
)
|
||||
jc_means = vmap(jacrev(contract))(positions.mean.view(-1, positions.mean.shape[-1]))
|
||||
jc_means = jc_means.view(list(positions.mean.shape) + [positions.mean.shape[-1]])
|
||||
|
||||
|
||||
@@ -84,7 +84,7 @@ class ProposalModel(Model):
|
||||
"""Set the fields and modules."""
|
||||
super().populate_modules()
|
||||
|
||||
scene_contraction = SceneContraction()
|
||||
scene_contraction = SceneContraction(order=float("inf"))
|
||||
|
||||
# Fields
|
||||
if self.config.use_appearance_conditioning:
|
||||
|
||||
@@ -204,6 +204,46 @@ def get_sphere(
|
||||
)
|
||||
|
||||
|
||||
def get_cube(
|
||||
side_length: float,
|
||||
center: TensorType[3] = None,
|
||||
color: str = "black",
|
||||
opacity: float = 1.0,
|
||||
) -> go.Mesh3d: # type: ignore
|
||||
"""Returns a cube object for plotting with plotly.
|
||||
|
||||
Args:
|
||||
side_length: side_length of cube.
|
||||
center: center of cube.
|
||||
color: color of cube.
|
||||
opacity: opacity of cube.
|
||||
|
||||
Returns:
|
||||
cube object.
|
||||
"""
|
||||
|
||||
x = np.array([-1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0])
|
||||
y = np.array([-1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0])
|
||||
z = np.array([-1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0])
|
||||
|
||||
pts = np.stack((x, y, z), axis=0)
|
||||
|
||||
pts *= side_length / 2.0
|
||||
if center is not None:
|
||||
pts += center
|
||||
|
||||
return go.Mesh3d(
|
||||
{
|
||||
"x": pts[0],
|
||||
"y": pts[1],
|
||||
"z": pts[2],
|
||||
"alphahull": 0,
|
||||
"opacity": opacity,
|
||||
"color": color,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def get_gaussian_ellipsiod(
|
||||
mean: TensorType[3],
|
||||
cov: TensorType[3, 3],
|
||||
|
||||
Reference in new issue
Block a user