TensorFlow服务“回归”方法REST API

问题描述 投票:1回答:1

我在TensorFlow服务服务器上使用“回归” API时遇到麻烦。请参阅下面的要点链接以更舒适地阅读。https://gist.github.com/krikit/32c918cc03b52315ade562267a91fa6b

我制作了一个简单的keras模型,该模型具有两个输入(x1,x2)并显示单个输出值(y)。通过这种模型,当我使用“回归” REST API时,我从TensorFlow服务服务器得到了错误结果。

# the model
inputs = {
    'x1': tf.keras.layers.Input(shape=(1, ), name='x1', dtype='float32'),
    'x2': tf.keras.layers.Input(shape=(1, ), name='x2', dtype='float32'),
}
concat = tf.keras.layers.Concatenate(name='concat')([inputs['x1'], inputs['x2']])
dense = tf.keras.layers.Dense(10, use_bias=True, activation='relu', name='dense')(concat)
outputs = tf.keras.layers.Dense(1, use_bias=True, activation='sigmoid', name='y')(dense)
model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='SGD', loss='binary_crossentropy')
model.summary()


# training
num_exam = 10000
model.fit({'x1': np.random.randn(num_exam), 'x2': np.random.rand(num_exam)}, np.random.randn(num_exam))


# save
input_infos = {name: tf.saved_model.build_tensor_info(tensor) for name, tensor in model.input.items()}
output_infos = {'y': tf.saved_model.build_tensor_info(model.outputs[0])}
signature = tf.saved_model.build_signature_def(
    inputs=input_infos,
    outputs=output_infos,
    method_name=tf.saved_model.signature_constants.REGRESS_METHOD_NAME
)
print(signature)

model_dir = './random_regression/1'
shutil.rmtree(model_dir, ignore_errors=True)
model_builder = tf.saved_model.builder.SavedModelBuilder(model_dir)
model_builder.add_meta_graph_and_variables(
    tf.keras.backend.get_session(),
    tags=[tf.saved_model.tag_constants.SERVING, ],
    signature_def_map={tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature}
)
model_builder.save()

保存模型后,在“ saved_model_cli”工具的输出中看起来还可以。

$ saved_model_cli show --dir ./random_regression/1 --all

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['x1'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: x1:0
    inputs['x2'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: x2:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['y'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y/Sigmoid:0
  Method name is: tensorflow/serving/regress

我开始使用该模型为服务器提供服务后,我使用“回归”方法测试了REST API。但是我遇到了如下错误,

$ curl -X POST -H "Content-Type: application/json" http://localhost:8501/v1/models/random_regression/versions/1:regress -d '
{
  "examples": [
    {
      "x1": [0.1],
      "x2": [0.2]
    },
    {
      "x1": [0.1],
      "x2": [0.3]
    }
  ]
}'

Response:
{ "error": "Expected one input Tensor." }

尽管我做了一个回归签名,但预测API也可用。

$ curl -X POST -H "Content-Type: application/json" http://localhost:8501/v1/models/random_regression/versions/1:predict -d '
{
  "instances": [
    {
      "x1": [0.1],
      "x2": [0.2]
    },
    {
      "x1": [0.1],
      "x2": [0.3]
    }
  ]
}'

Response:
{
    "predictions": [[0.143165469], [0.124352224]
    ]
}

我使用“回归”方法的原因是我需要如下所示的“上下文”字段。

$ curl -X POST -H "Content-Type: application/json" http://localhost:8501/v1/models/random_regression/versions/1:regress -d '
{
  "context": {
    "x1": [0.1]
  },
  "examples": [
    {
      "x2": [0.2]
    },
    {
      "x2": [0.3]
    }
  ]
}'

Response:
{ "error": "Expected one input Tensor." }

我很抱歉LONG ~~~问题,但是请问有人可以帮助我吗?

tensorflow-serving
1个回答
0
投票

只是为了确保我正确理解了这个问题。当您使用预测API时,它不会有任何错误,但是您需要使用回归方法,因为您需要上下文字段?

您使用的是TF 2.0吗?

© www.soinside.com 2019 - 2024. All rights reserved.