【问题标题】:Error performing online Prediction Google ML deployed model执行在线预测 Google ML 部署模型时出错
【发布时间】:2019-11-20 21:52:13
【问题描述】:

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

 Expected float32, got *******

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

我认为问题在于数据的格式。我已按照https://cloud.google.com/ml-engine/docs/online-predicthttps://cloud.google.com/ml-engine/docs/v1/predict-request 的说明进行操作。我曾尝试发送各种版本,例如

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

它应该将 3D 数组作为输入 - 基本上模型的输入是 150x150x3 数组,其中每个像素的值从 0 到 1.0(浮点数)。因为模型需要浮点输入,所以我无法将原始 150x150x3 像素值转换为 base64。所以,我使用上面链接中的解释来发送 3D 数组。我尝试了很多不同的方法,现在不知道该怎么做。 这是一张图像,显示了代表模型需要的缩放输入输入的 3D 字符串数组

注意:我正在从 JSON file 中读取数据——这与我在下面的命令行成功 gcloud 调用中使用的相同,以执行预测。这是一张说明内容的图片。

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

【问题讨论】:

    标签: android tensorflow google-cloud-ml


    【解决方案1】:

    这是一个常见的问题:

    gcloud 所需的格式与发送到服务的 HTTP 请求的正文之间存在细微差别。我们理解这令人困惑,我们将努力解决未来的困惑。以下是帮助您入门的简短说明。

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

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

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

    <instance 1>
    <instance 2>
    

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

    a) 由于使用 gcloud 命令可以正常工作,因此我将使用 AI Platform UI 来测试您的 JSON 输入,类似于 API 将使用的。

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

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

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

    在这种情况下你有

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

    b) 您需要将conv2d_input 替换为instances 并添加额外的[]: 我用我自己的模型测试了你的input,效果很好。

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

    我正常使用这段代码:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 2018-10-03
      • 2019-02-16
      • 2022-01-02
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多