quantize_model_ascend
功能说明
训练后量化接口,将输入的待量化的图结构按照给定的量化配置文件进行量化处理,在传入的图结构中插入量化相关的算子,生成量化因子记录文件record_file,并返回量化处理新增的算子列表。
函数原型
calibration_graph, calibration_outputs=quantize_model_ascend(graph, outputs, config_file, record_file)
参数说明
参数名  | 
输入/返回值  | 
含义  | 
使用限制  | 
|---|---|---|---|
graph  | 
输入  | 
用户传入的待量化模型的tf.Graph图。  | 
数据类型:tf.Graph 使用约束:graph必须为推理图,图中不能包含训练模式的算子,例如FusedBatchNormV3算子的is_training必须为False。图中必须加载了训练好的权重。  | 
outputs  | 
输入  | 
graph中输出算子的列表。  | 
数据类型:list,列表中元素类型为string  | 
config_file  | 
输入  | 
用户生成的量化配置文件,用于指定模型tf.Graph图中量化层的配置情况。  | 
数据类型:string  | 
record_file  | 
输入  | 
量化因子记录文件路径及名称。  | 
数据类型:string  | 
calibration_graph  | 
返回值  | 
工具修改后的图,插入了量化算子。  | 
数据类型:tf.Graph  | 
calibration_outputs  | 
返回值  | 
calibration_graph的输出算子列表。  | 
数据类型:list,列表中元素类型为string  | 
返回值说明
网络中量化的层名信息列表。
quantize_model_ascend对图做了BN融合,如果网络模型的outputs包含BN层,且该BN层也做融合,那么网络的输出节点会发生变化。例如,Conv+BN(或Conv+BiasAdd+BN)融合后为Conv+BiasAdd,BN等价的输出节点为BiasAdd节点。
调用示例
1 2 3 4 5 6 7 8 9  | import amct_tensorflow as amct # 建立待量化的网络结构 network = build_network() # 插入量化API calibration_graph, calibration_outputs = amct.quantize_model_ascend( graph=tf.get_default_graph(), config_file="./configs/config.json", record_file="./record_scale_offset.txt")  |