这是OpenVINO 2020 Post-Training Optimization Toolkit INT8量化工具的原理介绍和使用教程的翻译,原文档地址为:http://docs.openvinotoolkit.org/latest/_README.html
这个工具的主要功能是一个统一的量化工具。通常,此方法支持任意Bit(>=2)来表示权重和激活值。在量化过程中,会根据预先定义的硬件目标将FakeQuantize
操作自动插入到模型图中,以生成硬件友好的优化模型。然后,不同的量化算法可以调整FakeQuantize
参数或删除一些操作以满足精度标准。最后这个伪量化模型可以在运行时被解释并将其转换为真正的低精度模型,从而获得真正的性能改善。
该工具包提供了多种量化和辅助算法来帮助量化权重和激活图后的模型恢复精度。潜在地,算法可以形成独立的优化流水线去优化一个或者多个模型。但是,我们仅对以下两种用于8Bit量化的算法进行了验证,建议将其用于获得DNN模型量化稳定和可靠结果的方案。
量化是由量化范围和量化级数来参数化的。采样公式如下:
在这里插入图片描述
其中input_low
和input_high
代表量化范围,
代表四舍五入到最接近的整数。
该公式由在量化过程中调整的scale
参数来参数化:
在上面的规则中,level_low
和level_high
代表离散数值的范围。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
量化公式由作为可调参数的input_low
和input_range
参数化:
对于权重和激活图下面的量化模式被应用:
在这里插入图片描述
DefaultQuantization算法旨在执行快速且准确的神经网络的INT8量化。它包含三种依次应用给模型的算法:
该算法使用两阶段的统计信息收集程序,因此量化的间隔时间基本上取决于用于它的校准子集的大小。
该算法接受它所依赖的三种算法引入的所有参数。所有这些参数可以大致分为两组:必选和可选。
"name": "DefaultQuantization", // optimization algorithm name
"params": {
"preset": "performance", // Preset [performance (default), accuracy] which controls the quantization mode (symmetric and asymmetric respectively)
"stat_subset_size": 300, // Size of subset to calculate activations statistics used for quantization. The whole dataset is used if no parameter specified
}
"name": "DefaultQuantization", // optimization algorithm name
"params": {
/* Preset is a collection of optimization algorithm parameters that will specify to the algorithm
to improve which metric the algorithm needs to concentrate. Each optimization algorithm supports
[performance, accuracy] presets which control the quantization mode (symmetric and asymmetric respectively)*/
"preset": "accuracy",
"stat_subset_size": 300, // Size of subset to calculate activations statistics that can be used
// For quantization parameters calculation.
"ignored": {
"scope": [
"<NODE_NAME>" // List of nodes that are excluded from optimization
],
"operations": [ // List of types that are excluded from optimization
{
"type": "<NODE_TYPE>", // Type of ignored operation
"attributes": { // If attributes are defined they will be considered during the ignorance
"<NAME>": "<VALUE>" // Lists of values to filter by
}
}
]
},
/* Manually specified quantization parameters */
/* Quantization parameters for weights */
"weights": { // Weights quantization parameters used by MinMaxAlgorithm
"bits": 8, // Bit-width, default is 8
"mode": "symmetric", // Quantization mode, default is "symmetric"
"level_low": 0, // Minimum level in the integer range in which we quantize to, default is 0 for unsigned range, -2^(bit-1) - for signed
"level_high": 255, // Maximum level in the integer range in which we quantize to, default is 2^bits-1 for unsigned range, 2^(bit-1)-1 - for signed
"granularity": "perchannel", // Quantization scale granularity: ["pertensor" (default), "perchannel"]
"range_estimator": { // Range estimator that is used to get the quantization ranges and filter outliers based on the statistics
"max": { // Parameters to estimate top quantization border
"type": "quantile", // Estimator type: ["max" (default), "quantile"]
"outlier_prob": 0.0001 // Outlier probability used in the "quantile" estimator
},
"min": { // Parameters to estimate bottom quantization border (used only in asymmetric mode)
"type": "quantile", // Estimator type: ["max" (default), "quantile"]
"outlier_prob": 0.0001 // Outlier probability used in the "quantile" estimator
}
}
},
/* Quantization parameters for activations */
"activations": {
"range_estimator": { // Range estimator that is used to get the quantization ranges and filter outliers based on the statistics
"preset": "quantile",
/* OR */
/* minimum of quantization range */
/* maximum of quantization range */
"max": { // Parameters to estimate top quantization border
"aggregator": "mean", // Batch aggregation type: ["mean" (default), "max", "min", "median", "mean_no_outliers", "median_no_outliers", "hl_estimator"]
"type": "quantile", // Estimator type: ["max" (default), "quantile"]
"outlier_prob": 0.0001 // Outlier probability used in the "quantile" estimator
},
"min": { // Parameters to estimate top quantization border
"aggregator": "mean", // Batch aggregation type: ["mean" (default), "max", "min", "median", "mean_no_outliers", "median_no_outliers", "hl_estimator"]
"type": "quantile", // Estimator type [min, max, abs_max, quantile, abs_quantile]
"outlier_prob": 0.0001 // Outlier probability used in the "quantile" estimator
}
}
}
}
AccuracyAware算法旨在执行精确的Int8量化,并允许模型在保持精度下降的预定范围内如1%。和DefaultQuantization算法相比,这可能会导致性能下降,因为某些层可以被还原为原始精度。通常该算法包含以下步骤。
由于DefaultQuantization算法用作初始化,因此它的所有参数也是有效的并且可以指定。在这里,我们仅描述AccuracyAware特定参数:
"name": "AccuracyAwareQuantization", // compression algorithm name
"params": {
"metric_subset_ratio": 0.5, // A part of the validation set that is used to compare full-precision and quantized models
"ranking_subset_size": 300, // A size of a subset which is used to rank layers by their contribution to the accuracy drop
"max_iter_num": maxsize, // Maximum number of iterations of the algorithm (maximum of layers that may be reverted back to full-precision)
"maximal_drop": 0.005, // Maximum accuracy drop which has to be achieved after the quantization
"drop_type": "absolute", // Drop type of the accuracy metric: relative or absolute (default)
"use_prev_if_drop_increase": false, // Whether to use NN snapshot from the previous algorithm iteration in case if drop increases
"base_algorithm": "DefaultQuantization" // Base algorithm that is used to quantize model at the beginning
}
该工具包提供了通过API使用优化算法的功能。这意味着用户需要将优化代码嵌入到其自己的推理管道中,该管道通常是用于全精度模型的模型验证脚本。在这里,我们描述了如何将其嵌入ImageNet分类任务的示例。
为了使用优化功能,应实现优化过程所需的以下接口:
Sample演示了分类模型的量化,并使用上述API实施,可以在Sample文件夹中找到。
在下面的命令中,训练后优化工具目录<INSTALL_DIR>/deployment_tools/tools/post_training_optimization_toolkit
被当作<POT_DIR>
。<INSTALL_DIR>
是你的安装目录。
cd <POT_DIR>/libs/open_model_zoo/tools/downloader
python3 downloader.py --name <MODEL_NAME>
python3 converter.py --name <MODEL_NAME> --mo <PATH_TO_MODEL_OPTIMIZER>/mo.py
sample
文件夹并启动示例脚本cd <POT_DIR>/sample
python3 sample.py -m <PATH_TO_IR_XML> -a <IMAGENET_ANNOTATION_FILE> -d <IMAGENER_IMAGES>
可选的:你可以使用-w,--weights
来指定权重的目录。
这个工具包被设计为与配置文件一起使用,其中指定了优化所需要的所有参数。这些参数被组织为字典,并存储在JSON文件中。JSON文件允许使用jstyleson
Python包支持的注释。逻辑上,所有的参数都分为3
组:
本节仅包含3个参数:
"model_name"
模型名字,例如"MobileNetV2""model"
字符串参数,用于定义输入模型拓扑(.xml)的路径"weights"
字符串参数,用于定义输入模型权重(.bin)的路径当使用数据集进行DL模型推断时,该工具包依赖于深度学习准确性验证框架(AccuracyChecker)。因此,有两种方法可以定义其参数:
本节定义优化算法及其参数。上面已经讲过了。
为了快速入门,提供了一些流行的DL模型的配置文件示例。这些配置文件位于如下目录<INSTALL_DIR>/deployment_tools/tools/post_training_optimization_toolkit/configs/examples
,其中<INSTALL_DIR>
是OpenVINO的安装工具。有关如何使用示例配置文件运行训练后量化工具的详细信息请看下节。
请按照以下步骤,使用随英特尔®OpenVINO™工具包发行包一起提供的示例配置文件之一,运行训练后量化工具。
在下面的命令中,训练后优化工具目录<INSTALL_DIR>/deployment_tools/tools/post_training_optimization_toolkit
被当作<POT_DIR>
。<INSTALL_DIR>
是你的安装目录。
示例配置文件位于<POT_DIR>/configs/examples
目录下。
cd <POT_DIR>/libs/open_model_zoo/tools/downloader
python3 downloader.py --name <MODEL_NAME>
python3 converter.py --name <MODEL_NAME> --mo <PATH_TO_MODEL_OPTIMIZER>/mo.py
config
字段。如果要使用Open Model Zoo配置文件,请更新数据集定义文件<POT_DIR>/libs/open_model_zoo/tools/accuracy_checker/dataset_definitions.yml
。如果你已自定义预定义的引擎部分,则在POT配置需要时覆盖数据集和注释的路径。<POT_DIR>/libs/open_model_zoo/tools/accuracy_checker/dataset_definitions.yml.
与数据集的必要路径(如果您没有预定义的“引擎”部分)<POT_DIR>
目录中的配置文件启动训练后量化的工具:cd <POT_DIR>
python3 main.py -c <PATH_TO_POT_CONFIG>