当前位置:   article > 正文

瑞芯微RK3588 C++部署Yolov8检测和分割模型_用c++部署rknn3588

用c++部署rknn3588

最近这一个月在研究国产瑞芯微板子上部署yolov8的检测和分割模型,踩了很多坑,记录一下部署的过程和遇到的一些问题:

1 环境搭建

需要的环境和代码主要包括:
(1)rknn-toolkit2-1.5.2:工具链,开发环境
(2)rockchip-yolov8:pt模型转onnx模型
(3)yolov8_onnx2rknn:在(2)的基础上转检测rknn模型
(4)yolov8seg_onnx2rknn:在(2)的基础上转分割rknn模型
在这里插入图片描述
最好使用对应的环境,环境不匹配的话会出现很多问题。

2 ubuntu docker环境

Docker容器主要用来进行模型转换,也就是pt转onnx的过程,因此docker中需要用的的包主要是rockchip-yolov8,需要修改该代码,进行模型的转换,在linux服务器上安装docker环境,创建一个ubuntu系统的docker环境
这一部分的修改代码参考山水无移大哥的部署过程,贼清洗,膜拜一下,少走了很多弯路,直接贴上地址

3 模型转换问题

在转自己的pt到onnx模型时,容易出现以下问题:
(1)报错信息:

copying a param with shape torch.Size([64,64,3,3]) from checkpoint,the shape in current model is torch.Size(32,64,3,3)
  • 1

在这里插入图片描述
主要的问题有两种:
1)在最后一步导出onnx时,yolov8s.yaml里面没有修改成自己的模型的类别信息;
2)自己训练的yolov8m模型,但是选择的yaml是yolov8s.yaml


from ultralytics import YOLO

# model = YOLO('/cytech_ai/sipingtest/rknntest/model/20230228_yolov8_LiftPerson_filter.pt')
# results = model(task='detect', mode='predict', source='/cytech_ai/sipingtest/rknntest/2.jpg', line_thickness=3, save=True, device='cpu')

model = YOLO('/cytech_ai/sipingtest/rknntest/rockchip-yolov8/ultralytics/cfg/models/v8/yolov8s.yaml')
results = model(task='detect', mode='predict', source='/cytech_ai/sipingtest/rknntest/2.jpg', line_thickness=3, save=True, device='cpu')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

(2)多处修改时,最终的输出结果和分割模型的结果搞混了,导致模型输出对应不上:
在这里插入图片描述
在这里插入图片描述

4 RK3588上环境搭建

瑞芯微rk3588上,需要的环境主要是rknpu2,主要用来C++编写cmakelists文件时导入动态库和头文件,我这里将检测模型和分割模型全部集成到一个工程里面,分享一个个人的cmakelist文件:

cmake_minimum_required(VERSION 3.4.1)

# 声明一个 cmake 工程
set(PROJECT_NAME rknn_yolov8_AlgDetectModel)
project(${PROJECT_NAME})

set(CMAKE_CXX_STANDARD 11)


set(TARGET_SOC "rk3588")
set(CMAKE_C_COMPILER "aarch64")

# rknn api
if(TARGET_SOC STREQUAL "rk356x")
  set(RKNN_API_PATH ${CMAKE_SOURCE_DIR}/../../runtime/RK356X/${CMAKE_SYSTEM_NAME}/librknn_api)
  set(RKNN_API_PATH ${CMAKE_SOURCE_DIR}/../../runtime/RK356X/${CMAKE_SYSTEM_NAME}/librknn_api)
elseif(TARGET_SOC STREQUAL "rk3588")
  set(RKNN_API_PATH /home/siping/testrknn/rknpu2-1.5.2/runtime/RK3588/Linux/librknn_api/aarch64)
else()
  message(FATAL_ERROR "TARGET_SOC is not set, ref value: rk356x or rk3588 or rv110x")
endif()

if (CMAKE_SYSTEM_NAME STREQUAL "Android")
  set(RKNN_RT_LIB ${RKNN_API_PATH}/${CMAKE_ANDROID_ARCH_ABI}/librknnrt.so)
else()
  if (CMAKE_C_COMPILER MATCHES "aarch64")
    set(LIB_ARCH aarch64)
  else()
    set(LIB_ARCH armhf)
  endif()

  #直接链接这个库了
  set(RKNN_RT_LIB /home/siping/testrknn/rknpu2-1.5.2/runtime/RK3588/Linux/librknn_api/aarch64/librknnrt.so)
endif()

#链接头文件
include_directories(/home/siping/testrknn/rknpu2-1.5.2/runtime/RK3588/Linux/librknn_api/include)

#第三方依赖库
include_directories(${CMAKE_SOURCE_DIR}/../3rdparty)

