如何在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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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