当前位置: 首页 > news >正文

AX620Q上模型部署流程

1.安装依赖

pip install onnx
pip install onnxruntime
pip install onnxsim
pip install ultralytics

2.导出onnx模型

下面的 python 脚本用于 下载、运行、导出 YOLOv8s 的 ONNX 模型
export_for_AX620.py

from ultralytics import YOLO
# Load a model
model = YOLO("yolov8s.pt")
model.info()# Use the model
results = model("/home/qtang/images/ssd_dog.jpg")# Save the results
results[0].save("yolov8s-result.jpg")# Export to onnx with simplify
model.export(format='onnx', simplify=True)

3.ONNX 模型优化

在使用 pulsar2 工具链转换之前,先对前一步获得的 yolov8s.onnx 模型进行必要的计算图优化,便于提高模型部署效率。
export_sub_AX620.py

import onnx
input_path = "yolov8s.onnx"
output_path = "yolov8s-cut.onnx"
input_names = ["images"]
output_names = ["/model.22/Concat_output_0", "/model.22/Concat_1_output_0", "/model.22/Concat_2_output_0"]onnx.utils.extract_model(input_path, output_path, input_names, output_names)

yolov8-seg是我推导出来的,没有官方教程,暂定为测试

/model.22/Concat_1_output_0   1*144*80*80
/model.22/Concat_2_output_0   1*144*40*40
/model.22/Concat_3_output_0   1*144*20*20/model.22/cv4.0/cv4.0.2/Conv_output_0    1*32*80*80
/model.22/cv4.1/cv4.1.2/Conv_output_0    1*32*40*40
/model.22/cv4.2/cv4.2.2/Conv_output_0    1*32*20*20output1   1*32*160*160将上述模型优化代码中的output_names改为:
output_names = ["/model.22/Concat_1_output_0", "/model.22/Concat_2_output_0", "/model.22/Concat_3_output_0","/model.22/cv4.0/cv4.0.2/Conv_output_0","/model.22/cv4.1/cv4.1.2/Conv_output_0","/model.22/cv4.2/cv4.2.2/Conv_output_0","output1"]

安装docker

# 更新软件源
sudo apt update  # 安装所需依赖
sudo apt -y install apt-transport-https ca-certificates curl software-properties-common# 安装 GPG 证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
如果`安装 GPG 证书`的步骤失败,可以直接通过浏览器从如下的网址下载:
http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg
然后直接操作:
sudo apt-key add gpg # 在下载的文件目录进行操作# 新增软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"# 再次更新软件源
sudo apt -y update# 安装 Docker CE 版
sudo apt -y install docker-ce查看Docker版本  
sudo docker -v  查看Docker运行状态  
sudo systemctl status docker  

下载pulsar2工具链

工具链获取途径:
https://pan.baidu.com/s/1FazlPdW79wQWVY-Qn–qVQ?pwd=sbru#list/path=%2F

https://pulsar2-docs.readthedocs.io/zh-cn/latest/user_guides_quick/quick_start_prepare.html
的3.2节

载入 Docker Image
执行 sudo docker load -i ax_pulsar2_3.3.tar.gz 导入 docker 镜像文件. 正确导入镜像文件会打印以下日志:

Loaded image: pulsar2:3.3
完成后, 执行 sudo docker image ls 打印
REPOSITORY   TAG          IMAGE ID       CREATED         SIZE
pulsar2      ${version}   xxxxxxxxxxxx   9 seconds ago   3.27GB

启动工具链镜像
执行以下命令启动 Docker 容器, 运行成功后进入 bash 环境

sudo docker run -it --net host --rm -v $PWD:/data pulsar2:3.3

输入pulsar2 version 打印

version: 3.3  
commit: xxxxxxxx

数据准备

后续内容 模型编译、 仿真运行 所需要的 原始模型 、 数据 、 图片 、 仿真工具 已在 quick_start_example 文件夹中提供 点击下载示例文件 然后将下载的文件解压后拷贝到 docker 的 /data 路径下.

下载链接:
https://github.com/xiguadong/assets/releases/download/v0.1/quick_start_example.zip
或下面链接的3.2.2.2节中有下载链接  
https://pulsar2-docs.readthedocs.io/zh-cn/stable/user_guides_quick/quick_start_prepare.html#pulsar2  ls  config  dataset  model  output  pulsar2-run-helper
model: 存放原始的 ONNX 模型 mobilenetv2-sim.onnx (预先已使用 onnxsim 将 mobilenetv2.onnx 进行计算图优化)dataset: 存放离线量化校准 (PTQ Calibration) 需求的数据集压缩包 (支持 tar、tar.gz、gz 等常见压缩格式)config: 存放运行依赖的配置文件 config.jsonoutput: 存放结果输出pulsar2-run-helper: 支持 axmodel 在 X86 环境进行仿真运行的工具

