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:
Nikhil Mohan authored and GitHub committed 2022-11-19 17:06:34 +00:00
1 parent aaaa2fa393
commit a82d19f549
8 files changed
+64 -30

No files matched your search

+10 -2
View File
@@ -21,17 +21,21 @@ import torch
from PIL import Image
def get_image_mask_tensor_from_path(filepath: Path) -> torch.Tensor:
def get_image_mask_tensor_from_path(filepath: Path, scale_factor: float = 1.0) -> torch.Tensor:
"""
Utility function to read a mask image from the given path and return a boolean tensor
"""
pil_mask = Image.open(filepath)
if scale_factor != 1.0:
width, height = pil_mask.size
newsize = (int(width * scale_factor), int(height * scale_factor))
pil_mask = pil_mask.resize(newsize, resample=Image.NEAREST)
mask_tensor = torch.from_numpy(np.array(pil_mask)).unsqueeze(-1).bool()
return mask_tensor
def get_semantics_and_mask_tensors_from_path(
filepath: Path, mask_indices: Union[List, torch.Tensor]
filepath: Path, mask_indices: Union[List, torch.Tensor], scale_factor: float = 1.0
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Utility function to read segmentation from the given filepath
@@ -40,6 +44,10 @@ def get_semantics_and_mask_tensors_from_path(
if isinstance(mask_indices, List):
mask_indices = torch.tensor(mask_indices, dtype="int64").view(1, 1, -1)
pil_image = Image.open(filepath)
if scale_factor != 1.0:
width, height = pil_image.size
newsize = (int(width * scale_factor), int(height * scale_factor))
pil_image = pil_image.resize(newsize, resample=Image.NEAREST)
semantics = torch.from_numpy(np.array(pil_image, dtype="int64"))[..., None]
mask = torch.sum(semantics == mask_indices, dim=-1, keepdim=True) == 0
return semantics, mask