如何将base64编码图像传递给Tensorflow预测?

use*_*174 7 google-cloud-ml

我有一个google-cloud-ml模型,我可以通过传递float32的3维数组运行预测...

{ 'instances' [ { 'input' : '[ [ [ 0.0 ], [ 0.5 ], [ 0.8 ] ] ... ] ]' } ] }

然而,这不是传输图像的有效格式,因此我想传递base64编码的png或jpeg. 本文档讨论了这样做,但不清楚的是整个json对象是什么样的.是否{ 'b64' : 'x0welkja...' }代替了'[ [ [ 0.0 ], [ 0.5 ], [ 0.8 ] ] ... ] ]',留下封闭的"实例"和"输入"相同?还是其他一些结构?或者,张量流模型是否必须在base64 上进行训练

rha*_*l80 4

TensorFlow 模型不必在 Base64 数据上进行训练。保持你的训练图不变。但是,导出模型时,您需要导出一个可以接受 PNG 或 jpeg(或者可能是原始数据,如果它很小)数据的模型。然后,当您导出模型时,您需要确保使用以_bytes. 这表明CloudML Engine您将发送 Base64 编码的数据。把它们放在一起会像这样:

from tensorflow.contrib.saved_model.python.saved_model import utils

# Shape of [None] means we can have a batch of images.
image = tf.placeholder(shape = [None], dtype = tf.string)
# Decode the image.
decoded = tf.image.decode_jpeg(image, channels=3)
# Do the rest of the processing.
scores = build_model(decoded)

# The input name needs to have "_bytes" suffix.
inputs = { 'image_bytes': image }
outputs = { 'scores': scores }
utils.simple_save(session, export_dir, inputs, outputs)
Run Code Online (Sandbox Code Playgroud)

您发送的请求将如下所示:

{
    "instances": [{
        "b64": "x0welkja..."
    }]
}
Run Code Online (Sandbox Code Playgroud)