我在谷歌云人工智能平台上创建了2个模型,我想知道为什么我在用Python调用REST API时会得到不同的响应体?
更具体地说:
- 在第一种情况下,我得到2个字典(keys: "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")。
{'predictions': [[9.13060681e-23, 4.87227695e-23, 0.00293998793, 0.95742321, 0.0, 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]
我复制了相同的行为。从端点列表中,我已经测试了以下内容:
- europe-west1
- asia-east1
- us-east1
- australia-southeast1
它们都不像全局端点那样返回输出层的名称。
我已经通知了AI平台产品团队这一行为,并在issuetracker上创建了一个公共问题来跟踪他们的进展。
因此,我建议今后所有关于它的沟通都应该在issuetracker上进行。