我已经使用谷歌云平台训练了一个AutoML愿景模型。我训练了一个特定于边缘的版本,这样我就可以在我自己的硬件上将其部署到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_threshold或max_bounding_box_count添加到请求json包中。
我尝试了这样的东西:
instances = {
'instances': [
{'image_bytes': {'b64': str(encoded_image)},
'key': key}
],
'params': [
{'max_bounding_box_count': 10}
]
}无济于事。
有人知道如何将参数添加到json请求负载中吗?或者边缘部署的docker是否会接受它们?
发布于 2020-10-14 05:52:09
你应该尝试一下类似这样的东西:
{
"instances":[
{
"image_bytes":{
"b64":"/9j/4AAQSkZJRgABAQ....ABAAD2P//Z"
},
"params":{
"maxBoundingBoxCount":"100"
}
}
]
}此documentation显示了一个示例。
发布于 2021-05-03 20:03:27
只是想知道它是否工作,我正在尝试与docker score_threshold类似的东西,虽然这不会给出格式错误,但响应仍然超过阈值
{
"instances": [
{
"image_bytes": {
"b64": "<base64 encoded image>"
},
"key": "your-chosen-image-key123"
}
],
"params": {
"score_threshold": 0.7
}
}https://stackoverflow.com/questions/64271887
复制相似问题