Ubuntu 20.04 LTS 在3588安卓主板上测试yolov8-1.0版本的yolov8n-seg模型

0. 创建虚拟环境

#!< 创建虚拟环境yolov8
$ sudo pip install virtualenv
$ sudo pip install virtualenvwrapper
$ mkvirtualenv yolov8 -p /usr/bin/python3.8

1. 将yolov8n-seg.pt转换为yolov8n-seg.onnx文件

#!< 创建项目目录yolov8-rknn并下载yolov8n-seg.pt模型文件
(yolov8) $ mkdir -p /home/tianzx/Github/yolov8-rknn/epbox/model/
(yolov8) $ cd /home/tianzx/Github/yolov8-rknn/epbox/model/
(yolov8) $ wget https://github.com/ultralytics/assets/releases/download/v8.1.0/yolov8n-seg.pt

#!< 将yolov8n-seg.pt转换为yolov8n-seg.onnx
(yolov8) $ cd /home/tianzx/Github/yolov8-rknn/
(yolov8) $ git clone https://github.com/airockchip/ultralytics_yolov8.git
(yolov8) $ cd ultralytics_yolov8
(yolov8) $ vi ./ultralytics/cfg/default.yaml
model: /home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg.pt # (str, optional) path to model file, i.e. yolov8n.pt, yolov8n.yaml
(yolov8) $ export PYTHONPATH=./
(yolov8) $ python ./ultralytics/engine/exporter.py
......
Ultralytics YOLOv8.0.151 ???? Python-3.8.10 torch-2.1.0+cu121 CPU ()
YOLOv8n-seg summary (fused): 195 layers, 3404320 parameters, 0 gradients, 12.6 GFLOPs

PyTorch: starting from '/home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg.pt' with input shape (16, 3, 640, 640) BCHW and output shape(s) ((16, 64, 80, 80), (16, 80, 80, 80), (16, 1, 80, 80), (16, 32, 80, 80), (16, 64, 40, 40), (16, 80, 40, 40), (16, 1, 40, 40), (16, 32, 40, 40), (16, 64, 20, 20), (16, 80, 20, 20), (16, 1, 20, 20), (16, 32, 20, 20), (16, 32, 160, 160)) (6.7 MB)

RKNN: starting export with torch 2.1.0+cu121...

RKNN: feed /home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg.onnx to RKNN-Toolkit or RKNN-Toolkit2 to generate RKNN model.
Refer https://github.com/airockchip/rknn_model_zoo/tree/main/models/CV/object_detection/yolo
RKNN: export success ✅ 0.3s, saved as '/home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg.onnx' (13.0 MB)

Export complete (2.2s)
Results saved to /home/tianzx/Github/yolov8_rknn/epbox/model
Predict:         yolo predict task=segment model=/home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg.onnx imgsz=640 
Validate:        yolo val task=segment model=/home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg.onnx imgsz=640 data=coco.yaml 
Visualize:       https://netron.app

2. 将yolov8n-seg.onnx转换为yolov8n-seg-3588.rknn

# 安装rknn-toolkit2
(yolov8) $ cd  /home/tianzx/Github/yolov8_rknn/
(yolov8) $ git clone https://github.com/airockchip/rknn-toolkit2.git --depth 1
(yolov8) $ cd rknn-toolkit2/rknn-toolkit2/
(yolov8) $ pip install -r packages/requirements_cp38-2.0.0b0.txt
(yolov8) $ pip install packages/rknn_toolkit2-2.0.0b0+9bab5682-cp38-cp38-linux_x86_64.whl

#!< 3588 主板 只能使用v1.6.0版本  ☆☆☆☆☆ 
(yolov8) $ cd /home/tianzx/Github/yolov8_rknn
(yolov8) $ git clone -b v1.6.0 --depth=1 https://github.com/airockchip/rknn_model_zoo.git
(yolov8) $ cd rknn_model_zoo/examples/yolov8_seg/python
(yolov8) $ python convert.py /home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg.onnx rk3588 i8 /home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg-3588.rknn
I rknn-toolkit2 version: 2.0.0b0+9bab5682
--> Config model
done
--> Loading model
I It is recommended onnx opset 19, but your onnx model opset is 12!
I Model converted from pytorch, 'opset_version' should be set 19 in torch.onnx.export for successful convert!
I Loading : 100%|██████████████████████████████████████████████| 162/162 [00:00<00:00, 50331.65it/s]
done
--> Building model
W build: found outlier value, this may affect quantization accuracy
                        const name                        abs_mean    abs_std     outlier value
                        model.22.cv3.1.1.conv.weight      0.12        0.18        -12.310     
