volume_renderer.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # LICENSE HEADER MANAGED BY add-license-header
  2. #
  3. # Copyright 2018 Kornia Team
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. import torch
  18. from kornia.core import Tensor
  19. from kornia.core.check import KORNIA_CHECK_SHAPE
  20. from kornia.nerf.samplers import calc_ray_t_vals
  21. class VolumeRenderer(torch.nn.Module):
  22. r"""Base class for volume rendering.
  23. Implementation follows Ben Mildenhall et el. (2020) at https://arxiv.org/abs/2003.08934.
  24. """
  25. _huge = 1.0e10
  26. _eps = 1.0e-10
  27. def __init__(self, shift: int = 1) -> None:
  28. """Initialize the renderer.
  29. Args:
  30. shift: Size of far-field layer: int
  31. """
  32. super().__init__()
  33. self._shift = shift
  34. def _render(self, alpha: Tensor, rgbs: Tensor) -> Tensor:
  35. trans = torch.cumprod(1 - alpha + self._eps, dim=-2) # (*, N, 1)
  36. trans = torch.roll(trans, shifts=self._shift, dims=-2) # (*, N, 1)
  37. trans[..., : self._shift, :] = 1 # (*, N, 1)
  38. weights = trans * alpha # (*, N, 1)
  39. rgbs_rendered = torch.sum(weights * rgbs, dim=-2) # (*, 3)
  40. return rgbs_rendered
  41. def forward(self, rgbs: Tensor, densities: Tensor, points_3d: Tensor) -> Tensor:
  42. raise NotImplementedError
  43. class IrregularRenderer(VolumeRenderer):
  44. """Renders 3D irregularly sampled points along rays."""
  45. def forward(self, rgbs: Tensor, densities: Tensor, points_3d: Tensor) -> Tensor:
  46. r"""Render 3D irregularly sampled points along rays.
  47. Args:
  48. rgbs: RGB values of points along rays :math:`(*, N, 3)`
  49. densities: Volume densities of points along rays :math:`(*, N)`
  50. points_3d: 3D points along rays :math:`(*, N, 3)`
  51. Returns:
  52. Rendered RGB values for each ray :math:`(*, 3)`
  53. """
  54. t_vals = calc_ray_t_vals(points_3d)
  55. deltas = t_vals[..., 1:] - t_vals[..., :-1] # (*, N - 1)
  56. far = torch.empty(size=t_vals.shape[:-1], dtype=t_vals.dtype, device=t_vals.device).fill_(self._huge)
  57. deltas = torch.cat([deltas, far[..., None]], dim=-1) # (*, N)
  58. alpha = 1 - torch.exp(-1.0 * densities * deltas[..., None]) # (*, N)
  59. return self._render(alpha, rgbs)
  60. class RegularRenderer(VolumeRenderer):
  61. """Renders 3D regularly sampled points along rays."""
  62. def forward(self, rgbs: Tensor, densities: Tensor, points_3d: Tensor) -> Tensor:
  63. r"""Render 3D regularly sampled points along rays.
  64. Args:
  65. rgbs: RGB values of points along rays :math:`(*, N, 3)`
  66. densities: Volume densities of points along rays :math:`(*, N)`
  67. points_3d: 3D points along rays :math:`(*, N, 3)`
  68. Returns:
  69. Rendered RGB values for each ray :math:`(*, 3)`
  70. """
  71. KORNIA_CHECK_SHAPE(rgbs, ["*", "N", "3"])
  72. KORNIA_CHECK_SHAPE(densities, ["*", "N"])
  73. KORNIA_CHECK_SHAPE(points_3d, ["*", "N", "3"])
  74. num_ray_points: int = points_3d.shape[-2]
  75. points_3d = points_3d.reshape(-1, num_ray_points, 3) # (*, N, 3)
  76. delta_3d = points_3d[0, 1, :] - points_3d[0, 0, :] # (*, 3)
  77. delta = torch.linalg.norm(delta_3d, dim=-1)
  78. alpha = 1 - torch.exp(-1.0 * densities * delta) # (*, N)
  79. return self._render(alpha, rgbs)