You're reading an pre-release version of this documentation.
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 v20251222 https://gitlink.org.cn/nudt_dsp/pnna.git
  • 编译 YOLOv5s 示例

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

编译成功生成可执行文件

1yolov8s

部署到开发板

  • 创建工作目录

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

1scp yolov8s resource/car.jpg resource/yolov8s_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 yolov8s
4./yolov8s

示例输出:

 1INFO  init pnna lite, driver version=0x00010f00...
 2   at query_hardware_info (nn_api.c:34)
 3INFO  cid=0xb1, device_count=1
 4   at query_hardware_info (nn_api.c:47)
 5INFO    device[0] core_count=1
 6   at query_hardware_info (nn_api.c:50)
 7INFO  Input: [1920 640 1 1], Data format: UINT8, Quant format: QUANTIZE_NONE
 8   at get_input_quantize_param (quantize.c:936)
 9INFO  Output: [8400 84 1 0], Data format: FP32, Quant format: QUANTIZE_NONE
10   at get_output_quantize_param (quantize.c:992)
11HASHMAP 0xb6c609a0(record-list) INIT SUCCESS
12INFO  No preprocess required   at create_app_ctx (nn_api.c:121)
13create_app_ctx done
14Total detections: 5
150 0.91 176.75 240.69 272.75 511.81
160 0.91 39.00 236.94 195.75 535.56
170 0.91 527.03 230.50 639.91 523.25
185 0.88 0.95 132.31 640.05 440.69
190 0.47 0.25 322.00 59.25 515.25
20destroy app_ctx_t done.
21pnna closed

附图

flowchart TD %% 上位机部分 subgraph Host_PC [上位机: Netrans 编译器开发阶段] direction TB A[原始模型 .onnx, .pt, etc.] --> B[netrans load] B --> C[netrans quantize] C --> D[netrans add_pre_post] D --> E[netrans export] E --> F([生成 .nb 二进制文件]) end %% 文件传输 F -. 部署到 .-> I %% 下位机部分 subgraph Target_Device [下位机: PNNA NPU 推理阶段] direction TB G[开始运行] --> H[pnna_open] H --> I[create_app_ctx] %% 循环处理部分 subgraph Inference_Loop [推理循环处理] direction TB J[get_input] --> K[app] K --> L[处理/显示结果] end I --> J L -- 继续循环 --> J L -- 停止退出 --> M[destroy_app_ctx] M --> N[pnna_close] N --> O[结束] end %% 样式美化 style F fill:#f96,stroke:#333,stroke-width:2px style J fill:#bbf,stroke:#333,stroke-width:2px style K fill:#69f,stroke:#333,stroke-width:2px,color:#fff style Inference_Loop fill:#f5f5f5,stroke:#999,stroke-dasharray: 5 5

全栈协同工作流图


作者 @sq suiqiang@hngwg.com