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

How to load retrained_graph.pb and retrained_label.txt using pycharm editor

使用 pete warden 教程我已经训练了初始网络和训练我得到两个文件

1.retrained_graph.pb
2.retrained_label.txt

我想用这个对花卉图像进行分类。 我安装了 pycharm 并链接了所有的 tensorflow 库,我还测试了它工作正常的示例 tensorflow 代码。

现在当我 运行 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

谁能帮我解决这个问题。

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

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

或者您可以从命令行调用脚本并通过以下方式输入参数;

>python my_python_script.py my_python_parameter.jpg

编辑:

根据documents(我这台电脑上没有安装pycharm),你应该去Run/Debug配置菜单并编辑脚本的配置。将文件的绝对路径添加到 脚本参数 框中,用引号括起来。

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

您收到此错误是因为它需要图像名称(带路径)作为参数。

在 pycharm 中转到“查看”->“工具”windows->“终端”。

这与打开单独的终端相同。并且 运行

python label_image.py /image_path/image_name.jpg