【问题标题】:accuracy drop between tensorflow model and converted tflite张量流模型和转换后的 tflite 之间的精度下降
【发布时间】:2021-01-15 06:19:42
【问题描述】:

我遇到了一个问题,我将我的 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')
])

【问题讨论】:

  • 准确率下降不是转换本身造成的。我会首先检查经过训练的模型是否作为转换器的输入。如果您在训练期间有任何预处理,这种行为也可能是由于错过了预处理造成的。

标签: python tensorflow keras tensorflow-lite


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 2020-09-15
    • 2019-10-02
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多