I GraphPreparing : 100%|████████████████████████████████████████| 183/183 [00:00<00:00, 8442.40it/s]
I Quantizating : 100%|████████████████████████████████████████████| 183/183 [00:05<00:00, 35.36it/s]
W build: The default input dtype of 'images' is changed from 'float32' to 'int8' in rknn model for performance!
                       Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of '375' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of 'onnx::ReduceSum_383' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of '388' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of '354' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of '395' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of 'onnx::ReduceSum_403' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of '407' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of '361' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of '414' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of 'onnx::ReduceSum_422' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of '426' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of '368' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
W build: The default output dtype of '347' is changed from 'float32' to 'int8' in rknn model for performance!
                      Please take care of this change when deploy rknn model with Runtime API!
I rknn building ...
I rknn buiding done.
done
--> Export rknn model
--> The RKNN model saved in: /home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg-3588.rknn
done

3. 测试onnx模型和rknn模型

3.1 测试onnx模型

(yolov8) $ cd /home/tianzx/Github/yolov8_rknn/
(yolov8) $ cd rknn_model_zoo/examples/yolov8_seg/python/
(yolov8) $ python yolov8_seg.py --model_path /home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg.onnx --img_show                     
/home/tianzx/Github/yolov8_rknn/rknn_model_zoo/py_utils/onnx_executor.py:12: FutureWarning: In the future `np.bool` will be defined as the corresponding NumPy scalar.
  if getattr(np, 'bool', False):
Model-/home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg.onnx is onnx model, starting val
infer 1/1

IMG: bus.jpg
person @ (209 241 285 510) 0.867
bus  @ (95 137 558 440) 0.867
person @ (109 235 224 535) 0.842
person @ (476 232 560 520) 0.801

yolov8n-seg.onnx-detect

3.2 测试rknn模型

3588安卓主板如果要调用rknn模型,需要更新3588安卓主板内的 rknn_server 和 librknnrt.so 文件。

