C# Onnx實(shí)現(xiàn)DIS高精度圖像二類分割
介紹
github地址:https://github.com/xuebinqin/DIS
This is the repo for our new project Highly Accurate Dichotomous Image Segmentation
對(duì)應(yīng)的paper是ECCV2022的一篇文章《Highly Accurate Dichotomous Image Segmentation》, 跟BASNet和U2-Net都是出自同一個(gè)作者寫的。
效果
模型信息
Inputs
-------------------------
name:input
tensor:Float[1, 3, 480, 640]
---------------------------------------------------------------
Outputs
-------------------------
name:output
tensor:Float[1, 1, 480, 640]
---------------------------------------------------------------
項(xiàng)目
VS2022
.net framework 4.8
OpenCvSharp 4.8
Microsoft.ML.OnnxRuntime 1.16.2
代碼
using Microsoft.ML.OnnxRuntime.Tensors; using Microsoft.ML.OnnxRuntime; using OpenCvSharp; using System; using System.Collections.Generic; using System.Windows.Forms; using System.Linq; using System.Drawing; using static System.Net.Mime.MediaTypeNames; namespace Onnx_Demo { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png"; string image_path = ""; DateTime dt1 = DateTime.Now; DateTime dt2 = DateTime.Now; int inpWidth; int inpHeight; int outHeight, outWidth; Mat image; string model_path = ""; SessionOptions options; InferenceSession onnx_session; Tensor<float> input_tensor; Tensor<float> mask_tensor; List<NamedOnnxValue> input_ontainer; IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer; DisposableNamedOnnxValue[] results_onnxvalue; private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = fileFilter; if (ofd.ShowDialog() != DialogResult.OK) return; pictureBox1.Image = null; pictureBox2.Image = null; textBox1.Text = ""; image_path = ofd.FileName; pictureBox1.Image = new System.Drawing.Bitmap(image_path); image = new Mat(image_path); } private void Form1_Load(object sender, EventArgs e) { // 創(chuàng)建輸入容器 input_ontainer = new List<NamedOnnxValue>(); // 創(chuàng)建輸出會(huì)話 options = new SessionOptions(); options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO; options.AppendExecutionProvider_CPU(0);// 設(shè)置為CPU上運(yùn)行 // 創(chuàng)建推理模型類,讀取本地模型文件 model_path = "model/isnet_general_use_480x640.onnx"; inpHeight = 480; inpWidth = 640; outHeight = 480; outWidth = 640; onnx_session = new InferenceSession(model_path, options); // 創(chuàng)建輸入容器 input_ontainer = new List<NamedOnnxValue>(); image_path = "test_img/bike.jpg"; pictureBox1.Image = new Bitmap(image_path); } private unsafe void button2_Click(object sender, EventArgs e) { if (image_path == "") { return; } textBox1.Text = "檢測(cè)中,請(qǐng)稍等……"; pictureBox2.Image = null; System.Windows.Forms.Application.DoEvents(); image = new Mat(image_path); Mat resize_image = new Mat(); Cv2.Resize(image, resize_image, new OpenCvSharp.Size(inpWidth, inpHeight)); float[] input_tensor_data = new float[1 * 3 * inpWidth * inpHeight]; for (int c = 0; c < 3; c++) { for (int i = 0; i < inpHeight; i++) { for (int j = 0; j < inpWidth; j++) { float pix = ((byte*)(resize_image.Ptr(i).ToPointer()))[j * 3 + 2 - c]; input_tensor_data[c * inpHeight * inpWidth + i * inpWidth + j] = (float)(pix / 255.0 - 0.5); } } } input_tensor = new DenseTensor<float>(input_tensor_data, new[] { 1, 3, inpHeight, inpWidth }); //將 input_tensor 放入一個(gè)輸入?yún)?shù)的容器,并指定名稱 input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor)); dt1 = DateTime.Now; //運(yùn)行 Inference 并獲取結(jié)果 result_infer = onnx_session.Run(input_ontainer); dt2 = DateTime.Now; //將輸出結(jié)果轉(zhuǎn)為DisposableNamedOnnxValue數(shù)組 results_onnxvalue = result_infer.ToArray(); float[] pred = results_onnxvalue[0].AsTensor<float>().ToArray(); Mat mask = new Mat(outHeight, outWidth, MatType.CV_32FC1, pred); double min_value, max_value; Cv2.MinMaxLoc(mask, out min_value, out max_value); mask = (mask - min_value) / (max_value - min_value); mask *= 255; mask.ConvertTo(mask, MatType.CV_8UC1); Cv2.Resize(mask, mask, new OpenCvSharp.Size(image.Cols, image.Rows)); Mat result_image = mask.Clone(); if (pictureBox2.Image != null) { pictureBox2.Image.Dispose(); } pictureBox2.Image = new System.Drawing.Bitmap(result_image.ToMemoryStream()); textBox1.Text = "推理耗時(shí):" + (dt2 - dt1).TotalMilliseconds + "ms"; mask.Dispose(); image.Dispose(); resize_image.Dispose(); result_image.Dispose(); } private void pictureBox2_DoubleClick(object sender, EventArgs e) { Common.ShowNormalImg(pictureBox2.Image); } private void pictureBox1_DoubleClick(object sender, EventArgs e) { Common.ShowNormalImg(pictureBox1.Image); } } }
到此這篇關(guān)于C# Onnx實(shí)現(xiàn)DIS高精度圖像二類分割的文章就介紹到這了,更多相關(guān)C# Onnx圖像二類分割內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity3D實(shí)現(xiàn)簡(jiǎn)易五子棋源碼
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)簡(jiǎn)易五子棋源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)實(shí)例
這篇文章主要給大家介紹了基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)的相關(guān)資料,文中介紹的方法并未使用第三方類庫(kù),可以完美解決這個(gè)問題,需要的朋友可以參考下2021-07-07C#使用Aspose.Cells創(chuàng)建和讀取Excel文件
這篇文章主要為大家詳細(xì)介紹了C#使用Aspose.Cells創(chuàng)建和讀取Excel文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10C#實(shí)現(xiàn)HTML和UBB互相轉(zhuǎn)換的方法
這篇文章主要介紹了C#實(shí)現(xiàn)HTML和UBB互相轉(zhuǎn)換的方法,通過兩個(gè)自定義函數(shù)DoHtmlToUB與ubbtohtml來實(shí)現(xiàn)HTML代碼與ubb代碼間的相互轉(zhuǎn)換,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11比較2個(gè)datatable內(nèi)容是否相同的方法
這篇文章主要介紹了比較2個(gè)datatable內(nèi)容是否相同的方法,大家參考使用吧2014-01-01