请求 TensorFlow 服务的预测 API returns 错误 "Missing inputs"

Requests to TensorFlow serving's predict API returns error "Missing inputs"

我已经训练了一个简单的回归模型来拟合具有以下等式的线性函数:y = 3x + 1。出于测试目的,我将模型保存为检查点,这样我就可以恢复训练,而不必每次都从头开始。

现在我想通过 TF 服务提供此模型。为此,我不得不通过这个脚本将其转换为tensorflow的SavedModel格式:

import tensorflow as tf
import restoretest as rt  ## just the module that contains the linear model

tf.reset_default_graph()        

latest_checkpoint = tf.train.latest_checkpoint('path/to/checkpoints')
model = rt.LinearModel()
saver = tf.train.Saver()

export_path = 'path/to/export/folder'

with tf.Session() as sess:

    if latest_checkpoint:
        saver.restore(sess, latest_checkpoint)
    else:
        raise ValueError('No checkpoint file found') 

    print('Exporting trained model to', export_path)

    builder = tf.saved_model.builder.SavedModelBuilder(export_path)

    ## define inputs and outputs

    tensor_info_x = tf.saved_model.utils.build_tensor_info(model.x)
    tensor_info_y = tf.saved_model.utils.build_tensor_info(model.y_pred)

    prediction_signature = (
            tf.saved_model.signature_def_utils.build_signature_def(
                    inputs={'xvals': tensor_info_x},
                    outputs={'yvals': tensor_info_y},
                    method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))


    builder.add_meta_graph_and_variables(sess, 
                                         [tf.saved_model.tag_constants.SERVING],
                                         signature_def_map={tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: prediction_signature},
                                         main_op=tf.tables_initializer(),
                                         strip_default_attrs=True)

    builder.save()

    print('Done exporting')

这将创建一个包含以下内容的文件夹(如预期的那样):

export_folder
    |-saved_model.pb
    |-variables
        |-variables.index
        |-variables.data-00000-of-00001

为了使用 tf 服务和 docker 提供此服务,我通过以下命令从 docker 和 运行 容器中提取了 tensorflow/serving 图像:

sudo docker run -p 8501:8501 --mount type=bind,source=path/to/export/folder,target=models/linear -e MODEL_NAME=linear -t tensorflow/serving

这似乎执行起来没有问题,因为我得到了很多信息。在输出的最后一行它说

[evhttp_server.cc : 237] RAW: Entering the event loop ...

我想服务器正在等待请求。现在,当我尝试通过 curl 向它发送请求时,出现错误:

curl -d '{"xvals": [1.0 2.0 5.0]}' -X POST http://localhost:8501/v1/models/linear:predict

{ "error": "Missing \'inputs\' or \'instances\' key" }

我做错了什么?当我通过 saved_model_cli.

发送虚拟值时,该模型有效

看起来 POST 请求的正文应该修改。根据 documentation 格式应该是

{ "inputs": {"xvals": [1.0 2.0 5.0]} }