问题
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"
- 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
- 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 \
- 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
I've uploaded the model to Firebase, GCP validated and accepted my model, I was able to trigger my model.
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.
- 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).
- 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