Error: "serving_default not found in signature def" when testing prediction

Error: "serving_default not found in signature def" when testing prediction

我遵循了这个 tutorial 并来到了可以使用以下代码测试预测的地步:

{
   "instances": [
     {"csv_row": "44, Private, 160323, Some-college, 10, Married-civ-spouse, Machine-op-inspct, Husband, Black, Male, 7688, 0, 40, United-States", "key": "dummy-key"}
   ]
 }

但是,我收到以下错误:

{
  "error": "{ \"error\": \"Serving signature name: \\"serving_default\\" not found in signature def\" }"
}

我假设输入格式不代表预期的输入,但不完全应该是预期的。

关于导致示例代码抛出此错误的原因有什么想法吗?

我终于想通了:我在jupyter notebook中加载tensorflow模型并打印出签名:

new_model = tf.keras.models.load_model('modelPath')
print(list(new_model.signatures.keys())) 

结果是:[u'预测']

所以我用来获得预测的命令是:

georg@Georgs-MBP ~ % gcloud ai-platform 预测
--模型 $MODEL_NAME
--版本“v1”
--json-实例数sample_input.json
--格式“值(预测[0]。类[0])”
--签名名称“预测”

结果: 使用端点 [https://europe-west3-ml.googleapis.com/] <=50K

添加签名serving_default:

import tensorflow as tf
m = tf.saved_model.load("tf2-preview_inception_v3_classification_4")
print(m.signatures) # _SignatureMap({}) - Empty
t_spec = tf.TensorSpec([None,None,None,3], tf.float32)
c_func = m.__call__.get_concrete_function(inputs=t_spec)
signatures = {'serving_default': c_func}
tf.saved_model.save(m, 'tf2-preview_inception_v3_classification_5', signatures=signatures)

# Test new model
m5 = tf.saved_model.load("tf2-preview_inception_v3_classification_5")
print(m5.signatures) # _SignatureMap({'serving_default': <ConcreteFunction signature_wrapper(*, inputs) at 0x17316DC50>})