/
utils.py
executable file
·368 lines (314 loc) · 15.2 KB
/
utils.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
import sys
from operator import itemgetter
from keras_frcnn.pascal_voc_parser import get_data
import os
sys.path.append(os.getcwd())
import glob
import errno
import ntpath
from shutil import copyfile,move
import xml.etree.ElementTree as ET
import numpy as np
import pandas as pd
import operator
from operator import itemgetter
import random
import pickle
from numba import jit
from keras import backend as K
from keras.backend.tensorflow_backend import set_session
from keras.backend.tensorflow_backend import clear_session
from keras.backend.tensorflow_backend import get_session
import tensorflow
# Reset Keras Session
def reset_keras():
sess = get_session()
clear_session()
sess.close()
sess = get_session()
try:
del classifier # this is from global space - change this as you need
except:
pass
#print(gc.collect()) # if it's done something you should see a number being outputted
###################################
# TensorFlow wizardry
config = tensorflow.ConfigProto()
# Don't pre-allocate memory; allocate as-needed
config.gpu_options.allow_growth = True
# Only allow a total of half the GPU memory to be allocated
#config.gpu_options.per_process_gpu_memory_fraction = 0.5
# Create a session with the above options specified.
K.tensorflow_backend.set_session(tensorflow.Session(config=config))
print("available gpu divice: {}".format(tensorflow.test.gpu_device_name()))
# use the same config as you used to create the session
config = tensorflow.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 1
config.gpu_options.visible_device_list = "0"
set_session(tensorflow.Session(config=config))
# clear gpu
def clear_keras():
sess = get_session()
clear_session()
sess.close()
# config = tensorflow.ConfigProto(
# device_count = {'GPU': -1}
# )
# #sess = tensorflow.Session(config=config)
# K.tensorflow_backend.set_session(tensorflow.Session(config=config))
def entropy_sampling(prediction):
""" Diese Funktion rechnet die Entropie einer Prediction
für jedes Bilde wird einen list von Vorgesagtete class und ihre entsprechende Wahrscheinlichkeit
[(class,prbability),....]
"""
argmax =0
summe = 0
for elt in prediction:
pred= elt[1]/100
if argmax<pred:
argmax=pred
summe = summe + np.log10(pred)*pred
entropy = argmax-summe
return entropy
def least_confident(prediction):
""" Diese Funktion rechnet die Leas_confidence einer Prediction
für jedes Bilde wird einen list von Vorgesagtete class und ihre entsprechende Wahrscheinlichkeit
[(class,prbability),....]
"""
argmax = 0
for elt in prediction:
pred= elt[1]/100
if argmax<pred:
argmax=pred
lc = 1-argmax
return lc
def margin_sampling (prediction):
""" Diese Funktion rechnet die margin einer Prediction
für jedes Bilde wird einen list von Vorgesagtete class und ihre entsprechende Wahrscheinlichkeit
[(class,prbability),....]
"""
prediction=sorted(prediction,key=itemgetter(1),reverse=True)
argmax = 0
mg=0
mg_list = prediction[:2]
for prob in mg_list:
mg=prob[1]/100-mg
return abs(mg)
def berechnung_unsischerheit(prediction, methode):
print("-----> berechnung_unsischerheit")
unsischerheit = 0
if (methode=="entropie"):
unsischerheit = entropy_sampling(prediction)
elif (methode=="margin"):
unsischerheit = margin_sampling(prediction)
elif (methode=="least_confident"):
unsischerheit = least_confident(prediction)
else:
print("kein gültiges Uncertainty sampling")
unsischerheit=None
return unsischerheit
def sort_list_sampling(list,sampling_methode):
print("sortierung nach unsischerheit methode")
if (sampling_methode=="entropie"):
list = sorted(list,key=itemgetter(1),reverse=True)
elif (sampling_methode=="margin"):
list = sorted(list,key=itemgetter(1))
elif (sampling_methode=="least_confident"):
list = sorted(list,key=itemgetter(1),reverse=True)
else:
print("kein gültiges Uncertainty sampling")
return list
def create_mapping_cls(list_traimingsmenge):
""" Diese Funktion wird mapping für trainingsmenge erstellen"""
cls_count={}
cls_mapping={}
for val in list_traimingsmenge:
for bb in val['bboxes']:
if bb['class'] not in cls_count:
cls_count[bb['class']] = 1
else:
cls_count[bb['class']] += 1
if bb['class'] not in cls_mapping:
cls_mapping[bb['class']] = len(cls_mapping)
return cls_count,cls_mapping
def create_batchify_from_list(listdata,percentTotrain):
sizetotrain = int(round((len(listdata)* percentTotrain)/100))
number_of_batch = int(len(listdata)/sizetotrain)
return [listdata[i::number_of_batch] for i in range(number_of_batch)]
def create_batch_from_path (pathToDataSet,percentTotrain):
all_imgs, classes_count, class_mapping = get_data(pathToDataSet)
sizetotrain = int(round((len(all_imgs)* percentTotrain)/100))
number_of_batch = int(len(all_imgs)/sizetotrain)
return [all_imgs[i::number_of_batch] for i in range(number_of_batch)]
def createSeed_pro_batch(batch_elt,train_size_pro_batch):
""" Diese Funktion wird labellierte Data auswählen."""
print("##### Erstellung von Datenmenge Seed und unlabellierte ####")
seed_imgs=[]
all_imgs =[]
classes_count ={}
class_mapping ={}
#all_imgs, classes_count, class_mapping = get_data(pathToDataSet)
sizetotrain = int(round((len(batch_elt)* train_size_pro_batch)/100))
seed_imgs = batch_elt[:sizetotrain]
all_imgs = batch_elt[sizetotrain:]
print("Erstellung anotation, class_maping und class_count vom unlabellierte Daten")
for im in batch_elt:
for bb in im['bboxes']:
if bb['class'] not in classes_count:
classes_count[bb['class']] = 1
else:
classes_count[bb['class']] += 1
if bb['class'] not in class_mapping:
class_mapping[bb['class']] = len(class_mapping)
return all_imgs, seed_imgs, class_mapping,classes_count
@jit
def createSeedPascal_Voc(pathToDataSet,batch_size):
""" Diese Funktion wird labellierte Data auswählen."""
print("##### Erstellung von Datenmenge Seed und unlabellierte ####")
seed_imgs=[]
all_imgs =[]
seed_classes_count={}
seed_classes_mapping={}
classes_count ={}
class_mapping ={}
all_imgs, classes_count, class_mapping = get_data(pathToDataSet)
print("len all_image after get_data in {}".format(len(all_imgs)))
sizetotrain = int(round((len(all_imgs)* batch_size)/100))
seed_imgs = all_imgs[:sizetotrain]
all_imgs = all_imgs[sizetotrain:]
print("len all_image after get train_menge {}".format(len(all_imgs)))
print("Erstellung anotation, class_maping und class_count vom Seed")
for seed in seed_imgs:
for bb in seed['bboxes']:
if bb['class'] not in seed_classes_count:
seed_classes_count[bb['class']] = 1
else:
seed_classes_count[bb['class']] += 1
if bb['class'] not in seed_classes_mapping:
seed_classes_mapping[bb['class']] = len(seed_classes_mapping)
return all_imgs, seed_imgs,seed_classes_mapping,seed_classes_count
def Datei_vorbereitung (seed,Dateitype):
""" Erstellung von Matching file .txt zum training oder zum Testen
"""
xml_list = []
for xmlFile in seed:
tree = ET.parse(xmlFile[1])
root = tree.getroot()
for member in root.findall('object'):
value = (root.find('path').text,
int(member[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text),
member[0].text
)
xml_list.append(value)
column_name = ['path', 'xmin', 'ymin', 'xmax', 'ymax','class']
xml_df = pd.DataFrame(xml_list,columns=column_name)
xml_df.to_csv((basePath +'/'+ Dateitype + '_labels.txt'), index=None, header=False)
print('Successfully converted xml to txt.')
def Pool_based_sampling_test (predict_list, pool_size ,sampling_methode):
"""predict_list as list [(file_path,prediction)]
pool_size integer
sampling methode: entropie, marging or least confident
Compute uncertainty for each prdiction
return Pool to Query to Oracle
"""
# computer
Predict_uncertainty_list = []
Predict_uncertainty_listsorted =[]
for elt in predict_list:
uncertainty = berechnung_unsischerheit(elt[1],sampling_methode)
print("uncertainty of {} is {}".format(ntpath.basename(elt[0]),uncertainty))
Predict_uncertainty_list.append((elt[0], elt[1],uncertainty))
Predict_uncertainty_listsorted = sort_list_sampling(Predict_uncertainty_list,sampling_methode)
if pool_size < len(Predict_uncertainty_listsorted):
pool = Predict_uncertainty_listsorted[:pool_size]
else:
pool = Predict_uncertainty_listsorted
return pool
def appendDFToCSV_void(dictPerformance, csvFilePath):
df = pd.DataFrame(dictPerformance, index=[0])
if not os.path.isfile(csvFilePath):
df.to_csv(csvFilePath, mode='a', index=False, sep=";")
elif len(df.columns) != len(pd.read_csv(csvFilePath, nrows=1, sep=";").columns):
raise Exception("Columns do not match!! Dataframe has " + str(len(df.columns)) + " columns. CSV file has " + str(len(pd.read_csv(csvFilePath, nrows=1, sep=";").columns)) + " columns.")
elif not (df.columns == pd.read_csv(csvFilePath, nrows=1, sep=";").columns).all():
raise Exception("Columns and column order of dataframe and csv file do not match!!")
else:
df.to_csv(csvFilePath, mode='a', index=False, sep=";", header=False)
def writePerformanceModell(ModellParmeter, pathtofile):
Perform = pd.DataFrame(ModellParmeter, index=[0])
Perform.to_csv(pathtofile, sep=';', mode='a', header=False, index=False)
def check_predict(list_pred):
all_bg = False
list_found_bg=0
list_not_bg =[]
for el in list_pred:
if el[0] == 'bg':
list_found_bg+=1
else:
list_not_bg.append(el)
if len(list_pred)== list_found_bg:
all_bg = True
return all_bg,list_not_bg,list_found_bg
def update_config_file(pathtofile,con):
print("update config file")
with open(pathtofile, 'wb') as config_f:
pickle.dump(con, config_f)
return con
def count_class_in_dict_oracle (classname, dict_oracle, list_prediction, match_list):
count_in_dict_orcal =0
count_in_prediction_list =0
truePositiv = 0
trueNegativ = 0
found = False
for c in match_list:
if c == classname:
found = True
break
if found == False:
match_list.append(classname)
for cl in dict_oracle['bboxes']:
if cl['class']== classname:
count_in_dict_orcal +=1
for val in list_prediction:
if classname == val[0]:
count_in_prediction_list +=1
if count_in_prediction_list != 0 :
check = count_in_prediction_list - count_in_dict_orcal
if check == 0:
truePositiv = count_in_dict_orcal
trueNegativ = 0
else:
trueNegativ = abs(check)
if count_in_prediction_list < count_in_dict_orcal:
truePositiv = count_in_prediction_list
else:
truePositiv = count_in_dict_orcal
return truePositiv,trueNegativ,match_list
if __name__ == "__main__":
prediction = [('tvmonitor', 100.0), ('tvmonitor', 100.0), ('person', 100.0), ('person', 100.0), ('tvmonitor', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0), ('bg', 100.0)]
oracle ={'filepath': '/mnt/0CCCB718CCB6FB52/dataset/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/JPEGImages/2010_005372.jpg', 'width': 375, 'height': 500, 'bboxes': [{'class': 'chair', 'x1': 66, 'x2': 225, 'y1': 311, 'y2': 500, 'difficult': False}, {'class': 'tvmonitor', 'x1': 157, 'x2': 242, 'y1': 236, 'y2': 318, 'difficult': False}], 'imageset': 'trainval'}
all_bg,list_not_bg,list_found_bg = check_predict(prediction)
print(all_bg)
print(list_not_bg)
truePositiv_all =0
trueNegativ_all = 0
match_list =[]
for val in list_not_bg:
for cl in oracle['bboxes']:
print("oracle: die Liste Von Objekt auf dem Bild: {} ".format(cl['class']))
truePositiv,trueNegativ,match_list= count_class_in_dict_oracle(val[0],oracle,list_not_bg,match_list)
truePositiv_all = truePositiv_all+truePositiv
trueNegativ_all = trueNegativ_all + trueNegativ
print("{} : {} : {} ".format(truePositiv,trueNegativ,match_list))
""" if cl['class']== val[0]:
print("gut predicted!")
print("das Model predict : {} mit {} % Sicherheit".format(val[0],val[1]))
truePositiv +=1
else:
print("falsch Predict")
trueNegativ += 1
print("das Model predict : {} mit {} % Sicherheit".format(val[0],val[1])) """
print("true positive:{} true negativ: {} bd {}/{} ".format(truePositiv_all, trueNegativ_all, list_found_bg,len(prediction)))