aan*_*est 2 python jpeg classification image-processing tensorflow
我在玩Tensorflow进行图像分类。我用image_retraining / retrain.py重新训练与新的类别以来库并用它来使用label_image.py图片来自分类https://github.com/llSourcell/tensorflow_image_classifier/blob/master/src/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))
Run Code Online (Sandbox Code Playgroud)
我注意到了两个问题。重新训练新类别时,它只会训练JPG图片。我是机器学习的新手,因此不确定这是一个限制还是可以训练其他扩展图像(如PNG,GIF)?
另一个是在对图像分类时,输入再次仅适用于JPG。我试图在上面的label_image.py中将DecodeJpeg更改为DecodePng,但无法正常工作。我尝试的另一种方法是在将其他格式传递给JPG进行分类之前将其转换为JPG,例如:
im = Image.open('/root/Desktop/200_s.gif').convert('RGB')
im.save('/root/Desktop/test.jpg', "JPEG")
image_path1 = '/root/Desktop/test.jpg'
Run Code Online (Sandbox Code Playgroud)
还有其他方法吗?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))
Run Code Online (Sandbox Code Playgroud)
它适用于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
Run Code Online (Sandbox Code Playgroud)
谢谢并恭祝安康
该模型只能训练(并评估)JPEG图像,因为GraphDef您保存在/root/tf_files/output_graph.pb其中的JPEG图像仅包含一个tf.image.decode_jpeg()op,并使用该op的输出进行预测。使用其他图像格式至少有两个选择:
输入解析的图像而不是JPEG数据。在当前程序中,您将JPEG编码的图像作为张量的字符串值输入"DecodeJpeg/contents:0"。相反,可以在解码的图像数据的3-d阵列用于张量进料"DecodeJpeg:0"(这代表了输出的的tf.image.decode_jpeg()运算),并且可以使用NumPy的,PIL,或一些其他Python库创建此阵列。
重新映射在中输入的图像tf.import_graph_def()。该tf.import_graph_def()函数使您可以通过重新映射单个张量值来将两个不同的图形连接在一起。例如,您可以执行以下操作将新的图像处理操作添加到现有图形:
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})
# ...
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
3638 次 |
| 最近记录: |