我按照[tensorflow教程]训练了一个图像分类器。( https://www.tensorflow.org/hub/tutorials/image_retraining )
我用过这个snippet在训练过程后生成我的
SavedModel
。我按照 Google 的说明部署了模型,并尝试使用本地目录中的图像进行一些预测。
为了执行预测,我使用了以下命令:
# Create request message in json format python -c 'import base64, json; img = base64.b64encode(open("image.jpg").read()); print json.dumps({"image_bytes": {"b64": img}})' image.jpg &> request.json # Call prediction service API to get classifications gcloud ml-engine predict --model ${MODEL_NAME} --json-instances request.json
我收到以下错误:
"error": "Prediction failed: Error processing input: Expected float32, got {u'b64': u'/9j/4AA....lPqevnQf//Z'} of type 'dict' instead.
- 我训练的脚本确实[将图像转换为 float32]。( https://github.com/tensorflow/hub/blob/master/examples/image_retraining/retrain.py#L671 )
我应该使用不同的类型重新训练模型或者如何解决这个问题?非常感谢任何提示。
最佳答案
您需要确保您的服务函数如下所示。请注意,输入的名称是 image_bytes,可以是任何以 _bytes 结尾的名称。
def serving_input_fn():
feature_placeholders = {
'image_bytes': tf.placeholder(dtype=tf.string, shape=[None], name='source')}
single_image = tf.decode_raw(feature_placeholders['image_bytes'], tf.float32)
return tf.estimator.export.ServingInputReceiver(feature_placeholders, feature_placeholders)
要了解有关如何发送数据及其基本原理的更多详细信息,请查看 https://stackoverflow.com/a/49177909/6031363
此外,您可以访问 AI Platform 文档中有关发送预测请求的说明 https://cloud.google.com/ml-engine/docs/prediction-overview
关于tensorflow - 使用 tensorflow 模型在 Google Cloud 中进行预测失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54949218/