TQR tensorflow build_error ai_generated true

ValueError:操作'CONV_2D'的量化范围(最小值、最大值)不支持输入类型float32和输出类型uint8

ValueError: Quantization range (min, max) not supported for op 'CONV_2D' with input type float32 and output type uint8

ID: tensorflow/tflite-quantization-range

其他格式: JSON · Markdown 中文 · English
80%修复率
82%置信度
1证据数
2024-01-10首次发现

版本兼容性

版本状态引入弃用备注
tensorflow>=2.12.0 active
tflite>=2.12 active
python>=3.9 active

根因分析

在TFLite转换过程中,CONV_2D操作的量化参数(最小值/最大值)缺失或超出uint8量化的有效范围,通常是因为校准数据未覆盖完整的激活范围。

English

During TFLite conversion, the quantization parameters (min/max) for a CONV_2D op are either missing or out of the valid range for uint8 quantization, typically because calibration data did not cover the full activation range.

generic

官方文档

https://www.tensorflow.org/lite/performance/post_training_quantization

解决方案

  1. Ensure the calibration dataset includes diverse inputs that trigger both minimum and maximum activation values for the CONV_2D layer. For example, use a balanced subset of training data: representative_dataset = lambda: [tf.random.normal([1, 224, 224, 3]) for _ in range(100)] (but with real data).
  2. Switch to per-channel quantization or use tf.lite.TFLiteConverter with optimizations=[tf.lite.Optimize.DEFAULT] and specify supported_ops=[tf.lite.OpsSet.TFLITE_BUILTINS_INT8] after ensuring full integer quantization is possible.

无效尝试

常见但无效的做法:

  1. Increasing the number of calibration steps arbitrarily without verifying data diversity. 70% 失败

    More steps of the same data does not improve range coverage; the error persists if the calibration set lacks extreme values.

  2. Manually setting min/max to [-128, 127] in the converter options. 90% 失败

    TFLite converter ignores manual override for per-op ranges; it expects calibration to provide them.