serving icon indicating copy to clipboard operation
serving copied to clipboard

Export nmt trained model to tensorflow serving

Open luozhouyang opened this issue 8 years ago • 49 comments

I have trained nmt models, but I can not understand how to export the models to tensorflow serving. I read the documentations of MNIST and Inception, but I think these models are different with nmt models. Can you add a demo to show how to export the nmt models? This would be a great help to beginners like me, thanks!

luozhouyang avatar Jan 03 '18 07:01 luozhouyang

@harshS26 You are right. This is my inference function in spark cluster. The output is "decoder_output_bm".

def predict(iterator):
  """
  For each partition, load the pb model and feed in each item of the RDD to the placehold
  """
  from tensorflow.contrib.seq2seq.python.ops import beam_search_ops   # note: do not remove this line
  result = []
  sess = tf.Session()
  graph = tf.get_default_graph()
  tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], model_path)
  input_feature = graph.get_tensor_by_name("src_placeholder:0")
  output = graph.get_tensor_by_name("decoder_output_bm:0")
  for item in iterator:
    one_query_result = []
    predict_result = sess.run([output], feed_dict={input_feature: [item[1]]})
    predict_result = predict_result[0]
    predict_result = np.transpose(predict_result, [2, 1, 0])
    predict_result = np.squeeze(predict_result, axis=1)
    for idx in range(args.beam_size):
      one_query_result.append(" ".join(list(predict_result[idx, :])))
    result.append((item[0] + "\t" + item[1] + "\t" + item[2], one_query_result))
  return iter(result)

I can try using your approach but I have few questions on this: 1.predict_result = sess.run([output], feed_dict={input_feature: [item[1]]}) what is item[1] in this? 2.Which variable do i assign the name decoder_output_bm ?

Sorry, my mistake. Anthor modification in model.py of line 121

    elif self.mode == tf.contrib.learn.ModeKeys.INFER:
      self.infer_logits, _, self.final_context_state, self.sample_id = res
      self.sample_words = reverse_target_vocab_table.lookup(
          tf.to_int64(self.sample_id), name="decoder_output_bm")

This is same node with "inference_outputs = infer_model.model.sample_words". Maybe something else went wrong, a suggestion, you can print the source string before feeding into graph.

what is the iterator?

B1gMinnow avatar Feb 23 '19 11:02 B1gMinnow

@bidai541 Hi with the modifications in model_helper.py i get following error: Traceback (most recent call last): File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main "main", mod_spec) File "/usr/lib/python3.5/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/baishali/MLPerf/inference/cloud/translation/gnmt/tensorflow/nmt/nmt.py", line 735, in tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/platform/app.py", line 125, in run _sys.exit(main(argv)) File "/home/baishali/MLPerf/inference/cloud/translation/gnmt/tensorflow/nmt/nmt.py", line 727, in main run_main(FLAGS, default_hparams, train_fn, inference_fn) File "/home/baishali/MLPerf/inference/cloud/translation/gnmt/tensorflow/nmt/nmt.py", line 698, in run_main trans_file, hparams, num_workers, jobid) File "/home/baishali/MLPerf/inference/cloud/translation/gnmt/tensorflow/nmt/inference.py", line 134, in inference hparams) File "/home/baishali/MLPerf/inference/cloud/translation/gnmt/tensorflow/nmt/inference.py", line 167, in single_worker_inference infer_model.batch_size_placeholder: hparams.infer_batch_size File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 900, in run run_metadata_ptr) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1120, in _run self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 427, in init self._fetch_mapper = _FetchMapper.for_fetch(fetches) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 242, in for_fetch type(fetch))) TypeError: Fetch argument None has invalid type <class 'NoneType'>

So the exact place that throws the error is from inference.py with infer_model.graph.as_default(): sess.run( infer_model.iterator.initializer, feed_dict={ infer_model.src_placeholder: infer_data, infer_model.batch_size_placeholder: hparams.infer_batch_size })

I am guessing this is because of this line iterator = pre_process(src_placeholder,src_vocab_table,eos=hparams.eos,src_max_len=hparams.src_max_len_infer) Which makes infer_model.iterator.initializer =None

