【问题标题】:GCP Vertex AI Endpoint returning empty prediction arrayGCP Vertex AI Endpoint 返回空预测数组
【发布时间】:2022-08-18 00:03:09
【问题描述】:

KFP 管道作业成功执行,但在到达端点时,得到一个空的预测数组 ([])。我怀疑问题出在模型上传中,模型没有以某种方式正确注册。任何提示表示赞赏。

上传模型部署任务的代码:

    #Import a model programmatically
    model_upload = aiplatform.Model.upload(
        display_name = DISPLAY_NAME, 
        serving_container_image_uri = serving_container_image_uri,
        serving_container_health_route=\"/health_check\",
        serving_container_predict_route=\"/predict\",
        serving_container_ports=[8080],
        serving_container_environment_variables={
            \"MODEL_NAME\": MODEL_NAME,
        },       
    )

获取预测的代码:

response = endpoint.predict({\"user_id\": 150})
# response = endpoint.predict({\"instances\":{\"user_id\": 150}})
response

回复:

Prediction(predictions=[], deployed_model_id=\'4656867150235959296\', explanations=None)

    标签: python google-cloud-platform kubeflow-pipelines gcp-ai-platform-training


    【解决方案1】:

    TLDR:

    检查您的处理程序输出是否在响应中返回格式正确且没有额外斜杠 (\) 的字符串。

    或者使用raw predict 而不是predict 调用。 (guide)


    调试步骤:

    我也有这个问题,我使用 Vertex AI 自定义服务容器方法并调用 endpoint.predict(instances=[{...}]).predictions 只返回一个空列表:[]

    即使,当使用来自Python SDK 或通过REST endpointraw predict 时,它返回了data JSON 键的有效响应:

    Python SDK 响应:

    "{\"predictions\": \"[{\\\"key1\\\": \\\"string1\\\", \\\"key2\\\": [\\\"string2\\\"], \\\"key3\\\": 0.0}]\"}"
    

    REST API 响应:

    {"predictions": "[{\"key1\": \"string1\", \"key2\": [\"string2\"], \"key3\": 0.0}]"}
    

    但是,在这两种情况下,您都可以看到响应中有额外的斜杠 (\)。这意味着它是一个格式问题.


    解决方案:

    事实证明,在我的处理程序的后处理步骤中,我做了以下事情:

    prediction_result = {
                "key1": value1,
                "key2": value2,
                "key3": value3,
            }
    array = np.array([prediction_result])
    
    return json.dumps({"predictions": array.tolist()})
    

    将其更改为以下解决了我的问题:

    prediction_result = {
                "key1": value1,
                "key2": value2,
                "key3": value3,
            }
    
    return json.dumps({"predictions": [prediction_result]})
    

    然后调用endpoint.predict(instances=[{...}]).predictions 为我返回了以下列表:

    [{'key1': 'string1',
      'key2': ['string2'],
      'key3': 0.0}]
    

    修复后,来自raw predict 的响应不再包含多余的斜线 (\)。

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 1970-01-01
      • 2021-11-19
      • 2021-11-06
      • 2023-02-05
      • 1970-01-01
      • 2022-01-01
      • 2021-12-26
      • 2021-12-24
      相关资源
      最近更新 更多