我正在尝试导出TensorFlow图像分类模型,以使其接受base64字符串作为输入。
我尝试实现this question上提供的解决方案,但是出现以下错误:
“ InvalidArgumentError:形状必须为等级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行,“分数”提供了基于build_model
函数的模型输出。但是,我找不到原始问题的答案,也找不到该功能来源的TensorFlow文档。
答案 0 :(得分: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)]