nerfstudio-project / nerfstudio

A collaboration friendly studio for NeRFs
https://docs.nerf.studio
Apache License 2.0
9.34k stars 1.26k forks source link

Tricky thing about the F.grid_sample #135

Closed liruilong940607 closed 1 year ago

liruilong940607 commented 2 years ago

Issue of the F.grid_sample: padding_mode==zeros means padding zero voxel values outside the grid. a query point that is slightly outside the grid would be interpolated by the voxel values on the boarder of the grid and the outside zero voxels. So it gives non-zero value at regions slightly outside the grid.

Toy code to show:

grid = torch.ones((1, 1, 128, 128, 128))
positions = torch.tensor([[0.5, 1.004, 0.5]])
values = F.grid_sample(
    grid,
    positions.view(1, -1, 1, 1, 3),
    align_corners=True,
    padding_mode="zeros",
)
print(values.flatten())  # >> 0.7460

Relevant code in our code base:

https://github.com/plenoptix/pyrad/blob/91ef54963c43beb34f13301f8496faf2f0de8a2e/pyrad/fields/occupancy_fields/occupancy_grid.py#L131

liruilong940607 commented 2 years ago

It shouldn't affect the performance as the border of the occupancy grid would be learnt to be zero. But the cuda code I'm writting would make sure all points outside the boarder have zero values, which would results slightly different behavior with F.grid_sample but should be more suitable for our case.