Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

improve nerf docs (#2673)

Edgar Riba62a8a48a9d88
diff --git a/docs/source/nerf.rst b/docs/source/nerf.rst
index 143b52b26..c2eec97ac 100644
--- a/docs/source/nerf.rst
+++ b/docs/source/nerf.rst
@@ -9,24 +9,37 @@ Models
------
.. autoclass:: kornia.nerf.nerf_model.NerfModel
+ :members:
.. autoclass:: kornia.nerf.nerf_model.NerfModelRenderer
+ :members:
+.. autoclass:: kornia.nerf.nerf_model.MLP
+ :members:
Solvers
-------
.. autoclass:: kornia.nerf.nerf_solver.NerfSolver
+ :members:
+ :exclude-members: init_training
Renderers
---------
.. autoclass:: kornia.nerf.volume_renderer.VolumeRenderer
+ :members:
.. autoclass:: kornia.nerf.volume_renderer.IrregularRenderer
+ :members:
.. autoclass:: kornia.nerf.volume_renderer.RegularRenderer
+ :members:
Samplers
--------
.. autoclass:: kornia.nerf.samplers.RaySampler
+ :members:
.. autoclass:: kornia.nerf.samplers.RandomRaySampler
+ :members:
.. autoclass:: kornia.nerf.samplers.RandomGridRaySampler
+ :members:
.. autoclass:: kornia.nerf.samplers.UniformRaySampler
+ :members:
diff --git a/kornia/nerf/__init__.py b/kornia/nerf/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/kornia/nerf/nerf_model.py b/kornia/nerf/nerf_model.py
index 8ae034384..9f3406b11 100755
--- a/kornia/nerf/nerf_model.py
+++ b/kornia/nerf/nerf_model.py
@@ -22,15 +22,17 @@ class MLP(Module):
Skip connections span between the sub-units.
The model follows: Ben Mildenhall et el. (2020) at https://arxiv.org/abs/2003.08934.
-
- Args:
- num_dims: Number of input dimensions (channels)
- num_units: Number of sub-units: int
- num_unit_layers: Number of fully connected layers in each sub-unit
- num_hidden: Layer hidden dimensions: int
"""
def __init__(self, num_dims: int, num_units: int = 2, num_unit_layers: int = 4, num_hidden: int = 128) -> None:
+ """Constructor method.
+
+ Args:
+ num_dims: Number of input dimensions (channels).
+ num_units: Number of sub-units.
+ num_unit_layers: Number of fully connected layers in each sub-unit.
+ num_hidden: Layer hidden dimensions.
+ """
super().__init__()
self._num_unit_layers = num_unit_layers
layers = []
@@ -42,12 +44,12 @@ class MLP(Module):
layers.append(nn.Sequential(layer, nn.ReLU()))
self._mlp = nn.ModuleList(layers)
- def forward(self, inp: Tensor) -> Tensor:
- out = inp
- inp_skip = inp
+ def forward(self, x: Tensor) -> Tensor:
+ out = x
+ x_skip = x
for i, layer in enumerate(self._mlp):
if i > 0 and i % self._num_unit_layers == 0:
- out = torch.cat((out, inp_skip), dim=-1)
+ out = torch.cat((out, x_skip), dim=-1)
out = layer(out)
return out
@@ -56,14 +58,14 @@ class NerfModel(Module):
r"""Class to represent NeRF model.
Args:
- num_ray_points: Number of points to sample along rays: int
- irregular_ray_sampling: Whether to sample ray points irregularly: bool
- num_pos_freqs: Number of frequencies for positional encoding: int
- num_dir_freqs: Number of frequencies for directional encoding: int
- num_units: Number of sub-units: int
- num_unit_layers: Number of fully connected layers in each sub-unit
- num_hidden: Layer hidden dimensions: int
- log_space_encoding: Whether to apply log spacing for encoding: bool
+ num_ray_points: Number of points to sample along rays.
+ irregular_ray_sampling: Whether to sample ray points irregularly.
+ num_pos_freqs: Number of frequencies for positional encoding.
+ num_dir_freqs: Number of frequencies for directional encoding.
+ num_units: Number of sub-units.
+ num_unit_layers: Number of fully connected layers in each sub-unit.
+ num_hidden: Layer hidden dimensions.
+ log_space_encoding: Whether to apply log spacing for encoding.
"""
def __init__(
@@ -98,6 +100,15 @@ class NerfModel(Module):
self._rgb[0].bias.data = torch.tensor([0.02, 0.02, 0.02]).float()
def forward(self, origins: Tensor, directions: Tensor) -> Tensor:
+ """Forward method.
+
+ Args:
+ origins: Ray origins with shape :math:`(B, 3)`.
+ directions: Ray directions with shape :math:`(B, 3)`.
+
+ Returns:
+ Rendered image pixels :math:`(B, 3)`.
+ """
# Sample xyz for ray parameters
batch_size = origins.shape[0]
lengths = sample_lengths(
@@ -135,16 +146,19 @@ class NerfModel(Module):
class NerfModelRenderer:
- r"""Renders a novel synthesis view of a trained NeRF model for given camera.
-
- Args:
- nerf_model: NeRF model: NerfModel
- image_size: image size: tuple[int, int]
- """
+ """Renders a novel synthesis view of a trained NeRF model for given camera."""
def __init__(
self, nerf_model: NerfModel, image_size: tuple[int, int], device: torch.device | None, dtype: torch.dtype | None
) -> None:
+ """Constructor method.
+
+ Args:
+ nerf_model: NeRF model.
+ image_size: image size.
+ device: device to run the model on.
+ dtype: dtype to run the model on.
+ """
self._nerf_model = nerf_model
self._image_size = image_size
self._device = device
@@ -153,7 +167,7 @@ class NerfModelRenderer:
self._pixels_grid, self._ones = self._create_pixels_grid() # 1xHxWx2 and (H*W)x1
def _create_pixels_grid(self) -> tuple[Tensor, Tensor]:
- r"""Creates the pixels grid to unproject to plane z=1.
+ """Creates the pixels grid to unproject to plane z=1.
Args:
image_size: image size: tuple[int, int]
@@ -173,13 +187,13 @@ class NerfModelRenderer:
return pixels_grid, ones
def render_view(self, camera: PinholeCamera) -> Tensor:
- r"""Renders a novel synthesis view of a trained NeRF model for given camera.
+ """Renders a novel synthesis view of a trained NeRF model for given camera.
Args:
camera: camera for image rendering: PinholeCamera.
Returns:
- Rendered image: Tensor (H, W, C).
+ Rendered image with shape :math:`(H, W, 3)`.
"""
# create ray for this camera
rays: Ray = self._create_rays(camera)
diff --git a/kornia/nerf/nerf_solver.py b/kornia/nerf/nerf_solver.py
index 187c67ed0..835c63d8b 100755
--- a/kornia/nerf/nerf_solver.py
+++ b/kornia/nerf/nerf_solver.py
@@ -73,20 +73,20 @@ class NerfSolver:
log_space_encoding: bool = True,
lr: float = 1.0e-3,
) -> None:
- r"""Initializes training.
+ """Initializes training settings and model.
Args:
- cameras: Scene cameras in the order of input images: PinholeCamera
- min_depth: sampled rays minimal depth from cameras: float
- max_depth: sampled rays maximal depth from cameras: float
- ndc: convert ray parameters to normalized device coordinates: bool
- imgs: Scene 2D images (one for each camera): Images
- num_img_rays: Number of rays to randomly cast from each camera: math: `(B)`
- batch_size: Number of rays to sample in a batch: int
- num_ray_points: Number of points to sample along rays: int
- irregular_ray_sampling: Whether to sample ray points irregularly: bool
- log_space: Whether frequency sampling should be log spaced: bool
- lr: Learning rate: float
+ cameras: Scene cameras in the order of input images.
+ min_depth: sampled rays minimal depth from cameras.
+ max_depth: sampled rays maximal depth from cameras.
+ ndc: convert ray parameters to normalized device coordinates.
+ imgs: Scene 2D images (one for each camera).
+ num_img_rays: Number of rays to randomly cast from each camera: math: `(B)`.
+ batch_size: Number of rays to sample in a batch.
+ num_ray_points: Number of points to sample along rays.
+ irregular_ray_sampling: Whether to sample ray points irregularly.
+ log_space: Whether frequency sampling should be log spaced.
+ lr: Learning rate.
"""
self._cameras = cameras
self._min_depth = min_depth
@@ -138,21 +138,6 @@ class NerfSolver:
log_space_encoding: bool = True,
lr: float = 1.0e-3,
) -> None:
- r"""Initializes training.
-
- Args:
- cameras: Scene cameras in the order of input images: PinholeCamera
- min_depth: sampled rays minimal depth from cameras: float
- max_depth: sampled rays maximal depth from cameras: float
- ndc: convert ray parameters to normalized device coordinates: bool
- imgs: Scene 2D images (one for each camera): Images
- num_img_rays: Number of rays to randomly cast from each camera: math: `(B)`
- batch_size: Number of rays to sample in a batch: int
- num_ray_points: Number of points to sample along rays: int
- irregular_ray_sampling: Whether to sample ray points irregularly: bool
- log_space: Whether frequency sampling should be log spaced: bool
- lr: Learning rate: float
- """
self.setup_solver(
cameras,
min_depth,
diff --git a/kornia/nerf/volume_renderer.py b/kornia/nerf/volume_renderer.py
index 187d4765e..1fbd0ea20 100755
--- a/kornia/nerf/volume_renderer.py
+++ b/kornia/nerf/volume_renderer.py
@@ -6,7 +6,7 @@ from kornia.nerf.samplers import calc_ray_t_vals
class VolumeRenderer(torch.nn.Module):
- r"""Volume renderer class.
+ r"""Base class for volume rendering.
Implementation follows Ben Mildenhall et el. (2020) at https://arxiv.org/abs/2003.08934.
"""