大致流程-------------------------------------------------------------------------------------------------------
首先搭建好rknn_toolkit环境
-->
转化为rknn模型---------------------------------------------------------------------------------
1:进入docker
sudo docker run -t -i --privileged -v /dev/bus/usb:/dev/bus/usb \
-v /home/warren/RK_NPU_SDK_1.3.0/rknn-toolkit2-1.3.0/examples:/rknn_toolkit \
rknn-toolkit2:1.3.0-cp36 /bin/bash
3:复制一个resnet50v2并命名为MNIST
4:准备好如下文件
5:test.py
import os
import urllib
import traceback
import time
import sys
import numpy as np
import cv2
from rknn.api import RKNN
ONNX_MODEL = 'model.onnx'
RKNN_MODEL = 'model.rknn'
if __name__ == '__main__':
# Create RKNN object
rknn = RKNN()
# pre-process config
print('--> Config model')
rknn.config(target_platform='rk3588')
print('done')
# Load ONNX model
print('--> Loading model')
ret = rknn.load_onnx(model=ONNX_MODEL)
if ret != 0:
print('Load model failed!')
exit(ret)
print('done')
# Build model
print('--> Building model')
ret = rknn.build(do_quantization=False)
if ret != 0:
print('Build model failed!')
exit(ret)
print('done')
# Export RKNN model
print('--> Export RKNN model')
ret = rknn.export_rknn(RKNN_MODEL)
if ret != 0:
print('Export resnet50v2.rknn failed!')
exit(ret)
print('done')
# Set inputs
with open("./data/MNIST/raw/train-images-idx3-ubyte","rb") as f:
file=f.read()
num=100
i = 16+784*num
image1 = [int(str(item).encode('ascii'),16) for item in file[i:i+784]]
input_data = np.array(image1,dtype=np.float32).reshape(1,28,28,1)
#save the image
image1_np = np.array(image1,dtype=np.uint8).reshape(28,28,1)
file_name = "test.jpg"
cv2.imwrite(file_name,image1_np)
# init runtime environment
print('--> Init runtime environment')
ret = rknn.init_runtime()
if ret != 0:
print('Init runtime environment failed')
exit(ret)
print('done')
# Inference
print('--> Running model')
outputs = rknn.inference(inputs=input_data)
x = outputs[0]
output = np.exp(x)/np.sum(np.exp(x))
outputs = np.argmax([output])
print("----------outputs----------",outputs)
print('done')
rknn.release()
6:生成后目录结构
RKNPU板端--------------------------------------------------------------------------------------------
目录结构
build-linux_RK3588.sh
#!/bin/bash
set -e
TARGET_SOC="rk3588"
export TOOL_CHAIN=/home/warren/Downloads/gcc-buildroot-9.3.0-2020.03-x86_64_aarch64-rockchip-linux-gnu
# for aarch64
GCC_COMPILER=/home/warren/Downloads/gcc-buildroot-9.3.0-2020.03-x86_64_aarch64-rockchip-linux-gnu/bin/aarch64-rockchip-linux-gnu
export LD_LIBRARY_PATH=${TOOL_CHAIN}/lib64:$LD_LIBRARY_PATH
export CC=${GCC_COMPILER}-gcc
export CXX=${GCC_COMPILER}-g++
ROOT_PWD=$( cd "$( dirname $0 )" && cd -P "$( dirname "$SOURCE" )" && pwd )
# build
BUILD_DIR=${ROOT_PWD}/build/build_linux_aarch64
if [[ ! -d "${BUILD_DIR}" ]]; then
mkdir -p ${BUILD_DIR}
fi
cd ${BUILD_DIR}
cmake ../.. \
-DTARGET_SOC=${TARGET_SOC} \
-DCMAKE_C_COMPILER=${GCC_COMPILER}-gcc \
-DCMAKE_CXX_COMPILER=${GCC_COMPILER}-g++
make -j4
make install
cd -
main.cc
/*-------------------------------------------
Includes
-------------------------------------------*/
#include
#include
#include
#include
#include
#include
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "rknn_api.h"
using namespace std;
using namespace cv;
const int MODEL_IN_WIDTH = 28;
const int MODEL_IN_HEIGHT = 28;
const int MODEL_CHANNEL = 1;
int ret=0;
int loop_count=8000;
/*-------------------------------------------
Functions
-------------------------------------------*/
static inline int64_t getCurrentTimeUs()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000 + tv.tv_usec;
}
static void dump_tensor_attr(rknn_tensor_attr* attr) //dump tensor message
{
printf(" index=%d, name=%s, n_dims=%d, dims=[%d, %d, %d, %d], n_elems=%d, size=%d, fmt=%s, type=%s, qnt_type=%s, "
"zp=%d, scale=%f\n",
attr->index, attr->name, attr->n_dims, attr->dims[0], attr->dims[1], attr->dims[2], attr->dims[3],
attr->n_elems, attr->size, get_format_string(attr->fmt), get_type_string(attr->type),
get_qnt_type_string(attr->qnt_type), attr->zp, attr->scale);
}
static unsigned char *load_model(const char *filename, int *model_size) //load model
{
FILE *fp = fopen(filename, "rb");
if(fp == nullptr) {
printf("fopen %s fail!\n", filename);
return NULL;
}
fseek(fp, 0, SEEK_END);
int model_len = ftell(fp);
unsigned char *model = (unsigned char*)malloc(model_len);
fseek(fp, 0, SEEK_SET);
if(model_len != fread(model, 1, model_len, fp)) {
printf("fread %s fail!\n", filename);
free(model);
return NULL;
}
*model_size = model_len;
if(fp) {
fclose(fp);
}
return model;
}
void Bubble_sort(float *buffer)
{
float temp=0;
for(int i = 0; i < 10; i++){
for(int j=0;j<10-i-1;j++){
if(buffer[j]>buffer[j+1]){
temp=buffer[j];
buffer[j]=buffer[j+1];
buffer[j+1]=temp;
}
}
}
}
void Load_data(int num,unsigned char * input_image)
{
int j=16+784*num;
FILE *file = fopen("./model/data/MNIST/raw/train-images-idx3-ubyte", "rb");
if (file == NULL) {
printf("can't open the file!\n");
}
fseek(file,j,SEEK_SET);
fread(input_image,sizeof(char),784,file);
/* for(int i=0;i \n", argv[0]);
return -1;
}
// Load RKNN Model
printf("-------------load rknn model\n");
model = load_model(model_path, &model_len);
ret = rknn_init(&ctx, model, model_len, RKNN_FLAG_COLLECT_PERF_MASK, NULL);
//ret = rknn_init(&ctx, model, model_len, 0, NULL);
if(ret < 0) {
printf("rknn_init fail! ret=%d\n", ret);
return -1;
}
printf("--------------done\n");
// Get Model Input and Output Info
rknn_input_output_num io_num;
ret = rknn_query(ctx, RKNN_QUERY_IN_OUT_NUM, &io_num, sizeof(io_num));
if (ret != RKNN_SUCC) {
printf("rknn_query fail! ret=%d\n", ret);
return -1;
}
printf("model input num: %d, output num: %d\n", io_num.n_input, io_num.n_output);
//get input tensor message
printf("input tensors:\n");
rknn_tensor_attr input_attrs[io_num.n_input];
memset(input_attrs, 0, sizeof(input_attrs));
get_tensor_message(ctx,input_attrs,io_num.n_input,1);
//get output tensor message
printf("output tensors:\n");
rknn_tensor_attr output_attrs[io_num.n_output];
memset(output_attrs, 0, sizeof(output_attrs));
get_tensor_message(ctx,output_attrs,io_num.n_output,0);
for(int i=0;i= 0) {
rknn_destroy(ctx);
}
if(model) {
free(model);
}
return 0;
}
结果打印
sudo cat /sys/kernel/debug/rknpu/load 查看npu利用率
uint8
fp16