Pulsar2 编译

默认已经搭建好了基于 Docker 的 Pulsar2 工具链使用环境。转换依赖以下文件

yolov8s_config.json	模型转换必要的配置文件  
yolov8s-cut.onnx	待转换的 ONNX 模型  
coco_1000.tar	模型转换 PTQ 依赖的量化校准数据集  

配置文件 yolov8s_config.json,需要根据模型的输出节点进行修改,比如,以下配置文件中,需要将output_processors
中三个 tensor_name,修改为您根据文章内容得到的 yolov8s-cut.onnx 的模型的输出节点的名称,如下,我将其填充为
/model.22/Concat_output_0、/model.22/Concat_1_output_0、/model.22/Concat_2_output_0,
此步骤的作用是在模型输出节点上加上一个 transpose 以适应 ax-samples 中的后处理代码,将输出节点的 shape 从
11448080 改为 18080144
11444040 改为 14040144
11442020 改为 12020144

使用以下命令压缩数据集

tar cf 111.tar 111/

yolov8s_config.json 如下所示

{"model_type": "ONNX","npu_mode": "NPU1","quant": {"input_configs": [{"tensor_name": "images","calibration_dataset": "./dataset/coco_yolov8s.tar","calibration_size": 32,"calibration_mean": [0, 0, 0],"calibration_std": [255.0, 255.0, 255.0]}],"calibration_method": "MSE","precision_analysis": true,"precision_analysis_method":"PerLayer"},"input_processors": [{"tensor_name": "images","tensor_format": "BGR","src_format": "BGR","src_dtype": "U8","src_layout": "NHWC"}],"output_processors": [{"tensor_name": "/model.22/Concat_output_0","dst_perm": [0, 2, 3, 1]},    {"tensor_name": "/model.22/Concat_1_output_0","dst_perm": [0, 2, 3, 1]},    {"tensor_name": "/model.22/Concat_2_output_0","dst_perm": [0, 2, 3, 1]}],"compiler": {"check": 0}
}

yolov8s-seg_config.json 如下所示


{"model_type": "ONNX","npu_mode": "NPU1","quant": {"input_configs": [{"tensor_name": "images","calibration_dataset": "./dataset/coco_1000.tar","calibration_size": 32,"calibration_mean": [0, 0, 0],"calibration_std": [255.0, 255.0, 255.0]}],"calibration_method": "MinMax","precision_analysis": true,"precision_analysis_method":"EndToEnd"},"input_processors": [{"tensor_name": "images","tensor_format": "BGR","src_format": "BGR","src_dtype": "U8","src_layout": "NHWC"}],"output_processors": [{"tensor_name": "/model.22/Concat_1_output_0","dst_perm": [0, 2, 3, 1]},    {"tensor_name": "/model.22/Concat_2_output_0","dst_perm": [0, 2, 3, 1]},    {"tensor_name": "/model.22/Concat_3_output_0","dst_perm": [0, 2, 3, 1]},{"tensor_name": "/model.22/cv4.0/cv4.0.2/Conv_output_0","dst_perm": [0, 2, 3, 1]},    {"tensor_name": "/model.22/cv4.1/cv4.1.2/Conv_output_0","dst_perm": [0, 2, 3, 1]},    {"tensor_name": "/model.22/cv4.2/cv4.2.2/Conv_output_0","dst_perm": [0, 2, 3, 1]}],"compiler": {"check": 0}
}

转换命令如下

pulsar2 build --input model/yolov8s-cut.onnx --config config/yolov8s_config.json --output_dir output  --output_name yolov8s.axmodel --target_hardware AX620E

转换完成之后,将在 ./output 路径下生成 yolov8s.axmodel 文件,该文件用于最终上板运行。

查看编译后 axmodel 模型的输入输出信息,还有其他 -m -n -t 参数可以查看模型里的 meta / node / tensor 信息

onnx inspect -m -n -t output/compiled.axmodel

上板示例

ax_yolov8 请参考 ax-samples 项目编译获得。
按照 ax-samples/docs/compile_620e.md 进行交叉编译(如下)

https://github.com/AXERA-TECH/ax-samples

AX620Q(32位,arm-uclibc-linux)

