使用边缘部署的AutoML视觉模型时是否可以传递参数?

时间:2020-10-08 23:19:19

标签: google-cloud-platform google-cloud-automl 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())

但是,响应包含的预测比我想要的要多(即使我只希望5-10,也有40个预测)。我以为我可以向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甚至会接受它们?

2 个答案:

答案 0 :(得分:0)

您应该尝试以下操作:

{
   "instances":[
      {
         "image_bytes":{
            "b64":"/9j/4AAQSkZJRgABAQ....ABAAD2P//Z"
         },
         "params":{
            "maxBoundingBoxCount":"100"
         }
      }
   ]
}

documentation显示了一个示例。

答案 1 :(得分:0)

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

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