如何将base64编码图像传递给Tensorflow预测? [英] How to pass base64 encoded image to Tensorflow prediction?

查看:374
本文介绍了如何将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..."}]}

这篇关于如何将base64编码图像传递给Tensorflow预测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