执行在线预测 Google ML 部署模型时出错 [英] Error performing online Prediction Google ML deployed model

查看:33
本文介绍了执行在线预测 Google ML 部署模型时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试通过 HTTP 休息请求对已部署的在线预测进行调用时遇到问题谷歌人工智能平台上的模型.无论我如何打包数据,我都会收到以下错误.错误是:

 预期的 float32,得到 *******

我已确认 Google 凭据有效,并且可以从我的 Android 应用执行查询以获取有关模型的信息,并且成功.

我认为问题在于数据的格式.我已按照

注意:我正在从

注意:我可以使用 gcloud 在命令行上执行此操作,如下所示

解决方案

这是一个常见问题:

gcloud 所需的格式与发送到服务的 HTTP 请求正文之间略有不同.我们理解这令人困惑,我们将努力解决未来的困惑.以下是帮助您入门的简要说明.

发送到 API 的请求正文具有以下形式:

{"instances": [, , ...]}

gcloud 中使用的文件只是一个以换行符分隔的实例列表:

<实例2>

我查看了您的文件,我有 2 个建议:

a) 由于使用 gcloud 命令运行良好,我将使用 AI Platform UI 来测试您的 JSON 输入,类似于 API 将使用的.

注意:复制粘贴输入后使用测试

按照文档中的说明,我们需要这样的东西:

<代码>{实例":[<对象>...]}

在这种情况下你有

<代码>{conv2d_input":[<对象>...]}

b) 您需要将 conv2d_input 替换为 instances 并添加额外的 []:我用我的模型测试了你的 input拥有并运行良好.

<代码>{实例":[[[[0.23137255012989044, 0.27450981736183167, 0.250980406999588],...,[0.2980392277240753, 0.43529412150382996, 0.2078431397676468]]]]}

我通常使用此代码:

将 numpy 导入为 np导入json从 PIL 导入图像INPUT_FILE = 'image.jpg'OUTPUT_FILE = 'image_array.json'def convert_to_json(image_file):"""打开图像,将其转换为 numpy 并创建 JSON 请求"""img = Image.open(image_file).resize((224, 224))img_array = np.array(img)predict_request = {实例":[img_array.tolist()]}使用 open(OUTPUT_FILE, 'w') 作为 output_file:json.dump(predict_request, output_file)返回 predict_request预测数据 = convert_to_json(INPUT_FILE)

I am having a problem trying to invoke via HTTP rest request an online Prediction to a deployed model on Google AI Platform. I get the following error regardless of how I package the data. The error is:

 Expected float32, got *******

I have confirmed the Google Credentials work and I can perform a query from my Android App to get information about the model and that is successful.

The issue I believe is with formatting of the data. I have followed the instructions at https://cloud.google.com/ml-engine/docs/online-predict and also at https://cloud.google.com/ml-engine/docs/v1/predict-request. I have tried to send various versions like

{"instances": [xxx]} where have formatted xxx in many different ways.

It should take as input a 3D array -basically the input to the model is a 150x150x3 array where each pixel has a value from 0 to 1.0 (float). Because the model wants a float input I can not convert the original 150x150x3 pixel values to base64. So, I am using the explanation at the link above for sending in a 3D array. I have tried so many different ways and do not know what to do at this point. Here is an image showing the 3D string array representing the scaled input input the model needs

Note: I am reading the data in from a JSON file -the same one I use below in a command line successful gcloud call to perform prediction. Here is an image illustrating the contents.

Note: I am able to do this on the command line using gcloud as shown here

解决方案

This is a common problem:

There is a slight difference between the format required for gcloud and the body of the HTTP requests sent to the service. We understand it is confusing and we will work towards addressing the confusion in the future. Here's a brief explanation to help you get started.

The body of the request sent to the API has the form:

{"instances": [<instance 1>, <instance 2>, ...]}

The file used in gcloud is simply a newline-separated list of instances:

<instance 1>
<instance 2>

I looked at your file, I have 2 recommendations:

a) Since it works fine using gcloud command, I would use AI Platform UI to test your JSON input similar as the API will use.

Note: Use test after you copy and paste the input

As specified in documentation we need something like this:

{
    "instances": [
        <object>
        ...
    ]
}

In this case you have

{ 
    "conv2d_input": [
         <object>
     ...
    ]
}

b) You need to replace conv2d_input to instances and add extra []: I tested your input with a model of my own and works fine.

{  
    "instances": 
     [
       [[[0.23137255012989044, 0.27450981736183167, 0.250980406999588], 
       ...
       ,[0.2980392277240753, 0.43529412150382996, 0.2078431397676468]]]
     ]
}

I use this code normally:

import numpy as np
import json
from PIL import Image

INPUT_FILE = 'image.jpg'
OUTPUT_FILE = 'image_array.json'

def convert_to_json(image_file):
 """Open image, convert it to numpy and create JSON request"""
 img = Image.open(image_file).resize((224, 224))
 img_array = np.array(img)
 predict_request = {"instances": [img_array.tolist()]}
 with open(OUTPUT_FILE, 'w') as output_file:
   json.dump(predict_request, output_file)
 return predict_request

prediction_data = convert_to_json(INPUT_FILE)

这篇关于执行在线预测 Google ML 部署模型时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