Skip to content

Commit 67778ca

Browse files
bottlerfacebook-github-bot
authored andcommitted
avoid deprecated raysamplers
Summary: Migrate away from NDCGridRaysampler and GridRaysampler to their more flexible replacements. Reviewed By: patricklabatut Differential Revision: D33281584 fbshipit-source-id: 65f8702e700a32d38f7cd6bda3924bb1707a0633
1 parent 3eb4233 commit 67778ca

6 files changed

+30
-30
lines changed

docs/tutorials/fit_simple_neural_radiance_field.ipynb

+6-6
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"from pytorch3d.transforms import so3_exp_map\n",
101101
"from pytorch3d.renderer import (\n",
102102
" FoVPerspectiveCameras, \n",
103-
" NDCGridRaysampler,\n",
103+
" NDCMultinomialRaysampler,\n",
104104
" MonteCarloRaysampler,\n",
105105
" EmissionAbsorptionRaymarcher,\n",
106106
" ImplicitRenderer,\n",
@@ -186,7 +186,7 @@
186186
"The renderer is composed of a *raymarcher* and a *raysampler*.\n",
187187
"- The *raysampler* is responsible for emitting rays from image pixels and sampling the points along them. Here, we use two different raysamplers:\n",
188188
" - `MonteCarloRaysampler` is used to generate rays from a random subset of pixels of the image plane. The random subsampling of pixels is carried out during **training** to decrease the memory consumption of the implicit model.\n",
189-
" - `NDCGridRaysampler` which follows the standard PyTorch3D coordinate grid convention (+X from right to left; +Y from bottom to top; +Z away from the user). In combination with the implicit model of the scene, `NDCGridRaysampler` consumes a large amount of memory and, hence, is only used for visualizing the results of the training at **test** time.\n",
189+
" - `NDCMultinomialRaysampler` which follows the standard PyTorch3D coordinate grid convention (+X from right to left; +Y from bottom to top; +Z away from the user). In combination with the implicit model of the scene, `NDCMultinomialRaysampler` consumes a large amount of memory and, hence, is only used for visualizing the results of the training at **test** time.\n",
190190
"- The *raymarcher* takes the densities and colors sampled along each ray and renders each ray into a color and an opacity value of the ray's source pixel. Here we use the `EmissionAbsorptionRaymarcher` which implements the standard Emission-Absorption raymarching algorithm."
191191
]
192192
},
@@ -211,10 +211,10 @@
211211
"\n",
212212
"# 1) Instantiate the raysamplers.\n",
213213
"\n",
214-
"# Here, NDCGridRaysampler generates a rectangular image\n",
214+
"# Here, NDCMultinomialRaysampler generates a rectangular image\n",
215215
"# grid of rays whose coordinates follow the PyTorch3D\n",
216216
"# coordinate conventions.\n",
217-
"raysampler_grid = NDCGridRaysampler(\n",
217+
"raysampler_grid = NDCMultinomialRaysampler(\n",
218218
" image_height=render_size,\n",
219219
" image_width=render_size,\n",
220220
" n_pts_per_ray=128,\n",
@@ -844,7 +844,7 @@
844844
" fov=target_cameras.fov[0],\n",
845845
" device=device,\n",
846846
" )\n",
847-
" # Note that we again render with `NDCGridRaySampler`\n",
847+
" # Note that we again render with `NDCMultinomialRaysampler`\n",
848848
" # and the batched_forward function of neural_radiance_field.\n",
849849
" frames.append(\n",
850850
" renderer_grid(\n",
@@ -867,7 +867,7 @@
867867
"source": [
868868
"## 6. Conclusion\n",
869869
"\n",
870-
"In this tutorial, we have shown how to optimize an implicit representation of a scene such that the renders of the scene from known viewpoints match the observed images for each viewpoint. The rendering was carried out using the PyTorch3D's implicit function renderer composed of either a `MonteCarloRaysampler` or `NDCGridRaysampler`, and an `EmissionAbsorptionRaymarcher`."
870+
"In this tutorial, we have shown how to optimize an implicit representation of a scene such that the renders of the scene from known viewpoints match the observed images for each viewpoint. The rendering was carried out using the PyTorch3D's implicit function renderer composed of either a `MonteCarloRaysampler` or `NDCMultinomialRaysampler`, and an `EmissionAbsorptionRaymarcher`."
871871
]
872872
}
873873
],

docs/tutorials/fit_textured_volume.ipynb

+5-5
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"from pytorch3d.renderer import (\n",
9090
" FoVPerspectiveCameras, \n",
9191
" VolumeRenderer,\n",
92-
" NDCGridRaysampler,\n",
92+
" NDCMultinomialRaysampler,\n",
9393
" EmissionAbsorptionRaymarcher\n",
9494
")\n",
9595
"from pytorch3d.transforms import so3_exp_map\n",
@@ -164,7 +164,7 @@
164164
"The following initializes a volumetric renderer that emits a ray from each pixel of a target image and samples a set of uniformly-spaced points along the ray. At each ray-point, the corresponding density and color value is obtained by querying the corresponding location in the volumetric model of the scene (the model is described & instantiated in a later cell).\n",
165165
"\n",
166166
"The renderer is composed of a *raymarcher* and a *raysampler*.\n",
167-
"- The *raysampler* is responsible for emitting rays from image pixels and sampling the points along them. Here, we use the `NDCGridRaysampler` which follows the standard PyTorch3D coordinate grid convention (+X from right to left; +Y from bottom to top; +Z away from the user).\n",
167+
"- The *raysampler* is responsible for emitting rays from image pixels and sampling the points along them. Here, we use the `NDCMultinomialRaysampler` which follows the standard PyTorch3D coordinate grid convention (+X from right to left; +Y from bottom to top; +Z away from the user).\n",
168168
"- The *raymarcher* takes the densities and colors sampled along each ray and renders each ray into a color and an opacity value of the ray's source pixel. Here we use the `EmissionAbsorptionRaymarcher` which implements the standard Emission-Absorption raymarching algorithm."
169169
]
170170
},
@@ -186,14 +186,14 @@
186186
"volume_extent_world = 3.0\n",
187187
"\n",
188188
"# 1) Instantiate the raysampler.\n",
189-
"# Here, NDCGridRaysampler generates a rectangular image\n",
189+
"# Here, NDCMultinomialRaysampler generates a rectangular image\n",
190190
"# grid of rays whose coordinates follow the PyTorch3D\n",
191191
"# coordinate conventions.\n",
192192
"# Since we use a volume of size 128^3, we sample n_pts_per_ray=150,\n",
193193
"# which roughly corresponds to a one ray-point per voxel.\n",
194194
"# We further set the min_depth=0.1 since there is no surface within\n",
195195
"# 0.1 units of any camera plane.\n",
196-
"raysampler = NDCGridRaysampler(\n",
196+
"raysampler = NDCMultinomialRaysampler(\n",
197197
" image_width=render_size,\n",
198198
" image_height=render_size,\n",
199199
" n_pts_per_ray=150,\n",
@@ -462,7 +462,7 @@
462462
"source": [
463463
"## 6. Conclusion\n",
464464
"\n",
465-
"In this tutorial, we have shown how to optimize a 3D volumetric representation of a scene such that the renders of the volume from known viewpoints match the observed images for each viewpoint. The rendering was carried out using the PyTorch3D's volumetric renderer composed of an `NDCGridRaysampler` and an `EmissionAbsorptionRaymarcher`."
465+
"In this tutorial, we have shown how to optimize a 3D volumetric representation of a scene such that the renders of the volume from known viewpoints match the observed images for each viewpoint. The rendering was carried out using the PyTorch3D's volumetric renderer composed of an `NDCMultinomialRaysampler` and an `EmissionAbsorptionRaymarcher`."
466466
]
467467
}
468468
],

projects/nerf/nerf/raysampler.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import List
99

1010
import torch
11-
from pytorch3d.renderer import MonteCarloRaysampler, NDCGridRaysampler, RayBundle
11+
from pytorch3d.renderer import MonteCarloRaysampler, NDCMultinomialRaysampler, RayBundle
1212
from pytorch3d.renderer.cameras import CamerasBase
1313
from pytorch3d.renderer.implicit.sample_pdf import sample_pdf
1414

@@ -150,7 +150,7 @@ def __init__(
150150
self._stratified_test = stratified_test
151151

152152
# Initialize the grid ray sampler.
153-
self._grid_raysampler = NDCGridRaysampler(
153+
self._grid_raysampler = NDCMultinomialRaysampler(
154154
image_width=image_width,
155155
image_height=image_height,
156156
n_pts_per_ray=n_pts_per_ray,

tests/test_camera_pixels.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from common_testing import TestCaseMixin
1111
from pytorch3d.renderer import (
1212
MeshRasterizer,
13-
NDCGridRaysampler,
13+
NDCMultinomialRaysampler,
1414
PerspectiveCameras,
1515
PointsRasterizationSettings,
1616
PointsRasterizer,
@@ -172,7 +172,7 @@ def test_pointcloud(self):
172172

173173
def test_raysampler(self):
174174
data = _CommonData()
175-
gridsampler = NDCGridRaysampler(
175+
gridsampler = NDCMultinomialRaysampler(
176176
image_width=data.W,
177177
image_height=data.H,
178178
n_pts_per_ray=2,

tests/test_render_implicit.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
from pytorch3d.renderer import (
1313
BlendParams,
1414
EmissionAbsorptionRaymarcher,
15-
GridRaysampler,
1615
ImplicitRenderer,
1716
Materials,
1817
MeshRasterizer,
1918
MeshRenderer,
2019
MonteCarloRaysampler,
21-
NDCGridRaysampler,
20+
MultinomialRaysampler,
21+
NDCMultinomialRaysampler,
2222
PointLights,
2323
RasterizationSettings,
2424
RayBundle,
@@ -142,7 +142,7 @@ def test_input_types(self):
142142

143143
# init a trivial renderer
144144
renderer = ImplicitRenderer(
145-
raysampler=NDCGridRaysampler(
145+
raysampler=NDCMultinomialRaysampler(
146146
image_width=100,
147147
image_height=100,
148148
n_pts_per_ray=10,
@@ -180,7 +180,7 @@ def _compare_with_meshes_renderer(
180180
sphere_centroid.requires_grad = True
181181

182182
# init the grid raysampler with the ndc grid
183-
raysampler = NDCGridRaysampler(
183+
raysampler = NDCMultinomialRaysampler(
184184
image_width=image_size[1],
185185
image_height=image_size[0],
186186
n_pts_per_ray=256,
@@ -355,7 +355,7 @@ def _rotating_gif(self, image_size, n_frames=50, fps=15, sphere_diameter=0.5):
355355
cameras = init_cameras(n_frames, image_size=image_size)
356356

357357
# init the grid raysampler
358-
raysampler = GridRaysampler(
358+
raysampler = MultinomialRaysampler(
359359
min_x=0.5,
360360
max_x=image_size[1] - 0.5,
361361
min_y=0.5,

tests/test_render_volumes.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
AbsorptionOnlyRaymarcher,
1616
AlphaCompositor,
1717
EmissionAbsorptionRaymarcher,
18-
GridRaysampler,
1918
MonteCarloRaysampler,
20-
NDCGridRaysampler,
19+
MultinomialRaysampler,
20+
NDCMultinomialRaysampler,
2121
PerspectiveCameras,
2222
PointsRasterizationSettings,
2323
PointsRasterizer,
@@ -228,7 +228,7 @@ def test_input_types(self, batch_size: int = 10):
228228
with self.assertRaises(ValueError):
229229
VolumeRenderer(raysampler=bad_raysampler, raymarcher=bad_raymarcher)
230230

231-
raysampler = NDCGridRaysampler(
231+
raysampler = NDCMultinomialRaysampler(
232232
image_width=100,
233233
image_height=100,
234234
n_pts_per_ray=10,
@@ -339,7 +339,7 @@ def test_compare_with_pointclouds_renderer(
339339
# init the grid raysampler with the ndc grid
340340
coord_range = 1.0
341341
half_pix_size = coord_range / max(*image_size)
342-
raysampler = NDCGridRaysampler(
342+
raysampler = NDCMultinomialRaysampler(
343343
image_width=image_size[1],
344344
image_height=image_size[0],
345345
n_pts_per_ray=256,
@@ -431,7 +431,7 @@ def test_monte_carlo_rendering(
431431
):
432432
"""
433433
Tests that rendering with the MonteCarloRaysampler matches the
434-
rendering with GridRaysampler sampled at the corresponding
434+
rendering with MultinomialRaysampler sampled at the corresponding
435435
MonteCarlo locations.
436436
"""
437437
volumes = init_boundary_volume(
@@ -442,7 +442,7 @@ def test_monte_carlo_rendering(
442442
cameras = init_cameras(n_frames, image_size=image_size)
443443

444444
# init the grid raysampler
445-
raysampler_grid = GridRaysampler(
445+
raysampler_multinomial = MultinomialRaysampler(
446446
min_x=0.5,
447447
max_x=image_size[1] - 0.5,
448448
min_y=0.5,
@@ -475,11 +475,11 @@ def test_monte_carlo_rendering(
475475
(images_opacities_grid, ray_bundle_grid),
476476
) = [
477477
VolumeRenderer(
478-
raysampler=raysampler_grid,
478+
raysampler=raysampler_multinomial,
479479
raymarcher=raymarcher,
480480
sample_mode="bilinear",
481481
)(cameras=cameras, volumes=volumes)
482-
for raysampler in (raysampler_mc, raysampler_grid)
482+
for raysampler in (raysampler_mc, raysampler_multinomial)
483483
]
484484

485485
# convert the mc sampling locations to [-1, 1]
@@ -523,7 +523,7 @@ def _rotating_gif(
523523
cameras = init_cameras(n_frames, image_size=image_size)
524524

525525
# init the grid raysampler
526-
raysampler = GridRaysampler(
526+
raysampler = MultinomialRaysampler(
527527
min_x=0.5,
528528
max_x=image_size[1] - 0.5,
529529
min_y=0.5,
@@ -614,7 +614,7 @@ def test_rotating_cube_volume_render(self):
614614
volumes.features().requires_grad = True
615615
volumes.densities().requires_grad = True
616616

617-
raysampler = GridRaysampler(
617+
raysampler = MultinomialRaysampler(
618618
min_x=0.5,
619619
max_x=image_size[1] - 0.5,
620620
min_y=0.5,

0 commit comments

Comments
 (0)