0
votes

The question is about Tensorflow and its higher API Estimator and more generally about exporting and reusing models.

There exists several facilities to export a tensorflow models: tf.train.Saver, write and importing graph directly with their associated weights.

My interests lies in the tf.estimator.Estimator object and how we could reuse them. My use case is pretty simple: I fit a simple model say in tensorflow and would like to use its predictions in a more involved one.

There exists a export_savemodel method for serving the model, however I would need to make communication in my model through ports and I am not sure if this is optimal for training.

My question are the following:

  1. Is there a (simple) way to use and import the output of export_savemodel in python? I guessed you could technically use the C++ code and create wrapper around them?

  2. Is there an efficient way to compile an tf.estimator.Estimator and reuse it in Python? The XLA compiler seems be interesting, but I could not find out if I could use the result back in Python.

  3. Generally, what are the best practice on how to reuse previously trained models in Tensorflow?

Thanks a lot for your reading!

EDIT (my own answer): The accepted answer offers to use the saved_model API. However, I think it is worthwhile to understand the concept of graph and frozen graph. This gist link illustrates the main steps that are probably replicated in the saved_model api. Basically, you have to:

  1. rebuild the graph with redefined input ops;
  2. freeze this version;
  3. provide the new input to the input_map argument when calling tf.import_graph_def to reload the model backs.
1

1 Answers

0
votes
  1. Yes, see this doc https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md. In particular, the APIs.Loader.Python section.
  2. What do you mean by "compile an estimator"? All the data used by estimator is saved into SavedModel. The reset is simply high-level orchestrating logic. Actual operations like matrix multiplications are provided by the C++ library and run on whatever hardware you have - CPU, GPU, or TPU. XLA is a pretty low-level compiler, far from the Estimator API. For more info about it search for a talk about it "XLA: TensorFlow, Compiled! (TensorFlow Dev Summit 2017)"
  3. The link above provides a very high-level API. For the lower layer, see https://www.tensorflow.org/programmers_guide/meta_graph. At even lower layer, there is GraphDef (see links in the meta_graph page)