(yolov8) $ cd /home/tianzx/Github/yolov8_rknn/
(yolov8) $ cd rknn-toolkit2/rknpu2/
# 切换到 root 用户权限
(yolov8) $ adb root
# 挂载文件系统为可读写模式
(yolov8) $ adb remount
(yolov8) $ adb shell
rk3588_s:/ # cd /vendor/bin/
# 我这边3588安卓主板的rknn_server 和 librknnrt.so 均是1.5.0版本, arm64架构
rk3588_s:/ # cp rknn_server rknn_server.1.5.0
rk3588_s:/ # cd ../lib64
rk3588_s:/ # cp librknnrt.so librknnrt.so.1.5.0
rk3588_s:/ # exit
(yolov8) $ adb push runtime/Android/rknn_server/arm64/rknn_server /vendor/bin/
runtime/Android/rknn_server/arm64/rknn_server: 1 file pushed, 0 skipped. 227.7 MB/s (895216 bytes in 0.004s)
(yolov8) $ adb push runtime/Android/librknn_api/arm64-v8a/librknnrt.so /vendor/lib64/
runtime/Android/librknn_api/arm64-v8a/librknnrt.so: 1 file pushed, 0 skipped. 465.1 MB/s (7366800 bytes in 0.015s)
(yolov8) $ adb shell
rk3588_s:/ # chmod +x /vendor/bin/rknn_server
rk3588_s:/ # su
rk3588_s:/ # setenforce 0
rk3588_s:/ # /vendor/bin/rknn_server &                                                                                                                                                                             
[1] 3895
rk3588_s:/ # start rknn server, version:2.0.0b0 (18eacd0 build@2024-03-22T14:07:01)
rk3588_s:/ # exit
(yolov8) $ cd ~/Github/yolov8_rknn/rknn_model_zoo/examples/yolov8_seg/python
(yolov8) $ python yolov8_seg.py --model_path /home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg-3588.rknn --target rk3588 --img_show                            
I rknn-toolkit2 version: 2.0.0b0+9bab5682
--> Init runtime environment
adbd is already running as root
I target set by user is: rk3588
I Get hardware info: target_platform = rk3588, os = Android, aarch = aarch64
I Check RK3588 board npu runtime version
I Starting ntp or adb, target is RK3588
I Start adb...
I Connect to Device success!
I NPUTransfer: Starting NPU Transfer Client, Transfer version 2.1.0 (b5861e7@2020-11-23T11:50:36)
D RKNNAPI: ==============================================
D RKNNAPI: RKNN VERSION:
D RKNNAPI:   API: 2.0.0b0 (18eacd0 build@2024-03-22T06:07:59)
D RKNNAPI:   DRV: rknn_server: 2.0.0b0 (18eacd0 build@2024-03-22T14:07:01)
D RKNNAPI:   DRV: rknnrt: 2.0.0b0 (35a6907d79@2024-03-24T10:30:08)
D RKNNAPI: ==============================================
D RKNNAPI: Input tensors:
D RKNNAPI:   index=0, name=images, n_dims=4, dims=[1, 640, 640, 3], n_elems=1228800, size=1228800, w_stride = 0, size_with_stride = 0, fmt=NHWC, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003922
D RKNNAPI: Output tensors:
D RKNNAPI:   index=0, name=375, n_dims=4, dims=[1, 64, 80, 80], n_elems=409600, size=409600, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-55, scale=0.138304
D RKNNAPI:   index=1, name=onnx::ReduceSum_383, n_dims=4, dims=[1, 80, 80, 80], n_elems=512000, size=512000, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.002873
D RKNNAPI:   index=2, name=388, n_dims=4, dims=[1, 1, 80, 80], n_elems=6400, size=6400, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003169
D RKNNAPI:   index=3, name=354, n_dims=4, dims=[1, 32, 80, 80], n_elems=204800, size=204800, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=26, scale=0.023277
D RKNNAPI:   index=4, name=395, n_dims=4, dims=[1, 64, 40, 40], n_elems=102400, size=102400, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-40, scale=0.095424
D RKNNAPI:   index=5, name=onnx::ReduceSum_403, n_dims=4, dims=[1, 80, 40, 40], n_elems=128000, size=128000, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003418
D RKNNAPI:   index=6, name=407, n_dims=4, dims=[1, 1, 40, 40], n_elems=1600, size=1600, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003922
D RKNNAPI:   index=7, name=361, n_dims=4, dims=[1, 32, 40, 40], n_elems=51200, size=51200, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=32, scale=0.020263
D RKNNAPI:   index=8, name=414, n_dims=4, dims=[1, 64, 20, 20], n_elems=25600, size=25600, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-43, scale=0.075364
D RKNNAPI:   index=9, name=onnx::ReduceSum_422, n_dims=4, dims=[1, 80, 20, 20], n_elems=32000, size=32000, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003874
D RKNNAPI:   index=10, name=426, n_dims=4, dims=[1, 1, 20, 20], n_elems=400, size=400, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-128, scale=0.003922
D RKNNAPI:   index=11, name=368, n_dims=4, dims=[1, 32, 20, 20], n_elems=12800, size=12800, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=26, scale=0.022538
D RKNNAPI:   index=12, name=347, n_dims=4, dims=[1, 32, 160, 160], n_elems=819200, size=819200, w_stride = 0, size_with_stride = 0, fmt=NCHW, type=INT8, qnt_type=AFFINE, zp=-119, scale=0.029378
done
Model-/home/tianzx/Github/yolov8_rknn/epbox/model/yolov8n-seg-3588.rknn is rknn model, starting val
W inference: The 'data_format' is not set, and its default value is 'nhwc'!


IMG: bus.jpg
person @ (209 240 285 509) 0.868
bus  @ (94 137 557 439) 0.841
person @ (108 235 221 536) 0.829
person @ (475 233 560 521) 0.783

