Tancik/scene contraction (#70)

Contract the scene to a sphere of radius 2. This is the same contraction used in MipNeRF 360.
Add mipnerf 360 graph and config. It currently doesn't contain the other advancements made in the paper (sample proposal network, regularizers)
This commit is contained in:
Matthew Tancik authored and GitHub committed 2022-06-29 18:28:21 -07:00
1 parent 2bc6d9cc63
commit 304a759e85
11 files changed
+325 -12

No files matched your search

@@ -0,0 +1,73 @@
# Copyright 2022 The Plenoptix Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Space distortions."""
from typing import Union
import torch
from functorch import jacrev, vmap
from torch import nn
from torchtyping import TensorType
from pyrad.utils.math import Gaussians
class SpatialDistortion(nn.Module):
"""Apply spatial distortions"""
def forward(self, positions: Union[TensorType[..., 3], Gaussians]) -> Union[TensorType[..., 3], Gaussians]:
"""
Args:
positions (Union[TensorType[..., 3], Gaussians]): Sample to distort
Returns:
Union[TensorType[..., 3], Gaussians]: distorted sample
"""
class SceneContraction(SpatialDistortion):
"""Contract unbounded space into a sphere of radius 2. This contraction was proposed in MipNeRF-360.
We use the following contraction equation:
.. math::
f(x) = \\begin{cases}
x & ||x|| \\leq 1 \\\\
(2 - \\frac{1}{||x||})(\\frac{x}{||x||}) & ||x|| > 1
\\end{cases}
"""
def forward(self, positions: Union[TensorType[..., 3], Gaussians]) -> Union[TensorType[..., 3], Gaussians]:
def contract(x: TensorType[..., "in_dim"]) -> TensorType[..., "in_dim"]:
mag = x.norm(dim=-1)
mask = mag >= 1
x[mask] = (2 - (1 / mag[mask][..., None])) * (x[mask] / mag[mask][..., None])
return x
if isinstance(positions, Gaussians):
means = contract(positions.mean)
contract = lambda x: (2 - (1 / x.norm(dim=-1, keepdim=True))) * (x / x.norm(dim=-1, keepdim=True))
jc_means = vmap(jacrev(contract))(positions.mean)
# Only update covariances on positions outside the unit sphere
mag = positions.mean.norm(dim=-1)
mask = mag >= 1
positions.cov[mask] = jc_means[mask] @ positions.cov[mask] @ torch.transpose(jc_means[mask], -2, -1)
return Gaussians(mean=means, cov=positions.cov)
return contract(positions)
+10 -1
View File
@@ -26,6 +26,7 @@ from pyrad.fields.modules.field_heads import DensityFieldHead, FieldHead, FieldH
from pyrad.fields.modules.mlp import MLP
from pyrad.fields.base import Field
from pyrad.cameras.rays import PointSamples
from pyrad.fields.modules.spatial_distortions import SpatialDistortion
class NeRFField(Field):
@@ -40,6 +41,7 @@ class NeRFField(Field):
head_mlp_layer_width (int, optional): Width of output head MLP layers. Defaults to 128.
skip_connections (Tuple, optional): Where to add skip connection in base MLP. Defaults to (4,).
use_integrated_encoding (bool, optional): Used integrated samples as encoding input, Defaults to False.
spatial_distortion (SpatialDistortion, optional): Spatial distortion. Defaults to None.
"""
def __init__(
@@ -53,11 +55,13 @@ class NeRFField(Field):
skip_connections: Tuple[int] = (4,),
field_heads: Tuple[FieldHead] = (RGBFieldHead(),),
use_integrated_encoding: bool = False,
spatial_distortion: Optional[SpatialDistortion] = None,
) -> None:
super().__init__()
self.position_encoding = position_encoding
self.direction_encoding = direction_encoding
self.use_integrated_encoding = use_integrated_encoding
self.spatial_distortion = spatial_distortion
self.mlp_base = MLP(
in_dim=self.position_encoding.get_out_dim(),
@@ -80,9 +84,14 @@ class NeRFField(Field):
def get_density(self, point_samples: PointSamples):
if self.use_integrated_encoding:
gaussian_samples = point_samples.frustums.get_gaussian_blob()
if self.spatial_distortion is not None:
gaussian_samples = self.spatial_distortion(gaussian_samples)
encoded_xyz = self.position_encoding(gaussian_samples.mean, covs=gaussian_samples.cov)
else:
encoded_xyz = self.position_encoding(point_samples.frustums.get_positions())
positions = point_samples.frustums.get_positions()
if self.spatial_distortion is not None:
positions = self.spatial_distortion(positions)
encoded_xyz = self.position_encoding(positions)
base_mlp_out = self.mlp_base(encoded_xyz)
density = self.field_output_density(base_mlp_out)
return density, base_mlp_out