深度學習演算法優化系列十六 | OpenVINO Post-Training Optimization文檔翻譯
- 2020 年 3 月 5 日
- 筆記
這是OpenVINO 2020 Post-Training Optimization Toolkit INT8量化工具的原理介紹和使用教程的翻譯,原文檔地址為:http://docs.openvinotoolkit.org/latest/_README.html
Quantization
這個工具的主要功能是一個統一的量化工具。通常,此方法支援任意Bit(>=2)來表示權重和激活值。在量化過程中,會根據預先定義的硬體目標將FakeQuantize
操作自動插入到模型圖中,以生成硬體友好的優化模型。然後,不同的量化演算法可以調整FakeQuantize
參數或刪除一些操作以滿足精度標準。最後這個偽量化模型可以在運行時被解釋並將其轉換為真正的低精度模型,從而獲得真正的性能改善。
量化演算法
該工具包提供了多種量化和輔助演算法來幫助量化權重和激活圖後的模型恢復精度。潛在地,演算法可以形成獨立的優化流水線去優化一個或者多個模型。但是,我們僅對以下兩種用於8Bit量化的演算法進行了驗證,建議將其用於獲得DNN模型量化穩定和可靠結果的方案。
- DefaultQuantization 用作默認方法以獲得快速並且在大多數情況下比較準確的int8量化結果。
- AccuracyAwareQuantization 允許在量化後精度下降在預定的範圍內,同事犧牲一定的性能提升。可能需要更多的時間量化。
量化準則
量化是由量化範圍和量化級數來參數化的。取樣公式如下:

在這裡插入圖片描述
其中input_low
和input_high
代表量化範圍,

代表四捨五入到最接近的整數。
對稱量化
該公式由在量化過程中調整的scale
參數來參數化:


在上面的規則中,level_low
和level_high
代表離散數值的範圍。
- 對於權重:

在這裡插入圖片描述
- 對於無符號激活值:

在這裡插入圖片描述
- 對於帶符號激活值:

在這裡插入圖片描述
非對稱量化
量化公式由作為可調參數的input_low
和input_range
參數化:

對於權重和激活圖下面的量化模式被應用:

在這裡插入圖片描述
DefaultQuantization
DefaultQuantization演算法旨在執行快速且準確的神經網路的INT8量化。它包含三種依次應用給模型的演算法:
- ActivationChannelAlignment 用作量化之前的預備步驟,並允許你調整卷積層的輸出激活範圍,以減少量化誤差。
- MinMaxQuantization 這是一種原始的量化方法,可根據指定的目標硬體自動將FakeQuantize操作插入模型圖中,並使用在校準數據集上收集的統計資訊將其初始化。
- BiasCorrection 基於卷積層和全連接層的量化誤差來調整該層的偏置,以使整體誤差無偏。
該演算法使用兩階段的統計資訊收集程式,因此量化的間隔時間基本上取決於用於它的校準子集的大小。
參數
該演算法接受它所依賴的三種演算法引入的所有參數。所有這些參數可以大致分為兩組:必選和可選。
- 必選參數包括以下示例中描述的少量參數:
"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 } } } }
AccuracyAwareQuantization
概述
AccuracyAware演算法旨在執行精確的Int8量化,並允許模型在保持精度下降的預定範圍內如1%。和DefaultQuantization演算法相比,這可能會導致性能下降,因為某些層可以被還原為原始精度。通常該演算法包含以下步驟。
- 使用DefaultQuantization演算法對模型進行完全量化。
- 在驗證集的子集上比較量化模型和全精度模型,以便找到目標精度度量中的不匹配項。基於不匹配項提取排名子集。
- 為了獲得每個量化層對精度下降的貢獻,執行了逐層排名。
- 根據排名,最」有問題「的層將被還原為原始精度。進行這個更改之後,將對完整驗證集上獲得的模型進行評估,以獲取新的精度下降。
- 如果所有預定義精度指標均滿足,則演算法結束。否則,它將繼續還原下一個「有問題」層。
- 某次恢復可能無法獲得任何準確性的提高,甚至會降低準確性。然後按步驟3中所述重新排名。
參數
由於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 }
Post-training Optimization Toolkit API
該工具包提供了通過API使用優化演算法的功能。這意味著用戶需要將優化程式碼嵌入到其自己的推理管道中,該管道通常是用於全精度模型的模型驗證腳本。在這裡,我們描述了如何將其嵌入ImageNet分類任務的示例。
為了使用優化功能,應實現優化過程所需的以下介面:
- 引擎:自定義引擎類,允許進行模型推斷。我們基於DLDT IE非同步API創建了此類,該類也可以在用戶應用程式中重用。可以在壓縮目錄的engines文件夾中找到此引擎的示例。
- 數據載入器 :負責校準數據集的載入。在示例文件夾中可以找到ImageNet DataLoader的示例。
- 評價方式:如果使用準確性感知優化方法(例如AccuracyAwareQuantization演算法)並實現準確性度量計算,則需要使用此方法。可以在示例文件夾中找到「Top 1精確度」度量標準的示例。
- Loss:僅在優化方法需要按樣本損失計算時使用。
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
- 啟動下載程式工具以從Open Model Zoo存儲庫下載模型
python3 downloader.py --name <MODEL_NAME>
- 啟動轉換器工具以生成IRv10模型
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)。因此,有兩種方法可以定義其參數:
- 請參考由YAML文件表示的現有AccuracyChecker配置文件。 它可以是用於精確模型驗證的文件。在這種情況下,僅應定義一個參數:
- "config" – AccuracyChecker配置文件的路徑
- 直接在JSON文件中定義所有必需的AccuracyChecker參數。有關更多詳細資訊,請參考相應的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
- 啟動下載程式工具以從Open Model Zoo存儲庫下載模型
python3 downloader.py --name <MODEL_NAME>
- 啟動轉換器工具以生成IRv10模型
python3 converter.py --name <MODEL_NAME> --mo <PATH_TO_MODEL_OPTIMIZER>/mo.py
- 更新要啟動的示例配置文件中的模型/權重欄位。
- 如果需要,使用AccuracyChecker配置文件的路徑更新所需示例配置文件的
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>