如何向 Keras 中制作的 ML 引擎模型发送 POST 请求?
How can I send POST request to a ML Engine Model made in Keras?
我做了一个Keras模型
model = Sequential()
model.add(Dense(12, input_dim=7, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
在本地训练
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X_train, Y_train, epochs=150, batch_size=10)
经测试有效
example = np.array([X_test.iloc[0]])
model.predict(example)
使用此功能保存它
def to_savedmodel(model, export_path):
"""Convert the Keras HDF5 model into TensorFlow SavedModel."""
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'input': model.inputs[0]},
outputs={'income': model.outputs[0]})
K.clear_session()
sess = K.get_session()
builder.add_meta_graph_and_variables(
sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature}
)
sess.close()
K.clear_session()
builder.save()
模型现在以 .pb
格式保存在 GC 存储中。
我在 ML Engine 中创建了一个新模型并部署了第一个版本。
当我尝试使用此 json body
通过 HTTP POST
请求使用它时
{
"instances": [{
"input": [1, 2, 3, 4, 5, 6, 7 ]
}]
}
我得到这个错误:
{
"error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.NOT_FOUND, details=\"FeedInputs: unable to find feed output dense_34_input:0\")"
}
知道如何发送正确的正文或正确保存模型吗?
谢谢 sdcbr。你给我指明了正确的方向。保存模型的函数丢弃了我训练好的模型。我更改了它并且现在运行良好:
def to_savedmodel(fname, export_path):
with tf.Session() as sess:
K.set_session(sess)
model = load_model(fname)
sess.run(tf.initialize_all_variables())
K.set_learning_phase(0)
builder = SavedModelBuilder(export_path)
signature = predict_signature_def(
inputs={"inputs": model.input},
outputs={"outputs": model.output})
builder.add_meta_graph_and_variables(
sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={
'predict': signature})
builder.save()
我做了一个Keras模型
model = Sequential()
model.add(Dense(12, input_dim=7, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
在本地训练
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X_train, Y_train, epochs=150, batch_size=10)
经测试有效
example = np.array([X_test.iloc[0]])
model.predict(example)
使用此功能保存它
def to_savedmodel(model, export_path):
"""Convert the Keras HDF5 model into TensorFlow SavedModel."""
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'input': model.inputs[0]},
outputs={'income': model.outputs[0]})
K.clear_session()
sess = K.get_session()
builder.add_meta_graph_and_variables(
sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature}
)
sess.close()
K.clear_session()
builder.save()
模型现在以 .pb
格式保存在 GC 存储中。
我在 ML Engine 中创建了一个新模型并部署了第一个版本。
当我尝试使用此 json body
POST
请求使用它时
{
"instances": [{
"input": [1, 2, 3, 4, 5, 6, 7 ]
}]
}
我得到这个错误:
{
"error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.NOT_FOUND, details=\"FeedInputs: unable to find feed output dense_34_input:0\")"
}
知道如何发送正确的正文或正确保存模型吗?
谢谢 sdcbr。你给我指明了正确的方向。保存模型的函数丢弃了我训练好的模型。我更改了它并且现在运行良好:
def to_savedmodel(fname, export_path):
with tf.Session() as sess:
K.set_session(sess)
model = load_model(fname)
sess.run(tf.initialize_all_variables())
K.set_learning_phase(0)
builder = SavedModelBuilder(export_path)
signature = predict_signature_def(
inputs={"inputs": model.input},
outputs={"outputs": model.output})
builder.add_meta_graph_and_variables(
sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={
'predict': signature})
builder.save()