当前位置: 首页 > news >正文

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<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)
        {
            // 创建输入容器
            input_ontainer = new List<NamedOnnxValue>();

            // 创建输出会话
            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<NamedOnnxValue>();

            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<float>(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<float>().ToArray();
            float[] matches_B = results_onnxvalue[1].AsTensor<float>().ToArray();
            int num_points = results_onnxvalue[0].AsTensor<float>().Dimensions[0];

            List<KeyPoint> points_A = new List<KeyPoint>();
            List<KeyPoint> points_B = new List<KeyPoint>();

            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<float>().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<DMatch> matches=new List<DMatch>();
            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);
        }
    }
}

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<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)
        {
            // 创建输入容器
            input_ontainer = new List<NamedOnnxValue>();

            // 创建输出会话
            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<NamedOnnxValue>();

            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<float>(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<float>().ToArray();
            float[] matches_B = results_onnxvalue[1].AsTensor<float>().ToArray();
            int num_points = results_onnxvalue[0].AsTensor<float>().Dimensions[0];

            List<KeyPoint> points_A = new List<KeyPoint>();
            List<KeyPoint> points_B = new List<KeyPoint>();

            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<float>().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<DMatch> matches=new List<DMatch>();
            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);
        }
    }
}

下载 

源码下载

相关文章:

  • 2022最新版-李宏毅机器学习深度学习课程-P49 GPT的野望
  • SpringBoot学习笔记-实现微服务:匹配系统(上)
  • 家电回收小程序,省心省力的好帮手
  • MethodArgumentNotValidException 与 ConstraintViolationException
  • 【Element】el-progress 自定义进度条
  • 【C++】类与对象(中)
  • Javaweb实现数据库简单的增删改查
  • IOS输入框聚焦会把内容区域顶起
  • 数据库管理变更工具
  • frp内网穿透配置以及相关端口、过程解释
  • API之 要求接口上传pdf 以 合同PDF的二进制数据,multpart方式上传
  • Pickcode:教孩子们编码的新视觉语言
  • 【机器学习】Nonlinear Independent Component Analysis - Aapo Hyvärinen
  • c# 基础语法
  • 注解案例:山寨Junit与山寨JPA
  • 合肥中科深谷嵌入式项目实战——基于ARM语音识别的智能家居系统(三)
  • 【11月比赛合集】48场可报名的数据挖掘大奖赛,任君挑选!
  • 第二十章 多线程
  • java面试-zookeeper
  • 【Linux】-进程间通信-共享内存(SystemV),详解接口函数以及原理(使用管道处理同步互斥机制)
  • 台行政机构网站删除“汉人”改为“其余人口”,国台办回应
  • 人民日报访巴西总统卢拉:“巴中关系正处于历史最好时期”
  • 张笑宇:物质极大丰富之后,我们该怎么办?
  • 中国潜水救捞行业协会发布《呵护潜水员职业健康安全宣言》
  • 水豚“豆包”出逃已40天,扬州茱萸湾景区追加悬赏
  • 中国科考船在钓鱼岛附近活动,外交部:完全是中国主权权利范围内的事