-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[WIP] Depth nerfacto with visibility loss #2982
Open
ethanweber
wants to merge
11
commits into
main
Choose a base branch
from
ethan/visiblity-depth-nerfacto
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+276
−40
Open
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
159b370
visibility field code without modifications to pipeline yet
ethanweber 5447b21
changes before merging main
ethanweber b7c101b
added visibility loss method and to vanilla pipeline
ethanweber bc4c64f
resolved type errors
FrederikWarburg fbc68c7
create a renderer specific to visibility
FrederikWarburg 37b9337
minor changes
FrederikWarburg 526da86
temp changes to depth nerfacto
ethanweber 8518963
fixing merge conflicts
ethanweber 1717a2f
minor reverts
ethanweber 27aa299
visibility depth nerfacto
ethanweber fa9d7ab
Merge branch 'main' into ethan/visiblity-depth-nerfacto
maturk File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright 2022 the Regents of the University of California, Nerfstudio Team and contributors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Visibility Field""" | ||
|
||
import torch | ||
from torch import nn | ||
|
||
from nerfstudio.cameras.cameras import Cameras | ||
from nerfstudio.cameras.rays import RaySamples | ||
|
||
|
||
class VisibilityField(nn.Module): | ||
"""Visibility Field""" | ||
|
||
def __init__(self, cameras: Cameras) -> None: | ||
super().__init__() | ||
# training camera tranforms | ||
# TODO: use optimized cameras | ||
self.c2ws = cameras.camera_to_worlds | ||
self.c2whs = torch.cat([self.c2ws, torch.zeros_like(self.c2ws[:, :1, :])], dim=1) | ||
self.c2whs[:, 3, 3] = 1.0 | ||
self.w2chs = torch.inverse(self.c2whs) | ||
self.K = cameras.get_intrinsics_matrices() | ||
self.image_height = cameras.height | ||
self.image_width = cameras.width | ||
|
||
@torch.no_grad() | ||
def forward(self, ray_samples: RaySamples, camera_chunk_size=50, ray_chunk_size=4096) -> torch.Tensor: | ||
""" | ||
Args: | ||
ray_samples: Ray samples. | ||
camera_chunk_size: Number of cameras to process at once to avoid memory issues. | ||
ray_chunk_size: Number of rays to process at once to avoid memory issues. | ||
Returns: | ||
""" | ||
# get positions | ||
positions = ray_samples.frustums.get_positions() # [N, S, 3] | ||
# project positions into each camera | ||
# move to homogeneous coordinates | ||
positions = torch.cat([positions, torch.ones_like(positions[..., :1])], dim=-1) | ||
N, S, _ = positions.shape | ||
B = self.w2chs.shape[0] # num cameras | ||
p = positions.view(N * S, 4).transpose(0, 1).unsqueeze(0) # [1, 4, N*S] | ||
p = p.expand(B, *p.shape[1:]) # [B, 4, N*S] | ||
|
||
num_views = torch.zeros([N, S, 1], device=positions.device) | ||
for i in range(0, B, camera_chunk_size): | ||
ccs = min(camera_chunk_size, B - i) | ||
for j in range(0, N, ray_chunk_size): | ||
rcs = min(ray_chunk_size, N - j) | ||
|
||
ptemp = p.reshape(B, 4, N, S)[i : i + ccs, :, j : j + rcs, :].reshape(ccs, 4, rcs * S) | ||
cam_coords = torch.bmm(self.w2chs[i : i + ccs, :], ptemp) | ||
|
||
# flip y and z axes | ||
cam_coords[:, 1, :] *= -1 | ||
cam_coords[:, 2, :] *= -1 | ||
|
||
z = cam_coords[:, 2:3, :].transpose(1, 2).view(ccs, rcs, S, 1) # [CS, RCS, S, 1] | ||
mask_z = z[..., 0] > 0 | ||
|
||
# divide by z | ||
cam_coords = cam_coords[:, :3, :] / cam_coords[:, 2:3, :] | ||
|
||
cam_points = torch.bmm(self.K[i : i + ccs], cam_coords) | ||
|
||
pixel_coords = cam_points[:, :2, :].transpose(1, 2).view(ccs, rcs, S, 2) # [CS, RCS, S, 2] | ||
x = pixel_coords[..., 0] | ||
y = pixel_coords[..., 1] | ||
mask_x = (x >= 0) & (x < self.image_width.view(B, 1, 1)[i : i + ccs]) | ||
mask_y = (y >= 0) & (y < self.image_height.view(B, 1, 1)[i : i + ccs]) | ||
mask = mask_x & mask_y & mask_z | ||
# sum over the batch dimension | ||
nv = mask.sum(dim=0).unsqueeze(-1) | ||
# nv is [N, S, 1] # this is the number of camera frustums that the point belongs to | ||
# for this particular chunk of cameras | ||
num_views[j : j + rcs] += nv | ||
return num_views |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it maybe make sense to take in positions (Nx3) directly instead of ray_samples? this would make it compatible with other methods which might want to use visibility like splatting.