【问题标题】:ML Engine Online Prediction - Unexpected tensor name: valuesML Engine 在线预测 - 意外的张量名称:值
【发布时间】:2018-08-20 11:40:44
【问题描述】:

尝试对我的 ML Engine 模型进行在线预测时出现以下错误。 关键的“价值观”不正确。 (请参阅图像上的错误。) enter image description here

我已经用 RAW 图像数据进行了测试:{"image_bytes":{"b64": base64.b64encode(jpeg_data)}} & 将数据转换为 numpy 数组。

目前我有以下代码:

from googleapiclient import discovery
import base64
import os
from PIL import Image
import json
import numpy as np

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/jacob/Desktop/******"

def predict_json(project, model, instances, version=None):
    """Send json data to a deployed model for prediction.

    Args:
        project (str): project where the Cloud ML Engine Model is deployed.
        model (str): model name.
        instances ([Mapping[str: Any]]): Keys should be the names of Tensors
            your deployed model expects as inputs. Values should be datatypes
            convertible to Tensors, or (potentially nested) lists of datatypes
            convertible to tensors.
        version: str, version of the model to target.
    Returns:
        Mapping[str: any]: dictionary of prediction results defined by the
            model.
    """
    # Create the ML Engine service object.
    # To authenticate set the environment variable
    # GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
    service = discovery.build('ml', 'v1')
    name = 'projects/{}/models/{}'.format(project, model)

    if version is not None:
        name += '/versions/{}'.format(version)

    response = service.projects().predict(
        name=name,
        body={'instances': instances}
    ).execute()

    if 'error' in response:
        raise RuntimeError(response['error'])

    return response['predictions']


savepath = 'upload/11277229_F.jpg'

img = Image.open('test/01011000/11277229_F.jpg')
test = img.resize((299, 299))
test.save(savepath)

img1 = open(savepath, "rb").read()

def load_image(filename):
    with open(filename) as f:
        return np.array(f.read())

predict_json('image-recognition-25***08', 'm500_200_waug', [{"values": str(base64.b64encode(img1).decode("utf-8")), "key": '87'}], 'v1')

【问题讨论】:

    标签: google-cloud-platform google-cloud-ml google-prediction


    【解决方案1】:

    错误消息本身表明(正如您在问题中指出的那样),键“值”不是模型中指定的输入之一。要检查模型的输入,请使用saved_model_cli show --all --dir=/path/to/model。这将向您显示输入名称的列表。您需要使用正确的名称。

    也就是说,似乎还有另一个问题。从问题中不清楚您的模型期望什么类型的输入,尽管它可能是以下两件事之一:

    1. 整数或浮点数矩阵
    2. 带有原始图像文件的字节字符串 内容。

    确切的解决方案将取决于您导出的模型使用的是上述哪个。 saved_model_cli 将根据输入的类型和形状提供帮助。它将分别是 DT_FLOAT32(或其他一些 int/float 类型)和 [NONE, 299, 299, CHANNELS]DT_STRING[NONE]

    如果您的模型是类型 (1),那么您需要发送一个整数/浮点矩阵(不使用 base64 编码):

    predict_json('image-recognition-25***08', 'm500_200_waug', [{CORRECT_INPUT_NAME: load_image(savepath).tolist(), "key": '87'}], 'v1')
    

    注意使用tolist 将numpy 数组转换为列表列表。

    在类型(2)的情况下,您需要通过添加 {"b64": ...}: 来告诉服务您有一些 base64 数据:

    predict_json('image-recognition-25***08', 'm500_200_waug', [{CORRECT_INPUT_NAME: {"b64": str(base64.b64encode(img1).decode("utf-8"))}, "key": '87'}], 'v1')
    

    当然,所有这一切都取决于为CORRECT_INPUT_NAME 使用正确的名称。

    最后一点,我假设您的模型实际上确实有 key 作为附加输入,因为您将它包含在您的请求中;同样,这都可以根据saved_model_cli show 的输出进行验证。

    【讨论】:

      【解决方案2】:

      我以前也遇到过这个错误。如果有人遇到此错误,并使用 gcloud。

      张量自动称为 csv_rows。例如,这对我现在有效

      "instances": [{
          "csv_row": "STRING,7,4.02611534,9,14,0.66700000,0.17600000,0.00000000,0.00000000,1299.76500000,57",
          "key": "0"
      }]
      

      【讨论】:

        猜你喜欢
        • 2018-06-23
        • 2019-08-24
        • 1970-01-01
        • 2019-02-17
        • 2017-12-29
        • 2018-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多