YOLOv8
This document describes how to run YOLOv8 on NPU.
Refer to Model Zoo Download for the example.
YOLOv8 Example Directory Structure:
$ tree ./
./
├── CMakeLists.txt
├── convert_model
│ ├── config_yml.py
│ ├── convert_model_env.sh
│ ├── python
│ │ └── onnx_extract.py
│ └── yolov8n_6.onnx
├── figures
│ ├── banner-yolo-vision-2023.png
│ ├── bus.jpg
│ ├── out_yolov8_640.png
│ └── yolo-comparison-plots.png
├── main.cpp
├── model
│ └── dog.jpg
├── model_config.h
├── README.md
├── yolov8_6_post.cpp
└── yolov8_6_pre.cpp
Model Conversion
Configure Virtual Environment
python -m venv .venv && source .venv/bin/activate
pip install ultralytics==8.1.0 onnxsim
Export ONNX Model
cd convert_model/python/
yolo export model=yolov8n.pt format=onnx dynamic=True opset=11
Fixed Shape
python3 -m onnxsim yolov8n.onnx yolov8n_640_sim.onnx --input-shape=1,3,640,640
Prune Model
python3 onnx_extract.py
cd ..
Create Symlink for Conversion Script
./convert_model_env.sh
Model Import/Quantization/Conversion
You need to enter the container development environment first. Refer to the Create Container section in Model Zoo Download.
Different platforms use corresponding Docker images:
- A733: ubuntu-npu:v2.0.10.1
- T527: ubuntu-npu:v1.8.11
docker exec -it model-zoo /bin/bash
After entering the container, navigate to the corresponding directory and run the script.
cd /workspace/examples/yolov8/convert_model/
./pegasus_import.sh yolov8n_6
./pegasus_quantize.sh yolov8n_6 uint8 12
- A733
- T527
./pegasus_export_ovx_nbg.sh yolov8n_6 uint8 a733
./pegasus_export_ovx_nbg.sh yolov8n_6 uint8 t527
The exported model files are stored in the ../model directory.
Compile Example
Now you can compile the example. First exit the container, then execute the following command to compile the example.
First, you need to configure third-party libraries and cross-compilation toolchain.
You can skip this step if you have already configured third-party libraries and cross-compilation toolchain in other examples.
cd ../../../3rdparty/opencv/
unzip opencv-4.9.0-aarch64-linux-sunxi-glibc.zip
cd ../../0-toolchains/
You need to manually download via this link first, then place it in 0-toolchains/ before executing the following command:
tar -xvf gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu.tar.xz
cd ../examples/yolov8/
- A733
- T527
../build_linux.sh -t a733 -s debian11
../build_linux.sh -t t527 -s debian11
Model Deployment
After compilation, the example will be installed in the install directory. You can use scp to transfer it to the board.
Configure NPU Driver
You can skip this step if you have already configured NPU driver in other examples.
Transfer the driver library to the board's lib directory via scp.
- A733 corresponds to the common/lib_linux_aarch64/A733 directory
- T527 corresponds to the common/lib_linux_aarch64/T527 directory
Then execute the following command to export to environment variables.
echo 'export LD_LIBRARY_PATH=$HOME/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
Run Example
After configuring the driver, you can run the example.
For T527 platform, you need to first enable NPU by referring to the A5E's "Enable NPU on Board" documentation, then use the following command to grant the current user permission to use /dev/vipcore.
sudo chmod 777 /dev/vipcore
- A733
- T527
cd yolov8_demo_linux_a733/
chmod +x ./yolov8_demo_a733
./yolov8_demo_a733 -nb model/yolov8n_6_uint8_a733.nb -i model/dog.jpg
The running result is as follows:
$ ./yolov8_demo_a733 -nb model/yolov8n_6_uint8_a733.nb -i model/dog.jpg
model_file=model/yolov8n_6_uint8_a733.nb, input=model/dog.jpg, loop_count=1, malloc_mbyte=10
VIPLite driver software version 2.0.3.2-AW-2024-08-30
input 0 dim 3 640 640 1, data_format=2, quant_format=0, name=input/output[0], none-quant
output 0 dim 80 80 64 1, data_format=0, name=uid_11_out_0b_uid_1_out_0, none-quant
output 1 dim 80 80 80 1, data_format=0, name=uid_10_out_0b_uid_1_out_0, none-quant
output 2 dim 40 40 64 1, data_format=0, name=uid_9_out_0ub_uid_1_out_0, none-quant
output 3 dim 40 40 80 1, data_format=0, name=uid_8_out_0ub_uid_1_out_0, none-quant
output 4 dim 20 20 64 1, data_format=0, name=uid_7_out_0ub_uid_1_out_0, none-quant
output 5 dim 20 20 80 1, data_format=0, name=uid_6_out_0ub_uid_1_out_0, none-quant
nbg name=model/yolov8n_6_uint8_a733.nb, size: 2452448.
create network 0: 11517 us.
prepare network: 1821 us.
buffer ptr: 0x10cdb600, buffer size: 1228800
network: 0, loop count: 1
run time for this network 0: 12567 us.
output 0, ptr 0x10e07740, size 409600.
output 1, ptr 0x10f977c0, size 512000.
output 2, ptr 0x1118b840, size 102400.
output 3, ptr 0x111ef8c0, size 128000.
output 4, ptr 0x1126c980, size 25600.
output 5, ptr 0x11285a00, size 32000.
detection num: 3
1: 87%, [ 130, 136, 568, 419], bicycle
16: 95%, [ 131, 220, 308, 541], dog
2: 68%, [ 467, 74, 695, 171], car
destroy npu finished.
~NpuUint.
This performance data only calculates the time consumption of model inference. Unless otherwise specified, it does not include the time consumption of pre-processing and post-processing.
| SoC | NPU | Model | Input Resolution | Network Creation Time | Network Preparation Time | Single Frame Inference Time | Post-processing Time | Total Time | Frame Rate |
|---|---|---|---|---|---|---|---|---|---|
| Allwinner A733 | Vivante VIP9000 | yolov8n | 640×640 | 11.5 ms | 1.8 ms | 12.6 ms | 25.9 ms | 79.4 FPS |
cd yolov8_demo_linux_t527/
chmod +x ./yolov8_demo_t527
./yolov8_demo_t527 -nb model/yolov8n_6_uint8_t527.nb -i model/dog.jpg
The running result is as follows:
$ ./yolov8_demo_t527 -nb model/yolov8n_6_uint8_t527.nb -i model/dog.jpg
model_file=model/yolov8n_6_uint8_t527.nb, input=model/dog.jpg, loop_count=1, malloc_mbyte=10
VIPLite driver software version 1.13.0.0-AW-2023-10-19
input 0 dim 3 640 640 1, data_format=2, quant_format=0, name=input[0], none-quant
output 0 dim 80 80 64 1, data_format=0, name=uid_20000_sub_uid_1_out_0, none-quant
output 1 dim 80 80 80 1, data_format=0, name=uid_20001_sub_uid_1_out_0, none-quant
output 2 dim 40 40 64 1, data_format=0, name=uid_20003_sub_uid_1_out_0, none-quant
output 3 dim 40 40 80 1, data_format=0, name=uid_20004_sub_uid_1_out_0, none-quant
output 4 dim 20 20 64 1, data_format=0, name=uid_20005_sub_uid_1_out_0, none-quant
output 5 dim 20 20 80 1, data_format=0, name=uid_20006_sub_uid_1_out_0, none-quant
nbg name=model/yolov8n_6_uint8_t527.nb, size: 2915520.
create network 0: 16603 us.
prepare network: 5775 us.
buffer ptr: 0x3e36c600, buffer size: 1228800
network: 0, loop count: 1
run time for this network 0: 30967 us.
output 0, ptr 0x3e498700, size 409600.
output 1, ptr 0x3e628780, size 512000.
output 2, ptr 0x3e81c840, size 102400.
output 3, ptr 0x3e8808c0, size 128000.
output 4, ptr 0x3e8fd940, size 25600.
output 5, ptr 0x3e9169c0, size 32000.
detection num: 3
1: 88%, [ 130, 135, 568, 419], bicycle
16: 93%, [ 131, 219, 307, 540], dog
2: 68%, [ 466, 74, 695, 171], car
destroy npu finished.
~NpuUint.
This performance data only calculates the time consumption of model inference. Unless otherwise specified, it does not include the time consumption of pre-processing and post-processing.
| SoC | NPU | Model | Input Resolution | Network Creation Time | Network Preparation Time | Single Frame Inference Time | Post-processing Time | Total Time | Frame Rate |
|---|---|---|---|---|---|---|---|---|---|
| Allwinner T527 | Vivante VIP9000 | yolov8n | 640×640 | 16.6 ms | 5.8 ms | 31.0 ms | 53.4 ms | 32.3 FPS |