# opencv
#if (CMAKE_SYSTEM_NAME STREQUAL "Android")
#    set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/../3rdparty/opencv/OpenCV-android-sdk/sdk/native/jni/abi-${CMAKE_ANDROID_ARCH_ABI})
#else()
#  if(LIB_ARCH STREQUAL "armhf")
#    set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/../3rdparty/opencv/opencv-linux-armhf/share/OpenCV)
#  else()
#    set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/../3rdparty/opencv/opencv-linux-aarch64/share/OpenCV)
#  endif()
#endif()
#find_package(OpenCV REQUIRED)

#手动链接opencv480
set(OpenCV_DIR "/home/siping/thirdparty/opencv480/")
set(OpenCV_INCLUDE_DIRS "/home/siping/thirdparty/opencv480/include/opencv4")
set(OpenCV_LDFLAGS "/home/siping/thirdparty/opencv480/lib")

include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LDFLAGS})

message(STATUS "OpenCV library status:")

message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "    libraries: ${OpenCV_LDFLAGS}")

#rga
if(TARGET_SOC STREQUAL "rk356x")
  set(RGA_PATH ${CMAKE_SOURCE_DIR}/../3rdparty/rga/RK356X)
elseif(TARGET_SOC STREQUAL "rk3588")
  set(RGA_PATH ${CMAKE_SOURCE_DIR}/../3rdparty/rga/RK3588)
else()
  message(FATAL_ERROR "TARGET_SOC is not set, ref value: rk356x or rk3588")
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
  set(RGA_LIB ${RGA_PATH}/lib/Android/${CMAKE_ANDROID_ARCH_ABI}/librga.so)
else()
  if (CMAKE_C_COMPILER MATCHES "aarch64")
    set(LIB_ARCH aarch64)
  else()
    set(LIB_ARCH armhf)
  endif()
  #链接库,就这一个
  set(RGA_LIB ${RGA_PATH}/lib/Linux//${LIB_ARCH}/librga.so)
endif()
include_directories( ${RGA_PATH}/include)


#瑞芯微 glog日志库
set(GLOG_INCLUDE "/home/siping/thirdparty/glog_arm64/include/")
set(GLOG_LIB "/home/siping/thirdparty/glog_arm64/lib")

include_directories(${GLOG_INCLUDE})
link_directories(${GLOG_LIB})

message(STATUS "GLOG library status:")
message(STATUS "    include path: ${GLOG_INCLUDE}")
message(STATUS "    libraries: ${GLOG_LIB}")

#链接头文件
include_directories( ${CMAKE_SOURCE_DIR}/include)

#链接cpp文件
aux_source_directory(src DIR_CPP)

#==============================================================

# install target and libraries 将所有需要的依赖库放在同一个位置
#set install path
set(CMAKE_BUILD_RPATH "${OpenCV_LDFLAGS}")
set(CMAKE_INSTALL_PREFIX /home/siping/algunion/alglib)
message(STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")



# set runtime path
set(CMAKE_INSTALL_RPATH ".")



# 如果想生成动态库,SHARE .so
#add_library(${PROJECT_NAME} SHARED  ${DIR_CPP})
#set(${PROJECT_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
add_executable(${PROJECT_NAME} src/main.cc ${DIR_CPP})

target_link_libraries(${PROJECT_NAME}
        ${RKNN_RT_LIB} #必须的runtime  librknnrt.so
        ${RGA_LIB} #rga  librga.so
        ${OpenCV_LDFLAGS}
        -lopencv_world
        ${GLOG_LIB}
        -lglog
        )

install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX})

file(GLOB GLOG_LIB "${GLOG_LIB}/lib*.so.*")
file(GLOB OpenCV_LDFLAGS "${OpenCV_LDFLAGS}/lib*.so.*")

install(PROGRAMS
        ${OpenCV_LDFLAGS}
        ${RKNN_RT_LIB}
        ${RGA_LIB}
        ${GLOG_LIB}
        DESTINATION ${CMAKE_INSTALL_PREFIX})

install(DIRECTORY model DESTINATION "/home/siping/algunion")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149

前面用到的环境和代码打个包,上传到了百度网盘,C++的部署的代码参考的里面都有,我自己这边只是根据自己的项目做了集成,如有需要可私信。

5 参考

检测模型:https://blog.csdn.net/zhangqian_1/article/details/135523096?spm=1001.2014.3001.5502
分割模型:https://blog.csdn.net/zhangqian_1/article/details/131571838?spm=1001.2014.3001.5502

另外一种部署方法,仅检测模型(Python):
https://blog.csdn.net/m0_48979117/article/details/135628375

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/933308
推荐阅读
相关标签
  

闽ICP备14008679号