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

Added All pixel Sampler #3031

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
30 changes: 30 additions & 0 deletions nerfstudio/data/pixel_samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,33 @@ def sample_method( # pylint: disable=no-self-use
pair_indices += indices
indices = torch.hstack((indices, pair_indices)).view(rays_to_sample * 2, 3)
return indices


@dataclass
class AllPixelSamplerConfig(PixelSamplerConfig):
_target: Type = field(default_factory=lambda: AllPixelSampler)


class AllPixelSampler(PixelSampler):
config: AllPixelSamplerConfig

def sample(self, image_batch: Dict):
"""
Samples all pixels from a single image. The image is selected randomly from the batch.
"""
device = image_batch["image"][0].device
num_images, image_height, image_width, _ = image_batch["image"].shape

image_index = torch.randint(num_images, (1,), device=device)
i, j = torch.meshgrid(
torch.arange(image_height, device=device), torch.arange(image_width, device=device), indexing="ij"
)

pixels = torch.stack([j, i], dim=-1).reshape(-1, 2)
pixels = torch.cat((image_index.repeat(pixels.shape[0], 1), pixels), dim=1)

collated_batch = {}
collated_batch["image"] = image_batch["image"][image_index]
collated_batch["indices"] = pixels

return collated_batch
Loading