• C# Onnx 特征匹配 DeDoDe 检测,不描述---描述,不检测


    目录

    介绍

    效果

    模型信息

    项目

    代码 

    下载 


    介绍

    github地址:https://github.com/Parskatt/DeDoDe

    DeDoDe 🎶 Detect, Don't Describe - Describe, Don't Detect, for Local Feature Matching

    The DeDoDe detector learns to detect 3D consistent repeatable keypoints, which the DeDoDe descriptor learns to match. The result is a powerful decoupled local feature matcher.

    Training DeDoDe

    DISCLAMER: I've (Johan) not yet tested that the training scripts here reproduces our original results. This repo is very similar to the internal training repo, but there might be bugs introduced by refactoring etc. Let me know if you face any issues reproducing our results (or if you somehow get better results :D).

    See experiments for the scripts to train DeDoDe. We trained on a single A100-40GB with a batchsize of 8. Note that you need to do the data prep first, see data_prep.

    As usual, we require that you have the MegaDepth dataset already downloaded, and that you have the prepared scene info from DKM.

    效果

    模型信息

    Inputs
    -------------------------
    name:images
    tensor:Float[-1, 3, -1, -1]
    ---------------------------------------------------------------

    Outputs
    -------------------------
    name:matches_A
    tensor:Float[-1, -1]
    name:matches_B
    tensor:Float[-1, -1]
    name:batch_ids
    tensor:Int64[-1]
    ---------------------------------------------------------------

    项目

    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;
    using System.Numerics;

    namespace Onnx_Demo
    {
        public partial class frmMain : Form
        {
            public frmMain()
            {
                InitializeComponent();
            }

            string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
            string image_path = "";
            string image_path2 = "";

            DateTime dt1 = DateTime.Now;
            DateTime dt2 = DateTime.Now;

            int inpWidth;
            int inpHeight;

            float[] mean =new float[] { 0.485f, 0.456f, 0.406f };
            float[] std = new float[] { 0.229f, 0.224f, 0.225f };

            Mat image;
            Mat image2;

            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/dedode_end2end_1024.onnx";

                inpHeight = 256;
                inpWidth = 256;

                onnx_session = new InferenceSession(model_path, options);

                // 创建输入容器
                input_ontainer = new List();

                image_path = "test_img/im_A.jpg";
                pictureBox1.Image = new Bitmap(image_path);

                image_path2 = "test_img/im_B.jpg";
                pictureBox3.Image = new Bitmap(image_path2);

            }

            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);
                image2 = new Mat(image_path2);

                float[] input_tensor_data = new float[2 * 3 * inpWidth * inpHeight];

                //preprocess
                Mat dstimg = new Mat();
                Cv2.CvtColor(image, dstimg, ColorConversionCodes.BGR2RGB);
                Cv2.Resize(dstimg, dstimg, new OpenCvSharp.Size(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*)(dstimg.Ptr(i).ToPointer()))[j * 3 + c];
                            input_tensor_data[c * inpWidth * inpHeight + i * inpWidth + j] = (float)((pix / 255.0 - mean[c]) / std[c]);
                        }
                    }
                }

                Cv2.CvtColor(image2, dstimg, ColorConversionCodes.BGR2RGB);
                Cv2.Resize(dstimg, dstimg, new OpenCvSharp.Size(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*)(dstimg.Ptr(i).ToPointer()))[j * 3 + c];
                            input_tensor_data[(3+c )* inpWidth * inpHeight + i * inpWidth + j] = (float)((pix / 255.0 - mean[c]) / std[c]);
                        }
                    }
                }

                input_tensor = new DenseTensor(input_tensor_data, new[] { 2, 3, inpHeight, inpWidth });

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

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

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

                float[] matches_A = results_onnxvalue[0].AsTensor().ToArray();
                float[] matches_B = results_onnxvalue[1].AsTensor().ToArray();
                int num_points = results_onnxvalue[0].AsTensor().Dimensions[0];

                List points_A = new List();
                List points_B = new List();

                KeyPoint temp;
                for (int i = 0; i < num_points; i++)
                {
                    temp = new KeyPoint();
                    temp.Pt.X = (float)((matches_A[i * 2] + 1) * 0.5 * image.Cols);
                    temp.Pt.Y = (float)((matches_A[i * 2 + 1] + 1) * 0.5 * image.Rows);
                    temp.Size = 1f;
                    points_A.Add(temp);
                }

                num_points = results_onnxvalue[1].AsTensor().Dimensions[0];
                for (int i = 0; i < num_points; i++)
                {
                    temp = new KeyPoint();
                    temp.Pt.X = (float)((matches_B[i * 2] + 1) * 0.5 * image2.Cols);
                    temp.Pt.Y = (float)((matches_B[i * 2 + 1] + 1) * 0.5 * image2.Rows);
                    temp.Size = 1f;
                    points_B.Add(temp);
                }

                //匹配结果放在matches里面
                num_points = points_A.Count();
                List matches=new List();
                for (int i = 0; i < num_points; i++)
                {
                    matches.Add(new DMatch(i, i, 0f));
                }

                //按照匹配关系将图画出来,背景图为match_img
                Mat match_img = new Mat();
                Cv2.DrawMatches(image, points_A, image2, points_B, matches, match_img);

                pictureBox2.Image = new System.Drawing.Bitmap(match_img.ToMemoryStream());
                textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";

            }

            private void pictureBox2_DoubleClick(object sender, EventArgs e)
            {
                Common.ShowNormalImg(pictureBox2.Image);
            }

            private void button3_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = fileFilter;
                if (ofd.ShowDialog() != DialogResult.OK) return;

                pictureBox3.Image = null;
                pictureBox2.Image = null;
                textBox1.Text = "";

                image_path2 = ofd.FileName;
                pictureBox3.Image = new System.Drawing.Bitmap(image_path2);
                image2 = new Mat(image_path2);
            }

            private void pictureBox3_DoubleClick(object sender, EventArgs e)
            {
                Common.ShowNormalImg(pictureBox3.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. using System.Numerics;
    11. namespace Onnx_Demo
    12. {
    13. public partial class frmMain : Form
    14. {
    15. public frmMain()
    16. {
    17. InitializeComponent();
    18. }
    19. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    20. string image_path = "";
    21. string image_path2 = "";
    22. DateTime dt1 = DateTime.Now;
    23. DateTime dt2 = DateTime.Now;
    24. int inpWidth;
    25. int inpHeight;
    26. float[] mean =new float[] { 0.485f, 0.456f, 0.406f };
    27. float[] std = new float[] { 0.229f, 0.224f, 0.225f };
    28. Mat image;
    29. Mat image2;
    30. string model_path = "";
    31. SessionOptions options;
    32. InferenceSession onnx_session;
    33. Tensor<float> input_tensor;
    34. Tensor<float> mask_tensor;
    35. List<NamedOnnxValue> input_ontainer;
    36. IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
    37. DisposableNamedOnnxValue[] results_onnxvalue;
    38. private void button1_Click(object sender, EventArgs e)
    39. {
    40. OpenFileDialog ofd = new OpenFileDialog();
    41. ofd.Filter = fileFilter;
    42. if (ofd.ShowDialog() != DialogResult.OK) return;
    43. pictureBox1.Image = null;
    44. pictureBox2.Image = null;
    45. textBox1.Text = "";
    46. image_path = ofd.FileName;
    47. pictureBox1.Image = new System.Drawing.Bitmap(image_path);
    48. image = new Mat(image_path);
    49. }
    50. private void Form1_Load(object sender, EventArgs e)
    51. {
    52. // 创建输入容器
    53. input_ontainer = new List<NamedOnnxValue>();
    54. // 创建输出会话
    55. options = new SessionOptions();
    56. options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
    57. options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行
    58. // 创建推理模型类,读取本地模型文件
    59. model_path = "model/dedode_end2end_1024.onnx";
    60. inpHeight = 256;
    61. inpWidth = 256;
    62. onnx_session = new InferenceSession(model_path, options);
    63. // 创建输入容器
    64. input_ontainer = new List<NamedOnnxValue>();
    65. image_path = "test_img/im_A.jpg";
    66. pictureBox1.Image = new Bitmap(image_path);
    67. image_path2 = "test_img/im_B.jpg";
    68. pictureBox3.Image = new Bitmap(image_path2);
    69. }
    70. private unsafe void button2_Click(object sender, EventArgs e)
    71. {
    72. if (image_path == "")
    73. {
    74. return;
    75. }
    76. textBox1.Text = "检测中,请稍等……";
    77. pictureBox2.Image = null;
    78. System.Windows.Forms.Application.DoEvents();
    79. image = new Mat(image_path);
    80. image2 = new Mat(image_path2);
    81. float[] input_tensor_data = new float[2 * 3 * inpWidth * inpHeight];
    82. //preprocess
    83. Mat dstimg = new Mat();
    84. Cv2.CvtColor(image, dstimg, ColorConversionCodes.BGR2RGB);
    85. Cv2.Resize(dstimg, dstimg, new OpenCvSharp.Size(inpWidth, inpHeight));
    86. for (int c = 0; c < 3; c++)
    87. {
    88. for (int i = 0; i < inpHeight; i++)
    89. {
    90. for (int j = 0; j < inpWidth; j++)
    91. {
    92. float pix = ((byte*)(dstimg.Ptr(i).ToPointer()))[j * 3 + c];
    93. input_tensor_data[c * inpWidth * inpHeight + i * inpWidth + j] = (float)((pix / 255.0 - mean[c]) / std[c]);
    94. }
    95. }
    96. }
    97. Cv2.CvtColor(image2, dstimg, ColorConversionCodes.BGR2RGB);
    98. Cv2.Resize(dstimg, dstimg, new OpenCvSharp.Size(inpWidth, inpHeight));
    99. for (int c = 0; c < 3; c++)
    100. {
    101. for (int i = 0; i < inpHeight; i++)
    102. {
    103. for (int j = 0; j < inpWidth; j++)
    104. {
    105. float pix = ((byte*)(dstimg.Ptr(i).ToPointer()))[j * 3 + c];
    106. input_tensor_data[(3+c )* inpWidth * inpHeight + i * inpWidth + j] = (float)((pix / 255.0 - mean[c]) / std[c]);
    107. }
    108. }
    109. }
    110. input_tensor = new DenseTensor<float>(input_tensor_data, new[] { 2, 3, inpHeight, inpWidth });
    111. //input_tensor 放入一个输入参数的容器,并指定名称
    112. input_ontainer.Add(NamedOnnxValue.CreateFromTensor("images", input_tensor));
    113. dt1 = DateTime.Now;
    114. //运行 Inference 并获取结果
    115. result_infer = onnx_session.Run(input_ontainer);
    116. dt2 = DateTime.Now;
    117. //Postprocessing
    118. //将输出结果转为DisposableNamedOnnxValue数组
    119. results_onnxvalue = result_infer.ToArray();
    120. float[] matches_A = results_onnxvalue[0].AsTensor<float>().ToArray();
    121. float[] matches_B = results_onnxvalue[1].AsTensor<float>().ToArray();
    122. int num_points = results_onnxvalue[0].AsTensor<float>().Dimensions[0];
    123. List<KeyPoint> points_A = new List<KeyPoint>();
    124. List<KeyPoint> points_B = new List<KeyPoint>();
    125. KeyPoint temp;
    126. for (int i = 0; i < num_points; i++)
    127. {
    128. temp = new KeyPoint();
    129. temp.Pt.X = (float)((matches_A[i * 2] + 1) * 0.5 * image.Cols);
    130. temp.Pt.Y = (float)((matches_A[i * 2 + 1] + 1) * 0.5 * image.Rows);
    131. temp.Size = 1f;
    132. points_A.Add(temp);
    133. }
    134. num_points = results_onnxvalue[1].AsTensor<float>().Dimensions[0];
    135. for (int i = 0; i < num_points; i++)
    136. {
    137. temp = new KeyPoint();
    138. temp.Pt.X = (float)((matches_B[i * 2] + 1) * 0.5 * image2.Cols);
    139. temp.Pt.Y = (float)((matches_B[i * 2 + 1] + 1) * 0.5 * image2.Rows);
    140. temp.Size = 1f;
    141. points_B.Add(temp);
    142. }
    143. //匹配结果放在matches里面
    144. num_points = points_A.Count();
    145. List<DMatch> matches=new List<DMatch>();
    146. for (int i = 0; i < num_points; i++)
    147. {
    148. matches.Add(new DMatch(i, i, 0f));
    149. }
    150. //按照匹配关系将图画出来,背景图为match_img
    151. Mat match_img = new Mat();
    152. Cv2.DrawMatches(image, points_A, image2, points_B, matches, match_img);
    153. pictureBox2.Image = new System.Drawing.Bitmap(match_img.ToMemoryStream());
    154. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
    155. }
    156. private void pictureBox2_DoubleClick(object sender, EventArgs e)
    157. {
    158. Common.ShowNormalImg(pictureBox2.Image);
    159. }
    160. private void button3_Click(object sender, EventArgs e)
    161. {
    162. OpenFileDialog ofd = new OpenFileDialog();
    163. ofd.Filter = fileFilter;
    164. if (ofd.ShowDialog() != DialogResult.OK) return;
    165. pictureBox3.Image = null;
    166. pictureBox2.Image = null;
    167. textBox1.Text = "";
    168. image_path2 = ofd.FileName;
    169. pictureBox3.Image = new System.Drawing.Bitmap(image_path2);
    170. image2 = new Mat(image_path2);
    171. }
    172. private void pictureBox3_DoubleClick(object sender, EventArgs e)
    173. {
    174. Common.ShowNormalImg(pictureBox3.Image);
    175. }
    176. private void pictureBox1_DoubleClick(object sender, EventArgs e)
    177. {
    178. Common.ShowNormalImg(pictureBox1.Image);
    179. }
    180. }
    181. }

    下载 

    源码下载

  • 相关阅读:
    next.js app目录 i18n国际化简单实现
    windows自启动,修改注册表的方式
    内存管理——C++
    c# - - - CentOS 7 部署ASP.Net Core项目
    高考志愿填报选专业,解读“冲稳保”三步策略
    阿里云->宝塔配置
    spring中的ApplicationEventMulticaster
    wangEditor5在vue中的基本使用
    【智能优化算法-鲸鱼算法】基于鲸鱼算法求解多目标优化问题附matlab代码(NSWOA)
    maven报orace,jdbc错误,并出现大量红色波浪线Unresolved dependency
  • 原文地址:https://blog.csdn.net/lw112190/article/details/134547669