baishalichaudhury avatar Mar 20 '19 22:03 baishalichaudhury

@harshS26

How did you get the proper prediction, can you please paste the entire code?

baishalichaudhury avatar Mar 26 '19 19:03 baishalichaudhury

Hi @baishalichaudhury @B1gMinnow , i have attached my code here..you guys can check nmt.zip

harshS26 avatar Mar 27 '19 06:03 harshS26

Hi

Thanks for your reply. Do you have a readme to briefly explain some of the changes you had to make in the code. Also out of curiosity, did you try restroring the saved .pb file and making an test translation or inference without a client based approach. So for example I want to make a simple test inference code, which will load the saved .pb model, restore placeholders, read an input file with few sentences, (preoprocess the strings maybe?) and translate . My confusions in this approach is how to restore the iterator and initialize before inference. Do you have any experience with this?

Thanks for all your help.

Regards

On Tue, Mar 26, 2019 at 11:42 PM Harshvardhan Singh < [email protected]> wrote:

Hi @baishalichaudhury https://github.com/baishalichaudhury @B1gMinnow https://github.com/B1gMinnow , i have attached my code here..you guys can check nmt.zip https://github.com/tensorflow/serving/files/3011842/nmt.zip

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/serving/issues/712#issuecomment-477000900, or mute the thread https://github.com/notifications/unsubscribe-auth/AL_uU50ChuO6BsZMjkIv42s6csnyt9m_ks5vaxLJgaJpZM4RRa9b .

baishalichaudhury avatar Mar 27 '19 13:03 baishalichaudhury

Hi

Another thing, when I try to export the model with your code I get this error

File "/home/baishali/MLPerf/inference/cloud/translation/gnmt/tensorflow/nmt/inference.py", line 130, in inference hparams) File "/home/baishali/MLPerf/inference/cloud/translation/gnmt/tensorflow/nmt/inference.py", line 161, in single_worker_inference infer_model.batch_size_placeholder: hparams.infer_batch_size File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 900, in run run_metadata_ptr) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1120, in _run self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 427, in init self._fetch_mapper = _FetchMapper.for_fetch(fetches) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 242, in for_fetch type(fetch))) TypeError: Fetch argument None has invalid type <class 'NoneType'>

This is because the iterator is none, so how did you solve this?

thanks

On Wed, Mar 27, 2019 at 6:05 AM Baishali Chaudhury < [email protected]> wrote:

Hi

Thanks for your reply. Do you have a readme to briefly explain some of the changes you had to make in the code. Also out of curiosity, did you try restroring the saved .pb file and making an test translation or inference without a client based approach. So for example I want to make a simple test inference code, which will load the saved .pb model, restore placeholders, read an input file with few sentences, (preoprocess the strings maybe?) and translate . My confusions in this approach is how to restore the iterator and initialize before inference. Do you have any experience with this?

Thanks for all your help.

Regards

On Tue, Mar 26, 2019 at 11:42 PM Harshvardhan Singh < [email protected]> wrote:

Hi @baishalichaudhury https://github.com/baishalichaudhury @B1gMinnow https://github.com/B1gMinnow , i have attached my code here..you guys can check nmt.zip https://github.com/tensorflow/serving/files/3011842/nmt.zip

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/serving/issues/712#issuecomment-477000900, or mute the thread https://github.com/notifications/unsubscribe-auth/AL_uU50ChuO6BsZMjkIv42s6csnyt9m_ks5vaxLJgaJpZM4RRa9b .

baishalichaudhury avatar Mar 27 '19 22:03 baishalichaudhury

@baishalichaudhury I have mailed u the steps taken to export the model

harshS26 avatar Mar 28 '19 07:03 harshS26

@harshS26 I used your code for exporting the model and successfully exported but while using client I am getting only one word Output. For example: "input": "when will you pay", "output": "when". Could you guide me what changes should I do ?

dharm033075 avatar Jun 21 '19 09:06 dharm033075

hey @dharm033075 sorry for late reply, did you check my export.py file is it the same? also did u change model_helper.py, model.py & nmt.py?

harshS26 avatar Aug 26 '19 09:08 harshS26