GCP ML 引擎预测失败:处理输入时出错:预期的 float32 得到了 base64
GCP ML Engine Prediction failed: Error processing input: Expected float32 got base64
我正在尝试对部署到 GCP ML 引擎的经过自定义训练的 TensorFlow 模型调用预测。当我尝试对模型调用预测时,它返回以下错误消息 "Expected float32 got base64"
- 我用过迁移学习和TensorFlow的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
- 我已经使用 TensorFlow 的 label_img.py 脚本在本地测试了预测,预测在本地运行 我的图像
python label_image.py --graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt --input_layer=Placeholder --output_layer=final_result \
- 我已经按照 retrain.py 脚本文档中的描述导出了我的模型以与 Tensorflow Serving 一起使用。
python retrain.py --image_dir ~/training_images --saved_model_dir /saved_model_directory
我已经将模型上传到 Firebase,GCP 验证并接受了我的模型,我能够触发我的模型。
尝试调用在线预测时,我收到“预期的 float32”错误。
test.json ={"image_bytes": {"b64": "/9j/4AAQSkZJ.......=="}}
gcloud ml-engine predict \
--model my_model \
--version v1 \
--json-instances ./test.json
我是否需要修改 retrain.py 以使我保存的模型接受 base64 或是否有其他解决问题的方法?
我已经检查了以下答案,但不幸的是它没有解决我的问题:
问题是 retrain.py
导出一个模型,其输入期望以浮点形式的已解码和调整大小的图像(请参阅此 line),但您传递的是原始的、未解码的图像数据。
有两种解决方案。
- 以预期的格式(浮点数)创建一个 JSON 请求。这是一个简单的修复,但可能会对性能产生影响(将 float32 数据发送为 JSON 可能效率低下)。
- 改变模型以接受原始图像数据作为输入。这需要对模型进行一些改造。
对于 (1),您将发送一个 JSON 类似于以下内容的文件:
{"images": [[[0.0, 0.0, 0.0], [0,0,0], [...]], [...], ...]}
当然,您可能会使用一些客户端库来构造它
(2) 有点复杂。 This sample 可以指导您如何操作。
虽然在 json 中发送 float32 数组可以工作,但您会发现由于网络延迟,它非常慢。如果可能的话,您希望使用 base64 编码的字符串。
为此,您可以对导出脚本进行一个非常简单的更改,即更改图像输入名称以 _bytes
结尾。发生这种情况时,tensorflow serving 会自动为您解码 base64 编码的图像字符串。基本上更新这一行 https://github.com/tensorflow/hub/blob/a96bbd73abbecfad8c5517684cf3655b48bab39b/examples/image_retraining/retrain.py#L963
到inputs={'image_bytes': in_image},
这可能是处理图像输入时最有用但鲜为人知的 tensorflow 服务行为之一。
导出模型时也可以使用tf.io.decode自己编写解码函数
最后,您的 json 负载看起来像这样
{"inputs": {"image_bytes": {"b64": "/9j/4AAQSkZJ.......=="}}}
或者,如果您更喜欢实例格式
{"instances": [{"image_bytes": {"b64": "/9j/4AAQSkZJ.......=="}}]}
我正在尝试对部署到 GCP ML 引擎的经过自定义训练的 TensorFlow 模型调用预测。当我尝试对模型调用预测时,它返回以下错误消息 "Expected float32 got base64"
- 我用过迁移学习和TensorFlow的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
- 我已经使用 TensorFlow 的 label_img.py 脚本在本地测试了预测,预测在本地运行 我的图像
python label_image.py --graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt --input_layer=Placeholder --output_layer=final_result \
- 我已经按照 retrain.py 脚本文档中的描述导出了我的模型以与 Tensorflow Serving 一起使用。
python retrain.py --image_dir ~/training_images --saved_model_dir /saved_model_directory
我已经将模型上传到 Firebase,GCP 验证并接受了我的模型,我能够触发我的模型。
尝试调用在线预测时,我收到“预期的 float32”错误。
test.json ={"image_bytes": {"b64": "/9j/4AAQSkZJ.......=="}}
gcloud ml-engine predict \
--model my_model \
--version v1 \
--json-instances ./test.json
我是否需要修改 retrain.py 以使我保存的模型接受 base64 或是否有其他解决问题的方法?
我已经检查了以下答案,但不幸的是它没有解决我的问题:
问题是 retrain.py
导出一个模型,其输入期望以浮点形式的已解码和调整大小的图像(请参阅此 line),但您传递的是原始的、未解码的图像数据。
有两种解决方案。
- 以预期的格式(浮点数)创建一个 JSON 请求。这是一个简单的修复,但可能会对性能产生影响(将 float32 数据发送为 JSON 可能效率低下)。
- 改变模型以接受原始图像数据作为输入。这需要对模型进行一些改造。
对于 (1),您将发送一个 JSON 类似于以下内容的文件:
{"images": [[[0.0, 0.0, 0.0], [0,0,0], [...]], [...], ...]}
当然,您可能会使用一些客户端库来构造它
(2) 有点复杂。 This sample 可以指导您如何操作。
虽然在 json 中发送 float32 数组可以工作,但您会发现由于网络延迟,它非常慢。如果可能的话,您希望使用 base64 编码的字符串。
为此,您可以对导出脚本进行一个非常简单的更改,即更改图像输入名称以 _bytes
结尾。发生这种情况时,tensorflow serving 会自动为您解码 base64 编码的图像字符串。基本上更新这一行 https://github.com/tensorflow/hub/blob/a96bbd73abbecfad8c5517684cf3655b48bab39b/examples/image_retraining/retrain.py#L963
到inputs={'image_bytes': in_image},
这可能是处理图像输入时最有用但鲜为人知的 tensorflow 服务行为之一。
导出模型时也可以使用tf.io.decode自己编写解码函数
最后,您的 json 负载看起来像这样
{"inputs": {"image_bytes": {"b64": "/9j/4AAQSkZJ.......=="}}}
或者,如果您更喜欢实例格式
{"instances": [{"image_bytes": {"b64": "/9j/4AAQSkZJ.......=="}}]}