MNN/tools/MNNPythonOfflineQuant
xiaying 7629ba674e [MNN:Sync] Sync Internal 2.4.1 2023-03-20 11:32:29 +08:00
..
ReadMe.txt [MNN:Sync] Sync internal Gitlab 2021-09-18 15:52:30 +08:00
calibration_dataset.py [MNN:Sync] Sync internal Gitlab 2021-04-08 15:34:23 +08:00
config.yaml [MNN:Sync] Sync internal Gitlab 2021-04-08 15:34:23 +08:00
mnn_offline_quant.py [MNN:Sync] Sync Internal 2.4.1 2023-03-20 11:32:29 +08:00

ReadMe.txt

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

这是用MNN的python接口改造的离线量化工具适用于如下情况
    1. 你的模型无法使用MNN离线量化工具tools/quantization进行量化例如多输入数据预处理比较复杂
    2. 你的模型无法使用MNN进行训练量化受限于MNN的训练能力

为了使用这个工具,你需要提供:
    0. 使用 MNNConvert工具加上 --forTraining 将你的模型转换成MNN模型 (这步主要是为了保留模型中的BatchNorm因此你保存pb或者onnx时不要做BatchNorm融合)
    1. 一个 calibration_dataset.py 文件,里面包含了你的校准数据集的定义
    2. 一个 config.yaml 文件,里面包含了你模型的输入输出的相关信息

可以参考提供的 calibration_dataset.py 和 config.yaml 来实现

特别注意校准集中返回输入数据的顺序和config文件中输入的顺序应该是对应的

使用方法batch size可以根据你的模型调整
    python mnn_offline_quant.py --mnn_model origin_float_model.mnn --quant_model quant_model.mnn --batch_size 32

使用建议:
    1. 如果你的模型中卷积的激活是prelu的话使用relu/relu6代替prelu可能会取得更好的量化精度和推理速度这可能需要重新训练模型
    2. 如果模型的输入无法固定请将batch size设置为1并且calibration_dataset的返回值也使用实际输入值的形状


############################################################################


This is a python version of MNN offline quant tool, use this tool when:
    1. you can not use MNN offline quant tool (tools/quantization) to quantize your model, cases like multi-input, complecated preprocessing
    2. you can not use MNN's quant-aware-training (QAT) tool to quantize your model, because of MNN's limited training features

in order to use this tool, you need to provide:
    0. use --forTraining flag of MNNConvert to convert your model to MNN (this is mainly for preserving BatchNorm, 
        so you should NOT fuse BatchNorm when you save pb or onnx model files)
    1. a calibration_dataset.py file, in which you define your calibration dataset
    2. a config.yaml file, in which you provide information of inputs and outputs of your model

you can refer to the example file to write your own.

please Note, the order of returned input data in your calibration dataset should be aligned with the order of input your provide in your config.yaml file.

usage of the tool (you can adjust batch size according to your own model):
    python mnn_offline_quant.py --mnn_model origin_float_model.mnn --quant_model quant_model.mnn --batch_size 32

usage tips:
    1. if the activation function of conv is prelu in your model, use relu/relu6 instead of prelu may improve precision and inference speed of quantized model. re-training may be required.
    2. if the input shape can not be fixed, your should set batch_size=1, and the shape of returned values of calibration_dataset should be actual input's shape.