2022-07-01 10:00:25 +00:00
|
|
|
from pathlib import Path
|
2021-08-16 00:53:00 +00:00
|
|
|
|
2022-07-04 12:38:48 +00:00
|
|
|
import albumentations as A
|
2021-08-16 00:53:00 +00:00
|
|
|
import numpy as np
|
2022-07-04 12:38:48 +00:00
|
|
|
from albumentations.pytorch import ToTensorV2
|
2021-08-16 00:53:00 +00:00
|
|
|
from PIL import Image
|
|
|
|
from torch.utils.data import Dataset
|
|
|
|
|
|
|
|
|
2022-07-08 14:23:22 +00:00
|
|
|
class SyntheticDataset(Dataset):
|
|
|
|
def __init__(self, image_dir, transform):
|
2022-07-01 10:00:25 +00:00
|
|
|
self.images = list(Path(image_dir).glob("**/*.jpg"))
|
2022-06-28 09:36:43 +00:00
|
|
|
self.transform = transform
|
2021-08-16 00:53:00 +00:00
|
|
|
|
|
|
|
def __len__(self):
|
2022-06-28 09:36:43 +00:00
|
|
|
return len(self.images)
|
2022-06-27 14:40:04 +00:00
|
|
|
|
2022-06-28 09:36:43 +00:00
|
|
|
def __getitem__(self, index):
|
2022-07-08 14:23:22 +00:00
|
|
|
# open and convert image
|
2022-07-01 10:00:25 +00:00
|
|
|
image = np.array(Image.open(self.images[index]).convert("RGB"), dtype=np.uint8)
|
2021-08-16 00:53:00 +00:00
|
|
|
|
2022-07-08 14:23:22 +00:00
|
|
|
# create empty mask of same size
|
|
|
|
mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8)
|
|
|
|
|
|
|
|
# augment image and mask
|
|
|
|
augmentations = self.transform(image=image, mask=mask)
|
|
|
|
image = augmentations["image"]
|
|
|
|
mask = augmentations["mask"]
|
|
|
|
|
|
|
|
# convert image & mask to Tensor float in [0, 1]
|
|
|
|
post_process = A.Compose(
|
|
|
|
[
|
|
|
|
A.ToFloat(max_value=255),
|
|
|
|
ToTensorV2(),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
augmentations = post_process(image=image, mask=mask)
|
|
|
|
image = augmentations["image"]
|
|
|
|
mask = augmentations["mask"]
|
2022-06-30 21:28:38 +00:00
|
|
|
|
|
|
|
# make sure image and mask are floats
|
|
|
|
image = image.float()
|
|
|
|
mask = mask.float()
|
2021-08-16 00:53:00 +00:00
|
|
|
|
2022-06-28 09:36:43 +00:00
|
|
|
return image, mask
|
2022-07-08 14:23:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LabeledDataset(Dataset):
|
|
|
|
def __init__(self, image_dir):
|
|
|
|
self.images = list(Path(image_dir).glob("**/*.jpg"))
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.images)
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
# open and convert image
|
|
|
|
image = np.array(Image.open(self.images[index]).convert("RGB"), dtype=np.uint8)
|
|
|
|
|
|
|
|
# open and convert mask
|
|
|
|
mask_path = self.images[index].parent.joinpath("MASK.PNG")
|
|
|
|
mask = np.array(Image.open(mask_path).convert("L"), dtype=np.uint8) / 255
|
|
|
|
|
|
|
|
# convert image & mask to Tensor float in [0, 1]
|
|
|
|
post_process = A.Compose(
|
|
|
|
[
|
2022-07-10 15:12:00 +00:00
|
|
|
A.SmallestMaxSize(1024),
|
2022-07-08 14:23:22 +00:00
|
|
|
A.ToFloat(max_value=255),
|
|
|
|
ToTensorV2(),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
augmentations = post_process(image=image, mask=mask)
|
|
|
|
image = augmentations["image"]
|
|
|
|
mask = augmentations["mask"]
|
|
|
|
|
|
|
|
return image, mask
|