Repository: OpenNMT/OpenNMT-tf

Issue:
Crash trying to export model using SequenceRecordInputter 
What I see its calling to _get_serving_input inside SequenceRecordInputter before make_dataset
is called. so self.input_depth doesn't exist yet

File "/usr/local/lib/python3.6/dist-packages/opennmt/bin/main.py", line 179, in main
    export_dir_base=args.export_dir_base)
  File "/usr/local/lib/python3.6/dist-packages/opennmt/runner.py", line 376, in export
    **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/estimator/estimator.py", line 734, in export_saved_model
    strip_default_attrs=True)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/estimator/estimator.py", line 663, in export_savedmodel
    mode=model_fn_lib.ModeKeys.PREDICT)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/estimator/estimator.py", line 789, in _export_saved_model_for_mode
    strip_default_attrs=strip_default_attrs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/estimator/estimator.py", line 907, in _export_all_saved_models
    mode=model_fn_lib.ModeKeys.PREDICT)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/estimator/estimator.py", line 977, in _add_meta_graph_for_mode
    input_receiver = input_receiver_fn()
  File "/usr/local/lib/python3.6/dist-packages/opennmt/models/model.py", line 509, in <lambda>
    return lambda: self._serving_input_fn_impl(metadata)
  File "/usr/local/lib/python3.6/dist-packages/opennmt/models/model.py", line 497, in _serving_input_fn_impl
    return self._get_serving_input_receiver()
  File "/usr/local/lib/python3.6/dist-packages/opennmt/models/model.py", line 272, in _get_serving_input_receiver
    return self.features_inputter.get_serving_input_receiver()
  File "/usr/local/lib/python3.6/dist-packages/opennmt/inputters/inputter.py", line 104, in get_serving_input_receiver
    receiver_tensors, features = self._get_serving_input()
  File "/usr/local/lib/python3.6/dist-packages/opennmt/inputters/inputter.py", line 333, in _get_serving_input
    receiver_tensors, features = inputter._get_serving_input()  # pylint: disable=protected-access
  File "/usr/local/lib/python3.6/dist-packages/opennmt/inputters/record_inputter.py", line 42, in _get_serving_input
    "tensor": tf.placeholder(self.dtype, shape=(None, None, self.input_depth)),
AttributeError: 'SequenceRecordInputter' object has no attribute 'input_depth'



Discussion:
Mmh yes, that is an issue.

Initially, the user had to set the input depth in the configuration file. However, since https://github.com/OpenNMT/OpenNMT-tf/commit/d9f0cbd3cb7ae6830aa77675c63424dd2a959d45, the depth is directly inferred from the data but the export code was not updated accordingly. We may need to rethink this as the training data should not be required when exporting a model.
I glad to help :)

Resolving patch:
diff --git a/opennmt/models/model.py b/opennmt/models/model.py
--- a/opennmt/models/model.py
+++ b/opennmt/models/model.py
@@ -494,6 +494,11 @@ def input_fn(self,
   def _serving_input_fn_impl(self, metadata):
     """See ``serving_input_fn``."""
     self._initialize(metadata)
+    # This is a hack for SequenceRecordInputter that currently infers the input
+    # depth from the data files.
+    # TODO: This method should not require the training data.
+    if self.features_inputter is not None and "train_features_file" in metadata:
+      _ = self.features_inputter.make_dataset(metadata["train_features_file"])
     return self._get_serving_input_receiver()
 
   def serving_input_fn(self, metadata):


Test patch: