在张量流中提供图像数据以进行迁移学习
Feeding image data in tensorflow for transfer learning
我正在尝试使用 tensorflow 进行迁移学习。我从教程中下载了预训练模型inception3。在代码中,对于预测:
prediction = sess.run(softmax_tensor,{'DecodeJpeg/contents:0'}:image_data})
有没有办法提供png图像。我尝试将 DecodeJpeg
更改为 DecodePng
但它没有用。此外,如果我想像 numpy 数组或一批数组一样提供解码后的图像文件,我应该更改什么?
谢谢!!
classify_image.py
中使用的 InceptionV3 图仅支持 JPEG 图像 out-of-the-box。您可以通过两种方式将此图表用于 PNG 图像:
将 PNG 图像转换为 height
x width
x 3(通道)Numpy 数组,例如使用 PIL,然后将 'DecodeJpeg:0'
张量:
import numpy as np
from PIL import Image
# ...
image = Image.open("example.png")
image_array = np.array(image)[:, :, 0:3] # Select RGB channels only.
prediction = sess.run(softmax_tensor, {'DecodeJpeg:0': image_array})
可能令人困惑,'DecodeJpeg:0'
是 DecodeJpeg
操作的 输出 ,因此通过提供此张量,您可以提供原始图像数据。
向导入的图表添加一个 tf.image.decode_png()
操作。简单地将馈送张量的名称从 'DecodeJpeg/contents:0'
更改为 'DecodePng/contents:0'
是行不通的,因为交付的图中没有 'DecodePng'
操作。您可以使用 tf.import_graph_def()
:
的 input_map
参数将这样的节点添加到图中
png_data = tf.placeholder(tf.string, shape=[])
decoded_png = tf.image.decode_png(png_data, channels=3)
# ...
graph_def = ...
softmax_tensor = tf.import_graph_def(
graph_def,
input_map={'DecodeJpeg:0': decoded_png},
return_elements=['softmax:0'])
sess.run(softmax_tensor, {png_data: ...})
以下代码应处理这两种情况。
import numpy as np
from PIL import Image
image_file = 'test.jpeg'
with tf.Session() as sess:
# softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
if image_file.lower().endswith('.jpeg'):
image_data = tf.gfile.FastGFile(image_file, 'rb').read()
prediction = sess.run('final_result:0', {'DecodeJpeg/contents:0': image_data})
elif image_file.lower().endswith('.png'):
image = Image.open(image_file)
image_array = np.array(image)[:, :, 0:3]
prediction = sess.run('final_result:0', {'DecodeJpeg:0': image_array})
prediction = prediction[0]
print(prediction)
或带有直接字符串的更短版本:
image_file = 'test.png' # or 'test.jpeg'
image_data = tf.gfile.FastGFile(image_file, 'rb').read()
ph = tf.placeholder(tf.string, shape=[])
with tf.Session() as sess:
predictions = sess.run(output_layer_name, {ph: image_data} )
我正在尝试使用 tensorflow 进行迁移学习。我从教程中下载了预训练模型inception3。在代码中,对于预测:
prediction = sess.run(softmax_tensor,{'DecodeJpeg/contents:0'}:image_data})
有没有办法提供png图像。我尝试将 DecodeJpeg
更改为 DecodePng
但它没有用。此外,如果我想像 numpy 数组或一批数组一样提供解码后的图像文件,我应该更改什么?
谢谢!!
classify_image.py
中使用的 InceptionV3 图仅支持 JPEG 图像 out-of-the-box。您可以通过两种方式将此图表用于 PNG 图像:
将 PNG 图像转换为
height
xwidth
x 3(通道)Numpy 数组,例如使用 PIL,然后将'DecodeJpeg:0'
张量:import numpy as np from PIL import Image # ... image = Image.open("example.png") image_array = np.array(image)[:, :, 0:3] # Select RGB channels only. prediction = sess.run(softmax_tensor, {'DecodeJpeg:0': image_array})
可能令人困惑,
'DecodeJpeg:0'
是DecodeJpeg
操作的 输出 ,因此通过提供此张量,您可以提供原始图像数据。向导入的图表添加一个
的tf.image.decode_png()
操作。简单地将馈送张量的名称从'DecodeJpeg/contents:0'
更改为'DecodePng/contents:0'
是行不通的,因为交付的图中没有'DecodePng'
操作。您可以使用tf.import_graph_def()
:input_map
参数将这样的节点添加到图中png_data = tf.placeholder(tf.string, shape=[]) decoded_png = tf.image.decode_png(png_data, channels=3) # ... graph_def = ... softmax_tensor = tf.import_graph_def( graph_def, input_map={'DecodeJpeg:0': decoded_png}, return_elements=['softmax:0']) sess.run(softmax_tensor, {png_data: ...})
以下代码应处理这两种情况。
import numpy as np
from PIL import Image
image_file = 'test.jpeg'
with tf.Session() as sess:
# softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
if image_file.lower().endswith('.jpeg'):
image_data = tf.gfile.FastGFile(image_file, 'rb').read()
prediction = sess.run('final_result:0', {'DecodeJpeg/contents:0': image_data})
elif image_file.lower().endswith('.png'):
image = Image.open(image_file)
image_array = np.array(image)[:, :, 0:3]
prediction = sess.run('final_result:0', {'DecodeJpeg:0': image_array})
prediction = prediction[0]
print(prediction)
或带有直接字符串的更短版本:
image_file = 'test.png' # or 'test.jpeg'
image_data = tf.gfile.FastGFile(image_file, 'rb').read()
ph = tf.placeholder(tf.string, shape=[])
with tf.Session() as sess:
predictions = sess.run(output_layer_name, {ph: image_data} )