我已经保存了一个Tensorflow模型,并正在使用Tensorflow Serving(tensorflow / serving:1.12.0和tensorflow / serving:1.12.0-gpu)为它服务。
我想使用Predict REST API,但是调用失败,并显示“未正确格式化base64数据”错误。
请求:
POST / v1 / models / payfraud:predict
{
"inputs": [
{
"payFraudInput": [[44.26, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]
}
]
}
响应:
400
{
"error": "JSON Value: {\n \"payFraudInput\": [\n [\n 44.26,\n 0,\n 0,\n 0,\n 0,\n 1,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0\n ]\n ]\n} not formatted correctly for base64 data"
}
模型输入期望DT_FLOAT,所以我认为我不需要base64编码。
POST / v1 / models / payfraud / versions / 1 /元数据
{
"model_spec": {
"name": "payfraud",
"signature_name": "",
"version": "1"
},
"metadata": {
"signature_def": {
"signature_def": {
"predict_fraud": {
"inputs": {
"payFraudInput": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "-1",
"name": ""
},
{
"size": "15",
"name": ""
}
],
"unknown_rank": false
},
"name": "payFraudInput:0"
}
},
"outputs": {
"payFraudOutput": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "-1",
"name": ""
},
{
"size": "2",
"name": ""
}
],
"unknown_rank": false
},
"name": "payFraudOutput:0"
}
},
"method_name": "tensorflow/serving/predict"
},
"serving_default": {
"inputs": {
"inputs": {
"dtype": "DT_STRING",
"tensor_shape": {
"dim": [],
"unknown_rank": true
},
"name": "tf_example:0"
}
},
"outputs": {
"classes": {
"dtype": "DT_STRING",
"tensor_shape": {
"dim": [
{
"size": "-1",
"name": ""
},
{
"size": "2",
"name": ""
}
],
"unknown_rank": false
},
"name": "index_to_string_Lookup:0"
},
"scores": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "-1",
"name": ""
},
{
"size": "2",
"name": ""
}
],
"unknown_rank": false
},
"name": "TopKV2:0"
}
},
"method_name": "tensorflow/serving/classify"
}
}
}
}
}
这是保存模型的方式:
prediction_signature = (
tf.saved_model.signature_def_utils.build_signature_def(
inputs={"payFraudInput": tensor_info_x},
outputs={"payFraudOutput": tensor_info_y},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))
classification_signature = (
tf.saved_model.signature_def_utils.build_signature_def(
inputs={
tf.saved_model.signature_constants.CLASSIFY_INPUTS:
classification_inputs
},
outputs={
tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES:
classification_outputs_classes,
tf.saved_model.signature_constants.CLASSIFY_OUTPUT_SCORES:
classification_outputs_scores
},
method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME))
export_path = os.path.join(tf.compat.as_bytes(export_dir), tf.compat.as_bytes("1"))
print('Exporting trained model to ', export_path)
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
builder.add_meta_graph_and_variables( sess, [tf.saved_model.tag_constants.SERVING],
signature_def_map={
'predict_fraud':
prediction_signature,
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
classification_signature,
},
main_op=tf.tables_initializer(),
strip_default_attrs=True)
builder.save()
print('Done exporting!')
尝试b64也无效:
请求
{
"inputs": [
{
"payFraudInput":{"b64":"NDQuMjYsIDAsIDAsIDAsIDAsIDEsIDAsIDAsIDAsIDAsIDAsIDAsIDAsIDA="}
}
]
}
响应
{
"error": "JSON Value: {\n \"payFraudInput\": {\n \"b64\": \"NDQuMjYsIDAsIDAsIDAsIDAsIDEsIDAsIDAsIDAsIDAsIDAsIDAsIDAsIDA=\"\n }\n} not formatted correctly for base64 data"
}
我在做什么错了?
答案 0 :(得分:0)
清理并简化了训练脚本的模型保存部分后,我得到了预测响应。
现在的保存如下:
export_path = os.path.join(tf.compat.as_bytes(export_dir), tf.compat.as_bytes("1"))
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
predict_signature_def = (
tf.saved_model.signature_def_utils.predict_signature_def({"x": X}, {"y": Y_hat}))
signature_def_map = {
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
predict_signature_def
}
sess.run(tf.global_variables_initializer())
builder.add_meta_graph_and_variables(
sess, [tf.saved_model.tag_constants.SERVING],
signature_def_map=signature_def_map)
builder.save()
我能够从Tensorflow Serving获得有效的响应:
预测请求:
POST / v1 / models / payfraud:predict
{
"inputs": [[44.26, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
}
预测响应:
{
"outputs": [
[
0.5,
0.5
]
]
}
获取/ v1 / models / payfraud / versions / 1 /元数据
{
"model_spec": {
"name": "payfraud",
"signature_name": "",
"version": "1"
},
"metadata": {
"signature_def": {
"signature_def": {
"serving_default": {
"inputs": {
"x": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "-1",
"name": ""
},
{
"size": "15",
"name": ""
}
],
"unknown_rank": false
},
"name": "x:0"
}
},
"outputs": {
"y": {
"dtype": "DT_FLOAT",
"tensor_shape": {
"dim": [
{
"size": "-1",
"name": ""
},
{
"size": "2",
"name": ""
}
],
"unknown_rank": false
},
"name": "y:0"
}
},
"method_name": "tensorflow/serving/predict"
}
}
}
}
}