accuracy_based_auto_calibration
功能说明
根据用户输入的模型、配置文件进行自动的校准过程,搜索得到一个满足目标精度的量化配置,输出既可以在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 instance  | 
strategy  | 
输入  | 
搜索满足精度要求的量化配置的策略,默认是二分法策略。  | 
数据类型:string或python instance 默认值:BinarySearch  | 
sensitivity  | 
输入  | 
评价每一层量化层对于量化敏感度的指标,默认是余弦相似度。  | 
数据类型:string或python instance 默认值:CosineSimilarity  | 
返回值说明
无。
函数输出
- 既可以在TensorFlow环境进行精度仿真又可以在昇腾AI处理器做推理的pb模型文件。
 - 量化因子记录文件、量化配置文件、层相似度文件以及自动量化回退历史记录文件。
 
调用示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  | 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)  | 
父主题: 训练后量化接口