【发布时间】:2017-05-18 12:18:18
【问题描述】:
我正在使用 Tensorflow 进行图像分类。我使用 image_retraining/retrain.py 重新训练具有新类别的初始库,并使用它使用来自https://github.com/llSourcell/tensorflow_image_classifier/blob/master/src/label_image.py 的 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 图像。我是机器学习的菜鸟,所以不确定这是否是一种限制,或者是否可以训练其他扩展图像,如 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
【问题讨论】:
-
关于
IndexError,错误信息提示image没有三个维度。如果你做print(np.array(image).shape),你会得到什么?你可以试试np.asarray(image)吗? -
@mrry 现在可以让它工作了。做了一个 image.convert('RGB') 并随后输入数组。现在可以使用 JPG、PNG 和 GIF。
标签: python image-processing tensorflow jpeg classification