向 Tensorflow Serving 发出 Postman 请求预测 REST API

Making a Postman request to Tensorflow Serving predict REST API

Windows 10 和 Docker tensorflow/serving。在构造 JSON 请求预测时遇到问题,希望有人能帮助我。

我尝试了不同格式的json,其中none成功了。

有关 Tensorflow Serving 服务的模型的信息:

http://localhost:8501/v1/models/my_model/metadata:

{
    "model_spec": {
        "name": "my_model",
        "signature_name": "",
        "version": "1"
    },
    "metadata": {
        "signature_def": {
            "signature_def": {
                "__saved_model_init_op": {
                    "inputs": {},
                    "outputs": {
                        "__saved_model_init_op": {
                            "dtype": "DT_INVALID",
                            "tensor_shape": {
                                "dim": [],
                                "unknown_rank": true
                            },
                            "name": "NoOp"
                        }
                    },
                    "method_name": ""
                },
                "serving_default": {
                    "inputs": {
                        "lstm_input": {
                            "dtype": "DT_FLOAT",
                            "tensor_shape": {
                                "dim": [
                                    {
                                        "size": "-1",
                                        "name": ""
                                    },
                                    {
                                        "size": "50",
                                        "name": ""
                                    },
                                    {
                                        "size": "5",
                                        "name": ""
                                    }
                                ],
                                "unknown_rank": false
                            },
                            "name": "serving_default_lstm_input:0"
                        },
                        "tech_input": {
                            "dtype": "DT_FLOAT",
                            "tensor_shape": {
                                "dim": [
                                    {
                                        "size": "-1",
                                        "name": ""
                                    },
                                    {
                                        "size": "1",
                                        "name": ""
                                    }
                                ],
                                "unknown_rank": false
                            },
                            "name": "serving_default_tech_input:0"
                        }
                    },
                    "outputs": {
                        "dense_out": {
                            "dtype": "DT_FLOAT",
                            "tensor_shape": {
                                "dim": [
                                    {
                                        "size": "-1",
                                        "name": ""
                                    },
                                    {
                                        "size": "1",
                                        "name": ""
                                    }
                                ],
                                "unknown_rank": false
                            },
                            "name": "StatefulPartitionedCall:0"
                        }
                    },
                    "method_name": "tensorflow/serving/predict"
                }
            }
        }
    }
}

当 post 请求时,我认为 JSON 正文是正确的(但它不是):

{ 
   "inputs":{ 
      "lstm_input":[ 
         [ 
            0.178988,
            0.172787,
            0.182751,
            0.177997,
            0.138975
         ],
         .. ommitted, 50 in total ..
         [ 
            0.248429,
            0.244069,
            0.248119,
            0.245594,
            0.404607
         ]
      ],
      "tech_input":[ 
         [ 
            0.256009
         ]
      ]
   }
}

我期待返回一个预测(浮点数),但我收到了错误消息:

{"error": "transpose expects a vector of size 2. But input(1) is a vector of size 3\n\t [[{{node model_3/lstm_0/transpose}}]]"}

我现在得到了预期的预测

两个更改中的任何一个都成功了,可能是第一个。

  1. 在 lstm_input
  2. 中的 50 个元素周围添加了一个额外的 []
{ 
   "inputs":{ 
      "lstm_input":[ 
         [ 
            [ 
               0.178988,
               0.172787,
               0.182751,
               0.177997,
               0.138975
            ],
            .. ommitted, 50 in total ..
            [ 
               0.248429,
               0.244069,
               0.248119,
               0.245594,
               0.404607
            ]
         ]
      ],
      "tech_input":[ 
         [ 
            0.256009
         ]
      ]
   }
}
  1. Tensorflow 2.0,训练后直接创建 SavedModel model.save('YourSavedModelName', save_format='tf')。在我之前(失败的尝试)中,我将模型保存为 h5,重新加载它然后将其保存为 SavedModel

希望这些信息也能对其他人有所帮助