-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerator.py
62 lines (34 loc) · 1.2 KB
/
Generator.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
import keras
import numpy as np
import os
class DataGenerator(keras.utils.Sequence):
def __init__(self,data_path,ids,batch_size=32,shuffle=True):
self.ids = ids
self.batch_size = batch_size
self.shuffle = shuffle
self.data_path = data_path
self.on_epoch_end()
def __len__(self):
return int(np.floor(len(self.ids)/self.batch_size))
def __getitem__(self,index):
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
ids_temp = [self.ids[k] for k in indexes]
X,Y = self.__data_generation(ids_temp)
return X,Y
def on_epoch_end(self):
self.indexes = np.arange(len(self.ids))
if self.shuffle==True:
np.random.shuffle(self.indexes)
def __data_generation(self,ids):
X = []
Y = []
for id in ids:
img_path = self.data_path+"/{}/img.npy".format(id)
mask_path = self.data_path+"/{}/mask.npy".format(id)
img = np.load(img_path)
mask = np.load(mask_path)
X.append(img)
Y.append(mask)
X = np.array(X)
Y = np.array(Y)
return X,Y