Page 1 of 1

esp-dl int8量化模型数据集评估精度下降问题

Posted: Tue Jul 18, 2023 8:55 am
by hlypeter
您好,
一 试着将模型进行了esp-dl上int16和int8的量化,并在测试数据集上进行精度评估,其中int16的模型精度基本没有下降,但是int8的模型评估精度下降了很多,目前正在尝试修改Calibrator.set_method中的参数,与此同时有几个问题:
1.1 calibrator生成的model_calib.pickle文件,通过打印看到它是一个字典,关于其中的键值有没有相关的文档
1.2 eval.evalute_quantized_model 能否输出每一层的量化输出,而不只是最后一层,否则只能通过一次次的删除onnx模型后面的部分达到目的,太过麻烦
1.3 是否有pc端的eval/calibrator以及与libdl.a对应的c++库可以提供
这几个问题是遇到int8量化精度下降的问题时想到的关于esp-dl这边的定位方向,如果方向有问题或者有其他方向,也麻烦帮忙指出,感谢!

二 修改Calibrator.set_method遇到的问题:
2.1 calib = Calibrator('int8', 'per-tensor', 'minmax'),得到的模型参数cpp和hpp中的bias是int16的类型(可以正常评估推理),是否正常? 比如:
namespace mb_v1_ssd_coefficient
{
const dl::Filter<int8_t> *get_conv_0_filter();
const dl::Bias<int16_t> *get_conv_0_bias();
const dl::Activation<int8_t> *get_conv_0_activation();
const dl::Filter<int8_t> *get_conv_2_filter();
const dl::Bias<int16_t> *get_conv_2_bias();
const dl::Activation<int8_t> *get_conv_2_activation();
2.2 calib = Calibrator('int8', 'per-channel', 'minmax')
...
Evaluator('int8', 'per-channel', 'esp32s3')
...
[outputs, _] = eva.evalute_quantized_model(image, True)
在推理时出错:
quantization_tool/linux/evaluator.so in evaluator.Evaluator.evalute_quantized_model()

KeyError: 'input.4/QACC_shift1:0'

再次感谢!!!

Re: esp-dl int8量化模型数据集评估精度下降问题

Posted: Tue Aug 01, 2023 7:24 am
by Junru
1.1 是每层的量化系数
1.2 可以在代码中把每一层的 output print 出来
1.3 没有 C++ 库

Re: esp-dl int8量化模型数据集评估精度下降问题

Posted: Tue Aug 01, 2023 7:57 am
by Junru
2.1 属于正常情况

Re: esp-dl int8量化模型数据集评估精度下降问题

Posted: Fri Aug 04, 2023 8:51 am
by hlypeter
嗯 感谢回复;
1.1 字典中的键值可以参考onnx模型转换时的打印
1.2 设备端是一层层推理,可以打印出来,pc端的仿真以及onnx的模型每层输出打印还不太清楚怎么弄,有简单的方法麻烦告知下
2.2 int8的per-channel和per-tensor,理论上来讲per-channel的精度会高,但是实际上设备端和pc端的仿真整个模型运行下来都是per-tensor要高,其中第一层使用per-channel对精度影响很大;pc上per-channel的仿真把相应的键值input.4/QACC_shift1:0加进去可以运行,结果也是per-tensor要高,这个麻烦确认下,是我的模型问题还是per-channel本身的问题

Re: esp-dl int8量化模型数据集评估精度下降问题

Posted: Tue Aug 08, 2023 7:43 am
by Junru
最近的 DL2 已经合入到 github 上了,相比于 DL,直接给 onnx 模型就可以生成示例代码了,你可以先试试看

Re: esp-dl int8量化模型数据集评估精度下降问题

Posted: Thu May 15, 2025 3:14 am
by charlesfan
你好,我量化出了问题帮评估下!
代码:
from tensorflow.lite.python.optimize.calibrator import Calibrator
# Calibration
import tensorflow as tf
import onnx

pickle_file_path = "C:\\data\\pick"
optimized_model_path = output_path
model_proto = onnx.load(optimized_model_path)
print('Generating the quantization table:')

# Initialize an calibrator to quantize the optimized MNIST model to an int16 model using per-tensor minmax quantization method
#calib = Calibrator('int16', 'per-tensor', 'minmax')
calib = Calibrator('int8', 'per-channel', 'entropy')
calib.set_providers(['CPUExecutionProvider'])

# Obtain the quantization parameter
calib.generate_quantization_table(model_proto, calib_dataset, 'mnist_calib.pickle')

# Generate the coefficient files for esp32s3
calib.export_coefficient_to_cpp(model_proto, pickle_file_path, 'esp32s3', '.', 'mnist_coefficient', True)



错误如下:
TypeError Traceback (most recent call last)
~\AppData\Roaming\Python\Python37\site-packages\tensorflow\lite\python\optimize\calibrator.py in __init__(self, model_content, custom_op_registerers_by_name, custom_op_registerers_by_func)
70 model_content, custom_op_registerers_by_name,
---> 71 custom_op_registerers_by_func))
72 self._model_content = model_content

TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. tensorflow.lite.python.optimize._pywrap_tensorflow_lite_calibration_wrapper.CalibrationWrapper(arg0: handle, arg1: List[str], arg2: List[Callable[[int], None]])

Invoked with: 'int8', 'per-channel', 'entropy'