Tensorflow 在Google云中部署模型:创建版本失败。模型验证失败:SavedModel必须仅包含一个标记为serve的元图
我正试图在谷歌云中部署一个本地培训的模型。我训练了模型,保存了graph.pb,并对模型进行了量化。但是,现在我正在尝试部署它,我收到以下错误:Tensorflow 在Google云中部署模型:创建版本失败。模型验证失败:SavedModel必须仅包含一个标记为serve的元图,tensorflow,google-cloud-ml,Tensorflow,Google Cloud Ml,我正试图在谷歌云中部署一个本地培训的模型。我训练了模型,保存了graph.pb,并对模型进行了量化。但是,现在我正在尝试部署它,我收到以下错误: Create Version failed. Model validation failed: SavedModel must contain exactly one metagraph with tag: serve 有人知道如何将标签添加到模型中吗?因为Zapata的答案仍然让人无法想象。这是我的代码: input_node = tf.
Create Version failed. Model validation failed: SavedModel
must contain exactly one metagraph with tag: serve
有人知道如何将标签添加到模型中吗?因为Zapata的答案仍然让人无法想象。这是我的代码:
input_node = tf.placeholder(tf.float32,
shape=(None, spec.crop_size, spec.crop_size, spec.channels),
name='input')
y_ = tf.placeholder('float', shape=[None, 2])
# Create builder
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
tensor_info_x = utils.build_tensor_info(input_node)
tensor_info_y = utils.build_tensor_info(y_)
prediction_signature = signature_def_utils.build_signature_def(
inputs={'input': tensor_info_x},
outputs={'output': tensor_info_y},
method_name=signature_constants.PREDICT_METHOD_NAME)
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
builder.add_meta_graph_and_variables(
sess, [tf.saved_model.tag_constants.SERVING],
signature_def_map={
'predict_images':
prediction_signature
},
legacy_init_op=legacy_init_op)
# Save the SavedModel to disk.
builder.save()
with tf.Session() as sess:
# init = tf.initialize_all_variables()
init = tf.global_variables_initializer()
input_x = sess.graph.get_tensor_by_name("0:0") # input
outputs1 = sess.graph.get_tensor_by_name("add_10:0")
output_tf_pb = sess.run(
[outputs1], feed_dict={input_x: np.random.randn(1, 3, 64, 64)}
)
# output_tf_pb = sess.run([outputs1, outputs2], feed_dict=
{input_x:np.random.randn(1, 3, 224, 224)})
print("output_tf_pb = {}".format(output_tf_pb))
# os.removedirs("output2")
builder = tf.saved_model.builder.SavedModelBuilder("output2")
prediction_signature =
tf.saved_model.signature_def_utils.build_signature_def(
inputs={"images": tf.saved_model.utils.build_tensor_info(input_x)},
outputs={"scores": tf.saved_model.utils.build_tensor_info(outputs1)},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME,
)
tensor_info_x = tf.saved_model.utils.build_tensor_info(input_x)
tensor_info_y = tf.saved_model.utils.build_tensor_info(outputs1)
classification_signature = tf.saved_model.signature_def_utils.build_signature_def(
inputs={tf.saved_model.signature_constants.CLASSIFY_INPUTS: tensor_info_x},
outputs={
tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES: tensor_info_y
},
method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME,
)
builder.add_meta_graph_and_variables(
sess,
[tf.saved_model.tag_constants.SERVING],
signature_def_map={
"predict_images": prediction_signature,
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: classification_signature,
},
main_op=tf.tables_initializer(),
)
builder.save()
完整文件可在以下位置找到:
您可能会发现此链接很有帮助:您能否提供有关如何导出模型的更多详细信息(代码片段最有帮助)?当您导出模型时,您可以保存多个图形,但服务只需要一个,它必须被称为“服务”。@rhaertel80我能够解决它。我已经发布了答案。非常感谢您的帮助:)