简体   繁体   English

向TensorFlow服务的预测API的请求返回错误“缺少输入”

[英]Requests to TensorFlow serving's predict API returns error “Missing inputs”

I have trained a simple regression model to fit a linear function with the following equation: y = 3x + 1 . 我已经训练了一个简单的回归模型,可以使用以下方程式拟合线性函数: y = 3x + 1 For testing purposes, I saved the model as checkpoints, so that I could resume training and wouldn't have to start from scratch every time. 为了进行测试,我将模型另存为检查点,这样我就可以继续训练,而不必每次都从头开始。

Now I want to make this model available via TF serving. 现在,我想通过TF服务提供此模型。 For this reason, I had to convert it into the SavedModel format of tensorflow via this script: 因此,我必须通过以下脚本将其转换为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')

This creates a folder (as expected) with the contents: 这将创建一个包含以下内容的文件夹(按预期):

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

To serve this with tf serving and docker, I pulled the tensorflow/serving image from docker and ran the container via the command: 为了通过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

This seems to execute without problems, as I get a lot of infos. 执行此操作似乎没有问题,因为我得到了很多信息。 In the last line of the output it says 在输出的最后一行说

[evhttp_server.cc : 237] RAW: Entering the event loop ... [evhttp_server.cc:237] RAW:进入事件循环...

I guess the server is waiting for requests. 我猜服务器正在等待请求。 Now, when I try to send a request to it via curl, I get an error: 现在,当我尝试通过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" } {“ error”:“缺少\\'inputs \\'或\\'instances \\'键”}

What am I doing wrong? 我究竟做错了什么? The model works when I send dummy values via the saved_model_cli . 当我通过saved_model_cli发送伪值时,该模型即可工作。

Looks like the body of the POST request should be modified. 看起来POST请求的主体应该被修改。 According to documentation the format should be 根据文档 ,格式应为

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

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

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