How do I change the Signatures of my SavedModel without retraining the model?

允我心安 提交于 2019-12-19 04:16:55

问题


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?

(One common problem is setting the wrong shape for CloudML Engine).


回答1:


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.

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)
...

To correct this:

# 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()


来源:https://stackoverflow.com/questions/42801551/how-do-i-change-the-signatures-of-my-savedmodel-without-retraining-the-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!