【发布时间】:2019-07-11 10:06:31
【问题描述】:
我通过 AJAX POST 将 base64 编码图像发送到存储在 Google CloudML 中的模型。我收到一条错误消息,告诉我我的 input_fn(): 无法解码图像并将其转换为 jpeg。
错误:
Prediction failed: Error during model execution:
AbortionError(code=StatusCode.INVALID_ARGUMENT,
details="Expected image (JPEG, PNG, or GIF), got
unknown format starting with 'u\253Z\212f\240{\370
\351z\006\332\261\356\270\377' [[{{node map/while
/DecodeJpeg}} = DecodeJpeg[_output_shapes=
[[?,?,3]], acceptable_fraction=1, channels=3,
dct_method="", fancy_upscaling=true, ratio=1,
try_recover_truncated=false,
_device="/job:localhost/replica:0 /task:0
/device:CPU:0"](map/while/TensorArrayReadV3)]]")
下面是完整的 Serving_input_receiver_fn():
-
我认为第一步是处理传入的 b64 编码字符串并对其进行解码。这是通过以下方式完成的:
image = tensorflow.io.decode_base64(image_str_tensor) -
我相信下一步是打开字节,但这是我不知道如何使用 tensorflow 代码处理解码的 b64 字符串并需要帮助的地方。
使用 python Flask 应用程序可以做到这一点:
image = Image.open(io.BytesIO(decoded))
- 传递字节以被
tf.image.decode_jpeg解码????
image = tensorflow.image.decode_jpeg(image_str_tensor, channels=CHANNELS)
完整的 input_fn(): 代码
def serving_input_receiver_fn():
def prepare_image(image_str_tensor):
image = tensorflow.io.decode_base64(image_str_tensor)
image = tensorflow.image.decode_jpeg(image_str_tensor, channels=CHANNELS)
image = tensorflow.expand_dims(image, 0) image = tensorflow.image.resize_bilinear(image, [HEIGHT, WIDTH], align_corners=False)
image = tensorflow.squeeze(image, axis=[0])
image = tensorflow.cast(image, dtype=tensorflow.uint8)
return image
如何将我的 b64 字符串解码回 jpeg,然后将 jpeg 转换为张量?
【问题讨论】:
-
你能展示你的 image_str_tensor 吗? AFAIK TF 服务 http 可以将图像 base64 字符串编码为 JSON。
-
感谢您指出 image_str_tensor,johnjohnlys。一直困扰着我的事情。它只是出现在这个答案中:(stackoverflow.com/questions/51432589/…)和这个答案:(github.com/mhwilder/tf-keras-gcloud-deployment/blob/master/…)——正如你所看到的,我只是从第一个答案中复制的,看起来 image_str_tensor 只是通过而没有被声明。
-
我已经对我的图像进行了编码并通过 Ajax 帖子使用 json.stringify 发送。一旦到达服务输入接收器函数,它就会解码并转换回 jpeg。
-
要将图像(二进制)输入作为 http 有效负载的一部分发送,ml 引擎要求您将其作为 base64 编码发送。 ML 引擎会在将其发送到您的图表之前对其进行解码。所以你不需要在你的图表中解码。
标签: image tensorflow base64 tensorflow-serving google-cloud-ml