做网站排行,fatal error: class wordpress,建一个展示的网站要多少钱,广州网站建设系统开发文章目录 1. 前言1.1 OpenVINO™ C# API1.2 YOLOv10 2. 模型获取2.1 源码下载2.2 配置环境2.3 下载模型 3. Yolov10 项目配置3.1 项目创建与环境配置3.2 定义模型预测方法3.2.1 定义目标检测模型方法3.2.2 使用OpenVINO™ 预处理接口编译模型 3.2 模型预测方法调用 4. 项目运行… 文章目录 1. 前言1.1 OpenVINO™ C# API1.2 YOLOv10 2. 模型获取2.1 源码下载2.2 配置环境2.3 下载模型 3. Yolov10 项目配置3.1 项目创建与环境配置3.2 定义模型预测方法3.2.1 定义目标检测模型方法3.2.2 使用OpenVINO™ 预处理接口编译模型 3.2 模型预测方法调用 4. 项目运行与演示4.1 项目编译和运行4.2 YOLOv10 目标检测模型运行结果 5. 总结 最近YOLO家族又添新成员YOLOv10YOLOv10 提出了一种一致的双任务方法用于无nms训练的YOLOs它同时带来了具有竞争力的性能和较低的推理延迟。此外还介绍了整体效率-精度驱动的模型设计策略,从效率和精度两个角度对YOLOs的各个组成部分进行了全面优化大大降低了计算开销增强了性能。在本文中我们将结合OpenVINO™ C# API 使用最新发布的OpenVINO™ 2024.1部署YOLOv10 目标检测模型 OpenVINO™ C# API项目链接 https://github.com/guojin-yan/OpenVINO-CSharp-API.git使用 OpenVINO™ C# API 部署 YOLOv10 全部源码 https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/blob/master/model_samples/yolov10/1. 前言
1.1 OpenVINO™ C# API 英特尔发行版 OpenVINO™ 工具套件基于 oneAPI 而开发可以加快高性能计算机视觉和深度学习视觉应用开发速度工具套件适用于从边缘到云的各种英特尔平台上帮助用户更快地将更准确的真实世界结果部署到生产系统中。通过简化的开发工作流程OpenVINO™ 可赋能开发者在现实世界中部署高性能应用程序和算法。 2024年4月25日英特尔发布了开源 OpenVINO™ 2024.1 工具包用于在各种硬件上优化和部署人工智能推理。更新了更多的 Gen AI 覆盖范围和框架集成以最大限度地减少代码更改。同时提供了更广泛的 LLM 模型支持和更多的模型压缩技术。通过压缩嵌入的额外优化减少了 LLM 编译时间改进了采用英特尔®高级矩阵扩展 Intel® AMX 的第 4 代和第 5 代英特尔®至强®处理器上 LLM 的第 1 令牌性能。通过对英特尔®锐炫™ GPU 的 oneDNN、INT4 和 INT8 支持实现更好的 LLM 压缩和改进的性能。最后实现了更高的可移植性和性能可在边缘、云端或本地运行 AI。 OpenVINO™ C# API 是一个 OpenVINO™ 的 .Net wrapper应用最新的 OpenVINO™ 库开发通过 OpenVINO™ C API 实现 .Net 对 OpenVINO™ Runtime 调用使用习惯与 OpenVINO™ C API 一致。OpenVINO™ C# API 由于是基于 OpenVINO™ 开发所支持的平台与 OpenVINO™ 完全一致具体信息可以参考 OpenVINO™。通过使用 OpenVINO™ C# API可以在 .NET、.NET Framework等框架下使用 C# 语言实现深度学习模型在指定平台推理加速。
1.2 YOLOv10 在过去的几年里由于在计算成本和检测性能之间取得了有效的平衡YOLOs已经成为实时目标检测领域的主导范式。然而对非最大抑制(NMS)的后处理依赖阻碍了yolo的端到端部署并对推理延迟产生不利影响。为了解决这些问题首先提出了一种一致的双任务方法用于无nms训练的YOLOs它同时带来了具有竞争力的性能和较低的推理延迟。此外我们还介绍了整体效率-精度驱动的模型设计策略。我们从效率和精度两个角度对YOLOs的各个组成部分进行了全面优化大大降低了计算开销增强了性能。我们的努力成果是用于实时端到端目标检测的新一代YOLO系列称为YOLOv10。大量的实验表明YOLOv10在各种模型尺度上都达到了最先进的性能和效率。例如我们的YOLOv10-S在COCO上类似的AP下比RT-DETR-R18快1.8倍同时参数数量和FLOPs减少2.8倍。与YOLOv9-C相比在相同性能下YOLOv10-B的延迟减少了46%参数减少了25%。 下图为YOLOv10官方提供的模型训练精度以及不同模型数据量可以看出YOLOv10与之前其他系列相比数据量在减少的同时精度依旧有所提升。 2. 模型获取
2.1 源码下载 YOLOv10 模型需要源码进行下载首先克隆GitHub上的源码输入以下指令
git clone https://github.com/THU-MIG/yolov10.git
cd yolov102.2 配置环境 接下来安装模型下载以及转换环境此处使用Anaconda进行程序集管理输入以下指令创建一个yolov10环境
conda create -n yolov10 python3.9
conda activate yolov10
pip install -r requirements.txt
pip install -e .然后安装OpenVINO™环境输入以下指令
pip install openvino2024.1.02.3 下载模型 首先导出目标识别模型此处以官方预训练模型为例首先下载预训练模型文件然后调用yolo导出ONBNX格式的模型文件最后使用 OpenVINO™ 的模型转换命令将模型转为IR格式依次输入以下指令即可
wget https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10s.pt
yolo export modelyolov10s.pt formatonnx opset13 simplify
ovc yolov10s.onnx模型的结构如下图所示 3. Yolov10 项目配置
3.1 项目创建与环境配置 在Windows平台开发者可以使用Visual Studio平台开发程序但无法跨平台实现为了实现跨平台此处采用dotnet指令进行项目的创建和配置。 首先使用dotnet创建一个测试项目在终端中输入一下指令
dotnet new console --framework net6.0 --use-program-main -o yolov10此处以Windows平台为例安装项目依赖首先是安装OpenVINO™ C# API项目依赖在命令行中输入以下指令即可
dotnet add package OpenVINO.CSharp.API
dotnet add package OpenVINO.runtime.win
dotnet add package OpenVINO.CSharp.API.Extensions
dotnet add package OpenVINO.CSharp.API.Extensions.OpenCvSharp关于在不同平台上搭建 OpenVINO™ C# API 开发环境请参考以下文章 《在Windows上搭建OpenVINO™C#开发环境》 、《在Linux上搭建OpenVINO™C#开发环境》、《在MacOS上搭建OpenVINO™C#开发环境》
接下来安装使用到的图像处理库 OpenCvSharp在命令行中输入以下指令即可
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.Extensions
dotnet add package OpenCvSharp4.runtime.win关于在其他平台上搭建 OpenCvSharp 开发环境请参考以下文章《【OpenCV】在Linux上使用OpenCvSharp》 、《【OpenCV】在MacOS上使用OpenCvSharp》
添加完成项目依赖后项目的配置文件如下所示
Project SdkMicrosoft.NET.SdkPropertyGroupOutputTypeExe/OutputTypeTargetFrameworknet6.0/TargetFrameworkImplicitUsingsenable/ImplicitUsingsNullableenable/Nullable/PropertyGroupItemGroupPackageReference IncludeOpenCvSharp4 Version4.9.0.20240103 /PackageReference IncludeOpenCvSharp4.Extensions Version4.9.0.20240103 /PackageReference IncludeOpenCvSharp4.runtime.win Version4.9.0.20240103 /PackageReference IncludeOpenVINO.CSharp.API Version2024.0.0.1 /PackageReference IncludeOpenVINO.CSharp.API.Extensions.OpenCvSharp Version1.0.4 /PackageReference IncludeOpenVINO.runtime.win Version2024.0.0.1 //ItemGroup/Project3.2 定义模型预测方法 使用 OpenVINO™ C# API 部署模型主要包括以下几个步骤
初始化 OpenVINO Runtime Core读取本地模型将图片数据预处理方式编译到模型将模型编译到指定设备创建推理通道处理图像输入数据设置推理输入数据模型推理获取推理结果处理结果数据
3.2.1 定义目标检测模型方法 按照 OpenVINO™ C# API 部署深度学习模型的步骤编写YOLOv10模型部署流程在之前的项目里我们已经部署了YOLOv5~9等一系列模型其部署流程是基本一致的YOLOv10模型部署代码如下所示
static void yolov10_det(string model_path, string image_path, string device)
{// -------- Step 1. Initialize OpenVINO Runtime Core --------Core core new Core();// -------- Step 2. Read inference model --------Model model core.read_model(model_path);OvExtensions.printf_model_info(model);// -------- Step 3. Loading a model to the device --------CompiledModel compiled_model core.compile_model(model, device);// -------- Step 4. Create an infer request --------InferRequest infer_request compiled_model.create_infer_request();// -------- Step 5. Process input images --------Mat image new Mat(image_path); // Read image by opencvsharpint max_image_length image.Cols image.Rows ? image.Cols : image.Rows;Mat max_image Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);Rect roi new Rect(0, 0, image.Cols, image.Rows);image.CopyTo(new Mat(max_image, roi));float factor (float)(max_image_length / 640.0);// -------- Step 6. Set up input data --------Tensor input_tensor infer_request.get_input_tensor();Shape input_shape input_tensor.get_shape();Mat input_mat CvDnn.BlobFromImage(max_image, 1.0 / 255.0, new OpenCvSharp.Size(input_shape[2], input_shape[3]), 0, true, false);float[] input_data new float[input_shape[1] * input_shape[2] * input_shape[3]];Marshal.Copy(input_mat.Ptr(0), input_data, 0, input_data.Length);input_tensor.set_datafloat(input_data);// -------- Step 7. Do inference synchronously --------infer_request.infer();// -------- Step 8. Get infer result data --------Tensor output_tensor infer_request.get_output_tensor();int output_length (int)output_tensor.get_size();float[] output_data output_tensor.get_datafloat(output_length);// -------- Step 9. Process reault --------ListRect position_boxes new ListRect();Listint class_ids new Listint();Listfloat confidences new Listfloat();// Preprocessing output resultsfor (int i 0; i output_data.Length / 6; i){int s 6 * i;if ((float)output_data[s 4] 0.5){float cx output_data[s 0];float cy output_data[s 1];float dx output_data[s 2];float dy output_data[s 3];int x (int)((cx) * factor);int y (int)((cy) * factor);int width (int)((dx - cx) * factor);int height (int)((dy - cy) * factor);Rect box new Rect();box.X x;box.Y y;box.Width width;box.Height height;position_boxes.Add(box);class_ids.Add((int)output_data[s 5]);confidences.Add((float)output_data[s 4]);}}for (int i 0; i class_ids.Count; i){int index i;Cv2.Rectangle(image, position_boxes[index], new Scalar(0, 0, 255), 2, LineTypes.Link8);Cv2.Rectangle(image, new OpenCvSharp.Point(position_boxes[index].TopLeft.X, position_boxes[index].TopLeft.Y 30),new OpenCvSharp.Point(position_boxes[index].BottomRight.X, position_boxes[index].TopLeft.Y), new Scalar(0, 255, 255), -1);Cv2.PutText(image, class_ids[index] - confidences[index].ToString(0.00),new OpenCvSharp.Point(position_boxes[index].X, position_boxes[index].Y 25),HersheyFonts.HersheySimplex, 0.8, new Scalar(0, 0, 0), 2);}string output_path Path.Combine(Path.GetDirectoryName(Path.GetFullPath(image_path)),Path.GetFileNameWithoutExtension(image_path) _result.jpg);Cv2.ImWrite(output_path, image);Slog.INFO(The result save to output_path);Cv2.ImShow(Result, image);Cv2.WaitKey(0);
}3.2.2 使用OpenVINO™ 预处理接口编译模型 OpenVINO™提供了推理数据预处理接口用户可以更具模型的输入数据预处理方式进行设置。在读取本地模型后调用数据预处理接口按照模型要求的数据预处理方式进行输入配置然后再将配置好的预处理接口与模型编译到一起这样便实现了将模型预处理与模型结合在一起实现OpenVINO对于处理过程的加速。主要是现在代码如下所示
static void yolov10_det_process(string model_path, string image_path, string device)
{// -------- Step 1. Initialize OpenVINO Runtime Core --------Core core new Core();// -------- Step 2. Read inference model --------Model model core.read_model(model_path);OvExtensions.printf_model_info(model);PrePostProcessor processor new PrePostProcessor(model);Tensor input_tensor_pro new Tensor(new OvType(ElementType.U8), new Shape(1, 640, 640, 3));InputInfo input_info processor.input(0);InputTensorInfo input_tensor_info input_info.tensor();input_tensor_info.set_from(input_tensor_pro).set_layout(new Layout(NHWC)).set_color_format(ColorFormat.BGR);PreProcessSteps process_steps input_info.preprocess();process_steps.convert_color(ColorFormat.RGB).resize(ResizeAlgorithm.RESIZE_LINEAR).convert_element_type(new OvType(ElementType.F32)).scale(255.0f).convert_layout(new Layout(NCHW));Model new_model processor.build();// -------- Step 3. Loading a model to the device --------CompiledModel compiled_model core.compile_model(new_model, device);// -------- Step 4. Create an infer request --------InferRequest infer_request compiled_model.create_infer_request();// -------- Step 5. Process input images --------Mat image new Mat(image_path); // Read image by opencvsharpint max_image_length image.Cols image.Rows ? image.Cols : image.Rows;Mat max_image Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);Rect roi new Rect(0, 0, image.Cols, image.Rows);image.CopyTo(new Mat(max_image, roi));Cv2.Resize(max_image, max_image, new OpenCvSharp.Size(640, 640));float factor (float)(max_image_length / 640.0);// -------- Step 6. Set up input data --------Tensor input_tensor infer_request.get_input_tensor();Shape input_shape input_tensor.get_shape();byte[] input_data new byte[input_shape[1] * input_shape[2] * input_shape[3]];//max_image.GetArrayint(out input_data);Marshal.Copy(max_image.Ptr(0), input_data, 0, input_data.Length);IntPtr destination input_tensor.data();Marshal.Copy(input_data, 0, destination, input_data.Length);// -------- Step 7. Do inference synchronously --------... ...(后续与上文代码一致)
}3.2 模型预测方法调用 定义完模型推理接口后便可以在主函数里进行调用。此处为了让大家更好的复现本文代码提供了在线模型用户只需要运行以下代码便可以直接下载转换好的模型进行模型推理无需再自行转换主函数代码如下所示
static void Main(string[] args)
{string model_path ;string image_path ;string device AUTO;if (args.Length 0){if (!Directory.Exists(./model)){Directory.CreateDirectory(./model);}if (!File.Exists(./model/yolov10s.bin) !File.Exists(./model/yolov10s.bin)){if (!File.Exists(./model/yolov10s.tar)){_ Download.download_file_async(https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/releases/download/Model/yolov10s.tar,./model/yolov10s.tar).Result;}Download.unzip(./model/yolov10s.tar, ./model/);}if (!File.Exists(./model/test_image.jpg)){_ Download.download_file_async(https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/releases/download/Image/test_det_02.jpg,./model/test_image.jpg).Result;}model_path ./model/yolov10s.xml;image_path ./model/test_image.jpg;}else if (args.Length 2){model_path args[0];image_path args[1];device args[2];}else{Console.WriteLine(Please enter the correct command parameters, for example:);Console.WriteLine( 1. dotnet run);Console.WriteLine( 2. dotnet run model path image path device name);}// -------- Get OpenVINO runtime version --------OpenVinoSharp.Version version Ov.get_openvino_version();Slog.INFO(---- OpenVINO INFO----);Slog.INFO(Description : version.description);Slog.INFO(Build number: version.buildNumber);Slog.INFO(Predict model files: model_path);Slog.INFO(Predict image files: image_path);Slog.INFO(Inference device: device);Slog.INFO(Start yolov8 model inference.);//yolov10_det(model_path, image_path, device);yolov10_det_process(model_path, image_path, device);
}代码提示 由于篇幅限制上文中只展示了部分代码想要获取全部源码请访问项目GitHub自行下载 使用OpenVINO™ C# API部署YOLOv10目标检测模型 https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/blob/master/model_samples/yolov10/yolov10_det_opencvsharp/Program.cs此外为了满足习惯使用EmguCV处理图像数据的开发者此处我们也提供了EmguCV版本代码、 使用OpenVINO™ C# API部署YOLOv10目标检测模型 https://github.com/guojin-yan/OpenVINO-CSharp-API-Samples/blob/master/model_samples/yolov10/yolov10_det_emgucv/Program.cs4. 项目运行与演示
4.1 项目编译和运行 接下来输入项目编译指令进行项目编译输入以下指令即可
dotnet build接下来运行编译后的程序文件在CMD中输入以下指令运行编译后的项目文件
dotnet run --no-build运行后项目输出为
4.2 YOLOv10 目标检测模型运行结果 下图为YOLOv10 目标检测模型运行输出信息此处我们使用在线转换好的模型进行推理。首先会下载指定模型以及推理数据到本地这样避免了开发者在自己配置环境和下载模型接下来是输出打印 OpenVINO™ 版本信息此处我们使用NuGet安装的依赖项已经是OpenVINO™ 2024.0最新版本接下来就是打印相关的模型信息并输出每个过程所消耗时间。 下图为使用YOLOv10 目标检测模型推理结果 5. 总结 在该项目中我们结合之前开发的 OpenVINO™ C# API 项目部署YOLOv10模型成功实现了对象目标检测与实例分割并且根据不同开发者的使用习惯同时提供了OpenCvSharp以及Emgu.CV两种版本供各位开发者使用。最后如果各位开发者在使用中有任何问题欢迎大家与我联系。