最近这一个月在研究国产瑞芯微板子上部署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)在最后一步导出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')
(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 apiif(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)#手动链接opencv480set(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}")#rgaif(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 pathset(CMAKE_BUILD_RPATH "${OpenCV_LDFLAGS}")set(CMAKE_INSTALL_PREFIX /home/siping/algunion/alglib)message(STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")# set runtime pathset(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")
前面用到的环境和代码打个包,上传到了百度网盘,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