如何使用pycharm编辑器加载retrated_graph.pb和retrained_label.txt



使用Pete Warden教程,我已经培训了我收到两个文件的成立网络和培训


1. retrained_graph.pb
2. retrained_label.txt

使用它,我想对花图像进行分类。我已经安装了Pycharm并链接了所有TensorFlow库,我还测试了示例张量Flow代码。

现在,当我运行label_image.py程序

import tensorflow as tf, sys
image_path = sys.argv[1]
# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line 
                   in tf.gfile.GFile("/tf_files/retrained_labels.txt")]
# Unpersists graph from file
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:
    # 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/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))

我收到此错误消息

/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

您正在尝试通过调用sys.argv[1]来获取命令行参数。因此,您需要给出命令行参数以满足它。看来所需的参数是测试图像,您应该将其位置作为参数传递。

pycharm应该具有脚本参数和解释器选项对话框,您可以使用该脚本参数输入所需的参数。

或者您可以从命令行调用脚本并通过;

输入参数
>python my_python_script.py my_python_parameter.jpg

编辑:

根据文档(我没有在此计算机上安装Pycharm),您应该转到 Run/debug Configuration 菜单>菜单,并为脚本编辑配置。将文件的绝对路径添加到脚本参数 box in quotes。

或者,如果您只想完全跳过参数内容,只需将路径作为 raw_input(python3中的 input)或只需将其提供给image_path = r"absolute_image_path.jpg"

最新更新