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

后端 未结 1 1929
清酒与你
清酒与你 2021-01-07 07:24

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 probl

相关标签:
1条回答
  • 2021-01-07 08:20

    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()
    
    0 讨论(0)
提交回复
热议问题