使用pete warden的教程,我训练了Inception网络,并得到了两个文件
1. retrained_graph.pb
2. retrained_label.txt
我希望使用这些文件来对花朵图像进行分类。我已经安装了PyCharm并链接了所有TensorFlow库,我也测试了TensorFlow的示例代码,运行正常。
现在当我运行label_image.py程序时,程序内容如下:
import tensorflow as tf, 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("/tf_files/retrained_labels.txt")]
# 从文件中读取图形
with tf.gfile.FastGFile("/tf_files/retrained_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:
# 将图像数据作为输入输入图形,并获取第一个预测
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data})
# 排序以按置信度顺序显示第一个预测的标签
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))
我得到了以下错误信息
/home/chandan/Tensorflow/bin/python /home/chandan/PycharmProjects/tf/tf_folder/tf_files/label_image.py
Traceback (most recent call last):
File "/home/chandan/PycharmProjects/tf/tf_folder/tf_files/label_image.py", line 7, in <module>
image_path = sys.argv[1]
IndexError: list index out of range
请问有人可以帮助我解决这个问题吗?
回答:
您得到这个错误是因为程序期待一个图像文件名(包括路径)作为参数。
在PyCharm中,点击“查看”->“工具窗口”->“终端”。
这相当于打开一个独立的终端。然后运行
python label_image.py /image_path/image_name.jpg