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

#### 产品支持情况

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


#### 功能说明

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


#### 函数原型

```
accuracy_based_auto_calibration(model_file,outputs,record_file,config_file,save_dir,evaluator,strategy='BinarySearch',sensitivity='CosineSimilarity')
```


#### 参数说明

| 参数名 | 输入/输出 | 说明 |
| --- | --- | --- |
| model\_file | 输入 | 含义：用户未量化的TensorFlow模型的定义文件，格式为.pb。 数据类型：string |
| outputs | 输入 | 含义：输出节点的字符串列表。 数据类型：string |
| config\_file | 输入 | 含义：用户生成的量化配置文件。 数据类型：string |
| record\_file | 输入 | 含义：存储量化因子的路径，如果该路径下已存在文件，则会被重写。 数据类型：string |
| save\_dir | 输入 | 含义：模型存放路径。 该路径需要包含模型名前缀，例如./quantized\_model/\*model。 数据类型：string |
| evaluator | 输入 | 含义：自动量化进行校准和评估精度的Python实例。 数据类型：Python实例 |
| strategy | 输入 | 含义：搜索满足精度要求的量化配置的策略，默认是二分法策略。 数据类型：string或Python实例 默认值：BinarySearch |
| sensitivity | 输入 | 含义：评价每一层量化层对于量化敏感度的指标，默认是余弦相似度。 数据类型：string或Python实例 默认值：CosineSimilarity |


#### 返回值说明

无


#### 调用示例

```
import amct_tensorflow as amct
from amct_tensorflow.accuracy_based_auto_calibration import accuracy_based_auto_calibration

def main():
    args_check(args)
    outputs = [PREDICTIONS]
    record_file = os.path.join(RESULT_DIR, 'record.txt')
    config_file = os.path.join(RESULT_DIR, 'config.json')
    with tf.io.gfile.GFile(args.model, mode='rb') as model:
        graph_def = tf.compat.v1.GraphDef()
        graph_def.ParseFromString(model.read())
    tf.import_graph_def(graph_def, name='')
    graph = tf.compat.v1.get_default_graph()
    amct.create_quant_config(config_file, graph)
    save_dir = os.path.join(RESULT_DIR, 'MobileNetV2')
    evaluator = MobileNetV2Evaluator(args.dataset, args.keyword, args.num_parallel_reads, args.batch_size)
    accuracy_based_auto_calibration(args.model, outputs, record_file, config_file, save_dir, evaluator)
```

落盘文件说明：

- 既可以在TensorFlow环境进行精度仿真又可以在AI处理器做推理的pb模型文件。
- 量化因子记录文件、量化配置文件、层相似度文件以及自动量化回退历史记录文件。
