OpenNMT / OpenNMT-tf

Neural machine translation and sequence learning using TensorFlow
https://opennmt.net/
MIT License
1.46k stars 392 forks source link

TFLite Support? #713

Closed iamweiweishi closed 4 years ago

iamweiweishi commented 4 years ago

Hi, I trained a translation model successfully, and then exported the Base-Transformer model to "SavedModel".

I refered to this(link) and tried to convert the "SavedModel" to tflite.

` import tensorflow as tf

filename = 'path_to_model_directory' model = tf.saved_model.load(filename)

concrete_func = model.signatures[ tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]

concrete_func.inputs[0].set_shape([1,8]) converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func]) converter.experimental_new_converter = True

Tflite_model = converter.convert() open("nmt_05_12_19_test_beng.tflite", "wb").write(Tflite_model) `

I tried to print concrete_func.inputs, and found there are about 266 Tensors:

Tensor("length:0", shape=(None,), dtype=int32) Tensor("tokens:0", shape=(None, None), dtype=string) Tensor("unknown:0", dtype=resource) Tensor("unknown_0:0", shape=(), dtype=int64) Tensor("unknown_1:0", dtype=resource) Tensor("unknown_2:0", dtype=resource) Tensor("unknown_3:0", dtype=resource) Tensor("unknown_4:0", dtype=resource) Tensor("unknown_5:0", dtype=resource) Tensor("unknown_6:0", dtype=resource) Tensor("unknown_7:0", dtype=resource) Tensor("unknown_8:0", dtype=resource) Tensor("unknown_9:0", dtype=resource) Tensor("unknown_10:0", dtype=resource) Tensor("unknown_11:0", dtype=resource) Tensor("unknown_12:0", dtype=resource) Tensor("unknown_13:0", dtype=resource) Tensor("unknown_14:0", dtype=resource) Tensor("unknown_15:0", dtype=resource) ......

I am not sure what to do next and how to 'set_shape'. Thank you.

guillaumekln commented 4 years ago

As far as I know, TensorFlow Lite still has some limitations when it comes to slightly complex decoding loop. We had some discussions to implement a simpler decoding loop with less features that TensorFlow Lite can convert. See also https://forum.opennmt.net/t/convert-to-keras-model-for-coremltools/3816

I'm closing this issue in favor of #490.