Skip to content

Commit b7f4ba0

Browse files
d4l3kfacebook-github-bot
authored andcommitted
VolumeSampler: expose padding_mode for inside out rendering (#1638)
Summary: This exposes a setting on VolumeSampler so you can change the padding_mode. This is very useful when using cameras inside a volume that doesn't cover the entire world. By setting the value to `border` you can get much better behavior than `zeros` which causes edge effects for things like the sky. Border emulates infinitely tall buildings instead. Pull Request resolved: #1638 Test Plan: Tested with torchdrive Example before: ![image](https://github.com/facebookresearch/pytorch3d/assets/909104/e99ffb7c-c4ba-40f8-b15c-ad5d1b53f0df) Example after: ![image](https://github.com/facebookresearch/pytorch3d/assets/909104/f8d9821b-93d5-44b5-b9d4-c1670711ddce) Reviewed By: MichaelRamamonjisoa Differential Revision: D49384383 Pulled By: bottler fbshipit-source-id: 202b526e07320a18944c39a148beec94c0f5d68c
1 parent 6f2212d commit b7f4ba0

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

pytorch3d/renderer/implicit/renderer.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -261,19 +261,28 @@ class VolumeSampler(torch.nn.Module):
261261
at 3D points sampled along projection rays.
262262
"""
263263

264-
def __init__(self, volumes: Volumes, sample_mode: str = "bilinear") -> None:
264+
def __init__(
265+
self,
266+
volumes: Volumes,
267+
sample_mode: str = "bilinear",
268+
padding_mode: str = "zeros",
269+
) -> None:
265270
"""
266271
Args:
267272
volumes: An instance of the `Volumes` class representing a
268273
batch of volumes that are being rendered.
269274
sample_mode: Defines the algorithm used to sample the volumetric
270275
voxel grid. Can be either "bilinear" or "nearest".
276+
padding_mode: How to handle values outside of the volume.
277+
One of: zeros, border, reflection
278+
See torch.nn.functional.grid_sample for more information.
271279
"""
272280
super().__init__()
273281
if not isinstance(volumes, Volumes):
274282
raise ValueError("'volumes' have to be an instance of the 'Volumes' class.")
275283
self._volumes = volumes
276284
self._sample_mode = sample_mode
285+
self._padding_mode = padding_mode
277286

278287
def _get_ray_directions_transform(self):
279288
"""
@@ -375,6 +384,7 @@ def forward(
375384
rays_points_local_flat,
376385
align_corners=True,
377386
mode=self._sample_mode,
387+
padding_mode=self._padding_mode,
378388
)
379389

380390
# permute the dimensions & reshape densities after sampling
@@ -392,6 +402,7 @@ def forward(
392402
rays_points_local_flat,
393403
align_corners=True,
394404
mode=self._sample_mode,
405+
padding_mode=self._padding_mode,
395406
)
396407

397408
# permute the dimensions & reshape features after sampling

0 commit comments

Comments
 (0)