【问题标题】:How do I change the Signatures of my SavedModel without retraining the model?如何在不重新训练模型的情况下更改已保存模型的签名?
【发布时间】:2017-08-05 16:58:38
【问题描述】:

我刚刚完成模型的训练,却发现我导出的服务模型存在签名问题。如何更新它们?

(一个常见的问题是为 CloudML Engine 设置了错误的形状)。

【问题讨论】:

    标签: tensorflow tensorflow-serving google-cloud-ml google-cloud-ml-engine


    【解决方案1】:

    不用担心,您无需重新训练模型。也就是说,还有一些工作要做。您将创建一个新的(已修正的)服务图,将检查点加载到该图中,然后导出该图。

    例如,假设您添加了一个占位符,但没有设置形状,即使您打算这样做(例如,在 CloudML 上运行)。在这种情况下,您的图表可能如下所示:

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

    【讨论】:

      猜你喜欢
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 2021-03-30
      • 2018-01-29
      • 2019-11-02
      • 2018-12-22
      • 1970-01-01
      相关资源
      最近更新 更多