Last active
March 24, 2018 05:14
-
-
Save edwardleoni/2c0fb0ca9d4a180025c243f1d11b3fa6 to your computer and use it in GitHub Desktop.
As seen here: https://mlis.fun/leveraging-transfer-learning-to-build-a-tensor-flow-image-classifier-in-less-than-10-minutes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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