0
votes

I have a model in keras using 1 layer of LSTM with bidirectional wrapper, which I want to convert to tensorflow lite.

I'm using the callback ModelCheckpoint while training the model to save the model and the best weights.

Then I'm using this code to reload the best trained model from the checkpoint:

predictor = None
path_Load = os.path.join(os.getcwd(),'LSTMB_CheckPoints.hdf5')
predictor = load_model(path_Load)
predictor.load_weights(path_Load)

Upon checking with validation data, the model loads succesfully and works as intended. Now I want to convert it to Tensorflow Lite, with a bit of code I found on stackoverflow -

keras_file = path_Load
converter = tf.lite.TFLiteConverter.from_keras_model_file(keras_file)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

I thought maybe the checkpoint file was causing problems, so I resaved the model, and called converter again using -

keras_file = "keras_model.h5"
tf.keras.models.save_model(predictor, keras_file)
converter = tf.lite.TFLiteConverter.from_keras_model_file(keras_file)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

upon which I am getting this error -

tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value bidirectional_1/backward_lstm_1/kernel
     [[{{node _retval_bidirectional_1/backward_lstm_1/kernel_0_0}}]]

I tried to run the global variable initializer function of tensorflow

predictor = None
path_Load = os.path.join(os.getcwd(),'LSTMB_CheckPoints.hdf5')
predictor = load_model(path_Load)
predictor.load_weights(path_Load)
with tf.Session() as sess:
     sess.run(tf.global_variables_initializer())
keras_file = "keras_model.h5"
tf.keras.models.save_model(predictor, keras_file)
converter = tf.lite.TFLiteConverter.from_keras_model_file(keras_file)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

before resaving the model, to check whether the model variables were getting uninitialized, but the error still remains.

has anyone faced a similar issue and found a solution? Is there a way to convert the sequential model to tflite directly without saving and reloading the file?

2
The LSTM op is not yet supported in TensorFlow Lite. You can try this. - Shubham Panchal
@ShubhamPanchal I tried using the method like - converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS] tflite_model = converter.convert() open(target_path, "wb").write(tflite_model) but I am getting the error - RuntimeError: MetaGraphDef associated with tags {'serve'} could not be found in SavedModel. To inspect available tag-sets in the SavedModel, please use the SavedModel CLI: saved_model_cli - Neil

2 Answers

0
votes

you should save your model in the .pb file. First, load you model if you saved it before and then run

YOUR_MODEL.save('NAME.pb').

Now you have a folder that contains saved model.pb and necessary other files and folders. Create converter instance:

convertor = tensorflow.lite.TFLiteConverter.from_saved_model('NAME.pb').

At the end convert your model and save it:

tfmodel = converter.convert()
open("model.tflite","wb").write(tfmodel)
0
votes

You first need to save your model into .h5 format by running

model.save("model.h5")

afterward, follow those steps

new_model= tf.keras.models.load_model(filepath="model.h5")
tflite_converter = tf.lite.TFLiteConverter.from_keras_model(new_model)
tflite_model = tflite_converter.convert()
open("tf_lite_model.tflite", "wb").write(tflite_model)