create_quant_config
功能说明
根据图的结构找到所有可量化的层(包括全连接层(InnerProduct)、卷积层(Convolution dilation为1、filter维度为4)、反卷积层(Deconvolution dilation为1、filter维度为4)、平均下采样层Pooling(下采样方式为AVE,且非global pooling)),自动生成量化配置文件,并将可量化层的量化配置信息写入量化配置文件。
约束说明
由于数据格式转换,生成的量化配置文件中与简单配置文件中的量化参数,数值上不完全一致,但不影响精度。
函数原型
create_quant_config(config_file, model_file, weights_file, skip_layers=None, batch_num=1, activation_offset=True, config_defination=None)
参数说明
参数名 |
输入/返回值 |
含义 |
使用限制 |
---|---|---|---|
config_file |
输入 |
待生成的量化配置文件存放路径及名称。 如果存放路径下已经存在该文件,则调用该接口时会覆盖已有文件。 |
数据类型:string |
model_file |
输入 |
用户Caffe模型的定义文件,格式为.prototxt。 |
数据类型:string |
weights_file |
输入 |
用户训练好的的Caffe模型权重文件,格式为.caffemodel。 |
数据类型:string |
skip_layers |
输入 |
可量化但不需要量化层的层名。 |
默认值:None 数据类型:list,列表中元素类型为string |
batch_num |
输入 |
量化使用的batch数量,即使用多少个batch的数据生成量化因子。 |
数据类型:int 取值范围:大于0的整数 默认值:1 batch_num不宜过大,batch_num与batch_size的乘积为量化过程中使用的图片数量,过多的图片会占用较大的内存。 |
activation_offset |
输入 |
数据量化是否带offset。 |
默认值:true 数据类型:bool |
config_defination |
输入 |
基于calibration_config.proto文件生成的简易量化配置文件quant.cfg,calibration_config.proto文件所在路径为:昇腾模型压缩工具安装目录/amct_caffe/proto/calibration_config.proto。 calibration_config.proto文件参数解释以及生成的quant.cfg简易量化配置文件样例请参见简易量化配置文件说明。 |
默认值:None 数据类型:字符串 当取值为None时,使用输入参数生成配置文件;否则,忽略输入的其他量化参数(skip_layers,batch_num,activation_offset),根据简易量化配置文件参数config_defination生成json格式的配置文件。 |
返回值说明
无。
函数输出
输出一个json格式的量化配置文件(重新执行量化时,该接口输出的量化配置文件将会被覆盖)。
举例:graph中只有layer_name1和layer_name2支持量化,使用create_quant_config(config_file, model_file, weights_file, skip_layers=None, batch_num=2,activation_offset=True, config_defination=None)生成的量化配置文件如下所示。
{ "version":1, "batch_num":2, "activation_offset":true, "do_fusion":true, "skip_fusion_layers":[], "layer_name1":{ "quant_enable":true, "activation_quant_params":{ "max_percentile":0.999999, "min_percentile":0.999999, "search_range":[ 0.7, 1.3 ], "search_step":0.01 }, "weight_quant_params":{ "wts_algo":"arq_quantize", "channel_wise":true } }, "layer_name2":{ "quant_enable":true, "activation_quant_params":{ "max_percentile":0.999999, "min_percentile":0.999999, "search_range":[ 0.7, 1.3 ], "search_step":0.01 }, "weight_quant_params":{ "wts_algo":"arq_quantize", "channel_wise":false } } }
调用示例
from amct_caffe import create_quant_config # 通过参数来生成量化配置文件 create_quant_config(config_file="./configs/config.json", model_file="./pretrained_model/model.prototxt", weights_file="./pretrained_model/model.caffemodel", skip_layers=None, batch_num=1, activation_offset=True) # 通过简易配置文件来生成量化配置文件 create_quant_config(config_file="./configs/config.json", model_file="./pretrained_model/model.prototxt", weights_file="./pretrained_model/model.caffemodel", config_defination="./configs/quant.cfg")