简体   繁体   English

如何将多个二维数组作为输入传递给为 api 服务的 tensorflow?

[英]How to pass multiple 2d array as input to tensorflow serving api?

I have a tensorflow model which takes two 2d arrays as input.我有一个 tensorflow model 需要两个 2d arrays 作为输入。

This is how I trained the model.这就是我训练 model 的方式。

x.shape == (100, 250)
y.shape == (100, 10)

model.fit([x,y], y_train)

Now I'm using tensorflow serving API for deploying into production.现在我使用 tensorflow 服务 API 部署到生产中。 Now when I tried to make a api request for predictions I'm getting an error现在,当我尝试发出 api 请求以进行预测时,出现错误

"{'error': 'instances is a plain list, but expecting list of objects as multiple input tensors required as per tensorinfo_map'}"

data1 = json.dumps({"signature_name": "serving_default",
                   "instances": [x.tolist(), y.tolist()]})
# both x and y are numpy 2d array 

json_response = requests.post('http://44.287.13.8:9000/v1/models/context_model/versions/1:predict',
                               data=data1, headers=headers)

pred = json.loads(json_response.text)

print(pred)
{'error': 'instances is a plain list, but expecting list of objects as multiple input tensors required as per tensorinfo_map'}

I had a quite similar problem.我有一个非常相似的问题。 I wanted to test my saved model in combination with the serving API.我想结合服务 API 测试我保存的 model。 First I wrote the following POST command to get the prediction: curl -g -d "{""instances""":" [[[158, 194, 8102, 5294, 15.404460999999998, 47.241882000000004, 1]]]}" -X POST http://localhost:8501/v1/models/my_model:predict .首先,我编写了以下 POST 命令来获得预测: curl -g -d "{""instances""":" [[[158, 194, 8102, 5294, 15.404460999999998, 47.241882000000004, 1]]]}" -X POST http://localhost:8501/v1/models/my_model:predict With this input I got the same error as you.有了这个输入,我得到了和你一样的错误。 As mentioned here https://www.tensorflow.org/tfx/serving/api_rest#predict_api , you have to serve the input data in the following way: curl -d "{"""instances""": [{"""NAME1W1""": [158], """NAME1W2""": [194], """ZIP""": [""8102""], """STREETW""": [5294], """LONGITUDE""": 15.404460999999998, """LATITUDE""": 47.241882000000004,"""ASG""": [1] }]}" -X POST http://localhost:8501/v1/models/my_model:predict . As mentioned here https://www.tensorflow.org/tfx/serving/api_rest#predict_api , you have to serve the input data in the following way: curl -d "{"""instances""": [{"""NAME1W1""": [158], """NAME1W2""": [194], """ZIP""": [""8102""], """STREETW""": [5294], """LONGITUDE""": 15.404460999999998, """LATITUDE""": 47.241882000000004,"""ASG""": [1] }]}" -X POST http://localhost:8501/v1/models/my_model:predict Don't get confused of the “””, I had to use them because I am using curl on Windows and not on Linux.不要对“””感到困惑,我必须使用它们,因为我在 Windows 上使用 curl 而不是在 Linux 上使用。

When you have multiple inputs, you need to know the names of inputs from GraphDef and pass a list of dict to tensorflow serving当您有多个输入时,您需要知道来自 GraphDef 的输入名称并将 dict 列表传递给 tensorflow serving

First, make sure that you have correct input names by running this command in terminal首先,通过在终端中运行此命令,确保您具有正确的输入名称

saved_model_cli show --dir /home/your_user_name/path/to/your/saved_model/1 --all

In my case, I get the following as part of the output.在我的情况下,我得到以下作为 output 的一部分。 What is important is that the names of the inputs are input_1 and input_2 , again in my case.重要的是输入的名称是input_1input_2 ,在我的例子中也是如此。

signature_def['serving_default']: The given SavedModel SignatureDef contains the following input(s): inputs['input_1'] tensor_info: dtype: DT_FLOAT shape: (-1, 2) name: serving_default_input_1:0 inputs['input_2'] tensor_info: dtype: DT_FLOAT shape: (-1, 2) name: serving_default_input_2:0 signature_def['serving_default']:给定的 SavedModel SignatureDef 包含以下输入:inputs['input_1'] tensor_info:dtype:DT_FLOAT 形状:(-1, 2) name: serving_default_input_1:0 inputs['input_2'] tensor_info : dtype: DT_FLOAT 形状: (-1, 2) 名称: serving_default_input_2:0

Also, as you can see, each input takes 2 fields, respectively.此外,如您所见,每个输入分别需要 2 个字段。

Finally, I can send an http request for a prediction with input_1 of [1.,0.] and input_2 of [0.36708057, 0.66139287]最后,我可以发送一个 http 请求进行预测,其中input_1为 [1.,0.] 和input_2为 [0.36708057, 0.66139287]

import requests
import json

data = json.dumps({"signature_name":"serving_default","instances":[{'input_1':[1., 0.],'input_2':[0.36708057, 0.66139287]}]})
headers = {"content-type":"application/json"}
json_response = requests.post('http://localhost:8501/v1/models/test:predict',data=data,headers=headers)
print(json.loads(json_response.text))

The output was {'predictions':[[0.315578]]} output 是{'predictions':[[0.315578]]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM