昇腾 - 用ATC(Ascend Tensor Compiler,简称ATC)工具进行模型转换
flyfish
昇腾 - 用ATC(Ascend Tensor Compiler,简称ATC)工具进行模型转换
flyfish
昇腾张量编译器(Ascend Tensor Compiler,简称ATC)是异构计算架构CANN体系下的模型转换工具, 它可以将开源框架的网络模型以及Ascend IR定义的单算子描述文件(json格式)转换为昇腾AI处理器支持的.om格式离线模型。
以Orange Pi AI Pro 开发板 举例子
atc --model=yolov5s.onnx --framework=5 --output=yolov5s --input_shape="images:1,3,640,640" --soc_version=Ascend310B4 --insert_op_conf=aipp.cfg
执行npu-smi info命令进行查询,在查询到的“Name”前增加Ascend信息
例如查找的是310B4,那么参数就是Ascend310B4
参数 soc_version
参数 framework
PyTorch框架的pth模型转化为ONNX格式的模型
这里用的ONNX,所以参数值是5
0:Caffe
1:MindSpore
3:TensorFlow
5:ONNX
参数 output
存放转换后的离线模型的路径以及文件名
参数 insert_op_conf
插入算子的配置文件路径与文件名,例如AIPP预处理算子
aipp.cfg的内容
aipp_op{
aipp_mode:static
input_format : YUV420SP_U8
src_image_size_w : 640
src_image_size_h : 640
csc_switch : true
rbuv_swap_switch : false
matrix_r0c0 : 256
matrix_r0c1 : 0
matrix_r0c2 : 359
matrix_r1c0 : 256
matrix_r1c1 : -88
matrix_r1c2 : -183
matrix_r2c0 : 256
matrix_r2c1 : 454
matrix_r2c2 : 0
input_bias_0 : 0
input_bias_1 : 128
input_bias_2 : 128
crop: true
load_start_pos_h : 0
load_start_pos_w : 0
crop_size_w : 640
crop_size_h : 640
min_chn_0 : 0
min_chn_1 : 0
min_chn_2 : 0
var_reci_chn_0: 0.0039215686274509803921568627451
var_reci_chn_1: 0.0039215686274509803921568627451
var_reci_chn_2: 0.0039215686274509803921568627451
}
-
aipp_mode:static
AIPP(Artificial Intelligence Pre-Processing)人工智能预处理,用于在AI Core上完成数据预处理,包括改变图像尺寸、色域转换(转换图像格式)、减均值/乘系数(改变图像像素),数据预处理之后再进行真正的模型推理。
静态AIPP:模型转换时设置AIPP模式为静态,同时设置AIPP参数,模型生成后,AIPP参数值被保存在离线模型中,每次模型推理过程采用固定的AIPP预处理参数进行处理,而且在之后的推理过程中无法通过业务代码进行直接的修改。
动态AIPP:模型转换时设置AIPP模式为动态,每次在执行推理前,根据需求动态修改AIPP参数值,然后在模型执行时可使用不同的AIPP参数。 -
input_format : YUV420SP_U8
AIPP支持的图像输入格式包括:YUV420SP_U8、RGB888_U8、XRGB8888_U8、YUV400_U8。 -
csc_switch : true
色域转换,用于将输入的图片格式,转换为模型需要的图片格式,在使能AIPP功能时,通过csc_switch参数控制色域转换功能是否开启
YVU420SP_U8转RGB
输入数据为JPEG图像
aipp_op {
aipp_mode: static
input_format : YUV420SP_U8
csc_switch : true
rbuv_swap_switch : true
matrix_r0c0 : 256
matrix_r0c1 : 0
matrix_r0c2 : 359
matrix_r1c0 : 256
matrix_r1c1 : -88
matrix_r1c2 : -183
matrix_r2c0 : 256
matrix_r2c1 : 454
matrix_r2c2 : 0
input_bias_0 : 0
input_bias_1 : 128
input_bias_2 : 128
}
-
crop: true
Crop/Padding配置说明,这里只用了Crop在配置文件中指出裁剪的起始位置左上点坐标loadStartPosW、loadStartPosH以及裁剪后的图像大小crop_size_w, crop_size_h。在padding环节,我们需要指明在裁剪后的图像四周padding的尺寸,即left_padding_size、right_padding_size、top_padding_size和bottom_padding_size。而经过图像尺寸改变之后最终图片大小,需要跟模型文件输入的图像大小即–input_shape中的宽和高相等
-
AIPP归一化
min_chn_0 : 0
min_chn_1 : 0
min_chn_2 : 0
var_reci_chn_0: 0.0039215686274509803921568627451
var_reci_chn_1: 0.0039215686274509803921568627451
var_reci_chn_2: 0.0039215686274509803921568627451
归一化就是要把需要处理的数据经过处理后限制在一定范围内,符合模型要求。 AIPP支持的归一化设置,通过减均值和乘系数的操作完成,其中,mean_chn i表示每个通道的均值,min_chn_i表示每个通道的最小值,var_reci_chn表示每个通道方差的倒数,各通路的这三个值都是需要进行配置的参数。
pixel_out_chx(i)=[pixel_in_chx(i)-mean_chn_i-min_chn_i]*var_reci_chn
给定的数值 0.0039… 等于 1/255
参考
https://www.hiascend.com/document/detail/zh/canncommercial/80RC2/devaids/auxiliarydevtool/auxiliarydevtool_0000.html
举例说明遇到的问题和如何解决的
atc --model=yolov5s.onnx --framework=5 --output=yolov5s --input_shape="images:1,3,640,640" --soc_version=Ascend310P3 --insert_op_conf=aipp_opencv.cfg
错误提示
ATC run failed, Please check the detail log, Try 'atc --help' for more information
Failed to import Python module [AttributeError: `np.float_` was removed in the NumPy 2.0 release. Use `np.float64` instead..].
Solution: Check that all required components are properly installed and the specified Python path matches the Python installation directory. (If the path does not match the directory, run set_env.sh in the installation package.)
TraceBack (most recent call last):
AOE Failed to call InitCannKB
[GraphOpt][InitializeInner][InitTbeFunc] Failed to init tbe.[FUNC:InitializeInner][FILE:tbe_op_store_adapter.cc][LINE:1719]
[SubGraphOpt][PreCompileOp][InitAdapter] InitializeAdapter adapter [tbe_op_adapter] failed! Ret [4294967295][FUNC:InitializeAdapter][FILE:op_store_adapter_manager.cc][LINE:79]
[SubGraphOpt][PreCompileOp][Init] Initialize op store adapter failed, OpsStoreName[tbe-custom].[FUNC:Initialize][FILE:op_store_adapter_manager.cc][LINE:120]
[FusionMngr][Init] Op store adapter manager init failed.[FUNC:Initialize][FILE:fusion_manager.cc][LINE:117]
PluginManager InvokeAll failed.[FUNC:Initialize][FILE:ops_kernel_manager.cc][LINE:82]
OpsManager initialize failed.[FUNC:InnerInitialize][FILE:gelib.cc][LINE:234]
GELib::InnerInitialize failed.[FUNC:Initialize][FILE:gelib.cc][LINE:162]
root用户
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy==1.23.5 --user
非root用户
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy==1.23.5
更多推荐
所有评论(0)