tensorflow - 如何在不重新训练模型的情况下更改我的 SavedModel 的签名?

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

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

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

最佳答案

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

例如,假设您添加了一个占位符,但没有设置形状,即使您打算这样做(例如,在 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()

关于tensorflow - 如何在不重新训练模型的情况下更改我的 SavedModel 的签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42801551/

相关文章:

python - 如何从自定义人工智能平台模型登录

c# - Google ML-Engine Predict from C# 身份验证问题

python - 如何记录 TensorFlow 变量中的各个标量值?

python - tf.contrib.learn.LinearRegressor 的拟合函数要求切换到 tf.train.get_global_step

tensorflow - Keras 序列模型到 Tensorflow EstimatorSpec 的准确性下降

python - 如何在不每次对模型充电的情况下进行预测 - tensorflow ?

tensorflow - 如何获得车牌上每个符号的边界框

python-2.7 - 分布式 TensorFlow 示例不适用于 TensorFlow 0.9

python - tensorflow-serving-apis - 找不到 python 文档

google-cloud-platform - 是否可以连接到 GCP Vertex AI 管道中 Cloud SQL 实例的私有(private) IP?