张量流模型和转换后的 tflite 之间的精度下降

时间:2021-01-15 06:19:42

标签: python tensorflow keras tensorflow-lite

我遇到了一个问题,我将我的 keras 模型转换为 tensorflow lite 格式,但是一旦我这样做,转换后模型的模型精度就会显着下降。该模型是一个相当简单的自然语言处理模型。在转换之前,模型的准确率约为 96%,但一旦转换为 tensorflow lite 格式(没有任何优化),它就会下降到 20% 左右。这是一个可笑的性能下降,所以我想知道这是可能发生的事情还是我在这里做错了什么?我正在运行 debian 的 beaglebone SBC 上运行 tflite 模型并在 python 上运行推理。

我的 tflite 转换代码:

 converter = tf.lite.TFLiteConverter.from_keras_model(model)
 tflite_model = converter.convert()

 with open('model.tflite', 'wb') as f:
   f.write(tflite_model)

我的型号代码:

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, 128, input_length=maxlen),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(24, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。我通过训练后量化解决了这个问题。所以我在我训练的模型上应用了量化,并重新训练它。在 keras 和 TFLite 上的差异不超过大约 2-10%,这显着降低了准确率。

似乎在将 keras 模型转换为 TFLite 时,还应用了某种量化,并将浮点参数转换为整数,从而导致精度下降。通过首先量化模型,我们用整数训练模型。我认为这或多或少是发生了什么。如果我错了,请纠正我

参考资料 https://www.tensorflow.org/model_optimization/guide/quantization/training https://www.tensorflow.org/lite/performance/model_optimization