【问题标题】:Tensorflow for poets诗人的 TensorFlow
【发布时间】:2017-06-12 14:49:53
【问题描述】:

在诗人的 tensorflow 中有一个名为 label_image.py 的脚本,它获取单个图像并根据概率吐出 5 个预测。有没有办法给它一个完整的图像文件夹并要求它将预测一个一个地存储在一个文本文件中?

文件的链接 -https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#5

【问题讨论】:

  • 添加指向该脚本的链接以大大增加能够回答您问题的人数。
  • 可能您不能只更改该脚本,否则其他一些代码将无法工作。相反,只需编写 另一个 函数,例如使用os.listdir 获取文件夹中的所有图像文件,将该脚本中的函数应用于每个文件,并返回结果,例如作为将文件名映射到预测的字典。
  • 谢谢。我试试看

标签: python tensorflow


【解决方案1】:

我试过了,对我来说效果很好。以下是您可能想尝试的代码。如果您需要一些非常具体的内容,您可以进行更改。

import os, sys
import glob
import tensorflow as tf

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# change this as you see fit
image_path = sys.argv[1]

extension = ['*.jpeg', '*.jpg']
files=[]

for e in extension:
directory = os.path.join(image_path, e)
fileList = glob.glob(directory)
for f in fileList:
    files.append(f)

# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
           in tf.gfile.GFile("/path_to_file/retrained_labels.txt")]

# Unpersists graph from file
with tf.gfile.FastGFile("/path_to_file/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')

    # Read in the image_data
    for file in files:
        image_data = tf.gfile.FastGFile(file, 'rb').read()

        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]
        print("Image Name: " + file)
        for node_id in top_k:
            human_string = label_lines[node_id]
            score = predictions[0][node_id]
            print('%s (score = %.5f)' % (human_string, score))

【讨论】:

  • 非常感谢您的回复
  • Shrikanth ,我不能 - 我的代表点数少于 15 - 但如果我设法超过它,我会回来接受它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2018-11-13
  • 2018-08-28
相关资源
最近更新 更多