mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-04 06:49:13 +08:00
dataloader logic, new folder structure, flatter configs (#26)
* renaming dataloader to data. adding some dataloader classes * restructuring configs * fixed instant ngp tcnn direction rescaling * working through test cases * black and lint and tests
This commit is contained in:
1 parent
24c1c77ef1
commit
457788d689
105 files changed
+8828
-682
No files matched your search
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
Code for sampling pixels.
|
||||
"""
|
||||
|
||||
import random
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
def collate_image_dataset_batch(batch, num_rays_per_batch, keep_full_image: bool = False):
|
||||
"""
|
||||
Operates on a batch of images and samples pixels to use for generating rays.
|
||||
Returns a collated batch which is input to the Graph.
|
||||
It will sample only within the valid 'mask' if it's specified.
|
||||
"""
|
||||
device = batch["image"].device
|
||||
num_images, image_height, image_width, _ = batch["image"].shape
|
||||
|
||||
# only sample within the mask, if the mask is in the batch
|
||||
if "mask" in batch:
|
||||
nonzero_indices = torch.nonzero(batch["mask"][..., 0], as_tuple=False)
|
||||
chosen_indices = random.sample(range(len(nonzero_indices)), k=num_rays_per_batch)
|
||||
indices = nonzero_indices[chosen_indices]
|
||||
else:
|
||||
indices = torch.floor(
|
||||
torch.rand((num_rays_per_batch, 3), device=device)
|
||||
* torch.tensor([num_images, image_height, image_width], device=device)
|
||||
).long()
|
||||
|
||||
c, y, x = [i.flatten() for i in torch.split(indices, 1, dim=-1)]
|
||||
image = batch["image"][c, y, x]
|
||||
if "mask" in batch:
|
||||
mask = batch["mask"][c, y, x]
|
||||
if "semantics" in batch:
|
||||
semantics = batch["semantics"][c, y, x]
|
||||
assert image.shape == (num_rays_per_batch, 3), image.shape
|
||||
|
||||
# Needed to correct the random indices to their actual camera idx locations.
|
||||
local_indices = indices.clone()
|
||||
indices[:, 0] = batch["image_idx"][c]
|
||||
collated_batch = {
|
||||
"local_indices": local_indices, # local to the batch returned
|
||||
"indices": indices, # with the abs camera indices
|
||||
"image": image,
|
||||
}
|
||||
if "mask" in batch:
|
||||
collated_batch["mask"] = mask
|
||||
if "semantics" in batch:
|
||||
collated_batch["semantics"] = semantics
|
||||
|
||||
if keep_full_image:
|
||||
collated_batch["full_image"] = batch["image"]
|
||||
|
||||
return collated_batch
|
||||
|
||||
|
||||
class PixelSampler: # pylint: disable=too-few-public-methods
|
||||
"""Samples 'pixel_batch's from 'image_batch's."""
|
||||
|
||||
def __init__(self, num_rays_per_batch, keep_full_image=False) -> None:
|
||||
self.num_rays_per_batch = num_rays_per_batch
|
||||
self.keep_full_image = keep_full_image
|
||||
|
||||
def sample(self, image_batch):
|
||||
"""Sample an image batch and return a pixel batch."""
|
||||
pixel_batch = collate_image_dataset_batch(
|
||||
image_batch, self.num_rays_per_batch, keep_full_image=self.keep_full_image
|
||||
)
|
||||
return pixel_batch
|
||||
Reference in new issue
Block a user