Skip to content

Errors in NeRF implementation #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rakhimovv opened this issue Oct 6, 2021 · 12 comments
Open

Errors in NeRF implementation #868

rakhimovv opened this issue Oct 6, 2021 · 12 comments
Assignees
Labels
do-not-reap Do not delete this pull request or issue due to inactivity. how to How to use PyTorch3D in my project

Comments

@rakhimovv
Copy link

rakhimovv commented Oct 6, 2021

Hello, first of all, thanks for the beautiful implementation!

However, certain things seem to be buggy. Please correct me if I am wrong:

Problem 1.

NDCGridRaysampler does not work properly with rectangular images. Since the NDC convention assumes that the XY coordinates lie in [-1, 1] x [u, u] or ([-u, u] x [-1, 1]) depending on which size is shorter.

The simple fix I expect would be:

from pytorch3d.renderer.mesh.rasterize_meshes import pix_to_non_square_ndc
min_x = pix_to_non_square_ndc(image_width - 1, image_width, image_height)
max_x = pix_to_non_square_ndc(0, image_width, image_height)
min_y = pix_to_non_square_ndc(image_height - 1, image_height, image_width)
max_y = pix_to_non_square_ndc(0, image_height, image_width)

A similar problem also persists here when passing arguments:

self._mc_raysampler = MonteCarloRaysampler(
min_x=-1.0,
max_x=1.0,
min_y=-1.0,
max_y=1.0,

If the fix is applied, then grid_sample should be fixed to account for it (the longer side should be divided by u)

xy_sample = -sampled_rays_xy.view(ba, -1, 1, 2).clone()

also, align_corners should be False since the in NDC convention, the pixel location corresponds to its center

images_sampled = torch.nn.functional.grid_sample(
target_images.permute(0, 3, 1, 2),
xy_sample,
align_corners=True,
mode="bilinear",
)

Problem 2.

deltas in _get_densities do not take into account the norm of ray_directions

deltas = torch.cat(
(
depth_values[..., 1:] - depth_values[..., :-1],
1e10 * torch.ones_like(depth_values[..., :1]),
),
dim=-1,
)[..., None]

this does not lead to error since the ray_directions were normalized in NeRFRaysampler

but this leads to another problem: since the ray_directions were normalized, ray_bundle_to_ray_points will now produce points that lie before the near plane if, e.g., I pass ray_lengths = near (so the depth values are not actually depths)

it seems to me it is better to avoid ray_directions normalization and take into account the norm of ray_directions when calculating densities like it is done in an original implementation https://github.com/bmild/nerf/blob/20a91e764a28816ee2234fcadb73bd59a613a44c/run_nerf.py#L123

Problem 3.

NeRFRaysampler does not correctly work with batch_size > 1

e.g., here

# Take the "sel_rays" rays from the full ray bundle.
ray_bundle = RayBundle(
*[
v.view(n_pixels, -1)[sel_rays]
.view(batch_size, sel_rays.numel() // batch_size, -1)
.to(device)
for v in full_ray_bundle
]
)

causes the case when the rays originally pointing to the different batch idx will now point to the same batch_idx

also, some index bound errors would be here:

n_pixels = full_ray_bundle.directions.shape[:-1].numel()

if chunksize is None:
chunksize = n_pixels * batch_size
start = chunk_idx * chunksize * batch_size
end = min(start + chunksize, n_pixels)
sel_rays = torch.arange(
start,
end,
dtype=torch.long,
device=full_ray_bundle.lengths.device,
)

here, e.g., also no batch dimension in data actually going to the function

camera: A batch of cameras from which the scene is rendered.
image: A batch of corresponding ground truth images of shape

camera: A batch of cameras from which the scene is rendered.
image: A batch of corresponding ground truth images of shape

@nikhilaravi nikhilaravi added the how to How to use PyTorch3D in my project label Oct 8, 2021
@rakhimovv
Copy link
Author

up

facebook-github-bot pushed a commit that referenced this issue Nov 5, 2021
Summary:
- Old NDC convention had xy coords in [-1,1]x[-1,1]
- New NDC convention has xy coords in [-1, 1]x[-u, u] or [-u, u]x[-1, 1]

where u > 1 is the aspect ratio of the image.

This PR fixes the NDC raysampler to use the new convention.

Partial fix for #868

Pull Request resolved: fairinternal/pytorch3d#29

Reviewed By: davnov134

Differential Revision: D31926148

Pulled By: bottler

fbshipit-source-id: c6c42c60d1473b04e60ceb49c8c10951ddf03c74
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Nov 29, 2021
@rakhimovv
Copy link
Author

up

@github-actions github-actions bot removed the Stale label Nov 30, 2021
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Dec 30, 2021
@github-actions
Copy link

github-actions bot commented Jan 4, 2022

This issue was closed because it has been stalled for 5 days with no activity.

@github-actions github-actions bot closed this as completed Jan 4, 2022
@bottler bottler reopened this Feb 7, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 5 days with no activity.

@bottler bottler reopened this Feb 17, 2022
@bottler bottler added do-not-reap Do not delete this pull request or issue due to inactivity. and removed Stale labels Feb 17, 2022
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Mar 20, 2022
@bottler bottler removed the Stale label Mar 21, 2022
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Apr 21, 2022
@bottler bottler removed the Stale label Apr 25, 2022
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label May 26, 2022
@bottler bottler removed the Stale label May 26, 2022
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Jun 26, 2022
@bottler bottler removed the Stale label Jun 27, 2022
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label Jul 28, 2022
@bottler bottler removed the Stale label Jul 28, 2022
@sergeyprokudin
Copy link
Contributor

Hi folks, NeRF implementation for non-square images indeed seems to be the problem. E.g. I cannot reproduce 'fern' dataset results, as also mentioned here by other researchers. Any plans on investigating the issues any time soon?..

Again, thanks for the great repo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-reap Do not delete this pull request or issue due to inactivity. how to How to use PyTorch3D in my project
Projects
None yet
Development

No branches or pull requests

5 participants