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?
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