如何在PyCharm编辑器中加载retrained_graph.pb和retrained_label.txt

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注