郑州有哪些搞网站开发的公司,奥利奥广告策划书,画册设计是什么,信息网站建设方案1. 前言 之前就想着使用rknn的c版本的api做推理看看#xff0c;找了一个简单的#xff0c;那就unet吧#xff0c;本来想着用rk的demo文件#xff0c;但是里面是mobilenet#xff0c;相关的函数没有#xff0c;卡这也卡了好久#xff0c;突然发现tengine相关的后处理找了一个简单的那就unet吧本来想着用rk的demo文件但是里面是mobilenet相关的函数没有卡这也卡了好久突然发现tengine相关的后处理拿来用用终于调试好了(环境自己配置)
2. c代码修改
2.1前处理
const char* img_path /home/ubuntu/npu_test/unet/img/01_test.tif;const char* roi_mask_path /home/ubuntu/npu_test/unet/img/01_test_mask.png;const char *model_path /home/ubuntu/npu_test/unet/model/eyes_unet-sim-3588.rknn;// Load ROI maskMat roi_img imread(roi_mask_path, IMREAD_GRAYSCALE);if (roi_img.empty()) {cout Image not found: roi_mask_path endl;return -1;}// Load imageMat original_img imread(img_path);if (original_img.empty()) {cout Image not found: img_path endl;return -1;}// Convert image to RGBcvtColor(original_img, original_img, COLOR_BGR2RGB);// Expand batch dimension// Mat img original_img.reshape(1, 1);Mat img original_img;
2.2 rknn的模型加载
const int MODEL_IN_WIDTH 565;const int MODEL_IN_HEIGHT 584;const int MODEL_IN_CHANNELS 3;rknn_context ctx 0;int ret;int model_len 0;unsigned char *model;// 初始化RKNN模型 model load_model(model_path, model_len);ret rknn_init(ctx, model, model_len, 0, NULL);if (ret 0){printf(rknn_init fail! ret%d\n, ret);return -1;}// 获取模型输入输出信息 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;}// 设置模型输入 // 使用rknn_input结构体存储模型输入信息, 表示模型的一个数据输入,用来作为参数传入给 rknn_inputs_set 函数rknn_input inputs[1];memset(inputs, 0, sizeof(inputs));inputs[0].index 0; // 设置模型输入索引inputs[0].type RKNN_TENSOR_UINT8; // 设置模型输入类型inputs[0].size img.cols * img.rows * img.channels() * sizeof(uint8_t); // 设置模型输入大小inputs[0].fmt RKNN_TENSOR_NHWC; // 设置模型输入格式NHWCinputs[0].buf img.data; // 设置模型输入数据// 使用rknn_inputs_set函数设置模型输入ret rknn_inputs_set(ctx, io_num.n_input, inputs);if (ret 0){printf(rknn_input_set fail! ret%d\n, ret);return -1;}// 推理 printf(rknn_run\n);ret rknn_run(ctx, nullptr);if (ret 0){printf(rknn_run fail! ret%d\n, ret);return -1;}// 获取模型输出 // 使用rknn_output结构体存储模型输出信息rknn_output outputs[1];memset(outputs, 0, sizeof(outputs));outputs[0].want_float 1;// 使用rknn_outputs_get函数获取模型输出ret rknn_outputs_get(ctx, 1, outputs, NULL);if (ret 0){printf(rknn_outputs_get fail! ret%d\n, ret);return -1;}
2.3 后处理
float *output_data (float *)outputs[0].buf;int output_size outputs[0].size / sizeof(uint32_t);// cout channel: channel endl;// cout res: res endl;// cout size: output_size endl;int img_h 584;int img_w 565;int channel output_size / img_h / img_w;int res output_size % (img_h * img_w);cout channel: channel endl;cout res: res endl;int* label_data new int[img_h * img_w];if (res ! 0){fprintf(stderr, output shape is not supported.\n);}else{/* multi-class segmentation */for (int i 0; i img_h; i){for (int j 0; j img_w; j){int argmax_id -1;float max_conf std::numeric_limitsfloat::min();for (int k 0; k channel; k){float out_value output_data[k * img_w * img_h i * img_w j];if (out_value max_conf){argmax_id k;max_conf out_value;}}label_data[i * img_w j] argmax_id;if (label_data[i * img_w j] 1) {label_data[i * img_w j] 255;}}}}// 将图像数据存储到一维数组中int* roi_array new int[img_h * img_w];for (int i 0; i img_h; i) {for (int j 0; j img_w; j) {roi_array[i * img_w j] static_castint(roi_img.atuchar(i, j));}}for (int i 0; i img_h; i) {for (int j 0; j img_w; j) {if (roi_array[i * img_w j] 0) {label_data[i * img_w j] roi_array[i * img_w j];}}}Mat result_img(img_h, img_w, CV_8UC1);for (int i 0; i img_h; i) {for (int j 0; j img_w; j) {result_img.atuchar(i, j) static_castuchar(label_data[i * img_w j]);}}imwrite(result.png, result_img);
2.4 rknn资源释放
// Release resourcesrknn_outputs_release(ctx, 1, outputs);if (ret 0){printf(rknn_outputs_release fail! ret%d\n, ret);return -1;}else if (ctx 0){// 释放RKNN模型 rknn_destroy(ctx);}// 释放模型数据 if (model){free(model);} 2.5 完整代码
#include iostream
#include opencv2/core/hal/interface.h
#include opencv2/opencv.hpp
#include opencv2/imgproc.hpp
#include rknn_api.h
#include chronousing namespace std;
using namespace cv;static unsigned char *load_model(const char *filename, int *model_size)
{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;
}int main() {auto start std::chrono::high_resolution_clock::now();const char* img_path /home/ubuntu/npu_test/unet/img/01_test.tif;const char* roi_mask_path /home/ubuntu/npu_test/unet/img/01_test_mask.png;const char *model_path /home/ubuntu/npu_test/unet/model/eyes_unet-sim-3588.rknn;// Load ROI maskMat roi_img imread(roi_mask_path, IMREAD_GRAYSCALE);if (roi_img.empty()) {cout Image not found: roi_mask_path endl;return -1;}// Load imageMat original_img imread(img_path);if (original_img.empty()) {cout Image not found: img_path endl;return -1;}// Convert image to RGBcvtColor(original_img, original_img, COLOR_BGR2RGB);// Expand batch dimension// Mat img original_img.reshape(1, 1);Mat img original_img;const int MODEL_IN_WIDTH 565;const int MODEL_IN_HEIGHT 584;const int MODEL_IN_CHANNELS 3;rknn_context ctx 0;int ret;int model_len 0;unsigned char *model;// 初始化RKNN模型 model load_model(model_path, model_len);ret rknn_init(ctx, model, model_len, 0, NULL);if (ret 0){printf(rknn_init fail! ret%d\n, ret);return -1;}// 获取模型输入输出信息 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;}// 设置模型输入 // 使用rknn_input结构体存储模型输入信息, 表示模型的一个数据输入,用来作为参数传入给 rknn_inputs_set 函数rknn_input inputs[1];memset(inputs, 0, sizeof(inputs));inputs[0].index 0; // 设置模型输入索引inputs[0].type RKNN_TENSOR_UINT8; // 设置模型输入类型inputs[0].size img.cols * img.rows * img.channels() * sizeof(uint8_t); // 设置模型输入大小inputs[0].fmt RKNN_TENSOR_NHWC; // 设置模型输入格式NHWCinputs[0].buf img.data; // 设置模型输入数据// 使用rknn_inputs_set函数设置模型输入ret rknn_inputs_set(ctx, io_num.n_input, inputs);if (ret 0){printf(rknn_input_set fail! ret%d\n, ret);return -1;}// 推理 printf(rknn_run\n);ret rknn_run(ctx, nullptr);if (ret 0){printf(rknn_run fail! ret%d\n, ret);return -1;}// 获取模型输出 // 使用rknn_output结构体存储模型输出信息rknn_output outputs[1];memset(outputs, 0, sizeof(outputs));outputs[0].want_float 1;// 使用rknn_outputs_get函数获取模型输出ret rknn_outputs_get(ctx, 1, outputs, NULL);if (ret 0){printf(rknn_outputs_get fail! ret%d\n, ret);return -1;}float *output_data (float *)outputs[0].buf;int output_size outputs[0].size / sizeof(uint32_t);// cout channel: channel endl;// cout res: res endl;// cout size: output_size endl;int img_h 584;int img_w 565;int channel output_size / img_h / img_w;int res output_size % (img_h * img_w);cout channel: channel endl;cout res: res endl;int* label_data new int[img_h * img_w];if (res ! 0){fprintf(stderr, output shape is not supported.\n);}else{/* multi-class segmentation */for (int i 0; i img_h; i){for (int j 0; j img_w; j){int argmax_id -1;float max_conf std::numeric_limitsfloat::min();for (int k 0; k channel; k){float out_value output_data[k * img_w * img_h i * img_w j];if (out_value max_conf){argmax_id k;max_conf out_value;}}label_data[i * img_w j] argmax_id;if (label_data[i * img_w j] 1) {label_data[i * img_w j] 255;}}}}// 将图像数据存储到一维数组中int* roi_array new int[img_h * img_w];for (int i 0; i img_h; i) {for (int j 0; j img_w; j) {roi_array[i * img_w j] static_castint(roi_img.atuchar(i, j));}}for (int i 0; i img_h; i) {for (int j 0; j img_w; j) {if (roi_array[i * img_w j] 0) {label_data[i * img_w j] roi_array[i * img_w j];}}}Mat result_img(img_h, img_w, CV_8UC1);for (int i 0; i img_h; i) {for (int j 0; j img_w; j) {result_img.atuchar(i, j) static_castuchar(label_data[i * img_w j]);}}imwrite(result.png, result_img);// Release resourcesrknn_outputs_release(ctx, 1, outputs);if (ret 0){printf(rknn_outputs_release fail! ret%d\n, ret);return -1;}else if (ctx 0){// 释放RKNN模型 rknn_destroy(ctx);}// 释放模型数据 if (model){free(model);}auto end std::chrono::high_resolution_clock::now();auto duration std::chrono::duration_caststd::chrono::milliseconds(end - start);std::cout Execution time: duration.count() milliseconds std::endl;return 0;
}感觉代码还是不优美很多的for循环看着难受但是已经实现了后续再修改吧
3. 结果展示 C的测试结果 time: 497 ms python的测试结果 time:660ms
文章转载自: http://www.morning.xdhcr.cn.gov.cn.xdhcr.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.yfddl.cn.gov.cn.yfddl.cn http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.hhxpl.cn.gov.cn.hhxpl.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.gthwr.cn.gov.cn.gthwr.cn http://www.morning.yyngs.cn.gov.cn.yyngs.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.xkgyh.cn.gov.cn.xkgyh.cn http://www.morning.srkwf.cn.gov.cn.srkwf.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn http://www.morning.gbljq.cn.gov.cn.gbljq.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.rfbt.cn.gov.cn.rfbt.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn http://www.morning.mfct.cn.gov.cn.mfct.cn http://www.morning.jkcpl.cn.gov.cn.jkcpl.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.zypnt.cn.gov.cn.zypnt.cn http://www.morning.ffbl.cn.gov.cn.ffbl.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.rzsxb.cn.gov.cn.rzsxb.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.jgttx.cn.gov.cn.jgttx.cn http://www.morning.srhqm.cn.gov.cn.srhqm.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.myxps.cn.gov.cn.myxps.cn http://www.morning.mjbnp.cn.gov.cn.mjbnp.cn http://www.morning.mngh.cn.gov.cn.mngh.cn http://www.morning.lzwfg.cn.gov.cn.lzwfg.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.qwyms.cn.gov.cn.qwyms.cn http://www.morning.crrjg.cn.gov.cn.crrjg.cn http://www.morning.dgsx.cn.gov.cn.dgsx.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.kphsp.cn.gov.cn.kphsp.cn http://www.morning.tkztx.cn.gov.cn.tkztx.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.lthtp.cn.gov.cn.lthtp.cn http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.rjqtq.cn.gov.cn.rjqtq.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.rbbyd.cn.gov.cn.rbbyd.cn http://www.morning.sdecsd.cn.gov.cn.sdecsd.cn http://www.morning.rlbfp.cn.gov.cn.rlbfp.cn http://www.morning.tjkth.cn.gov.cn.tjkth.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.qfdyt.cn.gov.cn.qfdyt.cn http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.tkchm.cn.gov.cn.tkchm.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.fgkrh.cn.gov.cn.fgkrh.cn http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.hffpy.cn.gov.cn.hffpy.cn http://www.morning.gthwr.cn.gov.cn.gthwr.cn http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn http://www.morning.wlstn.cn.gov.cn.wlstn.cn