接受base64图像作为TensorFlow模型的输入

问题描述 投票:0回答:1

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

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

“ InvalidArgumentError:形状必须为0,但对于1为1输入形状为[[]]的'DecodeJpeg_1'(op:'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行,“分数”提供了基于build_model函数的模型输出。但是,我找不到原始问题的答案或该函数来自的TensorFlow文档。

python rest tensorflow-serving
1个回答
0
投票

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

工作代码示例如下所示:

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)]
© www.soinside.com 2019 - 2024. All rights reserved.