繁体   English   中英

无法将Keras模型转换为tflite

[英]Can't convert Keras model to tflite

我有以下行保存的Keras模型:

tf.keras.models.save_model(model, "path/to/model.h5")

稍后,我尝试将其转换为tflite文件,如下所示:

converter = tf.contrib.lite.TFLiteConverter.from_keras_model_file('path/to/model.h5')
tflite_model = converter.convert()
open("path/to/model.tflite", "wb").write(tflite_model)

但是我收到一个奇怪的错误:

您试图将包含35层的权重文件加载到具有0层的模型中。

我知道我的模型运作良好。 我能够加载它并得出推断。 仅当尝试将其保存为tflite模型时才会显示此错误。

TensorFlow版本:tensorflow-gpu 1.12.0

我正在使用tf.keras。

原来,这个问题是由于显式定义的InputLayer一些input_shape

我的模型是这样的:

InputLayer(input_shape =(...))

BatchNormalization()

....剩余层

我将其更改为:

BatchNormalization(input_shape =(...))

....剩余层

并在此转移先前模型的权重。 现在,它可以完美运行了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM