完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
当我们转换并量化好模型后,在PC端简单验证后,一定想将自己的模型部署到板端运行测试,但瑞芯微官方给的纯C接口使用起来,感觉较为繁琐。抱着试一试的态度,本人尝试了将常用接口封装成类似于NCNN的接口调用形式,方便自己在工程部署上的应用,目前使用效果还凑合,特将其分享给大家,其中不足之处还望各位大佬指正。
1.RKNN API函数返回值错误码 [tr]错误码错误详情[/tr]
2.RKNN API函数介绍
3.封装代码 net.h #ifndef _RKNN_NET_H #define _RKNN_NET_H #include "opencv2/opencv.hpp" #include "rknn_api.h" typedef struct { int w; int h; int c; float* data; } Tensor; class Extractor; class Net { public: rknn_context ctx; Net() = default; ~Net(); Net(const Net& rhs) = delete; Net& operator=(const Net& rhs) = delete; void clear(); int loadModel(const char* filename); Extractor createExtractor() const; }; class Extractor { private: const Net* net; rknn_input_output_num io_num; rknn_output* outputs; rknn_tensor_attr* output_attr; public: ~Extractor(); int input(const cv::Mat& in); int extract(int blob_index, Tensor& feat); protected: friend Extractor Net::createExtractor() const; Extractor(const Net* net); }; #endif net.cpp #include "rknn_net.h" #include #include Net::~Net() { clear(); } int Net::loadModel(const char *filename) { FILE *fp = fopen(filename, "rb"); if (fp == nullptr) { printf("fopen %s fail!n", filename); return -1; } fseek(fp, 0, SEEK_END); size_t 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); model = nullptr; return -2; } if (fp) { fclose(fp); } int ret = rknn_init(&ctx, model, model_len, 0); if (ret < 0) { free(model); model = nullptr; printf("rknn_init fail! ret=%dn", ret); return -3; } free(model); model = nullptr; return 0; } Extractor Net::createExtractor() const { return Extractor(this); } void Net::clear() { if (ctx >= 0) { rknn_destroy(ctx); } } Extractor::Extractor(const Net *_net) : net(_net) { int ret = rknn_query(net->ctx, RKNN_QUERY_IN_OUT_NUM, &io_num, sizeof(io_num)); if (ret != RKNN_SUCC) { printf("rknn_query fail! ret=%dn", ret); return; } outputs = (rknn_output *)malloc(sizeof(rknn_output) * io_num.n_output); memset(outputs, 0, sizeof(rknn_output) * io_num.n_output); for (size_t i = 0; i < io_num.n_output; i++) { outputs.want_float = 1; } output_attr = (rknn_tensor_attr *)malloc(sizeof(rknn_tensor_attr) * io_num.n_output); memset(output_attr, 0, sizeof(rknn_tensor_attr) * io_num.n_output); for (size_t i = 0; i < io_num.n_output; i++) { output_attr.index = i; ret = rknn_query(net->ctx, RKNN_QUERY_OUTPUT_ATTR, &(output_attr), sizeof(rknn_tensor_attr)); if (ret != RKNN_SUCC) { printf("rknn_query fail! ret=%dn", ret); } } } Extractor::~Extractor() { rknn_outputs_release(net->ctx, io_num.n_output, outputs); if (outputs) { free(outputs); outputs = nullptr; } if (output_attr) { free(output_attr); output_attr = nullptr; } } int Extractor::input(const cv::Mat &in) { if (in.empty()) { return -1; } rknn_input inputs[io_num.n_input]; memset(inputs, 0, sizeof(inputs)); inputs[0].index = 0; inputs[0].type = RKNN_TENSOR_UINT8; inputs[0].size = in.cols * in.rows * in.channels(); inputs[0].fmt = RKNN_TENSOR_NHWC; inputs[0].buf = in.data; int ret = rknn_inputs_set(net->ctx, io_num.n_input, inputs); if (ret < 0) { printf("rknn_input_set fail! ret=%dn", ret); return -3; } ret = rknn_run(net->ctx, nullptr); if (ret < 0) { printf("rknn_run fail! ret=%dn", ret); return -4; } ret = rknn_outputs_get(net->ctx, io_num.n_output, outputs, NULL); if (ret < 0) { printf("rknn_outputs_get fail! ret=%dn", ret); return -5; } return 0; } int Extractor::extract(int blob_index, Tensor &feat) { feat.c = output_attr[blob_index].dims[2]; feat.h = output_attr[blob_index].dims[1]; feat.w = output_attr[blob_index].dims[0]; feat.data = (float *)outputs[blob_index].buf; return 0; } |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
基于米尔瑞芯微RK3576核心板/开发板的人脸疲劳检测应用方案
569 浏览 0 评论
839 浏览 1 评论
737 浏览 1 评论
1957 浏览 1 评论
3204 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 01:57 , Processed in 0.705160 second(s), Total 72, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号