1、交叉编译
官方给的一个流程: RKNN 模型推理测试为了避免踩坑在开头提出来
按照官方的流程可以跑通,他自己提供的yolov5s.rknn(640*640)的模型,但是跑自己的模型的时候加载就会出错E RKNN: failed to decode config data!Segmentation fault (core dumped),应该是这个地址的链接版本太老了,并且给出来的demo预处理也没有使用librga做硬件加速,直接跳过,官方也给出来了说这个github链接不在维护(https://github.com/rockchip-linux/rknpu2)
官方最新地址: rknn-toolkit2
所有实例程序就都在这里了
工具包库: rknn_model_zoo
下载下来
交叉编译需要先安装 LuckFox Pico SDK
git clone https://gitee.com/LuckfoxTECH/luckfox-pico.git
Pico SDK:https://github.com/LuckfoxTECH/luckfox-pico/
在 rknn_model_zoo目录下我们现在对这些例程进行交叉编译,编译例程前需要设置如下环境变量:
export RK_RV1106_TOOLCHAIN=<SDK目录>/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin/arm-rockchip830-linux-uclibcgnueabihf
# 这个SDK目录目录就是你刚才Pico SDK放的地址
cmake要求必须大于3.15,不满足自行百度解决
chmod a+x ./build-linux.sh
./build-linux.sh -t rv1106 -a armv7l -d yolov5
-t : target (rk356x/rk3588/rv1106)
-a : arch (aarch64/armhf)
-d : demo name
-b : build_type(Debug/Release)
-m : enable address sanitizer, build_type need set to Debug
编译完成之后会在当前目录下生成一个install目录进入里面就可以找到编译出来的程序和库和模型文件
2、板上跑通
通过adb,将库和model下的文件文件都推上去
# 跑通官方模型
./rknn_yolov5_demo ./model/exp415_best.onnx_sim_new1.rknn ./model/car_1012.jpg
# 通过adb将图片从板子上取出来
adb pull /root/yolov5/out.jpg .
可以看到没有任何问题,但是这就是最大的问题
3、验证自己模型
如果直接拿自己的模型不修改代码,直接推理,会出现这种报错,就是类别没有对上
从原本80修改成实际类别数,还有板上model目录下的coco_80_labels_list.txt文件也需要修改成你的类别名
进行推理
# 我的模型输出
model input height=224, width=384, channel=3
推理出来的结果就是这样子了,矩形框都是错乱的
这个文件就是源码里面埋了很大一个坑,取反了,模型输入要求是NHWC,但是他将h取到w上面了,所以出现错乱,为什么官方自己的模型没问题推理是正常的,因为官方的是640*640,高宽尺寸是一致的,所以没有出现这个bug
修改之后再次验证正常了
4、编译使用
将示例程序封装成so动态库,嵌入到其他地方使用
cmake文件,任使用官方给的脚本进行调用./build-linux.sh -t rv1106 -a armv7l -d yolov5
cmake_minimum_required(VERSION 3.10)
project(rknn_yolov5_demo)
if (ENABLE_ASAN)
message(STATUS "BUILD WITH ADDRESS SANITIZER")
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
endif ()
set(rknpu2_yolov5_file rknpu2/yolov5.cc)
if (TARGET_SOC STREQUAL "rv1106" OR TARGET_SOC STREQUAL "rv1103")
add_definitions(-DRV1106_1103)
set(rknpu2_yolov5_file rknpu2/yolov5_rv1106_1103.cc)
#dma
#include_directories(/root/anaconda3/envs/RKNN/rknn-toolkit2/rknpu2/runtime/Linux/librknn_api/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../3rdparty/allocator/dma)
endif()
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../../3rdparty/ 3rdparty.out)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../../utils/ utils.out)
set(CMAKE_INSTALL_RPATH "$ORIGIN/lib")
file(GLOB SRCS ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)
# 为 utils.out 生成位置无关代码
set_target_properties(imageutils PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(fileutils PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(imagedrawing PROPERTIES POSITION_INDEPENDENT_CODE ON)
add_library(${PROJECT_NAME} SHARED
main.cc
postprocess.cc
${rknpu2_yolov5_file}
)
target_link_libraries(${PROJECT_NAME}
imageutils
fileutils
imagedrawing
${LIBRKNNRT}
)
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
target_link_libraries(${PROJECT_NAME}
log
)
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} Threads::Threads)
endif()
target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${LIBRKNNRT_INCLUDES}
)
install(TARGETS ${PROJECT_NAME} DESTINATION .)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../model/bus.jpg DESTINATION ./model)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../model/coco_80_labels_list.txt DESTINATION ./model)
file(GLOB RKNN_FILES "${CMAKE_CURRENT_SOURCE_DIR}/../model/*.rknn")
install(FILES ${RKNN_FILES} DESTINATION model)
编写cpp进行调用封装的库,进行推理cmake文件
cmake_minimum_required(VERSION 3.10)
project(rknn_yolov5_test)
if (ENABLE_ASAN)
message(STATUS "BUILD WITH ADDRESS SANITIZER")
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
endif ()
set(rknpu2_yolov5_file rknpu2/yolov5.cc)
if (TARGET_SOC STREQUAL "rv1106" OR TARGET_SOC STREQUAL "rv1103")
add_definitions(-DRV1106_1103)
set(rknpu2_yolov5_file rknpu2/yolov5_rv1106_1103.cc)
#dma
#include_directories(/root/anaconda3/envs/RKNN/rknn-toolkit2/rknpu2/runtime/Linux/librknn_api/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../3rdparty/allocator/dma)
endif()
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../../3rdparty/ 3rdparty.out)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../../utils/ utils.out)
set(CMAKE_INSTALL_RPATH "$ORIGIN/lib")
link_directories(/root/anaconda3/envs/RKNN/rknn_model_zoo/build/build_rknn_yolov5_demo_rv1106_linux_armv7l_Release)
file(GLOB SRCS ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)
add_executable(${PROJECT_NAME}
main.cpp
)
target_link_libraries(${PROJECT_NAME}
rknn_yolov5_demo
imageutils
fileutils
imagedrawing
${LIBRKNNRT}
)
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
target_link_libraries(${PROJECT_NAME}
log
)
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} Threads::Threads)
endif()
target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${LIBRKNNRT_INCLUDES}
)
install(TARGETS ${PROJECT_NAME} DESTINATION .)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../model/bus.jpg DESTINATION ./model)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../model/coco_80_labels_list.txt DESTINATION ./model)
file(GLOB RKNN_FILES "${CMAKE_CURRENT_SOURCE_DIR}/../model/*.rknn")
install(FILES ${RKNN_FILES} DESTINATION model)
5、opencv安装测试
cd opencv
mkdir build
cd build
// 编译
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES ..
make -j4
sudo make install
默认安装路径为:
/usr/local/bin - executable files
/usr/local/lib - libraries (.so)
/usr/local/cmake/opencv4 - cmake package
/usr/local/include/opencv4 - headers
/usr/local/share/opencv4 - other files (e.g. trained cascades in XML format)
// 环境配置
sudo find / -iname opencv4.pc
vim ~/.bashrc
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
source ~/.bashrc
// 查看
pkg-config --libs opencv4
// 配置动态库
sudo vim /etc/ld.so.conf.d/opencv4.conf
/usr/local/lib
sudo ldconfig
cd opencv/samples/cpp/example_cmake目录下,依次执行以下命令:
cmake .
make
./opencv_example
测试main.cpp
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
cout<<"usage: DisplayImage.out <Image_Path>\n";
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
cout<<"No image data \n";
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
touch CMakeLists.txt
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Define project name
project(img)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Declare the executable target built from your sources
add_executable(img main.cpp)
# Link your application with OpenCV libraries
target_link_libraries(img PRIVATE ${OpenCV_LIBS})
测试使用
cmake .
make
./img 1.jpg