google / seq2seq

A general-purpose encoder-decoder framework for Tensorflow
https://google.github.io/seq2seq/
Apache License 2.0
5.61k stars 1.3k forks source link

export SavedModel error #296

Open dream-catcher opened 7 years ago

dream-catcher commented 7 years ago

I try to export seq2seq model to SavedModel protobuf for later inferencing with java language.
According to guides from tensorflow(https://www.tensorflow.org/api_docs/python/tf/saved_model/builder/SavedModelBuilder), we need to create a SavedModelBuilder, and then add meta graph to a session. But I find that, the framework in tensorflow use MonitoredTrainingSession as the session , and it will finalize the graph, so we I call builder.add_meta_graph_and_variables in after_create_session(self, session, coord) function in hooks.py , it reports errors: "graph is finalized and cannot be modified" And I try to add SavedModelBuilder in somewhere else, it does't have a session, the API can't be called. But when the session is created, the graph is already been finalized, and can't be changed anymore!

So can anyone help me how to export SavedModel for this framework?

brucechou1983 commented 6 years ago

Same problem here, I was not able to successfully export a tensorflow servable. Can anyone help?

Orion-Chen commented 6 years ago

Hi guys, I come across the same problem and try to solve this in another way. When training within MonitoredTrainingSession, I use a tf.train.SessionRunHook to save the model with name 'last_model.ckpt' at end(session). After training, I use 'with tf.Session() as sess:' to open another session, and restore 'last_model.ckpt' in this new session. So now I can use builder.add_meta_graph_and_variables and builder.saver() to save model as pb. If there's any other good solutions, pls let me know. Thanks.

CangHaiQingYue commented 6 years ago

@Orpin Hi, have can I use tf.train.SessionRunHook. Is there any example, I saw your code, but still confused..

Orion-Chen commented 6 years ago

@CangHaiQingYue Hi, I found another solution and forgot to update the comment. You can use this piece of code before creating tf.saved_model.builder to unfinalize the graph sess.graph._unsafe_unfinalize() And then it is ok to do builder.save().

sathyarr commented 5 years ago

is that possible to use this method defined in tensorflow with seq2seq? Or is there built-in support to export model by seq2seq?

There is also something like _maybe_export in official repo any usage with this possible?

Need to use the exported model in tf serving.

sathyarr commented 5 years ago

Hi guys, I come across the same problem and try to solve this in another way. When training within MonitoredTrainingSession, I use a tf.train.SessionRunHook to save the model with name 'last_model.ckpt' at end(session). After training, I use 'with tf.Session() as sess:' to open another session, and restore 'last_model.ckpt' in this new session. So now I can use builder.add_meta_graph_and_variables and builder.saver() to save model as pb. If there's any other good solutions, pls let me know. Thanks.

Could you share the code snippet?

ghtwht commented 5 years ago

Hi guys, I come across the same problem and try to solve this in another way. When training within MonitoredTrainingSession, I use a tf.train.SessionRunHook to save the model with name 'last_model.ckpt' at end(session). After training, I use 'with tf.Session() as sess:' to open another session, and restore 'last_model.ckpt' in this new session. So now I can use builder.add_meta_graph_and_variables and builder.saver() to save model as pb. If there's any other good solutions, pls let me know. Thanks.

Could you share the code snippet?

You can read these two pages:https://github.com/Microsoft/samples-for-ai/blob/master/projects/StyleTransfer/StyleTransferTraining/src/train.py#L218 and https://github.com/Microsoft/vs-tools-for-ai/issues/16