Google Cloud Platform - AI Platform:为什么我在调用 API 时会得到不同的响应体?

问题描述

我在 Google Cloud AI Platform 上创建了 2 个模型,但我想知道为什么在使用 Python 调用 REST API 时会得到不同的响应主体?
更具体地说:

  • 在第一种情况下,我得到 2 个字典(键:“predictions”和“dense_1”,后者是我的 tensorflow 模型的输出名称
    {'predictions': [{'dense_1': [9.130606807519459e-23,4.872276949885089e-23,0.002939987927675247,0.957423210144043,0.0,7.103511528994133e-11,6.0420668887672946e-05,0.039576299488544464,3.989315388447379e-12,8.409963248741903e-32]}]}
  • 在第二种情况下,我得到 1 个字典(关键字:“预测”)。
    {'predictions': [[9.13060681e-23,4.87227695e-23,0.00293998793,0.95742321,7.10351153e-11,6.04206689e-05,0.0395763,3.98931539e-12,8.40996325e-32]]}

这很奇怪,因为我使用的模型与 GCS 完全相同。这两个模型之间的唯一区别是第二个模型在欧洲有一个区域端点,并且它们不在相同的机器类型上运行(但我认为这与我的问题没有联系)。

编辑:这是我的请求方法。我在案例 1 中使用了 regional_endpoint = None,在案例 2 中使用了 regional_endpoint = "europe-west1"

project_id = "my_project_id"
model_id = "my_model_id"
version_id = None # if None,default version is used
regional_endpoint = None # "europe-west1"

def predict(project,model,instances,version=None,regional_endpoint=None):
'''
Send JSON data to a deployed model for prediction.
Args:
    - project (str): Project ID where the AI Platform model is deployed
    - model (str): Model ID
    - instances (tensor): model's expected inputs
    - version (str): Optional. Version ID
    - regional_endpoint (str): Optional. See https://cloud.google.com/dataflow/docs/concepts/regional-endpoints
Returns:
    - dictionary of prediction results

'''
input_data_json = {"signature_name": "serving_default","instances": instances.tolist()}

model_path = "projects/{}/models/{}".format(project_id,model_id)
if version is not None:
    model_path += "/versions/{}".format(version)

if regional_endpoint is not None:
    endpoint = 'https://{}-ml.googleapis.com'.format(regional_endpoint)
    regional_endpoint = ClientOptions(api_endpoint=endpoint)

ml_ressource = googleapiclient.discovery.build("ml","v1",client_options=regional_endpoint).projects()

request = ml_ressource.predict(name=model_path,body=input_data_json)
response = request.execute()

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

return response["predictions"]

我使用 gcloud 命令得到相同的结果:

$ gcloud ai-platform predict --model=my_model_id --json-request=data.json --region=europe-west1
Using endpoint [https://europe-west1-ml.googleapis.com/]
[[5.64439188e-06,1.11136234e-09,4.66703168e-05,1.34729596e-08,2.34136132e-05,1.52856941e-07,0.999924064,3.328397e-10,3.32789263e-08,3.37864092e-09]]

$ gcloud ai-platform predict --model=my_model_id --json-request=data.json
Using endpoint [https://ml.googleapis.com/]
DENSE_1
[5.644391876558075e-06,1.1113623354930269e-09,4.6670316805830225e-05,1.3472959636828818e-08,2.341361323487945e-05,1.528569413267178e-07,0.9999240636825562,3.328397002455574e-10,3.327892628135487e-08,3.378640922591103e-09]

解决方法

我重现了相同的行为。
从端点列表中,我已经测试了以下内容:

  • 欧洲西部1
  • asia-east1
  • us-east1
  • 澳大利亚-东南1

并且它们都没有像全局端点那样返回输出层的名称。

我已将此行为告知 AI Platform 产品团队,并在 issuetracker 上创建了一个 public issue 以跟踪他们的进度。
因此,我建议以后所有关于它的交流都应该在 issuetracker 上进行。