【问题标题】:Feeding image data in tensorflow for transfer learning在张量流中输入图像数据以进行迁移学习
【发布时间】:2016-04-01 17:59:23
【问题描述】:

我正在尝试使用 tensorflow 进行迁移学习。我从教程中下载了预训练模型 inception3。在代码中,用于预测:

prediction = sess.run(softmax_tensor,{'DecodeJpeg/contents:0'}:image_data})

有没有办法提供 png 图像。我尝试将DecodeJpeg 更改为DecodePng,但没有成功。另外,如果我想提供解码后的图像文件,如 numpy 数组或一批数组,我应该改变什么?

谢谢!!

【问题讨论】:

标签: python tensorflow


【解决方案1】:

classify_image.py 中使用的随附 InceptionV3 图形仅支持开箱即用的 JPEG 图像。有两种方法可以将此图表用于 PNG 图像:

  1. 将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 操作的输出,因此通过输入此张量,您可以输入原始图像数据。

  2. 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: ...})
    

【讨论】:

  • 我用你的第一种方法试过了。它输出“无法从提要中获取元素。”我不确定为什么。但是你的第二个工作。谢谢!!
  • 从你的第二个回答中,我假设 'DecodeJpeg:0' 是用 jpg_data = tf.placeholder(tf.string, shape=[]); decoded_jpg = tf.image.decode_jpeg(jpg_data, channels=3) 构造的,所以它等待一个字符串而不是一个 numpy 数组。
  • @Yamaneko:选项 1 展示了如何直接提供一个 numpy 数组。 (张量 DecodeJpeg:0 是解码 JPEG 图像的操作的输出,因此如果提供替换值,它也应该是解码图像。)
  • @milez 这些操作仍然以相同的方式工作,因此答案仍然有效。我怀疑您的 OpenCV 转换为字符串存在问题,但这里没有足够的空间来详细介绍。随意发布一个新问题,其中包含您尝试过的最小示例!
  • 好吧,我想通了。根据 tf 示例,预训练的 Inception v3 中的第一个节点是DecodeJpeg/contents:0,它需要一个 jpeg 编码的图像作为字符串。然而,第二个节点是DecodeJpeg:0,它接受解码后的图像作为 numpy 数组,就像你说的那样。我只是错过了/contents 未包含在您的示例中的事实:)
【解决方案2】:

下面的代码应该处理这两种情况。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2023-03-30
    • 2022-01-14
    • 2020-07-23
    • 2020-11-07
    相关资源
    最近更新 更多