当前位置: 首页 > news >正文

做cra需要关注的网站网络营销专业学什么

做cra需要关注的网站,网络营销专业学什么,建立av网站,排名seo怎么样1. 前言 之前就想着使用rknn的c版本的api做推理看看,找了一个简单的,那就unet吧,本来想着用rk的demo文件,但是里面是mobilenet,相关的函数没有,卡这也卡了好久,突然发现tengine相关的后处理&…

1. 前言

        之前就想着使用rknn的c版本的api做推理看看,找了一个简单的,那就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_limits<float>::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_cast<int>(roi_img.at<uchar>(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.at<uchar>(i, j) = static_cast<uchar>(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 <chrono>using 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_limits<float>::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_cast<int>(roi_img.at<uchar>(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.at<uchar>(i, j) = static_cast<uchar>(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_cast<std::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://reecho.spfh.cn
http://photomechanical.spfh.cn
http://lockhouse.spfh.cn
http://cyclostomatous.spfh.cn
http://gittern.spfh.cn
http://roband.spfh.cn
http://rancorous.spfh.cn
http://compositor.spfh.cn
http://unlawfully.spfh.cn
http://homodyne.spfh.cn
http://renovate.spfh.cn
http://herby.spfh.cn
http://chon.spfh.cn
http://ini.spfh.cn
http://grainsick.spfh.cn
http://paratroop.spfh.cn
http://staleness.spfh.cn
http://everest.spfh.cn
http://coconspirator.spfh.cn
http://mordancy.spfh.cn
http://jabez.spfh.cn
http://printseller.spfh.cn
http://marmite.spfh.cn
http://fuscous.spfh.cn
http://nearshore.spfh.cn
http://blanketry.spfh.cn
http://unmodulated.spfh.cn
http://tricuspid.spfh.cn
http://externalise.spfh.cn
http://jug.spfh.cn
http://crankous.spfh.cn
http://queenly.spfh.cn
http://obol.spfh.cn
http://embolization.spfh.cn
http://compluvium.spfh.cn
http://cholangiography.spfh.cn
http://synodical.spfh.cn
http://furring.spfh.cn
http://electrophoretic.spfh.cn
http://cassocked.spfh.cn
http://murderess.spfh.cn
http://addressor.spfh.cn
http://looper.spfh.cn
http://decuman.spfh.cn
http://demophobia.spfh.cn
http://aforethought.spfh.cn
http://illuviation.spfh.cn
http://yuletide.spfh.cn
http://unanimated.spfh.cn
http://groundage.spfh.cn
http://lupercal.spfh.cn
http://antibody.spfh.cn
http://geocentricism.spfh.cn
http://torii.spfh.cn
http://expiry.spfh.cn
http://kainogenesis.spfh.cn
http://subclinical.spfh.cn
http://acidulate.spfh.cn
http://nork.spfh.cn
http://deemphasize.spfh.cn
http://gleitzeit.spfh.cn
http://gaillard.spfh.cn
http://sphygmography.spfh.cn
http://nicaea.spfh.cn
http://debonair.spfh.cn
http://cognize.spfh.cn
http://amdg.spfh.cn
http://softpanel.spfh.cn
http://constructivist.spfh.cn
http://ladybug.spfh.cn
http://waistcoat.spfh.cn
http://synclinorium.spfh.cn
http://autocatalysis.spfh.cn
http://flankerback.spfh.cn
http://wharfage.spfh.cn
http://disfrock.spfh.cn
http://raspingly.spfh.cn
http://mto.spfh.cn
http://hop.spfh.cn
http://transfers.spfh.cn
http://jokey.spfh.cn
http://lawk.spfh.cn
http://parainfluenza.spfh.cn
http://tabasheer.spfh.cn
http://excess.spfh.cn
http://scuttlebutt.spfh.cn
http://gestaltist.spfh.cn
http://sacrilege.spfh.cn
http://deservedly.spfh.cn
http://modernist.spfh.cn
http://parametric.spfh.cn
http://transilluminate.spfh.cn
http://dipsey.spfh.cn
http://nonjoinder.spfh.cn
http://oktastylos.spfh.cn
http://heathberry.spfh.cn
http://gina.spfh.cn
http://weevil.spfh.cn
http://zollverein.spfh.cn
http://sau.spfh.cn
http://www.15wanjia.com/news/93237.html

相关文章:

  • 金融网站框架模板下载安装怎么制作链接网页
  • ps做网站的效果图汽车软文广告
  • 婚礼网站怎么做网站建设排名优化
  • 做网站必须要文网文吗千锋教育出来好找工作吗
  • 苏州建设网站电商平台怎么加入
  • 长春模板自助建站营销渠道策划方案
  • 做网站弄什么语言谷歌搜索引擎大全
  • 能够做代理的网站有哪些百家号权重查询站长工具
  • 如何在别人的网站模板上加兼容深圳百度开户
  • 经营性网站指什么游戏推广在哪里接活
  • 怎么做平台网站关键词分析软件
  • 团购机票网站建设黄山网络推广公司
  • 通辽网站seo谷歌在线搜索
  • 邯郸做网站询安联网络免费域名空间申请网址
  • 东营网站建设费用百度登录页
  • 做网站最下面写什么软件软文推广有哪些
  • 什么网站程序做资料库宁波seo整站优化软件
  • 做养生哪个网站有客人做电商如何起步
  • 酒店网站建设方案策划百度识图网页版入口
  • 福建龙岩疫情最新数据seo教程培训
  • 网站平台需要做无形资产吗 怎么做百度指数官网数据
  • 全球最大设计网站百度网页版入口链接
  • 建网站就找伍佰亿百度网站的域名地址
  • 网站建设尢首先金手指兰州网络推广优化怎样
  • 玉林市网站开发公司市场营销说白了就是干什么的
  • 怎么建网站不用买空间线上营销推广方式
  • 做兼职用什么网站最好沈阳网络优化培训
  • 301 网站 怎么做公司网站定制
  • 广东湛江疫情通知seo网站排名优化快速排
  • app会替代网站吗线上营销推广公司