|
同一个模型,python加载没有问题,使用c++去做init的时候出问题同一套c++代码,加载官方提供的mobilenet没有问题,加载这个模型出问题
c++代码如下
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <fstream>
- #include <iostream>
- #include <algorithm>
- #include <queue>
- #include <sys/time.h>
- #include "rknn_api.h"
- #include "opencv2/core/core.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/highgui/highgui.hpp"
- using namespace std;
- int main(int argc, char** argv)
- {
- const char *img_path = "../tmp/stand-06_13_02-01374.jpg";
- const char *model_path = "../tmp/rensnet50.rknn";
- const int output_elems = 2;
- const int img_width = 224;
- const int img_height = 224;
- const int img_channels = 3;
- const int input_index = 0; // node name "input" 输入输出节点的索引??
- const int output_index = 0; // node name "MobilenetV1/Predictions/Reshape_1"
- // Load image
- cv::Mat img = cv::imread(img_path, 1);
- if(!img.data) {
- printf("cv::imread %s fail!\n", img_path);
- return -1;
- }
- if(img.cols != img_width || img.rows != img_height)
- cv::resize(img, img, cv::Size(img_width, img_height), (0, 0), (0, 0), cv::INTER_LINEAR);
- //BGR->RGB
- //cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
- // Load model
- FILE *fp = fopen(model_path, "rb");
- if(fp == NULL) {
- printf("fopen %s fail!\n", model_path);
- return -1;
- }
- fseek(fp, 0, SEEK_END); //重定位流(数据流/文件)上的文件内部位置指针
- int model_len = ftell(fp); //函数 ftell() 用于得到文件位置指针当前位置相对于文件首的偏移字节数
- void *model = malloc(model_len);
- fseek(fp, 0, SEEK_SET); //SEEK_SET开头-0,SEEK_CUR当前位置-1,SEEK_END结束-2
- if(model_len != fread(model, 1, model_len, fp)) { //权重读到model中
- printf("fread %s fail!\n", model_path);
- free(model);
- return -1;
- }
- // Start Inference
- rknn_input inputs[1]; //输入结构体,index,buf:指向数据的指针,size,pass_through:数据是否需要转换,type:数据的类型,fmt:数据的格式
- rknn_output outputs[1]; //want_float,is_prealloc,index,buf,size
- rknn_tensor_attr output0_attr; //index,n_dims,dims,name,n_elems,size,fmt:NCHW-NHWC,type,qnt_type,f1,zp,scale
- int ret = 0;
- rknn_context ctx = 0; //模型的对象?
- ret = rknn_init(&ctx, model, model_len, RKNN_FLAG_PRIOR_MEDIUM | RKNN_FLAG_COLLECT_PERF_MASK); //中优先级,打开测试开关
- if(ret < 0) {
- printf("rknn_init fail! ret=%d\n", ret);
- goto Error;
- }
- output0_attr.index = 0; //设置
- ret = rknn_query(ctx, RKNN_QUERY_OUTPUT_ATTR, &output0_attr, sizeof(output0_attr)); //查询ctx对象输出属性
- if(ret < 0) {
- printf("rknn_query fail! ret=%d\n", ret);
- goto Error;
- }
- inputs[0].index = input_index; //这个索引搞得不是很清楚
- inputs[0].buf = img.data; //装载数据
- inputs[0].size = img_width * img_height * img_channels; //占内存大小
- inputs[0].pass_through = false;
- inputs[0].type = RKNN_TENSOR_UINT8;
- inputs[0].fmt = RKNN_TENSOR_NHWC;
- ret = rknn_inputs_set(ctx, 1, inputs); //对象,输入的个数,输入的数组
- if(ret < 0) {
- printf("rknn_input_set fail! ret=%d\n", ret);
- goto Error;
- }
- ret = rknn_run(ctx, nullptr);
- if(ret < 0) {
- printf("rknn_run fail! ret=%d\n", ret);
- goto Error;
- }
- outputs[0].want_float = true;
- outputs[0].is_prealloc = false;
- ret = rknn_outputs_get(ctx, 1, outputs, nullptr); //从ctx中将output取出
- if(ret < 0) {
- printf("rknn_outputs_get fail! ret=%d\n", ret);
- goto Error;
- }
- // Process output
- if(outputs[0].size == output0_attr.n_elems * sizeof(float))
- {
- for(int i = 0;i <output0_attr.n_elems;i++){
- std::cout << "outputs " << i << " : " << ((float*)outputs[0].buf)[i] << std::endl;
- }
- std::cout << "outputs[0].size : " << outputs[0].size << std::endl;
- std::cout << "outputs0_attr.n_elems : " << output0_attr.n_elems << std::endl;
- }
- else
- {
- printf("rknn_outputs_get fail! get output_size = [%d], but expect %u!\n",
- outputs[0].size, (uint32_t)(output0_attr.n_elems * sizeof(float)));
- }
- rknn_outputs_release(ctx, 1, outputs);
- Error:
- if(ctx > 0) rknn_destroy(ctx);
- if(model) free(model);
- if(fp) fclose(fp);
- return 0;
- }
报错
[toybrick@localhost build]$ ./resnet50
rknn_init fail! ret=-6
查看库版本
[toybrick@localhost tmp]$ rpm -aq rknn-api npuservice
npuservice-1.0.3-1.rockchip.fc28.aarch64
rknn-api-0.9.5-2.rockchip.fc28.aarch64
根据这个帖子http://t.rock-chips.com/forum.php?mod=viewthread&tid=445&highlight=rknn%5C_init%2Bfail%21%2Bret%3D-6
[toybrick@localhost tmp]$ sudo dnf clean all
0 files removed
[toybrick@localhost tmp]$ sudo dnf update -y
Error: Failed to synchronize cache for repo 'fedora-modular'
更新报错,这个错误解决不了
模型是我在keras上训练得到的.h5,转成了pb文件,在转成.rknn文件,模型在python接口是没有问题的,c++就出错
为什么呢
请大神回答
|
|