-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
32 lines (23 loc) · 937 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
31
32
import os
from torch.utils.data import Dataset
from PIL import Image
import numpy as np
class CarvanaDataset(Dataset):
def __init__(self, img_dir, mask_dir, transform=None):
self.img_dir = img_dir
self.mask_dir = mask_dir
self.transform = transform
self.imgs = os.listdir(img_dir)
def __len__(self):
return len(self.imgs)
def __getitem__(self, idx: int):
img_path = os.path.join(self.img_dir, self.imgs[idx])
mask_path = os.path.join(self.mask_dir, self.imgs[idx].replace('.jpg', '_mask.gif'))
img = np.array(Image.open(img_path))
mask = np.array(Image.open(mask_path).convert('L'), dtype=np.float32)
mask[mask == 255.] = 1.
if self.transform is not None:
augmentations = self.transform(image=img, mask=mask)
img = augmentations['image']
mask = augmentations['mask']
return img, mask