簡體   English   中英

向TensorFlow服務的預測API的請求返回錯誤“缺少輸入”

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

我已經訓練了一個簡單的回歸模型,可以使用以下方程式擬合線性函數: y = 3x + 1 為了進行測試,我將模型另存為檢查點,這樣我就可以繼續訓練,而不必每次都從頭開始。

現在,我想通過TF服務提供此模型。 因此,我必須通過以下腳本將其轉換為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:進入事件循環...

我猜服務器正在等待請求。 現在,當我嘗試通過curl向其發送請求時,出現錯誤:

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

{“ error”:“缺少\\'inputs \\'或\\'instances \\'鍵”}

我究竟做錯了什么? 當我通過saved_model_cli發送偽值時,該模型即可工作。

看起來POST請求的主體應該被修改。 根據文檔 ,格式應為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM