Franck-Dernoncourt / NeuroNER

Named-entity recognition using neural networks. Easy-to-use and state-of-the-art results.
http://neuroner.com
MIT License
1.7k stars 475 forks source link

How to export as `SavedModel` #43

Open ghost opened 7 years ago

ghost commented 7 years ago

Thanks for the project!

I want to save session checkpoint model as SavedModel. But I get an error. How can I add the missing values?

import tensorflow as tf
from pathlib import Path

if __name__ == '__main__':
    sess = tf.Session()
    sess_dir_path = Path("output/en_2017-07-17_13-50-14-257934/model").resolve()
    export_dir_path = Path("output/en_2017-07-17_13-50-14-257934/model2")
    model_file_path = sess_dir_path.joinpath('model_00042.ckpt.meta')
    saver = tf.train.import_meta_graph(str(model_file_path))
    saver.restore(sess, tf.train.latest_checkpoint(sess_dir_path))
    graph = tf.get_default_graph()
    builder = tf.saved_model.builder.SavedModelBuilder(str(export_dir_path))
    init_g = tf.global_variables_initializer()
    init_l = tf.local_variables_initializer()
    with tf.Session(graph=graph) as sess:
        builder.add_meta_graph_and_variables(sess, [])
        sess.run(init_g)
        sess.run(init_l)
    builder.save()
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1139, in _do_call
    return fn(*args)
  File "/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1121, in _run_fn
    status, run_metadata)
  File "/usr/lib/python3.6/contextlib.py", line 89, in __exit__
    next(self.gen)
  File "/usr/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value character_embedding/character_embedding_weights
         [[Node: character_embedding/character_embedding_weights/_0 = _Send[T=DT_FLOAT, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_10_character_embedding/character_embedding_weights", _device="/job:localhost/replica:0/task:0/gpu:0"](character_embedding/character_embedding_weights)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/amy/saved_model/__main__.py", line 16, in <module>
    builder.add_meta_graph_and_variables(sess, [])
  File "/usr/lib/python3.6/site-packages/tensorflow/python/saved_model/builder_impl.py", line 362, in add_meta_graph_and_variables
    saver.save(sess, variables_path, write_meta_graph=False, write_state=False)
  File "/usr/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1488, in save
    raise exc
  File "/usr/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1472, in save
    {self.saver_def.filename_tensor_name: checkpoint_file})
  File "/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)
  File "/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 997, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
    target_list, options, run_metadata)
  File "/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value character_embedding/character_embedding_weights
         [[Node: character_embedding/character_embedding_weights/_0 = _Send[T=DT_FLOAT, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_10_character_embedding/character_embedding_weights", _device="/job:localhost/replica:0/task:0/gpu:0"](character_embedding/character_embedding_weights)]]
willjohnathan commented 6 years ago

Any update on getting this to work?

JohnGiorgi commented 6 years ago

@willjohnathan Perhaps I am misunderstanding the problem, but you can simply use prepare_pretrained_model.py. All the variables you need to set are at the very end of the script.

willjohnathan commented 6 years ago

It is probably my lack of knowledge on this. I was trying to upload the model to the cloud ml-engine and it requires a savedmodeld.pb file. Again I am new to this so not sure the best way to incorporate this into say a flask app. I was looking into saving the model as mentioned to do this. The other option I was looking at is to upload it into a docker container onto the cloud compute engine. I feel like I am missing something simple / obvious though on the best way to tie my trained model into a web app.

JohnGiorgi commented 6 years ago

I know little about the Google Cloud ML-engine (if that is in fact what you are referring too). It obviously supports Tensorflow models, but I am not sure about the savedmodeld.pb problem.

As for a Flask app, the primary issue is that (as far as I can tell) NeuroNER will make a prediction when called and then exit. You would need to play with the code a bit, likely in main.py to set up a loop (receive data --> perform inference --> return results).