How do I convert a CloudML Alpha model to a SavedModel?

南笙酒味 提交于 2019-12-19 10:49:08

问题


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

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


回答1:


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


来源:https://stackoverflow.com/questions/42968628/how-do-i-convert-a-cloudml-alpha-model-to-a-savedmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!