mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-24 18:14:16 +08:00
Support downsample in dataset (#973)
* init * tests passing * pass through arguments * verbose * remove debug prints * switch to bilinear * update viewer * make parser outputs private * change name
This commit is contained in:
1 parent
aaaa2fa393
commit
a82d19f549
8 files changed
+64
-30
No files matched your search
@@ -17,6 +17,7 @@ Dataset.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import deepcopy
|
||||
from typing import Dict
|
||||
|
||||
import numpy as np
|
||||
@@ -35,15 +36,21 @@ class InputDataset(Dataset):
|
||||
|
||||
Args:
|
||||
dataparser_outputs: description of where and how to read input images.
|
||||
scale_factor: The scaling factor for the dataparser outputs
|
||||
"""
|
||||
|
||||
def __init__(self, dataparser_outputs: DataparserOutputs):
|
||||
def __init__(self, dataparser_outputs: DataparserOutputs, scale_factor: float = 1.0):
|
||||
super().__init__()
|
||||
self.dataparser_outputs = dataparser_outputs
|
||||
self.has_masks = self.dataparser_outputs.mask_filenames is not None
|
||||
self._dataparser_outputs = dataparser_outputs
|
||||
self.has_masks = dataparser_outputs.mask_filenames is not None
|
||||
self.scale_factor = scale_factor
|
||||
self.scene_box = deepcopy(dataparser_outputs.scene_box)
|
||||
self.metadata = deepcopy(dataparser_outputs.metadata)
|
||||
self.cameras = deepcopy(dataparser_outputs.cameras)
|
||||
self.cameras.rescale_output_resolution(scaling_factor=scale_factor)
|
||||
|
||||
def __len__(self):
|
||||
return len(self.dataparser_outputs.image_filenames)
|
||||
return len(self._dataparser_outputs.image_filenames)
|
||||
|
||||
def get_numpy_image(self, image_idx: int) -> npt.NDArray[np.uint8]:
|
||||
"""Returns the image of shape (H, W, 3 or 4).
|
||||
@@ -51,8 +58,12 @@ class InputDataset(Dataset):
|
||||
Args:
|
||||
image_idx: The image index in the dataset.
|
||||
"""
|
||||
image_filename = self.dataparser_outputs.image_filenames[image_idx]
|
||||
image_filename = self._dataparser_outputs.image_filenames[image_idx]
|
||||
pil_image = Image.open(image_filename)
|
||||
if self.scale_factor != 1.0:
|
||||
width, height = pil_image.size
|
||||
newsize = (int(width * self.scale_factor), int(height * self.scale_factor))
|
||||
pil_image = pil_image.resize(newsize, resample=Image.BILINEAR)
|
||||
image = np.array(pil_image, dtype="uint8") # shape is (h, w, 3 or 4)
|
||||
assert len(image.shape) == 3
|
||||
assert image.dtype == np.uint8
|
||||
@@ -66,9 +77,9 @@ class InputDataset(Dataset):
|
||||
image_idx: The image index in the dataset.
|
||||
"""
|
||||
image = torch.from_numpy(self.get_numpy_image(image_idx).astype("float32") / 255.0)
|
||||
if self.dataparser_outputs.alpha_color is not None and image.shape[-1] == 4:
|
||||
if self._dataparser_outputs.alpha_color is not None and image.shape[-1] == 4:
|
||||
assert image.shape[-1] == 4
|
||||
image = image[:, :, :3] * image[:, :, -1:] + self.dataparser_outputs.alpha_color * (1.0 - image[:, :, -1:])
|
||||
image = image[:, :, :3] * image[:, :, -1:] + self._dataparser_outputs.alpha_color * (1.0 - image[:, :, -1:])
|
||||
else:
|
||||
image = image[:, :, :3]
|
||||
return image
|
||||
@@ -83,8 +94,8 @@ class InputDataset(Dataset):
|
||||
data = {"image_idx": image_idx}
|
||||
data["image"] = image
|
||||
if self.has_masks:
|
||||
mask_filepath = self.dataparser_outputs.mask_filenames[image_idx]
|
||||
data["mask"] = get_image_mask_tensor_from_path(filepath=mask_filepath)
|
||||
mask_filepath = self._dataparser_outputs.mask_filenames[image_idx]
|
||||
data["mask"] = get_image_mask_tensor_from_path(filepath=mask_filepath, scale_factor=self.scale_factor)
|
||||
metadata = self.get_metadata(data)
|
||||
data.update(metadata)
|
||||
return data
|
||||
|
||||
@@ -32,12 +32,10 @@ class SemanticDataset(InputDataset):
|
||||
dataparser_outputs: description of where and how to read input images.
|
||||
"""
|
||||
|
||||
def __init__(self, dataparser_outputs: DataparserOutputs):
|
||||
super().__init__(dataparser_outputs)
|
||||
assert "semantics" in dataparser_outputs.metadata.keys() and isinstance(
|
||||
dataparser_outputs.metadata["semantics"], Semantics
|
||||
)
|
||||
self.semantics = dataparser_outputs.metadata["semantics"]
|
||||
def __init__(self, dataparser_outputs: DataparserOutputs, scale_factor: float = 1.0):
|
||||
super().__init__(dataparser_outputs, scale_factor)
|
||||
assert "semantics" in dataparser_outputs.metadata.keys() and isinstance(self.metadata["semantics"], Semantics)
|
||||
self.semantics = self.metadata["semantics"]
|
||||
self.mask_indices = torch.tensor(
|
||||
[self.semantics.classes.index(mask_class) for mask_class in self.semantics.mask_classes]
|
||||
).view(1, 1, -1)
|
||||
@@ -46,7 +44,7 @@ class SemanticDataset(InputDataset):
|
||||
# handle mask
|
||||
filepath = self.semantics.filenames[data["image_idx"]]
|
||||
semantic_label, mask = get_semantics_and_mask_tensors_from_path(
|
||||
filepath=filepath, mask_indices=self.mask_indices
|
||||
filepath=filepath, mask_indices=self.mask_indices, scale_factor=self.scale_factor
|
||||
)
|
||||
if "mask" in data.keys():
|
||||
mask = mask & data["mask"]
|
||||
|
||||
Reference in new issue
Block a user