From 7856aa006011404e0f0b4b77a6252f1ad5e8cc28 Mon Sep 17 00:00:00 2001 From: pierremerriaux-leddartech <42007976+pierremerriaux-leddartech@users.noreply.github.com> Date: Fri, 29 Sep 2023 19:49:20 -0400 Subject: [PATCH] fix:correction of PairPixelSampler in case of using masks with depth-nerfacto (#2477) fix:correction of PairPixelSampler in case of using masks --- nerfstudio/data/pixel_samplers.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/nerfstudio/data/pixel_samplers.py b/nerfstudio/data/pixel_samplers.py index 463d08294..d7e76806d 100644 --- a/nerfstudio/data/pixel_samplers.py +++ b/nerfstudio/data/pixel_samplers.py @@ -399,10 +399,11 @@ class PairPixelSampler(PixelSampler): # pylint: disable=too-few-public-methods mask: Optional[Tensor] = None, device: Union[torch.device, str] = "cpu", ) -> Int[Tensor, "batch_size 3"]: + rays_to_sample = self.rays_to_sample if isinstance(mask, Tensor): m = erode_mask(mask.permute(0, 3, 1, 2).float(), pixel_radius=self.radius) nonzero_indices = torch.nonzero(m[:, 0], as_tuple=False).to(device) - chosen_indices = random.sample(range(len(nonzero_indices)), k=self.rays_to_sample) + chosen_indices = random.sample(range(len(nonzero_indices)), k=rays_to_sample) indices = nonzero_indices[chosen_indices] else: rays_to_sample = self.rays_to_sample @@ -418,12 +419,12 @@ class PairPixelSampler(PixelSampler): # pylint: disable=too-few-public-methods ws = torch.randint(self.radius, image_width - self.radius, s, dtype=torch.long, device=device) indices = torch.concat((ns, hs, ws), dim=1) - pair_indices = torch.hstack( - ( - torch.zeros(rays_to_sample, 1, device=device, dtype=torch.long), - torch.randint(-self.radius, self.radius, (rays_to_sample, 2), device=device, dtype=torch.long), - ) + pair_indices = torch.hstack( + ( + torch.zeros(rays_to_sample, 1, device=device, dtype=torch.long), + torch.randint(-self.radius, self.radius, (rays_to_sample, 2), device=device, dtype=torch.long), ) - pair_indices += indices - indices = torch.hstack((indices, pair_indices)).view(rays_to_sample * 2, 3) + ) + pair_indices += indices + indices = torch.hstack((indices, pair_indices)).view(rays_to_sample * 2, 3) return indices