MNN/tools/MNNPythonOfflineQuant
tianhang.yth d85952d826 sync from internal repo 2021-04-28 18:02:10 +08:00
..
ReadMe.txt sync from internal repo 2021-04-28 18:02:10 +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 sync from internal repo 2021-04-28 18:02:10 +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

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

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