forked from Tony607/Keras_Deep_Clustering
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataGenerator.py
More file actions
executable file
·206 lines (165 loc) · 7.99 KB
/
Copy pathDataGenerator.py
File metadata and controls
executable file
·206 lines (165 loc) · 7.99 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import numpy as np
import keras
import cv2
from random import randint
import os, sys
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, list_IDs=None, labels=None, batch_size=32, dim=(32,32,32), n_channels=1,
n_classes=10, shuffle=True, datatype='.npy', datadirs=[], is_label_to_categorical=False, is_normalize_image_datatype=False,
is_apply_text_preprocessing = False, is_apply_sequence_preprocessing = False):
'Initialization need to provide either list_IDs or datadirs(dir paths)'
self.dim = dim
self.batch_size = batch_size
self.n_channels = n_channels
self.n_classes = n_classes
self.shuffle = shuffle
self.shuffle_cache = None
self.datatype = datatype
self.datadirs = datadirs
self.is_label_to_categorical = is_label_to_categorical
self.is_normalize_image_datatype = is_normalize_image_datatype
if not list_IDs == None:
self.is_dir_based_data_batches = False
self.list_IDs = list_IDs
else:
self.is_dir_based_data_batches = True
self.dir_batch_size = self.batch_size * 50
self.__init_IDs__(list_IDs, datadirs)
if not labels == None:
self.labels = labels
else:
if not list_IDs == None: #for datadirs the labels will be generated at runtime on first load and then updated and maintained on memory for the remaining runtime
self.labels = self.init_random_labels( len(list_IDs), n_classes)
self.on_epoch_end( is_xplicit_call = True )
def __init_IDs__(self, list_IDs, datadirs):
'usefull when number of records is large, to make it easier to even load ids by batch'
# support multiple dirs to make it easier to train large dataset in different dirs without moving them however to offset the gradient leaning towards
# one direction for one dir and for the other on the other direction it is made sure that large enough indexes are loaded together(to keep benefits
# of shuffeling) like loaded_indexes = batch_size * 50
self.datadirs = datadirs
self.datadirs_rec_count = []
#count total
self.total_records = 0
sdir = len(datadirs)
for i in range(0, sdir):
print( "__init_IDs__", self.datadirs[i] )
self.datadirs_rec_count.append( len([True for name in os.listdir(self.datadirs[i]) if os.path.isfile( os.path.join( self.datadirs[i], name ) ) ] ) )
self.total_records = self.total_records + self.datadirs_rec_count[i]
def load_dir_level_ID_batch(self, index):
''
tmp_cnt = 0
self.list_IDs = []
self.labels = []
sdir = len(self.datadirs)
self.dir_batch_index = index
self.bstart = index * self.dir_batch_size
self.benddd = (index * self.dir_batch_size) + self.dir_batch_size
for i in range(0, sdir):
if tmp_cnt + self.datadirs_rec_count[i] >= self.bstart:
for name in os.listdir(self.datadirs[i]):
if tmp_cnt >= self.bstart:
if tmp_cnt < self.benddd:
self.list_IDs.append( os.path.join( self.datadirs[i], name ) )
self.labels.append( randint(0, self.n_classes) )
else:
break
tmp_cnt = tmp_cnt + 1
if tmp_cnt < self.benddd:
tmp_cnt = tmp_cnt + self.datadirs_rec_count[i]
else:
break
#reset, useful especially when iterator is called manually
if index == self.__len__():
self.dir_batch_index = 0
def init_random_labels(self, list_IDs_len, n_classes):
'usefull for unsupervised learning.'
return np.random.choice( n_classes, list_IDs_len )
def __len__(self):
'Denotes the number of batches per epoch'
if not self.is_dir_based_data_batches:
return int(np.floor(len(self.list_IDs) / self.batch_size))
else:
print( "__len__", self.total_records, self.batch_size, int(np.floor(self.total_records / self.batch_size)) )
return int(np.floor(self.total_records / self.batch_size))
def __getitem__(self, index, is_return_tuple=True, is_return_only_x=True, is_keep_shuffled_index = False, is_return_last_paths = False ):
'Generate one batch of data'
#
if self.is_dir_based_data_batches:
if index*self.batch_size >= self.benddd:
self.load_dir_level_ID_batch(self.dir_batch_index + 1)
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
if is_return_last_paths:
self.indexes = self.shuffle_cache[index]
else:
np.random.shuffle(self.indexes)
if is_keep_shuffled_index:
if self.shuffle_cache is None:
self.shuffle_cache = {}
self.shuffle_cache[index] = self.indexes
# Generate indexes of the batch
indexes = self.indexes[ np.mod(index*self.batch_size, self.dir_batch_size) : np.mod((index+1)*self.batch_size, self.dir_batch_size) ]
# Find list of IDs
list_IDs_temp = [self.list_IDs[k] for k in indexes]
#
if is_return_last_paths:
return list_IDs_temp
# Generate data
X, y = self.__data_generation(list_IDs_temp)
print("__getitem__")
print(X.shape)
if self.is_normalize_image_datatype:
#reshapre
X = X.reshape((X.shape[0], -1))
#normalize
X = np.divide(X, 255.)
print(X.shape)
print(y.shape)
#if its reached the end of it then call on_epoch_end it's necessary in case of explicit loop calls in the code
if (index+1) == self.__len__():
self.on_epoch_end( is_xplicit_call = True )
if is_return_tuple:
if is_return_only_x:
return X, X
else:
return X, y
else:
return X
def on_epoch_end(self, is_xplicit_call=False):
'Updates indexes after each epoch'
if not is_xplicit_call:
print( "on_epoch_end called..." )
else:
print( "on_epoch_end called explicitly..." )
if not self.is_dir_based_data_batches:
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.shuffle(self.indexes)
else:
self.load_dir_level_ID_batch(0)
self.indexes = np.arange(len(self.list_IDs))
# #TODO better to do dir array shuffle in case dir based batches
# 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
if self.n_channels > 1:
X = np.empty((self.batch_size, *self.dim, self.n_channels))
else:
X = np.empty((self.batch_size, *self.dim))
y = np.empty((self.batch_size), dtype=int)
# Generate data
for i, ID in enumerate(list_IDs_temp):
# Store sample
if self.datatype == '.npy':
X[i,] = np.load( ID ) #absolute or relative path to data file
elif self.datatype == 'imgs_to_gray':
#print( "ID", ID )
X[i,] = cv2.cvtColor(cv2.imread( ID ), cv2.COLOR_BGR2GRAY) #absolute or relative path to record file
elif self.datatype == 'json':
X[i,] = cv2.cvtColor(cv2.imread( ID ), cv2.COLOR_BGR2GRAY) #TODO read as json array
# Store class
y[i] = self.labels[i]
return X, keras.utils.to_categorical(y, num_classes=self.n_classes) if self.is_label_to_categorical == True else y