本章节介绍 ONNX 模型如何转化为 OM 模型,并在昇腾AI处理器上做离线推理。

昇腾张量编译器(Ascend Tensor Compiler,简称ATC)是异构计算架构CANN体系下的模型转换工具, 它可以将开源框架的网络模型或Ascend IR定义的单算子描述文件(json格式)转换为昇腾AI处理器支持的.om格式离线模型。
ATC功能详见:https://www.hiascend.com/document/detail/zh/canncommercial/80...

设置环境变量

ATC工具内置在cann-toolkit中,因此使用时需设置ascend-toolkit环境变量:

source /usr/local/Ascend/ascend-toolkit/set_env.sh

ONNX转静态OM

命令格式

atc --framework=5 --model=XXX --output=XXX \
    --input_format=XXX --input_shape=model_input_name:model_input_shape \
    --log=error --soc_version=Ascend${chip_name}

参数说明

--framework : 5代表ONNX模型
--model : ONNX模型文件路径与文件名
--output : 输出的OM模型文件路径与文件名
--input_format : 可以选NCHW or ND
--input_shape : 模型输入的shape
--log : log日志有debug/info/warning/error/null级别
--soc_version : 指定模型转换时昇腾AI处理器的版本(可通过npu-smi info命令查看处理器版本)
特别说明:把log设置成debug,然后配合export ASCEND_GLOBAL_LOG_LEVEL=0 和 export ASCEND_SLOG_PRINT_TO_STDOUT=1可以重定向具体日志,方便定位问题。

执行转换

atc --framework=5 --model=inference/det_onnx/model.onnx --output=inference/om/det \
    --input_format=NCHW --input_shape=x:1,3,224,224 \
    --log=error --soc_version=Ascend910B4

转换成功:
01_ONNX转静态OM

输出结果

执行完毕后,OM 模型会被保存在 ./inference/om/路径下:

inference/om/
    └── det.om           # OM模型文件

常见问题

命令行格式问题

报错信息:
02_命令行格式问题
解决方法:删除“=”后的空格。

input_shape设置错误

报错信息:
03_input_name设置错误
解决方法:修改input_shape中的input_name改成x(以模型实际的input_name为准)。

查看模型input_shape

使用ONNX模型可视化工具(https://netron.app/)查看模型的input_shape,如下图所示:

  • 进入netron工具,加载model.onnx模型:
    04_加载model.onnx模型
  • 点击“properties”
    05_点击“properties”
  • 查看模型input_shape
    06_查看模型input_shape
    INPUTS参数说明:
    x:input_name
    p2o.DynamicDimension.0:批次大小,对应NCHW格式中B,可设置动态
    3:通道数 (排列方式BGR) ,对应NCHW格式中C,为固定值
    p2o.DynamicDimension.1:图像高度,对应NCHW格式中H,可设置动态
    p2o.DynamicDimension.2:图像宽度,对应NCHW格式中W,可设置动态

    动态BatchSize转OM

    命令格式

atc --model=$HOME/module/resnet50.onnx  --framework=5  \
    --output=$HOME/module/out/resnet50 --soc_version=<soc_version> \
    --input_shape="input_name:-1,3,224,224"  --dynamic_batch_size="1,2,4,8" \
    --log=error

特定参数说明:
dynamic_batch_size: 输入动态BatchSize的具体值

执行转换

atc --model=./inference/det_onnx/model.onnx  --framework=5 \
    --output=./inference/om/dynamic_batch_size_det --soc_version=Ascend910B4 \
    --input_shape="x:-1,3,256,256"  --dynamic_batch_size="1,2,4,8" \
    --log=error

输出结果

执行完毕后,OM 模型会被保存在 ./inference/om/路径下:

inference/om/
    └── dynamic_batch_size_det.om           # OM模型文件

转换成功:
07_动态BatchSize转OM

常见问题

input_shape设置错误

错误命令:

atc --model=./inference/det_onnx/model.onnx  --framework=5 \
    --output=./inference/om/dynamic_batch_size_det --soc_version=Ascend910B4 \
    --input_shape="x:-1,3,-1,-1"  --dynamic_batch_size="1,2,4,8"  \
    --log=error

报错信息:
08_非BatchSize参数设为-1
错误原因:动态BatchSize场景,除BatchSize外,其它参数不能设置为-1
解决方法:--input_shape设置为:"x:-1,3,256,256"

动态分辨率转OM

命令格式

atc --model=$HOME/module/resnet50.onnx  --framework=5 \
    --output=$HOME/module/out/resnet50 --soc_version=<soc_version>  \
    --input_shape="input_name:1,3,-1,-1"  --dynamic_image_size="224,224;448,448" \
    --log=error

特定参数说明:
input_format : 选ND
dynamic_image_size : 输入动态分辨率的具体值

执行转换

atc --model=./inference/det_onnx/model.onnx  --framework=5 \
    --output=./inference/om/dynamic_image_size_det --soc_version=Ascend910B4 \
    --input_shape="x:1,3,-1,-1"  --dynamic_image_size="224,224;448,448" \
    --log=error

转换成功:
09_动态分辨率转OM

输出结果

执行完毕后,OM 模型会被保存在 ./inference/om/路径下:

inference/om/
    └── dynamic_image_size_det.om           # OM模型文件

常见问题

input_shape设置错误

错误命令:

atc --model=./inference/det_onnx/model.onnx  --framework=5 \
    --output=./inference/om/dynamic_image_size_det --soc_version=Ascend910B4 \
    --input_shape="x:-1,3,-1,-1"  --dynamic_image_size="224,224;448,448" \
    --log=error

报错信息:
10_非分辨率参数设为-1
错误原因:动态分辨率场景,除分辨率外,其它参数不能设置为-1
解决方法:--input_shape设置为:"x:1,3,-1,-1"

动态维度转OM

命令格式

atc --model=$HOME/module/resnet50.onnx --framework=5 \
    --output=$HOME/module/out/resnet50 --soc_version=<soc_version>  \
    --input_shape="input_name:-1,3,-1,-1" --dynamic_dims="1,224,224;8,448,448" \
    --input_format=ND --log=error

特定参数说明:
input_format : 选ND
dynamic_dims : 输入动态维度的具体值

执行转换

atc --model=./inference/det_onnx/model.onnx  --framework=5 \
    --output=./inference/om/dynamic_dims_det --soc_version=Ascend910B4 \
    --input_shape="x:-1,3,-1,-1"  --dynamic_dims="1,224,224;8,448,448" \
    --input_format=ND --log=error

转换成功:
11_动态维度转OM

输出结果

执行完毕后,OM 模型会被保存在 ./inference/om/路径下:
inference/om/
    └── dynamic_dims_det.om          # OM模型文件

动态shape转OM

命令格式

atc --framework=5 --model=XXX --output=XXX --input_format=ND \
    --input_shape="input_name_0:1~10,32,208,208;input_name_1:16,64,100~208,100~208" \
    --log=error --soc_version=Ascend${chip_name}

特定参数说明:
input_format : 选ND
input_shape : 设置输入的动态shape范围

执行转换

atc --model=./inference/det_onnx/model.onnx  --framework=5 \
    --output=./inference/om/dynamic_shape_det --soc_version=Ascend910B4 \
    --input_shape="x:1~8,3,224~448,224~448" \
    --input_format=ND --log=error

转换成功:
12_动态shape转OM

输出结果

执行完毕后,OM 模型会被保存在 ./inference/om/路径下:

inference/om/
    └── dynamic_shape_det_linux_aarch64.om          # OM模型文件

讲道义的遥控器
1 声望0 粉丝