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