【发布时间】:2022-06-27 19:07:37
【问题描述】:
我在为 TensorFlow 对象检测模型提供服务时遇到问题。我从 tensorflow 模型存储库中训练了一个模型,并设置了一个 tensorflow 服务实例。但是当我提出请求时,维度存在问题。我正在使用 tolist() 方法将图像的 numpy 数组转换为 json 编码器可以使用的东西。 tolist() 函数似乎通过使列表相互递归来维护 numpy 数组的结构,所以我不确定 tf-serving 在哪里得到一个形状为 [339450,3] 的张量。提出请求时是否必须指定图像的形状?
错误:
Data: {"signature_name": "serving_default", "instances": ... 58, 63], [35, 59, 63], [37, 58, 63], [43, 67, 71]]]}
{'error': 'Specified a list with shape [?,?,3] from a tensor with shape [339450,3]\n\t [[{{function_node __inference_call_func_9686}}{{node map/TensorArrayUnstack/TensorListFromTensor}}]]'}
发出请求的代码:
import requests
import json
from PIL import Image
import numpy
# Load image
img = Image.open("Hilarious-Car-License-Plates-1.jpg")
img_np = numpy.array(img.getdata())
img_np.resize(tuple([1] + list(img_np.shape)))
data = json.dumps({"signature_name": "serving_default", "instances": img_np.tolist()})
print('Data: {} ... {}'.format(data[:50], data[len(data)-52:]))
headers = {"content-type": "application/json"}
json_response = requests.post('http://localhost:8501/v1/models/plate_detect:predict', data=data, headers=headers)
response = json.loads(json_response.text)
print(response)
模型元数据:
{
"model_spec":{
"name": "plate_detect",
"signature_name": "",
"version": "1"
}
,
"metadata": {"signature_def": {
"signature_def": {
"serving_default": {
"inputs": {
"input_tensor": {
"dtype": "DT_UINT8",
"tensor_shape": {
"dim": [
{
"size": "1",
"name": ""
},
{
"size": "-1",
"name": ""
},
{
"size": "-1",
"name": ""
},
{
"size": "3",
"name": ""
}
],
"unknown_rank": false
},
"name": "serving_default_input_tensor:0"
}
},
"outputs": {
"detection_boxes": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "1",
"name": ""
},
{
"size": "100",
"name": ""
},
{
"size": "4",
"name": ""
}
],
"unknown_rank": false
},
"name": "StatefulPartitionedCall:1"
},
"raw_detection_boxes": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "1",
"name": ""
},
{
"size": "1917",
"name": ""
},
{
"size": "4",
"name": ""
}
],
"unknown_rank": false
},
"name": "StatefulPartitionedCall:6"
},
"detection_scores": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "1",
"name": ""
},
{
"size": "100",
"name": ""
}
],
"unknown_rank": false
},
"name": "StatefulPartitionedCall:4"
},
"raw_detection_scores": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "1",
"name": ""
},
{
"size": "1917",
"name": ""
},
{
"size": "2",
"name": ""
}
],
"unknown_rank": false
},
"name": "StatefulPartitionedCall:7"
},
"detection_anchor_indices": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "1",
"name": ""
},
{
"size": "100",
"name": ""
}
],
"unknown_rank": false
},
"name": "StatefulPartitionedCall:0"
},
"detection_multiclass_scores": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "1",
"name": ""
},
{
"size": "100",
"name": ""
},
{
"size": "2",
"name": ""
}
],
"unknown_rank": false
},
"name": "StatefulPartitionedCall:3"
},
"detection_classes": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "1",
"name": ""
},
{
"size": "100",
"name": ""
}
],
"unknown_rank": false
},
"name": "StatefulPartitionedCall:2"
},
"num_detections": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "1",
"name": ""
}
],
"unknown_rank": false
},
"name": "StatefulPartitionedCall:5"
}
},
"method_name": "tensorflow/serving/predict"
},
"__saved_model_init_op": {
"inputs": {},
"outputs": {
"__saved_model_init_op": {
"dtype": "DT_INVALID",
"tensor_shape": {
"dim": [],
"unknown_rank": true
},
"name": "NoOp"
}
},
"method_name": ""
}
}
}
}
}
【问题讨论】:
标签: tensorflow tensorflow2.0 object-detection tensorflow-serving object-detection-api