Fixes bug in PairPixelSampler when working with masked dataset (#2368)

* Fixes bug in PairPixelSampler when working with masked dataset

* Formatting

* Black formatting now ok.

---------

Co-authored-by: AdamRashid96 <71362382+AdamRashid96@users.noreply.github.com>
Co-authored-by: Brent Yi <yibrenth@gmail.com>
This commit is contained in:
authored and GitHub committed 2023-12-01 01:11:11 +00:00
1 parent 49503c2175
commit 0cb410001b
1 file changed
+11 -13
+11 -13
View File
@@ -17,13 +17,7 @@ Code for sampling pixels.
""" """
import random import random
import torch
from jaxtyping import Int
from torch import Tensor
from dataclasses import dataclass, field from dataclasses import dataclass, field
from nerfstudio.data.utils.pixel_sampling_utils import erode_mask
from typing import ( from typing import (
Dict, Dict,
Optional, Optional,
@@ -31,9 +25,14 @@ from typing import (
Union, Union,
) )
import torch
from jaxtyping import Int
from torch import Tensor
from nerfstudio.configs.base_config import ( from nerfstudio.configs.base_config import (
InstantiateConfig, InstantiateConfig,
) )
from nerfstudio.data.utils.pixel_sampling_utils import erode_mask
@dataclass @dataclass
@@ -398,19 +397,18 @@ class PairPixelSampler(PixelSampler): # pylint: disable=too-few-public-methods
device: Union[torch.device, str] = "cpu", device: Union[torch.device, str] = "cpu",
) -> Int[Tensor, "batch_size 3"]: ) -> Int[Tensor, "batch_size 3"]:
rays_to_sample = self.rays_to_sample rays_to_sample = self.rays_to_sample
if batch_size is not None:
assert (
int(batch_size) % 2 == 0
), f"PairPixelSampler can only return batch sizes in multiples of two (got {batch_size})"
rays_to_sample = batch_size // 2
if isinstance(mask, Tensor): if isinstance(mask, Tensor):
m = erode_mask(mask.permute(0, 3, 1, 2).float(), pixel_radius=self.radius) 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) nonzero_indices = torch.nonzero(m[:, 0], as_tuple=False).to(device)
chosen_indices = random.sample(range(len(nonzero_indices)), k=rays_to_sample) chosen_indices = random.sample(range(len(nonzero_indices)), k=rays_to_sample)
indices = nonzero_indices[chosen_indices] indices = nonzero_indices[chosen_indices]
else: else:
rays_to_sample = self.rays_to_sample
if batch_size is not None:
assert (
int(batch_size) % 2 == 0
), f"PairPixelSampler can only return batch sizes in multiples of two (got {batch_size})"
rays_to_sample = batch_size // 2
s = (rays_to_sample, 1) s = (rays_to_sample, 1)
ns = torch.randint(0, num_images, s, dtype=torch.long, device=device) ns = torch.randint(0, num_images, s, dtype=torch.long, device=device)
hs = torch.randint(self.radius, image_height - self.radius, s, dtype=torch.long, device=device) hs = torch.randint(self.radius, image_height - self.radius, s, dtype=torch.long, device=device)