本文主要是介绍嘉楠勘智k230开发板上手记录(五)--nncase部署yolov5s,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
虽然没有找到hhb的官方示例,但是我找到了nncase的,在src/big/nncase/examples中
一、环境搭建
examples也有个readme,不过里面的环境搭建跟sdk中的有点差别,不过大差不差,docker容器已经启动了,需要在容器中安装nncase
cd /root/k230/k230_sdk-main/src/big/nncase/
pip install x86_64/*.whl
pip install nncase-kpu==2.2.0.20230728
虽然x86_64中提供的whl是2.1版本的,但是安装会自动下载2.2版本的安装
二、编译模型--CMakeList
移动到examples下
1. 生成kmodel模型
python3 ./scripts/yolov5s_onnx.py --target k230 --model models/yolov5s.onnx --dataset calibration_dataset
2. 验证生成的kmodel模型精度
export PATH=$PATH:/usr/local/lib/python3.8/dist-packages/
python3 scripts/yolov5s_onnx_simu.py --model models/yolov5s.onnx --model_input object_detect/data/input_fp32.bin --kmodel tmp/yolov5s_onnx/test.kmodel --kmodel_input object_detect/data/input_uint8.bin
3. 编译
./build_app.sh
可以在k230_bin下有可执行文件
三、用Makefile
前两步跟上面一样。另外希望官方可以出个Makefile的教程,我是参考CMakeList生成的link.txt写的Makefile
3. 新建一个文件Makefile
# 项目名称
TARGET_OBJECT := object_detect.elf
TARGET_CLASSIFY := image_classify.elf# 指定交叉编译器
CC := /root/k230/k230_sdk-main/toolchain/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/riscv64-unknown-linux-musl-gcc
CXX := /root/k230/k230_sdk-main/toolchain/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/riscv64-unknown-linux-musl-g++# 版本
VERSION := 1.00# 源码目录
SRC_DIR_OBJECT := ./object_detect
SRC_DIR_CLASSIFY := ./image_classify# 源文件列表
SRCS_OBJECT := $(wildcard $(SRC_DIR_OBJECT)/*.c) $(wildcard $(SRC_DIR_OBJECT)/*.cpp) $(wildcard $(SRC_DIR_OBJECT)/*.cc)
SRCS_CLASSIFY := $(wildcard $(SRC_DIR_CLASSIFY)/*.c) $(wildcard $(SRC_DIR_CLASSIFY)/*.cpp) $(wildcard $(SRC_DIR_CLASSIFY)/*.cc)# 构建输出目录
BUILD_DIR_OBJECT := ./build/$(SRC_DIR_OBJECT)
BUILD_DIR_CLASSIFY := ./build/$(SRC_DIR_CLASSIFY)# 对象文件列表
OBJS_OBJECT := $(patsubst $(SRC_DIR_OBJECT)/%.c, $(BUILD_DIR_OBJECT)/%.o, $(filter %.c, $(SRCS_OBJECT))) \$(patsubst $(SRC_DIR_OBJECT)/%.cpp, $(BUILD_DIR_OBJECT)/%.o, $(filter %.cpp, $(SRCS_OBJECT))) \$(patsubst $(SRC_DIR_OBJECT)/%.cc, $(BUILD_DIR_OBJECT)/%.o, $(filter %.cc, $(SRCS_OBJECT)))OBJS_CLASSIFY := $(patsubst $(SRC_DIR_CLASSIFY)/%.c, $(BUILD_DIR_CLASSIFY)/%.o, $(filter %.c, $(SRCS_CLASSIFY))) \$(patsubst $(SRC_DIR_CLASSIFY)/%.cpp, $(BUILD_DIR_CLASSIFY)/%.o, $(filter %.cpp, $(SRCS_CLASSIFY))) \$(patsubst $(SRC_DIR_CLASSIFY)/%.cc, $(BUILD_DIR_CLASSIFY)/%.o, $(filter %.cc, $(SRCS_CLASSIFY)))# 指定头文件搜索路径
k230_sdk := /root/k230/k230_sdk-main
# set opencv
INCLUDES := -I$(k230_sdk)/src/big/utils/lib/opencv/include/opencv4/
LIB_PATH := -L$(k230_sdk)/src/big/utils/lib/opencv/lib/ -L$(k230_sdk)/src/big/utils/lib/opencv/lib/opencv4/3rdparty
# set mmz
LIB_PATH += -L$(k230_sdk)/src/big/mpp/userapps/lib
# set nncase
INCLUDES += -I$(k230_sdk)/src/big/nncase/riscv64 -I$(k230_sdk)/src/big/nncase/riscv64/nncase/include -I$(k230_sdk)/src/big/nncase/riscv64/nncase/include/nncase/runtime
LIB_PATH += -L$(k230_sdk)/src/big/nncase/riscv64/nncase/lib/
# 源目录
INCLUDES += -I$(k230_sdk)# 指定库文件名称
LIB_NAMES := -lnncase.rt_modules.k230 -lNncase.Runtime.Native -lfunctional_k230 -lsys -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -llibjpeg-turbo -llibopenjp2 -llibpng -lzlib -llibtiff -llibwebp -lcsi_cv # -lnncase.rt_modules.k230 -lNncase.Runtime.Native -lfunctional_k230 -lsys -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -llibjpeg-turbo -llibopenjp2 -llibpng -llibtiff -llibwebp -lzlib -lcsi_cv
# 编译参数
CFLAGS := -fopenmp -march=rv64imafdcv -mabi=lp64d -mcmodel=medany -O2 -s -T /root/k230/k230_sdk-main/src/big/nncase/examples/cmake/link.lds# 编译
all: $(BUILD_DIR_CLASSIFY)/$(TARGET_CLASSIFY) $(BUILD_DIR_OBJECT)/$(TARGET_OBJECT)$(BUILD_DIR_OBJECT)/$(TARGET_OBJECT): $(OBJS_OBJECT) $(CXX) --static $^ $(LIB_PATH) $(LIB_NAMES) $(CFLAGS) -o $@ $(BUILD_DIR_OBJECT)/%.o: $(SRC_DIR_OBJECT)/%.cc | create_build@echo "-------------------------------------- Compiling $< --------------------------------------"$(CXX) $(CFLAGS) -c --static $< $(LIB_PATH) $(LIB_NAMES) $(INCLUDES) -o $@ $(BUILD_DIR_CLASSIFY)/$(TARGET_CLASSIFY): $(OBJS_CLASSIFY) $(CXX) $(CFLAGS) --static $^ $(LIB_PATH) $(LIB_NAMES) -o $@ $(BUILD_DIR_CLASSIFY)/%.o: $(SRC_DIR_CLASSIFY)/%.cc | create_build@echo "-------------------------------------- Compiling $< --------------------------------------"$(CXX) $(CFLAGS) -c --static $< $(LIB_PATH) $(LIB_NAMES) $(INCLUDES) -o $@ .PHONY: clean create_build
clean:@echo "test1"rm -rf $(BUILD_DIR_OBJECT) $(BUILD_DIR_CLASSIFY)create_build:mkdir -p $(BUILD_DIR_OBJECT)mkdir -p $(BUILD_DIR_CLASSIFY)
注意 --static &< 一定要在前面,不然会出错。具体原因参考 gcc(g++)编译的顺序问题_Sunny04的博客-CSDN博客
,按照这篇博客的说法,越是底层的库,越是往后面写,例如-lopencv_imgcodecs -lopencv_imgproc,opencv_imgcodecs有调用opencv_imgproc中的函数,两个顺序乱掉就会出现undefined reference to的错误
4. 写个build_makefile_app.sh
#!/bin/bash# 将脚本中的每一行命令在执行之前打印出来
set -x# set cross build toolchain
make clean
makek230_bin=build/k230_bin
mkdir -p ${k230_bin}if [ -f build/image_classify/image_classify.elf ]; thenimage_classify=${k230_bin}/image_classifyrm -rf ${image_classify}cp -a image_classify/data/ ${image_classify}cp build/image_classify/image_classify.elf ${image_classify}cp tmp/mbv2_tflite/test.kmodel ${image_classify}
fiif [ -f build/object_detect/object_detect.elf ]; thenobject_detect=${k230_bin}/object_detectrm -rf ${object_detect}cp -a object_detect/data/ ${object_detect}rm ${object_detect}/*.bincp build/object_detect/object_detect.elf ${object_detect}cp tmp/yolov5s_onnx/test.kmodel ${object_detect}
fi
5. 运行结果
运行build_makefile_app.sh
./build_makefile_app.sh
会在当前目录下生成build/k230_bin文件夹
上传到k230的大核运行
这篇关于嘉楠勘智k230开发板上手记录(五)--nncase部署yolov5s的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!