如何将CloudML Alpha模型转换为SavedModel? [英] How do I convert a CloudML Alpha model to a SavedModel?

查看:49
本文介绍了如何将CloudML Alpha模型转换为SavedModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CloudML在线预测服务的Alpha版本中,导出模型的格式为:

In the alpha release of CloudML's online prediction service, the format for exporting model was:

inputs = {"x": x, "y_bytes": y}
g.add_to_collection("inputs", json.dumps(inputs))
outputs = {"a": a, "b_bytes": b}
g.add_to_collection("outputs", json.dumps(outputs))

我想将其转换为SavedModel而不重新训练我的模型.我该怎么办?

I would like to convert this to a SavedModel without retraining my model. How can I do that?

推荐答案

我们可以通过导入旧模型,创建签名并重新导出将其转换为SavedModel.这段代码未经测试,但是类似的东西应该可以工作:

We can convert this to a SavedModel by importing the old model, creating the Signatures, and re-exporting it. This code is untested, but something like this should work:

import json
import tensorflow as tf
from tensorflow.contrib.session_bundle import session_bundle

# Import the "old" model
session, _ = session_bundle.load_session_bundle_from_path(export_dir)

# Define the inputs and the outputs for the SavedModel
old_inputs = json.loads(tf.get_collection('inputs'))
inputs = {name: tf.saved_model.utils.build_tensor_info(tensor)
          for name, tensor in old_inputs}

old_outputs = json.loads(tf.get_collection('outputs'))
outputs = {name: tf.saved_model.utils.build_tensor_info(tensor)
           for name, tensor in old_outputs}

signature = tf.saved_model.signature_def_utils.build_signature_def(
    inputs=inputs,
    outputs=outputs,
    method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
)

# Save out the converted model
b = builder.SavedModelBuilder(new_export_dir)
b.add_meta_graph_and_variables(session,
                               [tf.saved_model.tag_constants.SERVING],
                               signature_def_map={'serving_default': signature})
b.save()

这篇关于如何将CloudML Alpha模型转换为SavedModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