将keras模型转换为量化的tflite损失精度

时间:2020-05-28 07:36:18

标签: tensorflow keras tpu tf-lite

我正在尝试将keras模型转换为tflite量化模型,以便可以在珊瑚TPU上运行我的模型,但是我的keras模型和tflite模型的输出却大不相同。

红色点是量化的tflite模型输出,蓝色点是原始keras模型输出。

enter image description here

这是我的代码,用于将keras模型转换为量化的tflite模型:

quant = True
gc.collect()
import tensorflow as tf
import numpy as np
import pathlib
print(tf.__version__)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
if quant:
    print("Converting quant....")
    sample_size = 200
    rdm_idx = np.random.choice(range(len(X_test)),sample_size)
    rep_data = tf.cast(X_train[rdm_idx], tf.float32) / 255.0
    dataset = tf.data.Dataset.from_tensor_slices(rep_data).batch(1)

    def representative_data_gen():
        for input_value in dataset.take(sample_size):
            yield [input_value]

    converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
    converter.representative_dataset = representative_data_gen
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
    converter.inference_input_type = tf.uint8
    converter.inference_output_type = tf.uint8
    tflite_model_quant = converter.convert()
    open("MaskedLandMarkDetction_MobileNetV2_quant_fromKeras_v5.tflite", "wb").write(tflite_model_quant)

    print("Write quantization tflite done.")
else:
    print("Converting normal....")
    tflite_model = converter.convert()
    open("MaskedLandMarkDetction_MobileNetV2_fromKeras.tflite", "wb").write(tflite_model)
    print("Write tflite done.") 

X_train是我的训练数据,我将输入图像的值从255.除以0到1,所以我在representative_data_gen函数中也做同样的事情。

我们将不胜感激。

我使用的tensorflow版本是gpu 2.2.0

1 个答案:

答案 0 :(得分:2)

看来api的用法是正确的。并非所有模型都可以保证在训练后量化后获得良好的准确性。例如,需要更高精确度或较小模型的任务可能会遭受更多损失。

对于这些更困难的任务,我们建议使用量化感知训练,该训练可用于keras模型:https://www.tensorflow.org/model_optimization/guide/quantization/training

相关问题