在张量流中为转移学习提供图像数据

Geo*_* Wu 19 python tensorflow

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

prediction = sess.run(softmax_tensor,{'DecodeJpeg/contents:0'}:image_data})
Run Code Online (Sandbox Code Playgroud)

有没有办法喂png图像.我试过换DecodeJpegDecodePng但是没用.除此之外,如果我想要像numpy数组或一批数组那样提供解码图像文件,我应该改变什么?

谢谢!!

mrr*_*rry 29

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

  1. 将PNG图像转换为heightx widthx 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})
    
    Run Code Online (Sandbox Code Playgroud)

    或许令人混淆,'DecodeJpeg:0'输出的的DecodeJpeg运算,所以通过将该料张量,你能养活原始图像数据.

  2. tf.image.decode_png()在导入的图形中添加操作.从简单的开关美联储张的名字'DecodeJpeg/contents:0',以'DecodePng/contents:0'不起作用,因为没有'DecodePng'在运图运算.您可以使用以下input_map参数将此类节点添加到图形中tf.import_graph_def():

    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: ...})
    
    Run Code Online (Sandbox Code Playgroud)

  • 从你的第二个答案,我假设'DecodeJpeg:0'是用`jpg_data = tf.placeholder(tf.string,shape = [])构造的; decoding_jpg = tf.image.decode_jpeg(jpg_data,channels = 3)`所以它等待一个字符串而不是一个numpy数组. (2认同)
  • @Yamaneko:选项1显示了如何直接提供numpy数组.(张量`DecodeJpeg:0`是解码JPEG图像的op的输出,因此如果您提供替换值,它也应该是解码图像.) (2认同)