首页 > 解决方案 > TensorFlow:如何为 SavedModel 格式化 JSON,期望字符串格式为 3 个整数?

问题描述

我无法使用 Postman 将正确的预测 POST 请求发送到在 Docker 容器中使用 TensorFlow Serving 远距离运行的模型。

该模型响应 GET 请求,因此我知道它有效并且正在响应。该算法需要将 3 个 int ID 值作为类别处理才能起作用,但 SignatureDef 需要输入字符串,如下所示:

The given SavedModel SignatureDef contains the following input(s):
  inputs['inputs'] tensor_info:
      dtype: DT_STRING
      shape: (-1)
      name: input_example_tensor:0
The given SavedModel SignatureDef contains the following output(s):
  outputs['classes'] tensor_info:
      dtype: DT_STRING
      shape: (-1, 2166)
      name: linear/head/Tile:0
  outputs['scores'] tensor_info:
      dtype: DT_FLOAT
      shape: (-1, 2166)
      name: linear/head/predictions/probabilities:0
Method name is: tensorflow/serving/classify

我已经尝试按照此处 RESTful API 教程中显示的 JSON 格式进行操作:https ://www.tensorflow.org/tfx/serving/api_rest#example 但是除了错误响应之外,什么也没有发回给我对我了解正确的语法没有多大帮助。在任何情况下,它都需要是这样的:

{
 "inputs": ['int1': 1, 'int2': 2, 'int3': 3]
}

我希望它会向我发回教程中所示的内容:

    "predictions": [3.5, 4.0, 5.5]

相反,我得到“错误”:无论它不喜欢我这次发送的内容。任何帮助,将不胜感激

标签: pythontensorflowtensorflow-servingtensorflow-estimator

解决方案


在咨询了其他几个人之后,我们设法找到了解决方案。它期待一个字符串列表,每个字符串都包含相关的字典项,并在必要时为标签转义引号。

{
    "instances": 
        ["\"int1\": [1] , \"int2\" : [2], \"int3\":[3]"]

}

推荐阅读