【问题标题】:How to load retrained_graph.pb and retrained_label.txt using pycharm editor如何使用pycharm编辑器加载retrained_graph.pb和retrained label.text
【发布时间】:2017-07-27 05:11:09
【问题描述】:

使用 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

谁能帮我解决这个问题。

【问题讨论】:

    标签: python-2.7 machine-learning tensorflow classification image-recognition


    【解决方案1】:

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

    在 pycharm 中转到 View->Tool windows->Terminal.

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

    python label_image.py /image_path/image_name.jpg
    

    【讨论】:

      【解决方案2】:

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

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

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

      >python my_python_script.py my_python_parameter.jpg
      

      编辑:

      根据documents(我没有在这台电脑上安装pycharm),你应该去运行/调试配置菜单并编辑你的脚本的配置。将文件的绝对路径添加到 Script Parameters 框中的引号中。

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

      【讨论】:

      • 你能帮我通过pycharm传递“python /tf_files/label_image.py /tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg”命令吗
      • @Chandan 我没有安装 pycharm,但是根据我在网上看到的内容编辑了答案。请检查一下。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 2015-09-21
      • 2013-01-29
      • 2023-03-25
      相关资源
      最近更新 更多