当前位置:首页 » 《资源分享》 » 正文

C# OnnxRuntime yolo11 pest detection

13 人参与  2024年11月02日 10:41  分类 : 《资源分享》  评论

点击全文阅读


 

目录

介绍

效果

模型信息

项目

代码

下载


介绍

官网地址:GitHub - ultralytics/ultralytics: Ultralytics YOLO11 ?

YoloV11(You Only Look Once V11)是一种计算机视觉算法,用于目标检测。它是由 Joseph Redmon 提出的卷积神经网(CNN)模型,是Yolo系列算法中的最新版本。

YoloV11相对于前几个版本来说,主要有以下几点改进:1)更轻量级;2)更加准确;3)更快的推理速度。

YoloV11相对于其他目标检测算法具有更轻量级的优势。这是因为它的核心架构检测网络使用了更多的轻量级卷积和池化层,从而将其大小缩小到仅有20MB大小。这使得它在处理较小的图像上具有显著的优势,也使得它更易于部署到基于移动设备和嵌入式设备的应用中。YoloV11在准确性方面也有了显著的提升。这是因为它引入了更多的改进和优化来提高检测性能。例如,它使用了更多的卷积层来提取更丰富的特征信息,同时还使用了更多的跳跃连接来让高层特征信息能够被直接传递到低层特征信息中,从而在物体大小和位置检测中提高了准确性。YoloV11具有更高的推理速度。这是因为它采用了一些优秀的加速技术,如SPPnet和SAM,以提高网络的能力和速度。此外,通过与其他检测算法比较,我们可以看到,YoloV11在GPU测试中的运行速度相对较快,可以在实时检测中实现准确性和速度的平衡。

YoloV11是一种新型的轻量级、高精度、高速度的目标检测算法,具有广泛的应用前景,尤其是在基于嵌入式和移动设备的应用中具有巨大的潜力。

效果

模型信息

Model Properties
-------------------------
date:2024-10-22T08:46:38.254437
author:Ultralytics
version:8.3.5
task:detect
license:AGPL-3.0 License (https://ultralytics.com/license)
docs:https://docs.ultralytics.com
stride:32
batch:1
imgsz:[736, 736]
names:{0: 'rice leaf roller', 1: 'rice leaf caterpillar', 2: 'paddy stem maggot', 3: 'asiatic rice borer', 4: 'yellow rice borer', 5: 'rice gall midge', 6: 'Rice Stemfly', 7: 'brown plant hopper', 8: 'white backed plant hopper', 9: 'small brown plant hopper', 10: 'rice water weevil', 11: 'rice leafhopper', 12: 'grain spreader thrips', 13: 'rice shell pest', 14: 'grub', 15: 'mole cricket', 16: 'wireworm', 17: 'white margined moth', 18: 'black cutworm', 19: 'large cutworm', 20: 'yellow cutworm', 21: 'red spider', 22: 'corn borer', 23: 'army worm', 24: 'aphids', 25: 'Potosiabre vitarsis', 26: 'peach borer', 27: 'english grain aphid', 28: 'green bug', 29: 'bird cherry-oataphid', 30: 'wheat blossom midge', 31: 'penthaleus major', 32: 'longlegged spider mite', 33: 'wheat phloeothrips', 34: 'wheat sawfly', 35: 'cerodonta denticornis', 36: 'beet fly', 37: 'flea beetle', 38: 'cabbage army worm', 39: 'beet army worm', 40: 'Beet spot flies', 41: 'meadow moth', 42: 'beet weevil', 43: 'sericaorient alismots chulsky', 44: 'alfalfa weevil', 45: 'flax budworm', 46: 'alfalfa plant bug', 47: 'tarnished plant bug', 48: 'Locustoidea', 49: 'lytta polita', 50: 'legume blister beetle', 51: 'blister beetle', 52: 'therioaphis maculata Buckton', 53: 'odontothrips loti', 54: 'Thrips', 55: 'alfalfa seed chalcid', 56: 'Pieris canidia', 57: 'Apolygus lucorum', 58: 'Limacodidae', 59: 'Viteus vitifoliae', 60: 'Colomerus vitis', 61: 'Brevipoalpus lewisi McGregor', 62: 'oides decempunctata', 63: 'Polyphagotars onemus latus', 64: 'Pseudococcus comstocki Kuwana', 65: 'parathrene regalis', 66: 'Ampelophaga', 67: 'Lycorma delicatula', 68: 'Xylotrechus', 69: 'Cicadella viridis', 70: 'Miridae', 71: 'Trialeurodes vaporariorum', 72: 'Erythroneura apicalis', 73: 'Papilio xuthus', 74: 'Panonchus citri McGregor', 75: 'Phyllocoptes oleiverus ashmead', 76: 'Icerya purchasi Maskell', 77: 'Unaspis yanonensis', 78: 'Ceroplastes rubens', 79: 'Chrysomphalus aonidum', 80: 'Parlatoria zizyphus Lucus', 81: 'Nipaecoccus vastalor', 82: 'Aleurocanthus spiniferus', 83: 'Tetradacus c Bactrocera minax ', 84: 'Dacus dorsalis(Hendel)', 85: 'Bactrocera tsuneonis', 86: 'Prodenia litura', 87: 'Adristyrannus', 88: 'Phyllocnistis citrella Stainton', 89: 'Toxoptera citricidus', 90: 'Toxoptera aurantii', 91: 'Aphis citricola Vander Goot', 92: 'Scirtothrips dorsalis Hood', 93: 'Dasineura sp', 94: 'Lawana imitata Melichar', 95: 'Salurnis marginella Guerr', 96: 'Deporaus marginatus Pascoe', 97: 'Chlumetia transversa', 98: 'Mango flat beak leafhopper', 99: 'Rhytidodera bowrinii white', 100: 'Sternochetus frigidus', 101: 'Cicadellidae'}
---------------------------------------------------------------

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

Outputs
-------------------------
name:output0
tensor:Float[1, 106, 11109]
---------------------------------------------------------------

项目

代码

using Microsoft.ML.OnnxRuntime;using Microsoft.ML.OnnxRuntime.Tensors;using OpenCvSharp;using OpenCvSharp.Dnn;using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Imaging;using System.IO;using System.Linq;using System.Text;using System.Windows.Forms;namespace Onnx_Demo{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";        string image_path = "";        string model_path;        string classer_path;        public string[] class_names;        public int class_num;        DateTime dt1 = DateTime.Now;        DateTime dt2 = DateTime.Now;        int input_height;        int input_width;        float ratio_height;        float ratio_width;        InferenceSession onnx_session;        int box_num;        float conf_threshold;        float nms_threshold;        /// <summary>        /// 选择图片        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button1_Click(object sender, EventArgs e)        {            OpenFileDialog ofd = new OpenFileDialog();            ofd.Filter = fileFilter;            if (ofd.ShowDialog() != DialogResult.OK) return;            pictureBox1.Image = null;            image_path = ofd.FileName;            pictureBox1.Image = new Bitmap(image_path);            textBox1.Text = "";            pictureBox2.Image = null;        }        /// <summary>        /// 推理        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button2_Click(object sender, EventArgs e)        {            if (image_path == "")            {                return;            }            button2.Enabled = false;            pictureBox2.Image = null;            textBox1.Text = "";            Application.DoEvents();            Mat image = new Mat(image_path);            //图片缩放            int height = image.Rows;            int width = image.Cols;            Mat temp_image = image.Clone();            if (height > input_height || width > input_width)            {                float scale = Math.Min((float)input_height / height, (float)input_width / width);                OpenCvSharp.Size new_size = new OpenCvSharp.Size((int)(width * scale), (int)(height * scale));                Cv2.Resize(image, temp_image, new_size);            }            ratio_height = (float)height / temp_image.Rows;            ratio_width = (float)width / temp_image.Cols;            Mat input_img = new Mat();            Cv2.CopyMakeBorder(temp_image, input_img, 0, input_height - temp_image.Rows, 0, input_width - temp_image.Cols, BorderTypes.Constant, 0);            //Cv2.ImShow("input_img", input_img);            //输入Tensor            Tensor<float> input_tensor = new DenseTensor<float>(new[] { 1, 3, 736, 736 });            for (int y = 0; y < input_img.Height; y++)            {                for (int x = 0; x < input_img.Width; x++)                {                    input_tensor[0, 0, y, x] = input_img.At<Vec3b>(y, x)[0] / 255f;                    input_tensor[0, 1, y, x] = input_img.At<Vec3b>(y, x)[1] / 255f;                    input_tensor[0, 2, y, x] = input_img.At<Vec3b>(y, x)[2] / 255f;                }            }            List<NamedOnnxValue> input_container = new List<NamedOnnxValue>            {                NamedOnnxValue.CreateFromTensor("images", input_tensor)            };            //推理            dt1 = DateTime.Now;            var ort_outputs = onnx_session.Run(input_container).ToArray();            dt2 = DateTime.Now;            float[] data = Transpose(ort_outputs[0].AsTensor<float>().ToArray(), 4 + class_num, box_num);            float[] confidenceInfo = new float[class_num];            float[] rectData = new float[4];            List<DetectionResult> detResults = new List<DetectionResult>();            for (int i = 0; i < box_num; i++)            {                Array.Copy(data, i * (class_num + 4), rectData, 0, 4);                Array.Copy(data, i * (class_num + 4) + 4, confidenceInfo, 0, class_num);                float score = confidenceInfo.Max(); // 获取最大值                int maxIndex = Array.IndexOf(confidenceInfo, score); // 获取最大值的位置                int _centerX = (int)(rectData[0] * ratio_width);                int _centerY = (int)(rectData[1] * ratio_height);                int _width = (int)(rectData[2] * ratio_width);                int _height = (int)(rectData[3] * ratio_height);                detResults.Add(new DetectionResult(                   maxIndex,                   class_names[maxIndex],                   new Rect(_centerX - _width / 2, _centerY - _height / 2, _width, _height),                   score));            }            //NMS            CvDnn.NMSBoxes(detResults.Select(x => x.Rect), detResults.Select(x => x.Confidence), conf_threshold, nms_threshold, out int[] indices);            detResults = detResults.Where((x, index) => indices.Contains(index)).ToList();            //绘制结果            Mat result_image = image.Clone();            foreach (DetectionResult r in detResults)            {                Cv2.PutText(result_image, $"{r.Class}:{r.Confidence:P0}", new OpenCvSharp.Point(r.Rect.TopLeft.X, r.Rect.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);                Cv2.Rectangle(result_image, r.Rect, Scalar.Red, thickness: 2);            }            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());            textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";            button2.Enabled = true;        }        /// <summary>        ///窗体加载        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void Form1_Load(object sender, EventArgs e)        {            model_path = "model/yolo11-pest-detection.onnx";            //创建输出会话,用于输出模型读取信息            SessionOptions options = new SessionOptions();            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;            options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行            // 创建推理模型类,读取模型文件            onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径            input_height = 736;            input_width = 736;            box_num = 11109;            conf_threshold = 0.25f;            nms_threshold = 0.5f;            classer_path = "model/lable.txt";            class_names = File.ReadAllLines(classer_path, Encoding.UTF8);            class_num = class_names.Length;            image_path = "test_img/1.jpg";            pictureBox1.Image = new Bitmap(image_path);        }        /// <summary>        /// 保存        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button3_Click(object sender, EventArgs e)        {            if (pictureBox2.Image == null)            {                return;            }            Bitmap output = new Bitmap(pictureBox2.Image);            SaveFileDialog sdf = new SaveFileDialog();            sdf.Title = "保存";            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";            if (sdf.ShowDialog() == DialogResult.OK)            {                switch (sdf.FilterIndex)                {                    case 1:                        {                            output.Save(sdf.FileName, ImageFormat.Jpeg);                            break;                        }                    case 2:                        {                            output.Save(sdf.FileName, ImageFormat.Png);                            break;                        }                    case 3:                        {                            output.Save(sdf.FileName, ImageFormat.Bmp);                            break;                        }                    case 4:                        {                            output.Save(sdf.FileName, ImageFormat.Emf);                            break;                        }                    case 5:                        {                            output.Save(sdf.FileName, ImageFormat.Exif);                            break;                        }                    case 6:                        {                            output.Save(sdf.FileName, ImageFormat.Gif);                            break;                        }                    case 7:                        {                            output.Save(sdf.FileName, ImageFormat.Icon);                            break;                        }                    case 8:                        {                            output.Save(sdf.FileName, ImageFormat.Tiff);                            break;                        }                    case 9:                        {                            output.Save(sdf.FileName, ImageFormat.Wmf);                            break;                        }                }                MessageBox.Show("保存成功,位置:" + sdf.FileName);            }        }        private void pictureBox1_DoubleClick(object sender, EventArgs e)        {            ShowNormalImg(pictureBox1.Image);        }        private void pictureBox2_DoubleClick(object sender, EventArgs e)        {            ShowNormalImg(pictureBox2.Image);        }        public void ShowNormalImg(Image img)        {            if (img == null) return;            frmShow frm = new frmShow();            frm.Width = Screen.PrimaryScreen.Bounds.Width;            frm.Height = Screen.PrimaryScreen.Bounds.Height;            if (frm.Width > img.Width)            {                frm.Width = img.Width;            }            if (frm.Height > img.Height)            {                frm.Height = img.Height;            }            bool b = frm.richTextBox1.ReadOnly;            Clipboard.SetDataObject(img, true);            frm.richTextBox1.ReadOnly = false;            frm.richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Bitmap));            frm.richTextBox1.ReadOnly = b;            frm.ShowDialog();        }        public unsafe float[] Transpose(float[] tensorData, int rows, int cols)        {            float[] transposedTensorData = new float[tensorData.Length];            fixed (float* pTensorData = tensorData)            {                fixed (float* pTransposedData = transposedTensorData)                {                    for (int i = 0; i < rows; i++)                    {                        for (int j = 0; j < cols; j++)                        {                            int index = i * cols + j;                            int transposedIndex = j * rows + i;                            pTransposedData[transposedIndex] = pTensorData[index];                        }                    }                }            }            return transposedTensorData;        }    }    public class DetectionResult    {        public DetectionResult(int ClassId, string Class, Rect Rect, float Confidence)        {            this.ClassId = ClassId;            this.Confidence = Confidence;            this.Rect = Rect;            this.Class = Class;        }        public string Class { get; set; }        public int ClassId { get; set; }        public float Confidence { get; set; }        public Rect Rect { get; set; }    }}

下载

源码下载


点击全文阅读


本文链接:http://zhangshiyu.com/post/181331.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1