3rdparty 准备
  • 下载预编译好的 OpenCV 库文件;
  • 在 ax-samples 创建 3rdparty 文件,并将下载好的 OpenCV 库文件压缩包解压到该文件夹中,文件夹目录结构如下所示
ax-samples$ tree -L 3
.
├── 3rdparty
│   └── opencv-arm-uclibc-linux
│       ├── bin
│       ├── include
│       ├── lib
│       └── share
编译环境
  • cmake 版本大于等于 3.13
  • AX620E 配套的交叉编译工具链 arm-AX620E-linux-uclibcgnueabihf-gxx 已添加到环境变量中
安装交叉编译工具链
  • Arm32 Linux 交叉编译工具链 获取地址

    解压/home/baiwei/ai-card/ai-training/utils/AX620Q/交叉编译工具链/

    配置环境变量:export PATH=/home/baiwei/ai-card/AX620Q/arm-AX620E-linux-uclibcgnueabihf/bin/:$PATH

依赖库准备

请联系 FAE 获取 AX620E BSP 开发包,执行如下操作
/home/baiwei/ai-card/ai-training/utils/AX620Q/SDK/

tar -zxvf AX620E_SDK_XXX.tgz
cd AX620E_SDK_XXX/package
tar -zxvf msp.tgz

解压完成后将文件夹内此 msp/out 目录设置到临时环境变量,具体操作如下:

export ax_bsp=${ax620e_bsp_sdk PATH}/msp/out/arm_uclibc
源码编译

git clone 下载源码,进入 ax-samples 根目录,创建 cmake 编译任务:

git clone https://github.com/AXERA-TECH/ax-samples.git
cd ax-samples
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-AX620E-linux-uclibcgnueabihf.toolchain.cmake -DBSP_MSP_DIR=${ax_bsp}/ -DAXERA_TARGET_CHIP=ax620q ..
make -j6
make install

编译完成后,生成的可执行示例存放在 ax-samples/build/install/ax620q/ 路径下:

ax-samples/build$ tree install
install
└── ax620q├── ax_classification├── ax_crowdcount├── ax_depth_anything├── ax_imgproc├── ax_model_info├── ax_rtdetr├── ax_scrfd├── ax_simcc_pose├── ax_yolov5_face├── ax_yolov5s├── ax_yolov5s_seg├── ax_yolov6├── ax_yolov7├── ax_yolov7_tiny_face├── ax_yolov8├── ax_yolov8_pose├── ax_yolov8_seg├── ax_yolov9└── ax_yolox

执行命令如下

./ax_yolov8 -m yolov8s.axmodel -i ssd_dog.jpg
http://www.xdnf.cn/news/979867.html

相关文章:

  • Spring Security是如何完成身份认证的?
  • BUG调试案例十四:TL431/TL432电路发热问题案例
  • Python训练营打卡DAY51
  • 机器学习核心概念速览
  • 基于ElasticSearch的法律法规检索系统架构实践
  • livetalking实时数字人多并发
  • uni-app项目实战笔记1--创建项目和实现首页轮播图功能
  • 告别excel:AI 驱动的数据分析指南
  • elementui使用Layout布局-对齐方式
  • input+disabled/readonly问题
  • Vue3 + TypeScript + Element Plus 表格行按钮不触发 row-click 事件、不触发勾选行,只执行按钮的 click 事件
  • Explore Image Deblurring via Encoded Blur Kernel Space论文阅读
  • 时序数据库IoTDB数据模型建模实例详解
  • Jmeter中变量如何使用?
  • MySQL 三表 JOIN 执行机制深度解析
  • 基础数论一一同余定理
  • Qt 动态插件系统QMetaObject::invokeMethod
  • 【docker】docker registry搭建私有镜像仓库
  • 开源 java android app 开发(十二)封库.aar
  • SD-WAN 技术如何助力工业物联网(IIoT)数据传输?深度解析传统方案对比与应用实践
  • Chrome 优质插件计划
  • 智慧农业物联网实训中心建设方案
  • 趋境科技英特尔生态沙龙举办,打通大模型私有化“最后一公里”
  • 当简约美学融入小程序 UI 设计:开启高效交互新篇
  • 【Java学习日记38】:C语言 fabs 与 Java abs 绝对值函数
  • element plus的el-form重置无效
  • CavityPlus: 北大团队研发的综合性蛋白质结合位点检测及功能分析网络服务器
  • 【python】预测投保人医疗费用,附insurance.csv数据集
  • 嵌入式系统内核镜像相关(三)
  • React 状态管理指南:Redux 原理与优化策略