-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyDataGenerator.py
More file actions
60 lines (47 loc) · 1.88 KB
/
Copy pathMyDataGenerator.py
File metadata and controls
60 lines (47 loc) · 1.88 KB
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
import keras
from scipy.misc import imresize
import numpy as np
from PIL import Image
resize_factor = 0.6
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, path, list_IDs, labels, batch_size=32, dim=(32,32,32), n_channels=3,
shuffle=True, fully_convolutional = True):
'Initialization'
self.dim = dim
self.batch_size = batch_size
self.labels = labels
self.list_IDs = list_IDs
self.n_channels = n_channels
self.shuffle = shuffle
self.on_epoch_end()
self.path = path
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.list_IDs) / self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
# Generate indexes of the batch
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
# Find list of IDs
list_IDs_temp = [self.list_IDs[k] for k in indexes]
# Generate data
X, y = self.__data_generation(list_IDs_temp)
return X, y
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.shuffle(self.indexes)
def __data_generation(self, list_IDs_temp):
'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
# Initialization
X = np.empty((self.batch_size, *self.dim, self.n_channels))
y = np.empty((self.batch_size, 2), dtype=float)
# Generate data
for i, ID in enumerate(list_IDs_temp):
# Store sample
X[i,] = imresize(np.array(Image.open(self.path + ID + '.png')), resize_factor)/255.0
# Store class
y[i,0], y[i,1] = self.labels[ID]
return X, y