---
title: accuracy_based_auto_calibration
description: "| 产品 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/devaids/amct/atlasamct_16_0276.html
sourcePath: /source/zh/canncommercial/900/devaids/amct/atlasamct_16_0276.html
indexId: 8b14f0bbc8bc97f3107de5875dc8c166e1c2c92c291702398af324e292a5c0aa65
---
# accuracy_based_auto_calibration

#### 产品支持情况

| 产品 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | x |
| Atlas A3 训练系列产品 / Atlas A3 推理系列产品 | x |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | x |
| Atlas 200I/500 A2 推理产品 | √ |
| Atlas 推理系列产品 | √ |
| Atlas 训练系列产品 | √ |


#### 功能说明

根据用户输入的模型、配置文件进行自动的校准过程，搜索得到一个满足目标精度的量化配置，输出可以在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 |


#### 返回值说明

无


#### 调用示例

```
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)
```

落盘文件说明：

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