【问题标题】:Converting keras model to tensorflow lite gives "FailedPreconditionError"将 keras 模型转换为 tensorflow lite 会给出“FailedPreconditionError”
【发布时间】:2019-09-16 13:17:38
【问题描述】:

我在keras 中有一个模型,它使用 1 层带有双向包装的 LSTM,我想将其转换为 tensorflow lite。

我在训练模型时使用回调 ModelCheckpoint 来保存模型和最佳权重。

然后我使用此代码从检查点重新加载经过最佳训练的模型:

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

在检查验证数据后,模型会成功加载并按预期工作。现在我想将它转换为 Tensorflow Lite,并使用我在 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)

我认为可能是检查点文件引起了问题,所以我重新保存了模型,并再次使用 - 调用转换器 -

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)

我收到此错误 -

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}}]]

我尝试运行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)

在重新保存模型之前,检查模型变量是否未初始化,但错误仍然存​​在。

有没有人遇到过类似的问题并找到了解决方案?有没有办法直接将顺序模型转换为 tflite 而不保存和重新加载文件?

【问题讨论】:

  • TensorFlow Lite 尚不支持 LSTM 运算。你可以试试this
  • @ShubhamPanchal 我尝试使用类似的方法 - 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) 但我收到错误 - RuntimeError:在 SavedModel 中找不到与标签 {'serve'} 关联的 MetaGraphDef。要检查 SavedModel 中的可用标签集,请使用 SavedModel CLI:saved_model_cli

标签: tensorflow keras lstm tensorflow-lite


【解决方案1】:

您应该将模型保存在 .pb 文件中。 首先,如果您之前保存过模型,则加载您的模型,然后运行

YOUR_MODEL.save('NAME.pb').

现在您有一个文件夹,其中包含已保存的 model.pb 和其他必要的文件和文件夹。 创建转换器实例:

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

最后转换你的模型并保存:

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

【讨论】:

    【解决方案2】:

    您首先需要通过运行将模型保存为 .h5 格式

    model.save("model.h5")
    

    然后,按照这些步骤进行

    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)
    

    【讨论】:

      猜你喜欢
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      相关资源
      最近更新 更多