All Products
Document Center

Export Tesorflow Model

Last Updated: Jul 21, 2019

SavedModel format description

To deploy a TensorFlow model service online, you must first export the model to the SavedModel format. The SavedModel format is defined and recommended by TensorFlow. The directories of a SavedModel format model are as follows:

  1. assets/
  2. variables/
  4. variables.index
  5. saved_model.pb|saved_model.pbtxt

The directories are used to store the following data:

  • The assets directory is optional. It stores supporting files for the model service.
  • The variables directory stores the variables saved by calling the tf.train.Saver method.
  • The saved_model.pb or saved_model.pbtxt directory stores MetaGraphDef, SignatureDef, and the model training logic. MetaGraphDef contains the meta information and SignatureDef specifies the input and output of the model service.

Export SavedModel

You can use TensorFlow to export models to the SavedModel format. For more information, seeSave and restore. If the model is simple, you can export the model to the SavedModel format by calling the simple_save() method. The sample code is as follows:

  1. tf.saved_model.simple_save(
  2. session,
  3. "./savedmodel/",
  4. inputs={"image": x},
  5. outputs={"scores": y}
  6. )

When you call the online model service, you must include the signature_name of the model in the request. For a model that is exported by calling the simple_save() method, the signature_name is set to serving_default.

If the model is complex, you can manually export the model to the SavedModel format. The sample code is as follows:

  1. print 'Exporting trained model to', export_path
  2. builder = tf.saved_model.builder.SavedModelBuilder(export_path)
  3. tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
  4. tensor_info_y = tf.saved_model.utils.build_tensor_info(y)
  5. prediction_signature = (
  6. tf.saved_model.signature_def_utils.build_signature_def(
  7. inputs={'images': tensor_info_x},
  8. outputs={'scores': tensor_info_y},
  9. method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))
  10. legacy_init_op =, name='legacy_init_op')
  11. builder.add_meta_graph_and_variables(
  12. sess, [tf.saved_model.tag_constants.SERVING],
  13. signature_def_map={
  14. 'predict_images':
  15. prediction_signature,
  16. },
  17. legacy_init_op=legacy_init_op)
  19. print 'Done exporting!'

The descriptions for the parameters are as follows:

  • export_path specifies the path that the model is exported to.
  • prediction_signature is the SignatureDef that specifies the input and output of the model. For more information, see SignatureDef. In this example, the signature_name is predict_images.
  • The builder.add_meta_graph_and_variables method describes the parameters for exporting the model. Pay close attention to tf.saved_model.tag_constants.SERVING**.
  • You can download the complete code for using TensorFlow to export a model to the SavedModel format from saved_model.tar.gz
  • For more information, see TensorFlow SavedModel

Convert a Keras model to the SavedModel format

You can call the () method of Keras to convert a Keras model to the h5 format, call the load_model () method to load the h5 model, and then export the model to the SavedModel format. The sample code is as follows:

  1. import tensorflow as tf
  2. with tf.device("/cpu:0"):
  3. model = tf.keras.models.load_model('./mnist.h5')
  4. tf.saved_model.simple_save(
  5. tf.keras.backend.get_session(),
  6. "./h5_savedmodel/",
  7. inputs={"image": model.input},
  8. outputs={"scores": model.output}
  9. )

Convert a checkpoint format model to the SavedModel format

During the model training process, the tf.train.Saver() method saves the model to the checkpoint format. To deploy the model online, you must convert the model to the SavedModel format. You can call the saver.restore() method to load the checkpoint format model to a tf session, and then export the model to the SavedModel format. The sample code is as follows:

  1. import tensorflow as tf
  2. # variable define ...
  3. saver = tf.train.Saver()
  4. with tf.Session() as sess:
  5. # Initialize v1 since the saver will not.
  6. saver.restore(sess, "./lr_model/model.ckpt")
  7. tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
  8. tensor_info_y = tf.saved_model.utils.build_tensor_info(y)
  9. tf.saved_model.simple_save(
  10. sess,
  11. "./savedmodel/",
  12. inputs={"image": tensor_info_x},
  13. outputs={"scores": tensor_info_y}
  14. )