本文主要是介绍c#使用onnxruntime调用yolo模型导出的onnx模型分割图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.今天写下c#中怎么使用yolo模型系列导出的onnx分割图片
2.yolo训练好后,把模型导出为onnx模式。
3.导出模型为onnx模式后,在window中要引用,可以使用 Microsoft.ML.OnnxRuntime库
4.window系统要求win10或者更高,vs用vs2022或更高,.net使用的框架要在.net4.8或更高,才支持使用Microsoft.ML.OnnxRuntime库,。
5.下载 Microsoft.ML.OnnxRuntime。可以在vs2022的包管理器收索下载。
b站视频地址
添加链接描述
基本核心代码:
if (_onnx_model_is_exit){_object_list.Clear();_break_off = false;#region 获取输入输出的名称var input_datas_ = _session.InputMetadata;var input_names_ = _session.InputNames;var out_datas_ = _session.OutputMetadata;var out_names_ = _session.OutputNames;#endregion#region 创建输入数据PixelFormat p_f_ = _image.PixelFormat;MemoryStream ms = new MemoryStream();_image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);byte[] bytes = ms.GetBuffer(); //byte[] bytes= ms.ToArray(); 这两句都可以ms.Close();int leng_ = bytes.GetLength(0);//float[] input_data_ = { 1, 2, 3, 4 };long[] input_shape_ = { 1, 3, 640, 640 };float[] input_data_ = new float[leng_];for (int i = 0; i < leng_; i++){input_data_[i] = ((float)bytes[i]) / 255;//初拟255归一化}var input_ort_value_ = OrtValue.CreateTensorValueFromMemory(input_data_, input_shape_);var inputs1_ = new Dictionary<string, OrtValue> { { "images", input_ort_value_ } };#endregion#region 创建推理获取结果//创建创建运行的要求var run_options_ = new RunOptions();//推理IDisposableReadOnlyCollection<OrtValue> results_ = _session.Run(run_options_, inputs1_, out_names_);#endregion#region 获取输出结果var output0_type_and_shape_ = results_[0].GetTensorTypeAndShape();var output1_type_and_shape_ = results_[1].GetTensorTypeAndShape();var out_put0_ = results_[0].GetTensorDataAsSpan<float>();var out_put1_ = results_[1].GetTensorDataAsSpan<float>();#endregion#region 转成矩阵float[,] out_put0_float_ = new float[116, 8400];float[,,] out_put1_float_ = new float[32, 160, 160];//第一个out_put0转成矩阵int out_put0_index_ = 0;for (int row_ = 0; row_ < 116; row_++){for (int column_ = 0; column_ < 8400; column_++){out_put0_float_[row_, column_] = out_put0_[out_put0_index_];out_put0_index_++;}}//第二个out_put1转成矩阵int out_put1_index_ = 0;for (int channel_ = 0; channel_ < 32; channel_++){for (int row_ = 0; row_ < 160; row_++){for (int column_ = 0; column_ < 160; column_++){out_put1_float_[channel_, row_, column_] = out_put1_[out_put1_index_];out_put1_index_++;}}}//out_put0_转置处理float[,] out_put0_float_transpose_ = new float[8400, 116];for (int row_ = 0; row_ < 116; row_++){for (int column_ = 0; column_ < 8400; column_++){out_put0_float_transpose_[column_, row_] = out_put0_float_[row_, column_];}}//out_put1_转置处理float[,] out_put1_float_transpose_ = new float[32, 160 * 160];for (int channel_ = 0; channel_ < 32; channel_++){int out_put1_float_transpose_column_ = 0;for (int row_ = 0; row_ < 160; row_++){for (int column_ = 0; column_ < 160; column_++){out_put1_float_transpose_[channel_, out_put1_float_transpose_column_] = out_put1_float_[channel_, row_, column_];out_put1_float_transpose_column_++;}}}#endregionList<ObjectStruct> object_temp_list_ = new List<ObjectStruct>();#region 提取结果 第一个矩阵out_put0结果/********** * 第一个矩阵out_put0结果 0-4 x_center,y_center,width,height of bounding box* * 第一个矩阵out_put0结果 4-84 object class probabilities for all 80 classes, that this yolov8 model can detect* * 第一个矩阵out_put0结果 84-116 need muplty out_put1 it represent mask.* * ************/for (int row_ = 0; row_ < 8400 && _break_off == false; row_++)//循环每一个对象{//获取最大分数float prob_ = out_put0_float_transpose_[row_, 4];float class_id_ = 0;for (int column_ = 4; column_ < 84; column_++)//检测最大分数{if (prob_ < out_put0_float_transpose_[row_, column_]){prob_ = out_put0_float_transpose_[row_, column_];class_id_ = column_ - 4;}}if (prob_ > _prob_min){ObjectStruct ob_ = new ObjectStruct();ob_._prob = prob_;ob_._class_id = class_id_;ob_._class_label = _yolo_classes[(int)class_id_];ob_._boxe_center_x_ = out_put0_float_transpose_[row_, 0];ob_._boxe_center_y_ = out_put0_float_transpose_[row_, 1];ob_._boxe_center_width_ = out_put0_float_transpose_[row_, 2];ob_._boxe_center_height_ = out_put0_float_transpose_[row_, 3];ob_._x1 = ob_._boxe_center_x_- ob_._boxe_center_width_/2;ob_._x2 = ob_._boxe_center_x_ + ob_._boxe_center_width_ / 2;ob_._y1 = ob_._boxe_center_y_ - ob_._boxe_center_height_ / 2;ob_._y2 = ob_._boxe_center_x_ + ob_._boxe_center_height_ / 2;//取出maskfloat[,] mask_ = new float[1, 32];for (int column_ = 84; column_ < 116; column_++)//检测最大分数{if (prob_ < out_put0_float_transpose_[row_, column_]){mask_[0, column_ - 84] = out_put0_float_transpose_[row_, column_];}}//矩阵相乘masks_*out_put1_float_transpose_float[,] masks_multiply_output1_float_transpose_ = MultiplyMatrices(mask_, out_put1_float_transpose_);ob_._mask = new float[160, 160];int masks_multiply_output1_float_transpose_index_ = 0;for (int row_mask_image_ = 0; row_mask_image_ < 160; row_mask_image_++){for (int column_mask_image_ = 0; column_mask_image_ < 160; column_mask_image_++){float num_ = masks_multiply_output1_float_transpose_[0, masks_multiply_output1_float_transpose_index_];float num1_ = (float)(1 / (1 + Math.Exp(-num_)));if (num1_ > 0.5){ob_._mask[row_mask_image_, column_mask_image_] = 1;}else{ob_._mask[row_mask_image_, column_mask_image_] = 0;}masks_multiply_output1_float_transpose_index_++;}}object_temp_list_.Add(ob_);}}#endregion#region 去除重叠while (object_temp_list_.Count > 0){ObjectStruct object_temp_ = object_temp_list_[0]; for (int i = 0; i < object_temp_list_.Count; i++){double dis1_ = Math.Abs(object_temp_._x1 - object_temp_list_[i]._x1);double dis2_ = Math.Abs(object_temp_._y1 - object_temp_list_[i]._y1);double dis3_ = Math.Abs(object_temp_._x2 - object_temp_list_[i]._x2);double dis4_ = Math.Abs(object_temp_._y2 - object_temp_list_[i]._y2);if (dis1_ < _overlap_distance&& dis2_ < _overlap_distance&& dis3_ < _overlap_distance&& dis4_ < _overlap_distance){object_temp_list_.RemoveAt(i);i--;}}_object_list.Add(object_temp_);}#endregion#region 释放资源for (int i = 0; i < results_.Count; i++){results_[i].Dispose();}results_.Dispose();input_ort_value_.Dispose();inputs1_.Clear();#endregion_break_off = false;GC.Collect();}
要完成代码可以在b站我的工房购买 https://gf.bilibili.com/item/detail/1105641118
这篇关于c#使用onnxruntime调用yolo模型导出的onnx模型分割图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!