【问题标题】:Accepting base64-images as input for TensorFlow model接受 base64 图像作为 TensorFlow 模型的输入
【发布时间】:2019-09-16 00:01:30
【问题描述】:

我正在尝试导出我的 TensorFlow 图像分类模型,以便它接受 base64 字符串作为输入。

我已尝试实施this question 提供的解决方案,但出现以下错误:

"InvalidArgumentError: Shape 必须为 0 级,但为 1 级 'DecodeJpeg_1'(操作:'DecodeJpeg'),输入形状:[?]。”

由于第 4 行的代码出现错误。

export_dir = '~/models/1'
builder = saved_model_builder.SavedModelBuilder(export_dir)
image = tf.placeholder(dtype=tf.string, shape=[None], name='source')
decoded = tf.image.decode_jpeg(image)
scores = build_model(decoded)
signature = predict_signature_def(inputs={'image_bytes': image},
                                 outputs={'output': scores})

with K.get_session() as sess:
    builder.add_meta_graph_and_variables(sess=sess,
                                         tags=[tag_constants.SERVING],
                                         signature_def_map={'predict': signature})
    builder.save()
sess.close()

还有,

我看到在第 5 行,“scores”提供了基于 build_model 函数的模型输出。但是,我在原始问题的答案或 TensorFlow 文档中找不到该函数的来源。

【问题讨论】:

  • 你可以试试第 4 行的shape=[1] 吗?
  • 不幸的是不起作用。引发类似的错误,即:InvalidArgumentError: Shape must be rank 0 but is rank 1 for 'DecodeJpeg' (op: 'DecodeJpeg') with input shapes: [1].
  • 你可以试试shape=[]吗?
  • shape[] 有效,谢谢,现在没有出现错误。但是,现在我收到一个错误,说“build_model”(第 5 行)没有定义,我很害怕。你知道这个函数是从哪里来的吗?
  • 不,可能来自一些示例代码?

标签: python rest tensorflow-serving


【解决方案1】:

根据tf.decode_jpeg中提到的教程,我们应该在使用image = tf.image.decode_jpeg(image)之前使用image = tf.io.read_file(path)

工作代码示例如下所示:

import tensorflow as tf

path = '/home/mothukuru/Jupyter_Notebooks/Stack_Overflow/cat.jpeg'

z = tf.placeholder(tf.string, shape=())

image = tf.io.read_file(path)

image = tf.image.decode_jpeg(image)

with tf.Session() as sess:
   v = sess.run([image], feed_dict={z:path})
   print(v)

以上代码的输出是一个数组,如下图:

[array([[[216, 216, 216],
        [218, 218, 218],
        [220, 220, 220],
        ...,
        [226, 228, 217],
        [221, 223, 212],
        [221, 223, 212]],

       [[216, 216, 216],
        [217, 217, 217],
        [219, 219, 219],
        ...,
        [225, 227, 216],
        [222, 224, 213],
        [222, 224, 213]],

       [[216, 216, 216],
        [216, 216, 216],
        [218, 218, 218],
        ...,
        [223, 225, 214],
        [222, 224, 213],
        [222, 224, 213]],

       ...,

       [[240,  20,  64],
        [240,  20,  64],
        [243,  23,  67],
        ...,
        [  7,   3,   0],
        [  8,   4,   1],
        [  8,   4,   1]],

       [[241,  21,  65],
        [240,  20,  64],
        [245,  25,  69],
        ...,
        [  7,   3,   0],
        [  8,   4,   1],
        [  8,   4,   1]],

       [[242,  22,  66],
        [239,  19,  63],
        [246,  26,  70],
        ...,
        [  7,   3,   0],
        [  8,   4,   1],
        [  8,   4,   1]]], dtype=uint8)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2020-12-22
    • 2019-03-31
    • 2017-12-19
    • 2020-01-17
    相关资源
    最近更新 更多