You're reading the documentation for a development version.
For the latest stable release version, please have a look at master.

快速入门

开发流程

flowchart LR A[(原始模型)] --> B(Netrans转换) --> C[(NBG文件)] C --> D{精度评估} D -->|Yes| E[模型部署] D -->|No| F[调整量化参数] --> B

安装 Netrans

CPU : Intel® Core™ i5-6500 CPU @ 3.2 GHz x4 支持 the Intel® Advanced Vector Extensions.

RAM : 至少8GB

硬盘 : 160GB

操作系统 : Ubuntu 20.04 LTS 64-bit with Python 3.10

  • 安装依赖

 1sudo apt update
 2sudo apt install build-essential
 3
 4# 安装 mamba
 5# 下载 mamba 安装脚本
 6 wget "https://mirrors.tuna.tsinghua.edu.cn/github-release/conda-forge/miniforge/LatestRelease//Miniforge3-$(uname)-$(uname -m).sh"
 7 # 创建 mamba 的安装目录
 8 mkdir -p ~/app 
 9 # 安装 mamba 到 ~/app/ 
10 bash Miniforge3-Linux-x86_64.sh -b -p ${HOME}/app/miniforge3
11 # 添加 mamba 的初始化脚本到环境配置文件
12 echo "source " ${HOME}/app/miniforge3/etc/profile.d/mamba.sh"" >> ${HOME}/.bashrc
13 # 重新加载 ~/.bashrc 文件,使 mamba 初始化生效
14 source ${HOME}/.bashrc
15 # 创建一个名为 netrans 的虚拟环境,并安装 Python 3.10
16 mamba create -n netrans python=3.10 -y
17 # 激活 netrans 虚拟环境
18 mamba activate netrans
  • 下载 Netrans

1# 下载 Netrans 到 ~/app
2cd ~/app
3git clone --depth 1 --branch v251204 https://gitlink.org.cn/nudt_dsp/netrans.git netrans
  • 运行配置脚本

1cd ~/app/netrans
2bash setup.sh

模型转换

Netrans 提供了命令行接口,用于编译模型。

 1# 示例由 pytorch 导出 onnx 格式的 yolov8s 模型为例,演示使用 netrans 命令行工具完成转换的全过程。
 2# 1. 定义模型路径,模型路径默认为工作路径。
 3work_path='~/app/netrans/examples/infer_with_pre_post_process/yolov8s'
 4cd ${work_path}
 5# 2. 激活环境
 6mamba activate netrans
 7# 3. 模型导入
 8netrans load ./ --mean 0 0 0 --scale 255 255 255
 9# 4. 模型量化
10netrans quantize ./ asymu8 
11# 5. 将前后处理加入推理网络
12# 前处理进行归一化和量化操作,后处理为反量化操作
13netrans add_pre_post ./ asymu8
14# 6. 导出 nbg 文件
15netrans export ./ asymu8

精度评估

  • 下载 YOLOv8 并安装依赖包

1cd ~/wsp
2mamba activate netrans
3git clone https://github.com/ultralytics/ultralytics.git
4pip install ultralytics
  • 下载 Evaluation 到 ‘~/wsp/ultralytics’,安装依赖包

1cd ~/wsp/ultralytics/ultralytics
2git clone https://gitlink.org.cn/nudt_dsp/evaluation.git
3pip install websockets jsonrpcclient
  • 将 'validator.py' 移动到 ‘~/wsp/ultralytics/ultralytics/engine’ 目录下

1mv ~/wsp/ultralytics/ultralytics/evaluation/src/validator.py ~/wsp/ultralytics/ultralytics/engine/
  • 执行 'pip install ./' ,将修改后的源码安装到 Ultralytics 包中

1pip install ./
  • 进行评估。(脚本会自动下载COCO128数据集并解压到'~/wsp'目录下)

1cd ~/wsp/ultralytics 
2yolo val detect data=ultralytics/cfg/datasets/coco128.yaml model=ultralytics/evaluation/resource/yolov8s/yolov8s.onnx
  • 评估结果如下

1Class     Images  Instances      P          R      mAP50    mAP50-95
2all        128        929      0.718       0.51    0.619     0.461

安装交叉编译器

  • 下载交叉编译器

1git clone https://gitlink.org.cn/nudt_dsp/gcc_arm_8.3.git
  • 解压安装

1mkdir -p ~/app
2tar xvf ./gcc_arm_8.3/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz -C ~/app/
  • 配置环境变量

1export TOOLCHAIN_DIR=~/app/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf
2export PATH=$TOOLCHAIN_DIR/bin:$PATH

测试:

1arm-linux-gnueabihf-gcc -v

编译示例工程

  • 下载示例工程

1mkdir -p ~/ws && cd ~/ws
2git clone -b v20251215 https://gitlink.org.cn/nudt_dsp/pnna.git
  • 编译 YOLOv5s 示例

1cd pnna/examples/yolov5s
2make clean && make

编译成功生成可执行文件

1yolov5s

部署到开发板

  • 创建工作目录

1ssh root@192.168.23.100 "mkdir -p /home/root/workspace"
  • 上传可执行程序、模型文件、图片

1scp yolov5s resource/car.jpg resource/yolov5s-int16.nb \
2    root@192.168.23.100:/home/root/workspace

如需批量上传,可以使用:

1scp -r resource root@192.168.23.100:/home/root/workspace
  • 运行示例

1ssh root@192.168.23.100
2cd /home/root/workspace
3chmod +x yolov5s
4./yolov5s

示例输出:

 1init pnna lite, driver version=0x00010f00...
 2cid=0xb1, device_count=1
 3  device[0] core_count=1
 4memory pool size=14311808byte
 5network core count=1
 6HASHMAP 0xb6b22d2c(record-list) INIT SUCCESS
 7create_app_ctx done
 8Total detections: 5
 9Scores: 0.88, Boxes: [43.44, 231.70, 185.25, 543.48], Class ID: 0
10Scores: 0.88, Boxes: [175.69, 241.36, 271.75, 511.14], Class ID: 0
11Scores: 0.88, Boxes: [532.12, 229.05, 639.75, 520.08], Class ID: 0
12Scores: 0.59, Boxes: [-0.03, 325.05, 55.41, 517.27], Class ID: 0
13Scores: 0.47, Boxes: [13.00, 131.06, 619.75, 461.44], Class ID: 5
14postprocess done
15destroy app_ctx done.
16pnna closed

作者 @sq suiqiang@hngwg.com