YoloDotNet

YoloDotNet

基于C#的Yolov8和Yolov10实时目标检测库

YoloDotNet是基于.NET 8的C#库,支持Yolov8和Yolov10模型进行实时目标检测。该库集成ML.NET和ONNX运行时,并支持CUDA GPU加速,提供分类、目标检测、OBB检测、分割和姿态估计等功能。YoloDotNet在CPU和GPU上均可高效运行,适用于各种计算机视觉应用场景。

YoloDotNet对象检测深度学习图像处理性能优化Github开源项目

<img src="https://github.com/NickSwardh/YoloDotNet/assets/35733515/994287a9-556c-495f-8acf-1acae8d64ac0" height=24> YoloDotNet v2.0

YoloDotNet is a C# .NET 8 implementation of Yolov8 & Yolov10 for real-time detection of objects in images and videos using ML.NET and ONNX runtime, with GPU acceleration using CUDA.

YoloDotNet supports the following:

  ✓   Classification   Categorize an image
  ✓   Object Detection   Detect multiple objects in a single image
  ✓   OBB Detection   OBB (Oriented Bounding Box)
  ✓   Segmentation   Separate detected objects using pixel masks
  ✓   Pose Estimation   Identifying location of specific keypoints in an image

Batteries not included ;)

ClassificationObject DetectionOBB DetectionSegmentationPose Estimation
<img src="https://user-images.githubusercontent.com/35733515/297393507-c8539bff-0a71-48be-b316-f2611c3836a3.jpg" width=300><img src="https://user-images.githubusercontent.com/35733515/273405301-626b3c97-fdc6-47b8-bfaf-c3a7701721da.jpg" width=300><img src="https://github.com/NickSwardh/YoloDotNet/assets/35733515/d15c5b3e-18c7-4c2c-9a8d-1d03fb98dd3c" width=300><img src="https://github.com/NickSwardh/YoloDotNet/assets/35733515/3ae97613-46f7-46de-8c5d-e9240f1078e6" width=300><img src="https://github.com/NickSwardh/YoloDotNet/assets/35733515/b7abeaed-5c00-4462-bd19-c2b77fe86260" width=300>
<sub>image from pexels.com</sub><sub>image from pexels.com</sub><sub>image from pexels.com</sub><sub>image from pexels.com</sub><sub>image from pexels.com</sub>

What's new in YoloDotNet v2.0?

YoloDotNet 2.0 is a Speed Demon release where the main focus has been on supercharging performance to bring you the fastest and most efficient version yet. With major code optimizations, a switch to SkiaSharp for lightning-fast image processing, and added support for Yolov10 as a little extra ;) this release is set to redefine your YoloDotNet experience:

  • Speed Demon Mode: YoloDotNet is now faster than ever!
  • Code Overhaul: Tinkered and tweaked under the hood for blazing-fast execution.
  • Swapped Image Libraries: Out with ImageSharp, in with SkiaSharp. The result? Crazy fast image processing!
  • Memory Efficiency: Brutally more memory efficient, making the most of your system's resources.
  • Optimized GC Performance Greatly reduced GC pressure resulting in a sweet performance boost (thanks to louislewis2).
  • Benchmarking Benchmarking project added for testing and evaluating performance (thanks to louislewis2).
  • Yolov10 Support: Now featuring support for Yolov10 object detection. Because why not have the latest and greatest? ;)

Performance Analysis

[!NOTE] YoloDotNet v2.0 Performance Analysis

Processor: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
Ram: 16GB
Graphics: NVIDIA GeForce RTX 3060 12GB
OS: Windows 10

Performance was tested using the Yolov8s models in onnx format and test-images provided in the YoloDotNet project.

Taskv1.7 Mean (ms)v2.0 Mean (ms)Improvement (ms)Improvement (%)
ClassificationCpu12.7305.7346.99654.95%
ClassificationGpu7.7082.2555.45370.73%
ObjectDetectionCpu147.487113.95433.53322.74%
ObjectDetectionGpu39.93513.75126.18465.56%
SegmentationCpu623.313178.411444.90271.37%
SegmentationGpu477.53937.857439.68292.07%
PoseEstimationCpu140.823116.55724.26617.23%
PoseEstimationGpu31.58812.58219.00660.16%
ObbDetectionCpu401.694346.19355.50113.82%
ObbDetectionGpu71.93527.59144.34461.62%

