tensorflow - 使用 tensorflow 模型在 Google Cloud 中进行预测失败

标签 tensorflow machine-learning google-cloud-ml

  • 我按照[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.

我应该使用不同的类型重新训练模型或者如何解决这个问题?非常感谢任何提示。

最佳答案

您需要确保您的服务函数如下所示。请注意,输入的名称是 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/

相关文章:

python-3.x - 部署 Tensorflow 对象检测模型并提供预测

python-3.x - 使用 Python 在 lstm 中提前停止

tensorflow - Keras 自定义损失函数打印张量值

machine-learning - 使用带有测试训练分割的 DBSCAN 进行分类

r - R 中的 SVM - 对新实例进行分类

Azure 机器学习-Azure Blob 存储

python - Google Cloud ML FAILED_PRECONDITION

debugging - 如何可视化 TensorFlow Estimator 权重?

python - 使用本地数据在 EC2 上训练模型

machine-learning - AI Platform(ML Engine)如何为作业分配资源?