我使用下面的代码对文件夹中的多个图像进行分类,并面对下面提到的内容。这是诗人的教程张量流。
代码:
import tensorflow as tf
from PIL import Image
# change this as you see fit
image_dir = "/home/shri/Desktop/tf_files/test"
# Read in the image_data
image_data = tf.gfile.FastGFile(image_dir, 'rb').read()
image = Image.open(image_dir)
image_array = image.convert('RGB')
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.gfile.GFile("/root/tf_files/output_labels.txt")]
# Unpersists graph from file
with tf.gfile.FastGFile("/root/tf_files/output_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 sess:
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor, {'DecodeJpeg:0': image_array})
# 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))
和错误:
Traceback (most recent call last):
File "label_image3.py", line 8, in <module>
image_data = tf.gfile.FastGFile(image_dir, 'rb').read()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/lib/io/file_io.py", line 125, in read
pywrap_tensorflow.ReadFromStream(self._read_buf, length, status))
File "/usr/lib/python2.7/contextlib.py", line 24, in __exit__
self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: /home/shri/Desktop/tf_files/test
有人可以告诉我代码中是否有问题以及我该如何修复它。我也想把它放在excel表中。
此致 至尊
答案 0 :(得分:0)
您似乎正在尝试打开目录,提供目录路径,然后尝试使用FastGFile将其作为文件打开
# change this as you see fit
image_dir = "/home/shri/Desktop/tf_files/test"
# Read in the image_data
image_data = tf.gfile.FastGFile(image_dir, 'rb').read()