Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于PNG、GIF等的Tensorflow标签图片_Python_Image Processing_Tensorflow_Jpeg_Classification - Fatal编程技术网

Python 用于PNG、GIF等的Tensorflow标签图片

Python 用于PNG、GIF等的Tensorflow标签图片,python,image-processing,tensorflow,jpeg,classification,Python,Image Processing,Tensorflow,Jpeg,Classification,我在玩Tensorflow进行图像分类。我使用image_retaining/retain.py使用新类别对inception库进行重新训练,并使用它使用label_image.py对图像进行分类,如下所示: import tensorflow as tf import sys # change this as you see fit image_path = sys.argv[1] # Read in the image_data image_data = tf.gfile.FastGFi

我在玩Tensorflow进行图像分类。我使用image_retaining/retain.py使用新类别对inception库进行重新训练,并使用它使用label_image.py对图像进行分类,如下所示:

import tensorflow as tf
import sys

# change this as you see fit
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("/root/tf_files/output_labels.txt")]

# Unpersists graph from file
with tf.gfile.FastGFile("/root/tf_files/output_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})
        predictions = sess.run(softmax_tensor,{'DecodePng/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))
我注意到两个问题。当我重新训练新的分类时,它只训练JPG图像。我是一个机器学习的noob,所以不确定这是一个限制还是可以训练其他扩展图像,比如PNG、GIF

另一个是在对图像进行分类时,输入同样只针对JPG。我试图在上面的label_image.py中将DecodeJpeg更改为DecodePng,但无法工作。我尝试的另一种方法是将其他格式转换为JPG,然后将其传递给分类,如:

im = Image.open('/root/Desktop/200_s.gif').convert('RGB')
im.save('/root/Desktop/test.jpg', "JPEG")
image_path1 = '/root/Desktop/test.jpg'
还有别的办法吗?Tensorflow是否具有处理JPG以外的其他图像格式的功能

我尝试了以下方法,将解析后的图像与@mrry建议的JPEG图像进行对比

import tensorflow as tf
import sys
import numpy as np
from PIL import Image

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

# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()

image = Image.open(image_path)
image_array = np.array(image)[:,:,0:3]  # Select RGB channels only.

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

# Unpersists graph from file
with tf.gfile.FastGFile("/root/tf_files/output_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:0': image_array})

# 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))
它适用于JPEG图像,但当我使用PNG或GIF时,它会抛出

        Traceback (most recent call last):   
    File "label_image.py", line 17, in <module>
        image_array = np.array(image)[:,:,0:3]  # Select RGB channels only. 
IndexError: too many indices for array

感谢并问候

您应该看看包裹。它具有对JPEG、GIF和PNG进行解码/编码的良好功能。

该模型只能对JPEG图像进行训练和评估,因为保存在/root/tf_files/output_graph.pb中的GraphDef只包含一个op,并使用该op的输出进行预测。使用其他图像格式至少有两个选项:

输入已解析的图像,而不是JPEG数据。在当前程序中,输入一个JPEG编码的图像作为tensor DecodeJpeg/contents:0的字符串值。相反,您可以为表示tf.image.DecodeJpeg op输出的tensor DecodeJpeg:0输入解码图像数据的三维数组,并且可以使用NumPy、PIL或其他Python库创建此数组

重新映射中输入的图像。通过tf.import_graph_def函数,可以通过重新映射单个张量值将两个不同的图形连接在一起。例如,可以执行以下操作将新的图像处理op添加到现有图形中:

image_string_input = tf.placeholder(tf.string)
image_decoded = tf.image.decode_png(image_string_input)

# Unpersists graph from file
with tf.gfile.FastGFile("/root/tf_files/output_graph.pb", 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    softmax_tensor, = tf.import_graph_def(
        graph_def,
        input_map={"DecodeJpeg:0": image_decoded},
        return_operations=["final_result:0"])

with tf.Session() as sess:
    # Feed the image_data as input to the graph and get first prediction
    predictions = sess.run(softmax_tensor, {image_string_input: image_data})
    # ...

按照@mrry的建议输入解析图像,将图像数据转换为数组并转换为RGB,如下代码所述。现在我可以输入JPG、PNG和GIF

import tensorflow as tf
import sys
import numpy as np
from PIL import Image

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

# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()

image = Image.open(image_path)
image_array = image.convert('RGB')

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

# Unpersists graph from file
with tf.gfile.FastGFile("/root/tf_files/output_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:0': image_array})

# 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))

我尝试了你建议的第一种方法,输入了在我的问题中更新的解析图像引用。它适用于JPEG,但当我使用PNG或GIF时,它会抛出Indexer错误:数组错误的索引太多。我做错什么了吗?你能在问题中包括来自索引器的整个堆栈跟踪吗?是的,我在问题中包括了整个堆栈跟踪。关于索引器,错误消息表明图像没有三维。如果打印np.arrayimage.shape,会得到什么?你能试试np.asarrayimage吗?@mrry现在可以用了。做了一个图像。随后转换“RGB”并输入数组。现在能够使用JPG、PNG和GIF。我有一个问题:与top_k=预测[0]相关。argsort[-lenpredictions[0]:][:-1]。如何获得最高的预测值?简短的回答是,标记线[top_k[0]]。谢谢