Nuget

> dotnet add package YoloDotNet

Install CUDA (optional)

YoloDotNet with GPU-acceleration requires CUDA and cuDNN.

:information_source: Before installing CUDA and cuDNN, make sure to verify the ONNX runtime's current compatibility with specific versions.

Export Yolov8 model to ONNX

All models must be Yolov8-models exported to ONNX format. How to export to ONNX format.

Verify your model

using YoloDotNet; // Instantiate a new Yolo object with your ONNX-model using var yolo = new Yolo(@"path\to\model.onnx"); Console.WriteLine(yolo.OnnxModel.ModelType); // Output modeltype...

Example - Image inference

using YoloDotNet; using YoloDotNet.Enums; using YoloDotNet.Models; using YoloDotNet.Extensions; using SkiaSharp; // Instantiate a new Yolo object using var yolo = new Yolo(new YoloOptions { OnnxModel = @"path\to\model.onnx", // Your Yolov8 or Yolov10 model in onnx format ModelType = ModelType.ObjectDetection, // Model type Cuda = false, // Use CPU or CUDA for GPU accelerated inference. Default = true GpuId = 0 // Select Gpu by id. Default = 0 PrimeGpu = false, // Pre-allocate GPU before first. Default = false }); // Load image using var image = SKImage.FromEncodedData(@"path\to\image.jpg"); // Run inference and get the results var results = yolo.RunObjectDetection(image, confidence: 0.25, iou: 0.7); // Draw results using var resultsImage = image.Draw(results); // Save to file resultsImage.Save(@"save\as\new_image.jpg", SKEncodedImageFormat.Jpeg, 80);

Example - Video inference

[!IMPORTANT] Processing video requires FFmpeg and FFProbe

  • Download FFMPEG
  • Add FFmpeg and ffprobe to the Path-variable in your Environment Variables
using YoloDotNet; using YoloDotNet.Enums; using YoloDotNet.Models; // Instantiate a new Yolo object using var yolo = new Yolo(new YoloOptions { OnnxModel = @"path\to\model.onnx", // Your Yolov8 or Yolov10 model in onnx format ModelType = ModelType.ObjectDetection, // Model type Cuda = false, // Use CPU or CUDA for GPU accelerated inference. Default = true GpuId = 0 // Select Gpu by id. Default = 0 PrimeGpu = false, // Pre-allocate GPU before first. Default = false }); // Set video options var options = new VideoOptions { VideoFile = @"path\to\video.mp4", OutputDir = @"path\to\output\dir", //GenerateVideo = true, //DrawLabels = true, //FPS = 30, //Width = 640, // Resize video... //Height = -2, // -2 automatically calculate dimensions to keep proportions //Quality = 28, //DrawConfidence = true, //KeepAudio = true, //KeepFrames = false, //DrawSegment = DrawSegment.Default, //PoseOptions = MyPoseMarkerConfiguration // Your own pose marker configuration... }; // Run inference on video var results = yolo.RunObjectDetection(options, 0.25, 0.7); // Do further processing with 'results'...

Custom KeyPoint configuration for Pose Estimation

Example on how to configure Keypoints for a Pose Estimation model

// Pass in a KeyPoint options parameter to the Draw() extension method. Ex: image.Draw(poseEstimationResults, poseOptions);

Access ONNX metadata and labels

The internal ONNX metadata such as input & output parameters, version, author, description, date along with the labels can be accessed via the yolo.OnnxModel property.

Example:

using var yolo = new Yolo(@"path\to\model.onnx"); // ONNX metadata and labels resides inside yolo.OnnxModel Console.WriteLine(yolo.OnnxModel);

Example:

// Instantiate a new object using var yolo = new Yolo(@"path\to\model.onnx"); // Display metadata foreach (var property in yolo.OnnxModel.GetType().GetProperties()) { var value = property.GetValue(yolo.OnnxModel); Console.WriteLine($"{property.Name,-20}{value!}"); if (property.Name == nameof(yolo.OnnxModel.CustomMetaData)) foreach (var data in (Dictionary<string, string>)value!) Console.WriteLine($"{"",-20}{data.Key,-20}{data.Value}"); } // Get ONNX labels var labels = yolo.OnnxModel.Labels; Console.WriteLine(); Console.WriteLine($"Labels ({labels.Length}):"); Console.WriteLine(new string('-', 58)); // Display for (var i = 0; i < labels.Length; i++) Console.WriteLine($"index: {i,-8} label: {labels[i].Name,20} color: {labels[i].Color}"); // Output: // ModelType ObjectDetection // InputName images // OutputName output0 // CustomMetaData System.Collections.Generic.Dictionary`2[System.String,System.String] // date 2023-11-07T13:33:33.565196 // description Ultralytics YOLOv8n model trained on coco.yaml // author Ultralytics // task detect // license AGPL-3.0 https://ultralytics.com/license // version 8.0.202 // stride 32 // batch 1 // imgsz [640, 640] // names {0: 'person', 1: 'bicycle', 2: 'car' ... } // ImageSize Size [ Width=640, Height=640 ] // Input Input { BatchSize = 1, Channels = 3, Width = 640, Height = 640 } // Output ObjectDetectionShape { BatchSize = 1, Elements = 84, Channels = 8400 } // Labels YoloDotNet.Models.LabelModel[] // // Labels (80): // --------------------------------------------------------- // index: 0 label: person color: #5d8aa8 // index: 1 label: bicycle color: #f0f8ff // index: 2 label: car color: #e32636 // index: 3 label: motorcycle color: #efdecd // ...

Donate

https://paypal.me/nickswardh

References & Acknowledgements

https://github.com/ultralytics/ultralytics

https://github.com/sstainba/Yolov8.Net

https://github.com/mentalstack/yolov5-net

Benchmarks

There are some benchmarks included in the project. To run them, you simply need to build the project and run the YoloDotNet.Benchmarks project. The solution must be set to Release mode to run the benchmarks.

There is a if DEBUG section in the benchmark project that will run the benchmarks in Debug mode, but it is not recommended as it will not give accurate results. This is however useful to debug and step through the code. Two examples have been left in place to show how to run the benchmarks in Debug mode, but have been commented out.

Because there is no persistant storage for benchmark results, the results below are in the form of starting point and ending point. If one makes changes to the benchmarks, you would move the ending point to the starting point and run the benchmarks again to see the improvements and those values would be the new ending point.

Benchmark results would be very much based on the hardware used. It is important to try run benchmarks on the same hardware for future comparisons. If different hardware is used, it is important to note the hardware used, as the results would be different, thus the starting point and ending point would need to be updated. Hopefully in future a single hardware configuration can be used for benchmarks, before updating documentation.

Simple Benchmarks

Simple benchmarks were modeled around the test project. The test project uses the same images and models as the benchmarks. The benchmarks are run on the same images and models as the test project. These benchmarks provide a good starting point to identify bottlenecks and areas for improvement.

The hardware these benchmarks used are detailed below, the graphics card used was a NVIDIA GeForce RTX 3060 12GB.

* Summary *

BenchmarkDotNet v0.13.12, Windows 10 (10.0.19045.4529/22H2/2022Update)
Intel Core i7-7700K CPU 4.20GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK 8.0.302
[Host] : .NET 8.0.6 (8.0.624.26715), X64 RyuJIT AVX2
DefaultJob : .NET 8.0.6 (8.0.624.26715), X64 RyuJIT AVX2

Starting Point, YoloDotNet v7.1

MethodMeanErrorStdDevGen0Gen1Gen2Allocated
ClassificationCpu12.730 ms0.2525 ms0.2593 ms1546.8750125.000093.75006.4 MB
ClassificationGpu7.708 ms0.1509 ms0.2796 ms1546.8750125.000093.75006.4 MB
ObjectDetectionCpu147.487 ms2.6940 ms2.6459 ms18666.6667333.3333333.333377.97 MB
ObjectDetectionGpu39.935 ms0.2201 ms0.2059 ms18846.1538

编辑推荐精选

TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多