-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_generator.py
More file actions
316 lines (226 loc) · 10.7 KB
/
Copy pathdata_generator.py
File metadata and controls
316 lines (226 loc) · 10.7 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
''' Create generators from dataset '''
import numpy as np
import cv2
import random
HIGH_DIM = 512
GLLIM_K = 1
BATCH_SIZE = 128
# Mode for the validation set for our mixture model
rnEqui=1
rnHard=2
rnTra=3
def load_data_generator_List(rootpath, imIn, file_test, validation=1.0,subsampling=1.0,processingTarget=None,transform=[],outSize=(224,224),batch_size=BATCH_SIZE,shuffle=False):
''' create generators from data'''
def generator(rootpath, images):
N=len(images)
nbatches=N/batch_size+1
if N%batch_size==0:
nbatches-=1
if shuffle:
random.shuffle(images)
i=0
while 1:
X, Y = get_xy_from_file(rootpath, images[i*batch_size:(i+1)*batch_size],processingTarget=processingTarget,transform=transform,outSize=outSize)
yield(X, Y)
i=i+1
if i>=nbatches:
i=0
random.shuffle(images)
imTest = open(rootpath+file_test, 'r').readlines()
gen_test = generator(rootpath, imTest)
test_size=len(imTest)
# we subsample the data if needed
if subsampling!=1.0:
im=imIn[0:int(subsampling*len(imIn))][:]
else:
im=imIn[:]
if validation!=1.0: # if we use a validation set
Ntot=len(im)
training_size = int(validation*len(im))
val_size = Ntot-training_size
gen_train = generator(rootpath, im[:training_size])
gen_val = generator(rootpath, im[training_size:])
return (gen_train,training_size),(gen_val,val_size), (gen_test,test_size)
else: # without validation set
gen_train = generator(rootpath, im)
training_size = len(im)
return (gen_train,training_size), (gen_test,test_size)
def load_data_generator(rootpath, file_train, file_test, validation=1.0,subsampling=1.0,processingTarget=None,transform=[],outSize=(224,224),batch_size=BATCH_SIZE,shuffle=False):
im = open(rootpath+file_train, 'r').readlines()
return load_data_generator_List(rootpath, im[:], file_test, validation,subsampling,processingTarget=processingTarget,transform=transform,outSize=outSize,batch_size=batch_size,shuffle=shuffle)
def load_data_generator_List_simple(rootpath, imIn,transform=[],outSize=(224,224),batch_size=BATCH_SIZE,processingTarget=None,sample_weights=None,shuffle=False):
''' create generators from data'''
def generator(rootpath, images, batch_size=BATCH_SIZE):
N=len(images)
nbatches=N/batch_size+1
if N%batch_size==0:
nbatches-=1
i=0
if sample_weights is not None:
rn= sample_weights[:]
if shuffle:
if sample_weights is None:
random.shuffle(images)
else:
c = zip(images,rn)
np.random.shuffle(c)
images = np.asarray([e[0] for e in c])
rn = np.asarray([e[1] for e in c])
while 1:
X, Y = get_xy_from_file(rootpath, images[i*batch_size:(i+1)*batch_size],processingTarget=processingTarget,transform=transform,outSize=outSize)
if sample_weights is None:
yield(X, Y)
else:
yield(X, Y,rn[i*batch_size:(i+1)*batch_size])
i=i+1
if i>=nbatches:
i=0
if sample_weights is None:
random.shuffle(images)
else:
c = zip(images,rn)
np.random.shuffle(c)
images = np.asarray([e[0] for e in c])
rn = np.asarray([e[1] for e in c])
gen = generator(rootpath, imIn[:])
size=len(imIn)
return (gen,size)
def load_data_generator_simple(rootpath, fileName, transform=[],outSize=(224,224),batch_size=BATCH_SIZE,processingTarget=None,shuffle=False):
im = open(rootpath+fileName, 'r').readlines()
return load_data_generator_List_simple(rootpath, im[:],transform=transform,outSize=outSize,processingTarget=processingTarget,shuffle=shuffle)
def load_data_generator_Uniform_List(rootpath, imIn, file_test,rni, valMode=rnEqui,validation=0.8,subsampling=1.0,outSize=(224,224),processingTarget=None,batch_size=BATCH_SIZE):
''' create generators from data for a mixure of gaussian + uniform'''
def generator(rootpath, images,rniIn, batch_size=BATCH_SIZE):
# im = get_sublist(im,rniIn)
c = zip(images,rniIn)
np.random.shuffle(c)
im = np.asarray([e[0] for e in c])
rni = np.asarray([e[1] for e in c])
N=len(im)
print "Size of the Selected Data: " + str(N)
nbatches=N/batch_size
if N%batch_size==0:
nbatches-=1
i=0
while 1:
X, Y = get_xy_from_file(rootpath, im[i*batch_size:(i+1)*batch_size],processingTarget=processingTarget,transform=None,outSize=outSize)
yield([X,rni[i*batch_size:(i+1)*batch_size]], Y*rni[i*batch_size:(i+1)*batch_size])
i=i+1
if i>=nbatches:
i=0
c = zip(images,rniIn)
np.random.shuffle(c)
im = np.asarray([e[0] for e in c])
rni = np.asarray([e[1] for e in c])
# im = get_sublist(images,rniIn)
# N=len(im)
# nbatches=N/batch_size
# if N%batch_size==0:
# nbatches-=1
# we subsample the data if needed
imCp=imIn[:]
if subsampling!=1.0:
imCp=imCp[0:int(subsampling*len(imCp))]
rniCp=rni[:]
Ntot=len(imCp)
training_size = int(validation*len(imCp))
val_size = Ntot-training_size
gen_train = generator(rootpath, imCp[:training_size], rniCp[:training_size])
# gen_val = generator(rootpath, imCp[training_size:], np.ones(len(imCp[training_size:]),dtype=np.float))
if valMode==rnHard:
LOW_DIM=rni.shape[1]
rniVal=rniCp[training_size:,:]
rnOut=np.ones(rniVal.shape)
for i in range(LOW_DIM):
nbOutTraining=(len([True for rn in rniVal[:,i] if rn < 0.5])) # We count the number of outliers in the training set
indexes=np.argsort(rniVal) # We get the indexes of the srted rn for the validation set
nbOutVal=int(float(nbOutTraining)/training_size*val_size)
for idx in range(nbOutVal):
rnOut[indexes[idx],i]=0
gen_val = generator(rootpath, imCp[training_size:], rnOut)
elif valMode==rnEqui:
gen_val = generator(rootpath, imCp[training_size:], np.ones(rniCp[training_size:].shape))
else: # rn from training mixture
gen_val = generator(rootpath, imCp[training_size:], rniCp[training_size:])
return (gen_train,training_size),(gen_val,val_size)
def load_data_generator_Uniform(rootpath, file_train, file_test,rni, valMode=rnEqui, validation=0.8,subsampling=1.0,outSize=(224,224),processingTarget=None,batch_size=BATCH_SIZE):
imFile = open(rootpath+file_train, 'r').readlines()
return load_data_generator_Uniform_List(rootpath, imFile[:], file_test,rni, valMode, validation,subsampling,outSize,processingTarget=processingTarget)
def load_data_generator_Uniform_List_Simple(rootpath, imIn, rni,outSize=(224,224),processingTarget=None,batch_size=BATCH_SIZE):
''' create generators from data for a mixure of gaussian + uniform'''
def generator(rootpath, images,rniIn, batch_size=BATCH_SIZE):
# im = get_sublist(im,rniIn)
c = zip(images,rniIn)
np.random.shuffle(c)
im = np.asarray([e[0] for e in c])
rni = np.asarray([e[1] for e in c])
N=len(im)
print "Size of the Selected Data: " + str(N)
nbatches=N/batch_size
if N%batch_size==0:
nbatches-=1
i=0
while 1:
X, Y = get_xy_from_file(rootpath, im[i*batch_size:(i+1)*batch_size],processingTarget=processingTarget,transform=None,outSize=outSize)
yield([X,rni[i*batch_size:(i+1)*batch_size]], Y*rni[i*batch_size:(i+1)*batch_size])
i=i+1
if i>=nbatches:
i=0
c = zip(images,rniIn)
np.random.shuffle(c)
im = np.asarray([e[0] for e in c])
rni = np.asarray([e[1] for e in c])
gen_train = generator(rootpath, imIn[:], rni[:])
return (gen_train,len(imIn))
def load_data_generator_Uniform_Simple(rootpath, file_train, file_test,rni, subsampling=1.0,outSize=(224,224),processingTarget=None,batch_size=BATCH_SIZE):
imFile = open(rootpath+file_train, 'r').readlines()
return load_data_generator_Uniform_List_Simple(rootpath, imFile[:], rni, subsampling,outSize,processingTarget=processingTarget)
def load_data_generator_noise(rootpath, file_train, validation=0.8,subsampling=1.0):
def generator(rootpath, images, batch_size=BATCH_SIZE):
N=len(images)
nbatches=N/batch_size
if N%batch_size==0:
nbatches-=1
i=0
while 1:
sol=[x.strip().split(" ") for x in images[i*batch_size:(i+1)*batch_size]]
yield([[x[0],map(lambda y:int(y),x[1:])] if len(x)>1 else [x[0],[]] for x in sol])
i=i+1
if i>=nbatches:
i=0
imFile = open(rootpath+file_train, 'r').readlines()
im=imFile[0:int(subsampling*len(imFile))]
Ntot=len(im)
training_size = int(validation*Ntot)
gen_train = generator(rootpath, im[:training_size])
gen_val = generator(rootpath, im[training_size:])
return gen_train, gen_val
def applyTransform(x,transform):
for t in transform:
x=t(x)
return x
def get_xy_from_file(rootpath, images, processingTarget=None,transform=[],outSize=(224,224),batch_size=BATCH_SIZE):
'''Extract data arrays from text file'''
X = np.zeros((len(images),3, outSize[0], outSize[1]), dtype=np.float32)
Y=[]
for i,image in enumerate(images):
currentline=image.strip().split(" ")
imFile=currentline[0]
X[i]=get_image_for_vgg(rootpath+imFile,transform,outSize)
Y.append(np.asarray(map(lambda x: float(x),currentline[1:])))
if processingTarget:
Y=processingTarget(Y)
Y=np.squeeze(np.asarray(Y)).reshape((X.shape[0],len(Y[0])))
return (X,Y)
def get_image_for_vgg(imName,transform=[],outSize=(224,224),batch_size=BATCH_SIZE):
'''Preprocess images as VGG inputs'''
im = (cv2.resize(cv2.imread(imName), (outSize[1],outSize[0]))).astype(np.float32)
if outSize==(224,224):
im[:,:,0] -= 103.939
im[:,:,1] -= 116.779
im[:,:,2] -= 123.68
im = im.transpose(2,0,1)
if transform:
im=applyTransform(im,transform)
im = np.expand_dims(im, axis=0)
return im