python - 在 tensorflow 中输入图像数据以进行迁移学习

标签 python tensorflow

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

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

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

谢谢!!

最佳答案

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

  1. 将 PNG 图像转换为 height x width x 3 (channels) 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: ...})
    

关于python - 在 tensorflow 中输入图像数据以进行迁移学习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34484148/

相关文章:

python - 当我尝试在 Python 上反转矩阵中的行时出错

python - 如何根据列值在现有数据框中添加新行?

python - Django 无效的 HTTP_HOST header : '\x80\xc7\xda\x9e'

python - 训练后如何保存/恢复模型?

Tensorflow - 如何批量访问每个示例的损失值?

tensorflow - Syntaxnet/Parsey McParseface 中使用了哪些依赖关系表示?

python - python 脚本的输出读入 C 程序,无需创建临时文件以在程序之间传递值

python - 如何在 Python 3 中使用 https URL 下载图像?

python-3.x - 在 ANN 模型中加载 pickle 时接收错误

python - 如何使用经过训练的 tensorflow 算法