How do I change the Signatures of my SavedModel wi

2019-02-19 13:01发布

问题:

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