-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
31 lines (24 loc) · 1017 Bytes
/
dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
from PIL import Image
from torch.utils.data import Dataset
import numpy as np
class ImageDataset(Dataset):
def __init__(self, image_dir, mask_dir, transform=None):
super(ImageDataset, self).__init__()
self.image_dir = image_dir
self.mask_dir = mask_dir
self.transform = None
self.num_image = os.listdir(image_dir)
def __len__(self):
return len(self.num_image)
def __getitem__(self, index):
img_path = os.path.join(self.image_dir, self.num_image[index])
mask_path = os.path.join(self.mask_dir, self.num_image[index]).replace(".jpg", ".png")
image = np.array(Image.open(img_path).convert('RGB'))
mask = np.array(Image.open(mask_path).convert('L'), dtype=np.float32)
mask[mask==255.0] = 1
if self.transform is not None:
augmentation = self.transform(image=image, mask=mask)
image = augmentation['image']
mask = augmentation['mask']
return image, mask