首页 > 解决方案 > 使用边缘部署的 AutoML 视觉模型时是否可以传递参数?

问题描述

我已经使用 Google Cloud Platform 训练了一个 AutoML Vision 模型。我训练了一个特定于边缘的版本,因此我可以将它部署在我自己硬件上的 docker 映像中。

我按照这里的教程说明操作: https ://cloud.google.com/vision/automl/docs/containers-gcs-tutorial

并使用示例 python 代码成功执行了一些预测:

import base64
import io
import json

import requests


def container_predict(image_file_path, image_key, port_number=8501):
    """Sends a prediction request to TFServing docker container REST API.

    Args:
        image_file_path: Path to a local image for the prediction request.
        image_key: Your chosen string key to identify the given image.
        port_number: The port number on your device to accept REST API calls.
    Returns:
        The response of the prediction request.
    """

    with io.open(image_file_path, 'rb') as image_file:
        encoded_image = base64.b64encode(image_file.read()).decode('utf-8')

    # The example here only shows prediction with one image. You can extend it
    # to predict with a batch of images indicated by different keys, which can
    # make sure that the responses corresponding to the given image.
    instances = {
            'instances': [
                    {'image_bytes': {'b64': str(encoded_image)},
                     'key': image_key}
            ]
    }

    # This example shows sending requests in the same server that you start
    # docker containers. If you would like to send requests to other servers,
    # please change localhost to IP of other servers.
    url = 'http://localhost:{}/v1/models/default:predict'.format(port_number)

    response = requests.post(url, data=json.dumps(instances))
    print(response.json())

但是,响应包含比我想要的更多的预测(40,即使我只想要 5-10)。我想我可以在 POST 请求中添加一些参数来限制预测的数量,或者根据对象检测分数进行过滤。此处概述了此类功能: https ://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.models/predict#request-body

该文档建议score_thresholdmax_bounding_box_count应该能够添加到请求 json 包中。

我试过这样的事情:

    instances = {
        'instances': [
            {'image_bytes': {'b64': str(encoded_image)},
            'key': key}
        ],
        'params': [
            {'max_bounding_box_count': 10}
        ]
    }

无济于事。

有人知道如何将参数添加到 json 请求有效负载吗?或者边缘部署的 docker 是否会接受它们?

标签: google-cloud-platformgoogle-cloud-automlautoml

解决方案


只是想知道它是否有效我正在尝试与 docker score_threshold 类似的东西,而这不会给出格式错误响应仍然超过阈值

{
    "instances": [
        {
            "image_bytes": {
                "b64": "<base64 encoded image>"
            },
            "key": "your-chosen-image-key123"
        }
    ],
                "params": {
                "score_threshold": 0.7
            }
}

推荐阅读