如何在不重新训练模型的情况下更改SavedModel的签名? [英] How do I change the Signatures of my SavedModel without retraining the model?
问题描述
我刚刚完成模型训练,只是发现我导出了一个服务签名模型存在问题的模型.如何更新它们?
I just finished training my model only to find out that I exported a model for serving that had problems with the signatures. How do I update them?
(一个常见的问题是为CloudML Engine设置了错误的形状).
(One common problem is setting the wrong shape for CloudML Engine).
推荐答案
不用担心-您不需要重新训练模型.就是说,还有一点工作要做.您将要创建一个新的(更正后的)投放图表,将检查点加载到该图表中,然后导出该图表.
Don't worry -- you don't need to retrain your model. That said, there is a little work to be done. You're going to create a new (corrected) serving graph, load the checkpoints into that graph, and then export this graph.
例如,假设您添加了一个占位符,但没有设置形状,即使您打算这样做(例如,在CloudML上运行).在这种情况下,您的图形可能看起来像:
For example, suppose you add a placeholder, but didn't set the shape, even though you meant to (e.g., to run on CloudML). In that case, your graph may have looked like:
x = tf.placeholder(tf.float32)
y = foo(x)
...
要纠正此问题:
# Create the *correct* graph
with tf.Graph().as_default() as new_graph:
x = tf.placeholder(tf.float32, shape=[None])
y = foo(x)
saver = tf.train.Saver()
# (Re-)define the inputs and the outputs.
inputs = {"x": tf.saved_model.utils.build_tensor_info(x)}
outputs = {"y": tf.saved_model.utils.build_tensor_info(y)}
signature = tf.saved_model.signature_def_utils.build_signature_def(
inputs=inputs,
outputs=outputs,
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
)
with tf.Session(graph=new_graph) as session:
# Restore the variables
vars_path = os.path.join(old_export_dir, 'variables', 'variables')
saver.restore(session, vars_path)
# Save out the corrected model
b = builder.SavedModelBuilder(new_export_dir)
b.add_meta_graph_and_variables(session, ['serving_default'], signature)
b.save()
这篇关于如何在不重新训练模型的情况下更改SavedModel的签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!