pythonnumpynp.argsort

Remove argsort function in "classify" for images?


I'm trying to remove the argsort function, so that it will just print in order of prediction classes (I.e. if I am trying to predict "a-d" classifications, it will always just print in "a-d" rather than highest to lowest score). Any advice?

This is from: https://github.com/llSourcell/tensorflow_image_classifier/blob/master/src/label_image.py

'''with tf.Session() as sess: # Feed the image_data as input to the graph and get first prediction i.e. the most likely result softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')

predictions = sess.run(softmax_tensor, \
         {'DecodeJpeg/contents:0': image_data})

# Sort to show labels of first prediction in order of confidence
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]

for node_id in top_k:
    human_string = label_lines[node_id]
    score = predictions[0][node_id]
    print('%s (score = %.5f)' % (human_string, score))'''

Solution

  • Do you want to go through the same top_k values but in their original order, not sorted according to predictions? Then do for node_id in sort(top_k):.