GCP ML Engine Prediction failed: Error processing input: Expected float32 got base64

不打扰是莪最后的温柔 提交于 2020-02-06 04:25:26

问题


I am trying to call a prediction on a custom trained TensorFlow model deployed to GCP ML engine. When I am trying to call a prediction on the model it is returning the following error message "Expected float32 got base64"

  1. I've used transfer learning and the TensorFlow's retrain.py script to train my model on my images, following the official documentation
python retrain.py --image_dir ~/training_images saved_model_dir /saved_model_directory
  1. I've tested the prediction locally using TensorFlow's label_img.py script, the prediction worked locally for my images
python label_image.py --graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt --input_layer=Placeholder --output_layer=final_result \
  1. I've exported my model to use with Tensorflow Serving as described in the documentation of the retrain.py script.
python retrain.py --image_dir ~/training_images --saved_model_dir /saved_model_directory
  1. I've uploaded the model to Firebase, GCP validated and accepted my model, I was able to trigger my model.

  2. When trying to call an online prediction I am receiving the " Expected float32" error.

 test.json ={"image_bytes": {"b64": "/9j/4AAQSkZJ.......=="}}

 gcloud ml-engine predict \
        --model my_model \
        --version v1 \
        --json-instances ./test.json

Do I need to modify retrain.py to make my saved model accept base64 or is there any other solution for the problem?

I've already checked the following answer, but unfortunately it does not solved my problem: How to pass base64 encoded image to Tensorflow prediction?


回答1:


The problem is that retrain.py exports a model whose input is expecting an already decoded and resized image in the form of floats (see this line), but you are passing it raw, undecoded image data.

There are two solutions.

  1. Create a JSON request in the expected format (floats). This is an easy fix but could have performance implications (sending float32 data as JSON can be inefficient).
  2. Alter the model to accept raw image data as input. This requires some reworking of the model.

For (1), you would send a JSON file similar to:

{"images": [[[0.0, 0.0, 0.0], [0,0,0], [...]], [...], ...]}

Of course, you'd probably construct that using some client library

(2) is a little more involved. This sample can guide you on how to do that.



来源:https://stackoverflow.com/questions/54242029/gcp-ml-engine-prediction-failed-error-processing-input-expected-float32-got-ba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!