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 - 神经网络的输入大小不匹配

python - DNNClassifier 模型到 TensorFlow Serving 模型

python - 使用张量输入时 Keras 模型预测会发生变化

google-cloud-ml - 无法部署 Cloud ML 模型

python - 由于使用 "lambda",无法加载保存的 Keras 模型

TensorFlow 服务系统要求

tensorflow-serving - 如何使模型为带有 base64 编码图像的 TensorFlow Serving REST 接口(interface)做好准备?

python - 如何仅在评估模式下运行 TF 对象检测 API model_main.py

python - 使用 tensorflow tf-transform 进行数据规范化