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


    目录

    介绍

    效果

    模型信息

    项目

    代码

    下载


    介绍

    github地址:https://github.com/xuebinqin/DIS

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

    对应的paper是ECCV2022的一篇文章《Highly Accurate Dichotomous Image Segmentation》, 跟BASNet和U2-Net都是出自同一个作者写的。 

    效果

    模型信息

    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

    代码

    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 input_tensor;
            Tensor mask_tensor;
            List input_ontainer;

            IDisposableReadOnlyCollection 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_ontainer = new List();

                // 创建输出会话
                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_ontainer = new List();

                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(input_tensor_data, new[] { 1, 3, inpHeight, inpWidth });

                //将 input_tensor 放入一个输入参数的容器,并指定名称
                input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor));

                dt1 = DateTime.Now;
                //运行 Inference 并获取结果
                result_infer = onnx_session.Run(input_ontainer);
                dt2 = DateTime.Now;

                //将输出结果转为DisposableNamedOnnxValue数组
                results_onnxvalue = result_infer.ToArray();

                float[] pred = results_onnxvalue[0].AsTensor().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);
            }
        }
    }

    1. using Microsoft.ML.OnnxRuntime.Tensors;
    2. using Microsoft.ML.OnnxRuntime;
    3. using OpenCvSharp;
    4. using System;
    5. using System.Collections.Generic;
    6. using System.Windows.Forms;
    7. using System.Linq;
    8. using System.Drawing;
    9. using static System.Net.Mime.MediaTypeNames;
    10. namespace Onnx_Demo
    11. {
    12. public partial class frmMain : Form
    13. {
    14. public frmMain()
    15. {
    16. InitializeComponent();
    17. }
    18. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    19. string image_path = "";
    20. DateTime dt1 = DateTime.Now;
    21. DateTime dt2 = DateTime.Now;
    22. int inpWidth;
    23. int inpHeight;
    24. int outHeight, outWidth;
    25. Mat image;
    26. string model_path = "";
    27. SessionOptions options;
    28. InferenceSession onnx_session;
    29. Tensor<float> input_tensor;
    30. Tensor<float> mask_tensor;
    31. List input_ontainer;
    32. IDisposableReadOnlyCollection result_infer;
    33. DisposableNamedOnnxValue[] results_onnxvalue;
    34. private void button1_Click(object sender, EventArgs e)
    35. {
    36. OpenFileDialog ofd = new OpenFileDialog();
    37. ofd.Filter = fileFilter;
    38. if (ofd.ShowDialog() != DialogResult.OK) return;
    39. pictureBox1.Image = null;
    40. pictureBox2.Image = null;
    41. textBox1.Text = "";
    42. image_path = ofd.FileName;
    43. pictureBox1.Image = new System.Drawing.Bitmap(image_path);
    44. image = new Mat(image_path);
    45. }
    46. private void Form1_Load(object sender, EventArgs e)
    47. {
    48. // 创建输入容器
    49. input_ontainer = new List();
    50. // 创建输出会话
    51. options = new SessionOptions();
    52. options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
    53. options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行
    54. // 创建推理模型类,读取本地模型文件
    55. model_path = "model/isnet_general_use_480x640.onnx";
    56. inpHeight = 480;
    57. inpWidth = 640;
    58. outHeight = 480;
    59. outWidth = 640;
    60. onnx_session = new InferenceSession(model_path, options);
    61. // 创建输入容器
    62. input_ontainer = new List();
    63. image_path = "test_img/bike.jpg";
    64. pictureBox1.Image = new Bitmap(image_path);
    65. }
    66. private unsafe void button2_Click(object sender, EventArgs e)
    67. {
    68. if (image_path == "")
    69. {
    70. return;
    71. }
    72. textBox1.Text = "检测中,请稍等……";
    73. pictureBox2.Image = null;
    74. System.Windows.Forms.Application.DoEvents();
    75. image = new Mat(image_path);
    76. Mat resize_image = new Mat();
    77. Cv2.Resize(image, resize_image, new OpenCvSharp.Size(inpWidth, inpHeight));
    78. float[] input_tensor_data = new float[1 * 3 * inpWidth * inpHeight];
    79. for (int c = 0; c < 3; c++)
    80. {
    81. for (int i = 0; i < inpHeight; i++)
    82. {
    83. for (int j = 0; j < inpWidth; j++)
    84. {
    85. float pix = ((byte*)(resize_image.Ptr(i).ToPointer()))[j * 3 + 2 - c];
    86. input_tensor_data[c * inpHeight * inpWidth + i * inpWidth + j] = (float)(pix / 255.0 - 0.5);
    87. }
    88. }
    89. }
    90. input_tensor = new DenseTensor<float>(input_tensor_data, new[] { 1, 3, inpHeight, inpWidth });
    91. //将 input_tensor 放入一个输入参数的容器,并指定名称
    92. input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor));
    93. dt1 = DateTime.Now;
    94. //运行 Inference 并获取结果
    95. result_infer = onnx_session.Run(input_ontainer);
    96. dt2 = DateTime.Now;
    97. //将输出结果转为DisposableNamedOnnxValue数组
    98. results_onnxvalue = result_infer.ToArray();
    99. float[] pred = results_onnxvalue[0].AsTensor<float>().ToArray();
    100. Mat mask = new Mat(outHeight, outWidth, MatType.CV_32FC1, pred);
    101. double min_value, max_value;
    102. Cv2.MinMaxLoc(mask, out min_value, out max_value);
    103. mask = (mask - min_value) / (max_value - min_value);
    104. mask *= 255;
    105. mask.ConvertTo(mask, MatType.CV_8UC1);
    106. Cv2.Resize(mask, mask, new OpenCvSharp.Size(image.Cols, image.Rows));
    107. Mat result_image = mask.Clone();
    108. if (pictureBox2.Image != null)
    109. {
    110. pictureBox2.Image.Dispose();
    111. }
    112. pictureBox2.Image = new System.Drawing.Bitmap(result_image.ToMemoryStream());
    113. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
    114. mask.Dispose();
    115. image.Dispose();
    116. resize_image.Dispose();
    117. result_image.Dispose();
    118. }
    119. private void pictureBox2_DoubleClick(object sender, EventArgs e)
    120. {
    121. Common.ShowNormalImg(pictureBox2.Image);
    122. }
    123. private void pictureBox1_DoubleClick(object sender, EventArgs e)
    124. {
    125. Common.ShowNormalImg(pictureBox1.Image);
    126. }
    127. }
    128. }

    下载

    源码下载

  • 相关阅读:
    Web漏洞
    django restframework 中使用throttle进行限流
    用人话讲解深度学习中CUDA,cudatookit,cudnn和pytorch的关系
    NL50-MPI 西门子MPI转以太网通讯模块
    linux篇【11】:linux下的线程<后序>
    云游戏下,会带来哪些技术变革
    【分布式事务】
    972信息检索 | 第五章 国外综合性信息检索系统
    OpenCV(四十三):Shi-Tomas角点检测
    [附源码]计算机毕业设计学习帮扶网站设计与实现Springboot程序
  • 原文地址:https://blog.csdn.net/lw112190/article/details/134510487