Skip to content

Instantly share code, notes, and snippets.

@edwardleoni
Last active March 24, 2018 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardleoni/2c0fb0ca9d4a180025c243f1d11b3fa6 to your computer and use it in GitHub Desktop.
Save edwardleoni/2c0fb0ca9d4a180025c243f1d11b3fa6 to your computer and use it in GitHub Desktop.
import tensorflow as tf
import sys
image_path = sys.argv[1]
image_data = tf.gfile.FastGFile(image_path, 'rb').read()
label_lines = [line.rstrip() for line in tf.gfile.GFile('labels.txt')]
with tf.gfile.FastGFile('graph.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
with tf.Session() as session:
softmax_tensor = session.graph.get_tensor_by_name('final_result:0')
predictions = session.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data})
predictions_by_confidence = predictions[0].argsort()[-len(predictions[0]):][::-1]
for node_id in predictions_by_confidence:
human_readable = label_lines[node_id]
score = predictions[0][node_id]
print('%s (score = %5f)' % (human_readable, score))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment