文档
注册

accuracy_based_auto_calibration

功能说明

根据用户输入的模型、配置文件进行自动的校准过程,搜索得到一个满足目标精度的量化配置,输出可以在Caffe环境下做精度仿真的fake_quant模型,和可在昇腾AI处理器上做推理的deploy模型。

约束说明

无。

函数原型

accuracy_based_auto_calibration(model_file,weights_file,model_evaluator,config_file,record_file,save_dir,strategy='BinarySearch',sensitivity='CosineSimilarity')

参数说明

参数名

输入/返回值

含义

使用限制

model_file

输入

用户Caffe模型的定义文件,格式为.prototxt。

数据类型:string

weights_file

输入

用户训练好的的Caffe模型权重文件,格式为.caffemodel。

数据类型:string

model_evaluator

输入

自动量化进行校准和评估精度的python实例。

数据类型:python实例

config_file

输入

用户生成的量化配置文件。

数据类型:string

record_file

输入

存储量化因子的路径,如果该路径下已存在文件,则会被重写。

数据类型:string

save_dir

输入

模型存放路径。

该路径需要包含模型名前缀,例如./quantized_model/*model

数据类型:string

strategy

输入

搜索满足精度要求的量化配置的策略,默认是二分法策略。

数据类型:string或python实例

默认值:BinarySearch

sensitivity

输入

评价每一层量化层对于量化敏感度的指标,默认是余弦相似度。

数据类型:string或python实例

默认值:CosineSimilarity

返回值说明

无。

函数输出

  • 精度仿真模型文件:一个模型定义文件,一个模型权重文件,文件名中包含fake_quant;模型可在Caffe环境下做推理实现量化精度仿真。
  • 部署模型文件:一个模型定义文件,一个模型权重文件,文件名中包含deploy;模型经过ATC工具转换后可部署到昇腾AI处理器上。
  • 量化因子记录文件:在接口中的record_file中写入量化层的权重量化因子(scale_w,offset_w)。
  • 量化信息文件:该文件记录了昇腾模型压缩工具插入的量化算子位置以及算子融合信息,用于量化后的模型进行精度比对使用。
  • 敏感度信息文件:该文件记录了待量化层对于量化的敏感度信息,根据该信息进行量化回退层的选择。
  • 自动量化回退历史记录文件:记录的回退层的信息。

调用示例

import amct_caffe as amct 
from amct_caffe.common.auto_calibration import AutoCalibrationEvaluatorBase
from amct_caffe.common.auto_calibration import BinarySearchStrategy
from amct_caffe.common.auto_calibration import CosineSimilaritySensitivity
class AutoCalibrationEvaluator(AutoCalibrationEvaluatorBase):
    def __init__(self):
        """
            evaluate_batch_num is the needed batch num for evaluating
            the model. Larger evaluate_batch_num is recommended, because
            the evaluation metric of input model can be more precise
            with larger eval dataset.
        """
        super().__init__()
 
    def calibration(self, model_file, weights_file):
        """"
        Function:
            do the calibration with model
        Parameter:
            model_file: the prototxt model define file of caffe model
            weights_file: the binary caffemodel file of caffe model
        """
        run_caffe_model(args, model_file, weights_file, CALIBRATION_BATCH_NUM)
 
    def evaluate(self, model_file, weights_file):
        """"
        Function:
            evaluate the model with batch_num of data, return the eval
            metric of the input model, such as top1 for classification
            model, mAP for detection model and so on.
        Parameter:
            model_file: the prototxt model define file of caffe model
            weights_file: the binary caffemodel file of caffe model
        """
        return do_benchmark_test(args, model_file, weights_file, args.iterations)
 
    def metric_eval(self, original_metric, new_metric):
        """
        Function:
            whether the metric of new fake quant model can satisfy the
            requirement
        Parameter:
            original_metric: the metric of non quantized model
            new_metric: the metric of new quantized model
        """
        # the loss of top1 acc need to be less than 0.2%
        loss = original_metric - new_metric
        if loss * 100 < 0.2:
            return True, loss
        return False, loss
 
    # step 1: create the quant config file
    config_json_file = './config.json'
    skip_layers = []
    batch_num = CALIBRATION_BATCH_NUM
    activation_offset = True
    amct.create_quant_config(config_json_file, model_file, weights_file,
                        skip_layers, batch_num, activation_offset)
 
    scale_offset_record_file = os.path.join(TMP, 'scale_offset_record.txt')
    result_path = os.path.join(RESULT, 'MobileNetV2')
    evaluator = AutoCalibrationEvaluator()
 
    # step 2: start the accuracy_based_auto_calibration process
    amct.accuracy_based_auto_calibration(
        args.model_file,
        args.weights_file,
        evaluator,
        config_json_file,
        scale_offset_record_file,
        result_path)
搜索结果
找到“0”个结果

当前产品无相关内容

未找到相关内容,请尝试其他搜索词