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

问题描述:

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

I have a google-cloud-ml model that I can run prediction by passing a 3 dimensional array of float32...

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

但是,这不是传输图像的有效格式,因此我想传递base64编码的png或jpeg. 本文档讨论了这样做,但是该怎么做?目前尚不清楚整个json对象是什么样子. { 'b64' : 'x0welkja...' }是否可以代替'[ [ [ 0.0 ], [ 0.5 ], [ 0.8 ] ] ... ] ]',使包围的实例"和输入"保持相同?还是其他结构?还是必须在base64上对tensorflow模型进行训练?

However this is not an efficient format to transmit images, so I'd like to pass base64 encoded png or jpeg. This document talks about doing that, but what is not clear is what the entire json object looks like. Does the { 'b64' : 'x0welkja...' } go in place of the '[ [ [ 0.0 ], [ 0.5 ], [ 0.8 ] ] ... ] ]', leaving the enclosing 'instances' and 'input' the same? Or some other structure? Or does the tensorflow model have to be trained on base64?

不必在base64数据上训练TensorFlow模型.保持训练图不变.但是,在导出模型时,您需要导出一个可以接受png或jpeg(或者可能是原始的,如果很小的话)数据的模型.然后,在导出模型时,需要确保对以_bytes结尾的输出使用名称.这向CloudML Engine发出信号,表明您将发送base64编码的数据.将所有内容放在一起会是这样的:

The TensorFlow model does not have to be trained on base64 data. Leave your training graph as is. However, when exporting the model, you'll need to export a model that can accept png or jpeg (or possibly raw, if it's small) data. Then, when you export the model, you'll need to be sure to use a name for the output that ends in _bytes. This signals to CloudML Engine that you will be sending base64 encoded data. Putting it all together would like something like this:

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)

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

The request you send will look something like this:

{"instances": [{"b64": "x0welkja..."}]}