2017-03-23 52 views
1

在CloudML的在線預測服務的alpha版本,用於導出模型的格式是:如何將CloudML Alpha模型轉換爲SavedModel?

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沒有再訓練我的模型。我怎樣才能做到這一點?

回答

1

我們可以通過導入舊模式,創造了簽名將此轉換爲SavedModel,並重新將其導出。此代碼是未經測試,但這樣的事情應該工作:

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