-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
74 lines (55 loc) · 2.16 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import cv2
import numpy as np
from PIL import Image
from tensorflow.keras.utils import Sequence
class Dataset:
def __init__(self,
images_dir,
maps_dir,
augmentation=None,
preprocessing=None):
self.img_ids = os.listdir(images_dir)
self.img_fps = [os.path.join(images_dir, id) for id in self.img_ids]
self.map_fps = [os.path.join(maps_dir, id) for id in self.img_ids]
self.maps_dir = maps_dir
self.augmentation = augmentation
self.preprocessing = preprocessing
def __getitem__(self, idx):
image = cv2.imread(os.path.join(self.img_fps[idx]))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (512, 512))
map = Image.open(os.path.join(self.map_fps[idx]))
map = np.array(map).astype('uint8')
map[map > 0] = 1
map = cv2.resize(map, (512, 512))
map = np.expand_dims(map, axis=-1)
if self.augmentation:
aug_sample = self.augmentation(image=image, mask=map)
image, map = aug_sample['image'], aug_sample['mask']
if self.preprocessing:
processed_sample = self.preprocessing(image=image, mask=map)
image, map = processed_sample['image'], processed_sample['mask']
return image, map.astype(np.float32)
def __len__(self):
return len(self.img_ids)
class DataLoader(Sequence):
def __init__(self, dataset, batch_size=1, shuffle=False):
self.indices = np.arange(len(dataset))
self.dataset = dataset
self.batch_size = batch_size
self.shuffle = shuffle
self.on_epoch_end()
def __getitem__(self, i):
start = i * self.batch_size
stop = (i + 1) * self.batch_size
data = []
for idx in range(start, stop):
data.append(self.dataset[idx])
batch = [np.stack(samples, axis=0) for samples in zip(*data)]
return batch
def __len__(self):
return len(self.indices) // self.batch_size
def on_epoch_end(self):
if self.shuffle:
self.indices = np.random.permutation(self.indices)