Skip to content
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

Reduce memory consumption by gathering a subset of rays earlier #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def angle_to_rotation_matrix(a,axis):
M = M.roll((roll,roll),dims=(-2,-1))
return M

def get_center_and_ray(opt,pose,intr=None): # [HW,2]
def get_center_and_ray(opt,pose,intr=None,ray_idx=None): # [HW,2]
# given the intrinsic/extrinsic matrices, get the camera center and ray directions]
assert(opt.camera.model=="perspective")
with torch.no_grad():
Expand All @@ -232,6 +232,9 @@ def get_center_and_ray(opt,pose,intr=None): # [HW,2]
x_range = torch.arange(opt.W,dtype=torch.float32,device=opt.device).add_(0.5)
Y,X = torch.meshgrid(y_range,x_range) # [H,W]
xy_grid = torch.stack([X,Y],dim=-1).view(-1,2) # [HW,2]
if ray_idx is not None:
# consider only subset of rays
xy_grid = xy_grid[ray_idx]
# compute center and ray
batch_size = len(pose)
xy_grid = xy_grid.repeat(batch_size,1,1) # [B,HW,2]
Expand Down
7 changes: 2 additions & 5 deletions model/nerf.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,9 @@ def get_pose(self,opt,var,mode=None):

def render(self,opt,pose,intr=None,ray_idx=None,mode=None):
batch_size = len(pose)
center,ray = camera.get_center_and_ray(opt,pose,intr=intr) # [B,HW,3]
center,ray = camera.get_center_and_ray(opt,pose,intr=intr,ray_idx=ray_idx) # [B,HW,3]
while ray.isnan().any(): # TODO: weird bug, ray becomes NaN arbitrarily if batch_size>1, not deterministic reproducible
center,ray = camera.get_center_and_ray(opt,pose,intr=intr) # [B,HW,3]
if ray_idx is not None:
# consider only subset of rays
center,ray = center[:,ray_idx],ray[:,ray_idx]
center,ray = camera.get_center_and_ray(opt,pose,intr=intr,ray_idx=ray_idx) # [B,HW,3]
if opt.camera.ndc:
# convert center/ray representations to NDC
center,ray = camera.convert_NDC(opt,center,ray,intr=intr)
Expand Down