首页 > 解决方案 > 如何量化一个训练感知量化模型?

问题描述

我有一个 Keras 模型,我将其应用在 Training Aware Quantization 上,所以我有一个训练感知量化模型。我想要一个完全量化的 int8 模型(我有一个只接受整数运算的硬件)。

所以我在这个量化模型上应用了训练后量化。

我使用这部分代码来做到这一点:

converter = tf.lite.TFLiteConverter.from_keras_model(TAQ_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()

但我在 float32 中得到了输入和输出:

== Input details ==
name: dense_input
shape: [ 1 36]
type: <class 'numpy.float32'>

== Output details ==
name: Identity
shape: [  1 108]
type: <class 'numpy.float32'>

为了解决这个问题,我添加了一些选项并使用了如下的代表数据集:

converter.representative_dataset = representative_dataset
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8

但我得到了这个错误:

Traceback (most recent call last):
  File "D:/PhD_Thessaloniki/Codes/5G_Model/retrained_quantized_pb/PTQ.py", line 123, in <module>
    tflite_quant_model = converter.convert()
  File "C:\ProgramData\Anaconda3\envs\tensorflow_env\lib\site-packages\tensorflow\lite\python\lite.py", line 830, in convert
    return super(TFLiteKerasModelConverterV2,
  File "C:\ProgramData\Anaconda3\envs\tensorflow_env\lib\site-packages\tensorflow\lite\python\lite.py", line 638, in convert
    result = self._calibrate_quantize_model(result, **flags)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_env\lib\site-packages\tensorflow\lite\python\lite.py", line 450, in _calibrate_quantize_model
    return calibrate_quantize.calibrate_and_quantize(
  File "C:\ProgramData\Anaconda3\envs\tensorflow_env\lib\site-packages\tensorflow\lite\python\optimize\calibrator.py", line 95, in calibrate_and_quantize
    return self._calibrator.QuantizeModel(
RuntimeError: Quantization not yet supported for op: 

如何使用训练感知量化完全量化模型?

如果每个人都可以帮助我,我将不胜感激。

标签: pythontensorflowkerasquantization

解决方案


推荐阅读