Fix bug in pixel sampler (#3103)

* Fix bug in pixel sampler

* Refactor code for distributing rays across images
This commit is contained in:
Carl Lindström authored and GitHub committed 2024-04-30 09:57:20 -04:00
1 parent b190874ef1
commit 759fda8285
2 files changed
+34 -17

No files matched your search

+8 -16
View File
@@ -26,7 +26,7 @@ from jaxtyping import Int
from torch import Tensor
from nerfstudio.configs.base_config import InstantiateConfig
from nerfstudio.data.utils.pixel_sampling_utils import erode_mask
from nerfstudio.data.utils.pixel_sampling_utils import divide_rays_per_image, erode_mask
@dataclass
@@ -302,19 +302,15 @@ class PixelSampler:
all_images = []
all_depth_images = []
num_rays_in_batch = num_rays_per_batch // num_images
if num_rays_in_batch % 2 != 0:
num_rays_in_batch += 1
assert num_rays_per_batch % 2 == 0, "num_rays_per_batch must be divisible by 2"
num_rays_per_image = divide_rays_per_image(num_rays_per_batch, num_images)
if "mask" in batch:
for i in range(num_images):
for i, num_rays in enumerate(num_rays_per_image):
image_height, image_width, _ = batch["image"][i].shape
if i == num_images - 1:
num_rays_in_batch = num_rays_per_batch - (num_images - 1) * num_rays_in_batch
indices = self.sample_method(
num_rays_in_batch, 1, image_height, image_width, mask=batch["mask"][i].unsqueeze(0), device=device
num_rays, 1, image_height, image_width, mask=batch["mask"][i].unsqueeze(0), device=device
)
indices[:, 0] = i
all_indices.append(indices)
@@ -323,16 +319,12 @@ class PixelSampler:
all_depth_images.append(batch["depth_image"][i][indices[:, 1], indices[:, 2]])
else:
for i in range(num_images):
for i, num_rays in enumerate(num_rays_per_image):
image_height, image_width, _ = batch["image"][i].shape
if i == num_images - 1:
num_rays_in_batch = num_rays_per_batch - (num_images - 1) * num_rays_in_batch
if self.config.is_equirectangular:
indices = self.sample_method_equirectangular(
num_rays_in_batch, 1, image_height, image_width, device=device
)
indices = self.sample_method_equirectangular(num_rays, 1, image_height, image_width, device=device)
else:
indices = self.sample_method(num_rays_in_batch, 1, image_height, image_width, device=device)
indices = self.sample_method(num_rays, 1, image_height, image_width, device=device)
indices[:, 0] = i
all_indices.append(indices)
all_images.append(batch["image"][i][indices[:, 1], indices[:, 2]])