yolov8n-seg-3588.rknn-detect
查看3588安卓主板的基本信息,这里需要用到 adb 命令,需要自行安装,比如我这边系统的adb路径为: /home/tianzx/Android/Sdk/platform-tools/adb

(yolov8) $ cd /home/tianzx/Github/yolov8_rknn/
#!< 安卓主板返回数字
(yolov8) $ adb shell getprop ro.build.version.release
12
#!< 如果板端是 Android 系统,可以在计算机端执行以下命令查询系统架构:
# 该命令的输出信息参考如下,其中 arm64-v8a 表示 ARM 64 位架构、第八版本的 ABI。
(yolov8) $ adb shell getprop ro.product.cpu.abi 
arm64-v8a
#!< 查看RKNPU2驱动版本信息
# rknpu 0.8.8 20230428
(yolov8) $ adb shell
rk3588_s:/ # dmesg | grep -i rknpu
[    4.875203] RKNPU fdab0000.npu: Adding to iommu group 0
[    4.875369] RKNPU fdab0000.npu: RKNPU: rknpu iommu is enabled, using iommu mode
[    4.876734] RKNPU fdab0000.npu: can't request region for resource [mem 0xfdab0000-0xfdabffff]
[    4.876759] RKNPU fdab0000.npu: can't request region for resource [mem 0xfdac0000-0xfdacffff]
[    4.876777] RKNPU fdab0000.npu: can't request region for resource [mem 0xfdad0000-0xfdadffff]
[    4.877338] [drm] Initialized rknpu 0.8.8 20230428 for fdab0000.npu on minor 1
[    4.881011] RKNPU fdab0000.npu: RKNPU: bin=0
[    4.881195] RKNPU fdab0000.npu: leakage=8
[    4.881248] debugfs: Directory 'fdab0000.npu-rknpu' with parent 'vdd_npu_s0' already present!
[    4.889245] RKNPU fdab0000.npu: pvtm=855
[    4.894164] RKNPU fdab0000.npu: pvtm-volt-sel=2
[    4.895645] RKNPU fdab0000.npu: avs=0
[    4.895857] RKNPU fdab0000.npu: l=10000 h=85000 hyst=5000 l_limit=0 h_limit=800000000 h_table=0
[    4.907412] RKNPU fdab0000.npu: failed to find power_model node
[    4.907459] RKNPU fdab0000.npu: RKNPU: failed to initialize power model
[    4.907473] RKNPU fdab0000.npu: RKNPU: failed to get dynamic-coefficient
#!< 启动rknn_server服务
rk3588_s:/ # su
rk3588_s:/ # setenforce 0
rk3588_s:/ # /vendor/bin/rknn_server &
# 可以看到rknn_server已经更新版本为 2.0.0b0
rk3588_s:/ # start rknn server, version:2.0.0b0 (18eacd0 build@2024-03-22T14:07:01)
# 查询 librknnrt.so 库版本
# 64 位系统 [已更新64位librknnrt.so文件,版本信息为2.0.0b0
rk3588_s:/ # strings /vendor/lib64/librknnrt.so | grep -i "librknnrt version"
librknnrt version: 2.0.0b0 (35a6907d79@2024-03-24T10:30:08)
# 32 位系统 [未更新32位librknnrt.so文件,版本信息为1.5.0]
rk3588_s:/ # strings /vendor/lib/librknnrt.so | grep -i "librknnrt version"
librknnrt version: 1.5.0 (e6fe0c678@2023-05-25T08:09:02)

4. 3588安卓主板apk测试

(yolov8) $ cd /home/tianzx/Github/yolov8_rknn/rknn_model_zoo
#!< https://dl.google.com/android/repository/android-ndk-r19c-linux-x86_64.zip
(yolov8) $ export ANDROID_NDK_PATH=/home/tianzx/Android/Sdk/ndk/android-ndk-r19c/
(yolov8) $ chmod +x build-android.sh
(yolov8) $ ./build-android.sh -t rk3588 -a arm64-v8a 
上一篇:ES-7.12.0-官网阅读-Index lifecycle actions各action详解


下一篇:Firefox 关键词高亮插件的简单实现