-
Notifications
You must be signed in to change notification settings - Fork 16
/
sedinet_eval.py
executable file
·387 lines (323 loc) · 12.8 KB
/
sedinet_eval.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
## Written by Daniel Buscombe,
## MARDA Science
##> Release v1.3 (July 2020)
###===================================================
# import libraries
from sedinet_models import *
###===================================================
def get_data_generator(df, indices, greyscale, batch_size=16):
"""
This function generates data for a batch of images and no metric, for # "unseen" samples
"""
for_training = False
images = []
while True:
for i in indices:
r = df.iloc[i]
file = r['files']
if greyscale==True:
im = Image.open(file).convert('LA')
else:
im = Image.open(file)
im = im.resize((IM_HEIGHT, IM_HEIGHT))
im = np.array(im) / 255.0
if greyscale==True:
images.append(np.expand_dims(im[:,:,0], axis=2))
else:
images.append(im)
if len(images) >= batch_size:
yield np.array(images)
images = []
if not for_training:
break
###===================================================
def get_data_generator_1vars(df, indices, for_training, vars, greyscale, batch_size=16):
"""
This function generates data for a batch of images and 1 associated metric
"""
images, p1s = [], []
while True:
for i in indices:
r = df.iloc[i]
file, p1 = r['files'], r[vars[0]]
if greyscale==True:
im = Image.open(file).convert('LA')
else:
im = Image.open(file)
im = im.resize((IM_HEIGHT, IM_HEIGHT))
im = np.array(im) / 255.0
if greyscale==True:
images.append(np.expand_dims(im[:,:,0], axis=2))
else:
images.append(im)
p1s.append(p1)
if len(images) >= batch_size:
yield np.array(images), [np.array(p1s)]
images, p1s = [], []
if not for_training:
break
###===================================================
def estimate_categorical(vars, csvfile, res_folder, dropout,
numclass, greyscale, name, mode, wp):
"""
This function uses a SediNet model for categorical prediction
"""
ID_MAP = dict(zip(np.arange(numclass), [str(k) for k in range(numclass)]))
##======================================
## this randomly selects imagery for training and testing imagery sets
## while also making sure that both training and tetsing sets have
## at least 3 examples of each category
test_idx, test_df = get_df(csvfile)
# for 16GB RAM, used maximum of 1000 samples to test on
# need to change batch gnerator into a better keras one
valid_gen = get_data_generator_1image(test_df, test_idx, True, ID_MAP,
vars[0], np.min((1000, len(test_idx))), greyscale, False)
if type(wp)==list:
weights_path = wp
else:
if SHALLOW is True:
if DO_AUG is True:
weights_path = name+"_"+mode+"_batch"+str(BATCH_SIZE)+"_im"+str(IM_HEIGHT)+\
"_"+str(IM_WIDTH)+"_shallow_"+vars[0]+"_"+CAT_LOSS+"_aug.hdf5"
else:
weights_path = name+"_"+mode+"_batch"+str(BATCH_SIZE)+"_im"+str(IM_HEIGHT)+\
"_"+str(IM_WIDTH)+"_shallow_"+vars[0]+"_"+CAT_LOSS+"_noaug.hdf5"
else:
if DO_AUG is True:
weights_path = name+"_"+mode+"_batch"+str(BATCH_SIZE)+"_im"+str(IM_HEIGHT)+\
"_"+str(IM_WIDTH)+"_"+vars[0]+"_"+CAT_LOSS+"_aug.hdf5"
else:
weights_path = name+"_"+mode+"_batch"+str(BATCH_SIZE)+"_im"+str(IM_HEIGHT)+\
"_"+str(IM_WIDTH)+"_"+vars[0]+"_"+CAT_LOSS+"_noaug.hdf5"
if not os.path.exists(weights_path):
weights_path = res_folder + os.sep+ weights_path
print("Using %s" % (weights_path))
if numclass>0:
ID_MAP = dict(zip(np.arange(numclass), [str(k) for k in range(numclass)]))
if type(BATCH_SIZE)==list:
SMs = [];
for batch_size, valid_batch_size, wp in zip(BATCH_SIZE, VALID_BATCH_SIZE, weights_path):
sm = make_cat_sedinet(ID_MAP, dropout, greyscale)
sm.load_weights(wp)
SMs.append(sm)
else:
SM = make_cat_sedinet(ID_MAP, dropout, greyscale)
SM.load_weights(weights_path)
if type(BATCH_SIZE)==list:
predict_test_train_cat(test_df, None, test_idx, None, vars[0],
SMs, [i for i in ID_MAP.keys()], weights_path, greyscale,
name, DO_AUG)
else:
predict_test_train_cat(test_df, None, test_idx, None, vars[0],
SM, [i for i in ID_MAP.keys()], weights_path, greyscale,
name, DO_AUG)
K.clear_session()
##===================================
## move model files and plots to the results folder
tidy(name, res_folder)
###===================================================
def estimate_categorical_1image(vars, image, res_folder, dropout,
numclass, greyscale, name, mode, wp):
"""
This function uses a SediNet model for categorical prediction on 1 image
"""
ID_MAP = dict(zip(np.arange(numclass), [str(k) for k in range(numclass)]))
if type(BATCH_SIZE)==list:
SM = [];
for batch_size, valid_batch_size, w in zip(BATCH_SIZE, VALID_BATCH_SIZE, wp):
sm = make_cat_sedinet(ID_MAP, dropout, greyscale)
sm.load_weights(w)
SM.append(sm)
else:
SM = make_cat_sedinet(ID_MAP, dropout, greyscale)
SM.load_weights(wp)
if greyscale==True:
im = Image.open(image).convert('LA')
im = im.resize((IM_HEIGHT, IM_HEIGHT))
im = np.array(im)[:,:,0]
else:
im = Image.open(image)
im = im.resize((IM_HEIGHT, IM_HEIGHT))
im = np.array(im)
im = np.array(im) / 255.0
# if greyscale==True:
# result = SM.predict(np.expand_dims(np.expand_dims(im, axis=2), axis=0))
# else:
# result = SM.predict(np.expand_dims(im, axis=0))
if greyscale==True:
if type(SM) == list:
R=[]
for s in SM:
R.append(s.predict(np.expand_dims(np.expand_dims(im, axis=2), axis=0)))
result = np.median(np.hstack(R), axis=1)
del R
else:
result = SM.predict(np.expand_dims(np.expand_dims(im, axis=2), axis=0))
else:
# result = SM.predict(np.expand_dims(im, axis=0))
if type(SM) == list:
R=[]
for s in SM:
R.append(s.predict(np.expand_dims(im, axis=0)))
result = np.median(np.hstack(R), axis=1)
del R
else:
result = SM.predict(np.expand_dims(im, axis=0))
return np.argmax(result)
##sorted([np.round(r[0]) for r in result])
###===================================================
def estimate_siso_simo_1image(vars, image, greyscale,
dropout, numclass, scale, name, mode, res_folder, batch_size, weights_path):
"""
This function uses a sedinet model for continuous prediction on 1 image
"""
if type(BATCH_SIZE)==list:
SM = [];
for batch_size, valid_batch_size, wp in zip(BATCH_SIZE, VALID_BATCH_SIZE, weights_path):
sm = make_sedinet_siso_simo(vars, greyscale, dropout)
sm.load_weights(wp)
SM.append(sm)
else:
SM = make_sedinet_siso_simo(vars, greyscale, dropout)
if scale==True:
CS = joblib.load(weights_path.replace('.hdf5','_scaler.pkl'))
else:
CS = []
if type(SM) == list:
try:
counter = 0
for s,wp in zip(SM, weights_path):
exec('s.load_weights(os.getcwd()+os.sep+wp)')
counter += 1
except:
counter = 0
for s,wp in zip(SM, weights_path):
exec('s.load_weights(wp)')
counter += 1
else:
try:
SM.load_weights(os.getcwd()+os.sep+weights_path)
except:
SM.load_weights(weights_path)
if greyscale==True:
im = Image.open(image).convert('LA')
im = im.resize((IM_HEIGHT, IM_HEIGHT))
im = np.array(im)[:,:,0]
else:
im = Image.open(file)
im = im.resize((IM_HEIGHT, IM_HEIGHT))
im = np.array(im)
im = np.array(im) / 255.0
if greyscale==True:
if type(SM) == list:
R=[]
for s in SM:
R.append(s.predict(np.expand_dims(np.expand_dims(im, axis=2), axis=0)))
result = np.median(np.hstack(R), axis=1)
del R
else:
result = SM.predict(np.expand_dims(np.expand_dims(im, axis=2), axis=0))
else:
# result = SM.predict(np.expand_dims(im, axis=0))
if type(SM) == list:
R=[]
for s in SM:
R.append(s.predict(np.expand_dims(im, axis=0)))
result = np.median(np.hstack(R), axis=1)
del R
else:
result = SM.predict(np.expand_dims(im, axis=0))
result = [float(r[0]) for r in result]
if scale==True:
result_scaled = []
for r, cs in zip(result, CS):
result_scaled.append(float(cs.inverse_transform(np.array(r).reshape(1,-1))[0]))
del result
result = result_scaled
if type(SM) == list:
# files = glob(os.path.dirname(weights_path[0])+os.sep+'*.pkl') #.replace('.hdf5','_bias.pkl')
# file = [f for f in files if '_'.join(np.asarray(BATCH_SIZE, dtype='str')) in f][0]
# Z = joblib.load(file)
# Z = []
# for wp in weights_path:
# Z.append(joblib.load(wp.replace('.hdf5','_bias.pkl')))
pass
else:
Z = joblib.load(weights_path.replace('.hdf5','_bias.pkl'))
result_scaled = []
for z,r in zip(Z, result):
result_scaled.append(np.abs(np.polyval(z,r)))
del result
result = result_scaled
return result
###===================================================
def estimate_siso_simo(vars, csvfile, greyscale,
dropout, numclass, scale, name, mode, res_folder, batch_size, weights_path):
"""
This function uses a sedinet model for continuous prediction
"""
# if not os.path.exists(weights_path):
# weights_path = res_folder + os.sep+ weights_path
# print("Using %s" % (weights_path))
##======================================
## this randomly selects imagery for training and testing imagery sets
## while also making sure that both training and tetsing sets have
## at least 3 examples of each category
test_idx, test_df = get_df(csvfile)
##==============================================
## create a sedinet model to estimate category
# if type(BATCH_SIZE)==list:
# SMs = []
# for k in BATCH_SIZE:
# SMs.append(make_sedinet_siso_simo(vars, greyscale, dropout))
#
# else:
SM = make_sedinet_siso_simo(vars, greyscale, dropout)
if scale==True:
CS = []
for var in vars:
cs = RobustScaler() #MinMaxScaler()
cs.fit_transform(
np.r_[test_df[var].values].reshape(-1,1)
)
CS.append(cs)
del cs
else:
CS = []
# test model
# if numclass==0:
if type(BATCH_SIZE)==list:
predict_test_train_siso_simo(test_df, None, test_idx, None, vars,
SM, weights_path, name, mode, greyscale, CS,
dropout, scale, DO_AUG)
else:
predict_test_train_siso_simo(test_df, None, test_idx, None, vars,
SM, weights_path, name, mode, greyscale, CS,
dropout, scale, DO_AUG)
K.clear_session()
##===================================
## move model files and plots to the results folder
tidy(name, res_folder)
# if type(wp)==list:
# weights_path = wp
# else:
# if SHALLOW is True:
# if DO_AUG is True:
# weights_path = name+"_"+mode+"_batch"+str(BATCH_SIZE)+"_im"+str(IM_HEIGHT)+\
# "_"+str(IM_WIDTH)+"_shallow_"+vars[0]+"_"+CAT_LOSS+"_aug.hdf5"
# else:
# weights_path = name+"_"+mode+"_batch"+str(BATCH_SIZE)+"_im"+str(IM_HEIGHT)+\
# "_"+str(IM_WIDTH)+"_shallow_"+vars[0]+"_"+CAT_LOSS+"_noaug.hdf5"
# else:
# if DO_AUG is True:
# weights_path = name+"_"+mode+"_batch"+str(BATCH_SIZE)+"_im"+str(IM_HEIGHT)+\
# "_"+str(IM_WIDTH)+"_"+vars[0]+"_"+CAT_LOSS+"_aug.hdf5"
# else:
# weights_path = name+"_"+mode+"_batch"+str(BATCH_SIZE)+"_im"+str(IM_HEIGHT)+\
# "_"+str(IM_WIDTH)+"_"+vars[0]+"_"+CAT_LOSS+"_noaug.hdf5"
#
#
# if not os.path.exists(weights_path):
# weights_path = res_folder + os.sep+ weights_path
# print("Using %s" % (weights_path))