C# OnnxRuntime DIS高精度图像二类分割

说明

This is the repo for our new project Highly Accurate Dichotomous Image Segmentation

aed8f659c6a77b47b55ffbcfa39b6ed0.png

github地址:

https://github.com/xuebinqin/DIS

效果

965094bdbfceb8be528032679ea0a30a.png

模型信息

Inputs
-------------------------
name:input
tensor:Float[1, 3, 480, 640]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[1, 1, 480, 640]
---------------------------------------------------------------

项目

VS2022

.net framework 4.8

OpenCvSharp 4.8

Microsoft.ML.OnnxRuntime 1.16.2

9fd9541f0cc811b185cbd606c6139d82.png

代码

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_container;
 
        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)
        {
            // 创建输入容器
            input_container = new List<NamedOnnxValue>();
 
            // 创建输出会话
            options = new SessionOptions();
            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
            options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行
 
            // 创建推理模型类,读取本地模型文件
            model_path = "model/isnet_general_use_480x640.onnx";
 
            inpHeight = 480;
            inpWidth = 640;
 
            outHeight = 480;
            outWidth = 640;
 
            onnx_session = new InferenceSession(model_path, options);
 
            // 创建输入容器
            input_container = 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 = "检测中,请稍等……";
            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 放入一个输入参数的容器,并指定名称
            input_container.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor));
 
            dt1 = DateTime.Now;
            //运行 Inference 并获取结果
            result_infer = onnx_session.Run(input_container);
            dt2 = DateTime.Now;
 
            //将输出结果转为DisposableNamedOnnxValue数组
            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 = "推理耗时:" + (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);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值