-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathget_predictions.py
More file actions
427 lines (332 loc) · 12.7 KB
/
get_predictions.py
File metadata and controls
427 lines (332 loc) · 12.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# Copyright 2017 CrowdFlower, Inc.
#
# The Code below is adapted from Tensorflow code that is open source and available under Apache License at:
# https://github.com/tensorflow
# The TensorFlow Authors. All Rights Reserved.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""
Code to get predictions across a number of labels and order those labels for active learning
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
import os
import re
import operator
import glob
import random
import tensorflow as tf
parser = argparse.ArgumentParser()
parser.add_argument(
'--directory',
type=str,
default='test_data',
help='Evaluation data.')
parser.add_argument(
'--num_top_predictions',
type=int,
default=5,
help='Display this many predictions.')
parser.add_argument(
'--output_layer',
type=str,
default='final_result:0',
help='Name of the result operation')
parser.add_argument(
'--input_layer',
type=str,
default='DecodeJpeg/contents:0',
help='Name of the input operation')
parser.add_argument(
'--graph',
default='model_files/output_graph.pb',
type=str,
help='Absolute path to graph file (.pb)')
parser.add_argument(
'--labels',
default = 'model_files/output_labels.txt',
type=str,
help='Absolute path to labels file (.txt)')
def load_image(filename):
"""Read in the image_data to be classified."""
return tf.gfile.FastGFile(filename, 'rb').read()
def load_labels(filename):
"""Read in labels, one label per line."""
return [line.rstrip() for line in tf.gfile.GFile(filename)]
def load_graph(filename):
"""Unpersists graph from file as default graph."""
with tf.gfile.FastGFile(filename, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
def run_graph(sess, image_data, labels, input_layer_name, output_layer_name,
num_top_predictions):
# Feed the image_data as input to the graph.
# predictions will contain a two-dimensional array, where one
# dimension represents the input image count, and the other has
# predictions per class
softmax_tensor = sess.graph.get_tensor_by_name(output_layer_name)
predictions, = sess.run(softmax_tensor, {input_layer_name: image_data})
# Sort to show labels in order of confidence
prediction_index = num_top_predictions
top_k = predictions.argsort()[-num_top_predictions:][::-1]
label_predictions = {}
for node_id in top_k:
human_string = labels[node_id]
score = predictions[node_id]
#print('%s (score = %.5f)' % (human_string, score))
label_predictions[human_string] = score
return label_predictions
def get_image_prediction(sess, image, labels):
"""Get a prediction for a given image with the given labels."""
if not (image.endswith('.jpg') or image.endswith('.jpeg')):
return []
if not tf.gfile.Exists(image):
tf.logging.fatal('image file does not exist %s', image)
image_data = load_image(image)
predictions = run_graph(sess, image_data, labels, FLAGS.input_layer, FLAGS.output_layer,
FLAGS.num_top_predictions)
return predictions
def cp_file(path, directory):
"""Copies the given file to the new directory"""
if not os.path.exists(directory):
os.makedirs(directory)
label = re.sub(FLAGS.directory+"\/","",path)
label = re.sub("[^\/]*$","",label)
sub_directory = directory+"/"+label
filen = re.sub(".*\/","",path)
if not os.path.exists(sub_directory):
os.makedirs(sub_directory)
target = sub_directory+filen
if os.path.isfile(target):
return False
else:
os.system("cp "+path+" "+target)
return True
def main(argv):
"""Runs inference on images."""
if argv[1:]:
raise ValueError('Unused Command Line Args: %s' % argv[1:])
if not tf.gfile.Exists(FLAGS.labels):
tf.logging.fatal('labels file does not exist %s', FLAGS.labels)
if not tf.gfile.Exists(FLAGS.graph):
tf.logging.fatal('graph file does not exist %s', FLAGS.graph)
# load labels
labels = load_labels(FLAGS.labels)
# load graph, which is stored in the default session
load_graph(FLAGS.graph)
#look in directory
label_dirs = os.listdir(FLAGS.directory)
label_dirs_path = [FLAGS.directory+"/" + x for x in label_dirs]
microfscores = 0.0 # sum of f scores * counts
macrofscores = 0.0 # sum of f scores
total_images = 0
total_labels = 0
all_predictions = [] # record of all predictions, to use for active learning
# With current tensorflow session, get predictions
with tf.Session() as sess, tf.Graph().as_default():
# for each subdirectory, corresponding to one label
# NB: could remove this inner loop for active learning on raw data where the labels are not known
for label_dir in label_dirs_path:
total_labels += 1
label = re.sub("^.*\/", '', label_dir)
label = re.sub("[^a-z0-9]+", ' ', label.lower()) # get label name from dir, removing forbidden characters
files = os.listdir(label_dir)
images = [label_dir+"/" + x for x in files]
count = 0 # number of images seen overall
tp = 0 # number of true positives overall
fn = 0 # number of false negatives overall
fp = 0 # number of false positives overall
for image in images:
# if random.random() > 0.001: # speed up test by reducing to just 3%
# continue
image = image.rstrip()
# Get all predictions for this image!!
predictions = get_image_prediction(sess, image, labels)
# Get the two most confidently predicted labels
top_prediction = ""
top_confidence = 0.0
second_prediction = ""
second_confidence = 0.0
for prediction in predictions:
confidence = predictions[prediction]
if confidence > top_confidence:
if top_confidence > second_confidence:
second_prediction = top_prediction
second_confidence = top_confidence
top_prediction = prediction
top_confidence = confidence
elif confidence > second_confidence:
second_prediction = prediction
second_confidence = confidence
if top_confidence == 0.0 or second_confidence == 0.0:
continue # something went wrong, most likely a corrupted file
# get the ratio of top confidence to second most confident
ratio = top_confidence / second_confidence
rand = random.random()
difference = top_confidence - second_confidence
# WHAT WE WANT TO CAPTURE ABOUT EACH IMAGE TO USE FOR ACTIVE LEARNING
image_info = [image, top_prediction, top_confidence, second_prediction, second_confidence, ratio, rand, difference]
# print(image_info)
all_predictions.append(image_info)
# update accuracy metrics
if top_prediction == label :
tp+=1
else:
fn+=1
fp+=1
count += 1
total_images +=1
running_accuracy = tp / count
# for this label, report accuracy
if tp == 0:
fscore = 0.0
else:
precision = tp / (tp + fp)
recall = tp / ( tp + fn )
fscore = (2* precision * recall) / (precision + recall)
# print(label+ " f-score:")
# print(fscore)
microfscores += fscore * count
macrofscores += fscore
# report overal accuracies
microf = microfscores / total_images
macrof = macrofscores / total_labels
print("Micro-f: ")
print(microf)
print("Macro-f: ")
print(macrof)
if FLAGS.directory == "test_data":
exit()
# IMPLEMENT STRATEGY FOR ACTIVE LEARNING
print("CONFIDENCE")
# 1. ORDER BY THE LEAST CONFIDENT TO MOST CONFIDENT
all_predictions.sort(key=lambda x: x[2], reverse=False)
c = 0
for image_info in all_predictions:
print(image_info[0])
if c < 2000:
if cp_file(image_info[0], "training_data_confidence"):
c+=1
print("RATIOS")
# 2. ORDER BY THE CLOSEST RATIOS
all_predictions.sort(key=lambda x: x[5], reverse=False)
c = 0
for image_info in all_predictions:
print(image_info[0])
if c < 2000:
if cp_file(image_info[0], "training_data_ratios"):
c+=1
# 3. STRATIFY BY LABEL, ENSURING EQUAL DISTRIBUTION ACROSS PREDICTED LABELS
# Could be used in combination with 1. or 2.
print("STRATIFIED")
all_predictions.sort(key=lambda x: x[6], reverse=False) #randomize
ordered_labels = {} # dict of list for each label.
for image_info in all_predictions:
top_prediction = image_info[1]
if not top_prediction in ordered_labels:
ordered_labels[top_prediction] = []
ordered_labels[top_prediction].append(image_info[0]) # add url to list for that label
print(ordered_labels)
# exit()
# interleave the per-label lists
keep_going = True # to track whether there are any remaining to be ordered
c = 0
while keep_going:
keep_going = False
for label in ordered_labels:
images = ordered_labels[label]
print("There are "+str(len(images))+" images in "+label)
if len(images) > 0:
image = images.pop(0)
keep_going = True
print(image)
if c < 2000:
if cp_file(image, "training_data_stratified"):
c+=1
print("PAIRS")
# 4. STRATIFY BY LABELS, ENSURING EQUAL DISTRIBUTION ACROSS PAIR OF PREDICTED LABELS
# Could be used in combination with 1. or 2.
all_predictions.sort(key=lambda x: x[5], reverse=False)
ordered_label_pairs = {} # dict of lists for each pair of labels label.
for image_info in all_predictions:
top_prediction_pair = image_info[1]+" "+image_info[3]
if not top_prediction_pair in ordered_label_pairs:
ordered_label_pairs[top_prediction_pair] = []
ordered_label_pairs[top_prediction_pair].append(image_info[0]) # add url to list for that label pair
# interleave the per-label-pair lists
keep_going = True # to track whether there are any remaining to be ordered
c = 0
while keep_going:
keep_going = False
for label in ordered_label_pairs:
images = ordered_label_pairs[label]
if len(images) > 0:
image = images.pop(0)
keep_going = True
print(image)
if c < 2000:
if cp_file(image, "training_data_pairs"):
c += 1
print("DIFFERENCE")
# 5. ORDER BY THE DIFFERENCE BETWEEN MOST AND LEAST CONFIDENCE
all_predictions.sort(key=lambda x: x[7], reverse=False)
c = 0
for image_info in all_predictions:
print(image_info[0])
if c < 2000:
cp_file(image_info[0], "training_data_difference")
c += 1
print("RANDOM")
# 6. RANDOM ORDER FOR A BASELINE
all_predictions.sort(key=lambda x: x[7], reverse=False)
c = 0
for image_info in all_predictions:
print(image_info[0])
if c < 2000:
if cp_file(image_info[0], "training_data_random"):
c += 1
print("PATHOLOGICAL")
# 7. ORDER BY THE MOST CONFIDENT TO LEAST CONFIDENT
all_predictions.sort(key=lambda x: x[2], reverse=TRUE)
c = 0
for image_info in all_predictions:
print(image_info[0])
if c < 2000:
if cp_file(image_info[0], "training_data_pathological"):
c+=1
print("PATHSTRAT")
# 8. ORDER BY THE MOST CONFIDENT TO LEAST CONFIDENT, STRATIFIED
all_predictions.sort(key=lambda x: x[2], reverse=TRUE)
ordered_labels = {} # dict of list for each label.
for image_info in all_predictions:
top_prediction = image_info[1]
if not top_prediction in ordered_labels:
ordered_labels[top_prediction] = []
ordered_labels[top_prediction].append(image_info[0]) # add url to list for that label
# interleave the per-label lists
keep_going = True # to track whether there are any remaining to be ordered
c = 0
while keep_going:
keep_going = False
for label in ordered_labels:
images = ordered_labels[label]
print("There are "+str(len(images))+" images in "+label)
if len(images) > 0:
image = images.pop(0)
keep_going = True
print(image)
if c < 2000:
if cp_file(image, "training_data_path_strat"):
c+=1
if __name__ == '__main__':
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=sys.argv[:1]+unparsed)