要在C语言中使用OpenVINO™,你需要遵循以下步骤:1. **安装OpenVINO库**:首先,你需要从Intel的官方网站下载并安装OpenVINO库。2. **配置环境变量**:安装完成后,你需要配置环境变量,以便在编译时能够找到OpenVINO的

摘要:最近YOLO家族又添新成员:YOLOv10,YOLOv10 提出了一种一致的双任务方法,用于无nms训练的YOLOs,它同时带来了具有竞争力的性能和较低的推理延迟。此外,还介绍了整体效率-精度驱动的模型设计策略,从效率和精度两个角度对YOL
最近YOLO家族又添新成员:YOLOv10,YOLOv10 提出了一种一致的双任务方法,用于无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# 语言实现深度学习模型在指定平台推理加速。   下表为当前发布的 OpenVINO™ C# API NuGet Package,支持多个目标平台,可以通过NuGet一键安装所有依赖。 Core Managed Libraries Package Description Link OpenVINO.CSharp.API OpenVINO C# API core libraries OpenVINO.CSharp.API.Extensions OpenVINO C# API core extensions libraries OpenVINO.CSharp.API.Extensions.OpenCvSharp OpenVINO C# API core extensions libraries use OpenCvSharp OpenVINO.CSharp.API.Extensions.EmguCV OpenVINO C# API core extensions libraries use EmguCV Native Runtime Libraries Package Description Link OpenVINO.runtime.win Native bindings for Windows OpenVINO.runtime.ubuntu.22-x86_64 Native bindings for ubuntu.22-x86_64 OpenVINO.runtime.ubuntu.20-x86_64 Native bindings for ubuntu.20-x86_64 OpenVINO.runtime.ubuntu.18-x86_64 Native bindings for ubuntu.18-x86_64 OpenVINO.runtime.debian9-arm64 Native bindings for debian9-arm64 OpenVINO.runtime.debian9-armhf Native bindings for debian9-armhf OpenVINO.runtime.centos7-x86_64 Native bindings for centos7-x86_64 OpenVINO.runtime.rhel8-x86_64 Native bindings for rhel8-x86_64 OpenVINO.runtime.macos-x86_64 Native bindings for macos-x86_64 OpenVINO.runtime.macos-arm64 Native bindings for macos-arm64 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 yolov10 2.2 配置环境   接下来安装模型下载以及转换环境,此处使用Anaconda进行程序集管理,输入以下指令创建一个yolov10环境: conda create -n yolov10 python=3.9 conda activate yolov10 pip install -r requirements.txt pip install -e .   然后安装OpenVINO™环境,输入以下指令: pip install openvino==2024.1.0 2.3 下载模型   首先导出目标识别模型,此处以官方预训练模型为例,首先下载预训练模型文件,然后调用yolo导出ONBNX格式的模型文件,最后使用 OpenVINO™ 的模型转换命令将模型转为IR格式,依次输入以下指令即可: wget https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10s.pt yolo export model=yolov10s.pt format=onnx opset=13 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 Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="OpenCvSharp4" Version="4.9.0.20240103" /> <PackageReference Include="OpenCvSharp4.Extensions" Version="4.9.0.20240103" /> <PackageReference Include="OpenCvSharp4.runtime.win" Version="4.9.0.20240103" /> <PackageReference Include="OpenVINO.CSharp.API" Version="2024.0.0.1" /> <PackageReference Include="OpenVINO.CSharp.API.Extensions.OpenCvSharp" Version="1.0.4" /> <PackageReference Include="OpenVINO.runtime.win" Version="2024.0.0.1" /> </ItemGroup> </Project> 3.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 opencvsharp int 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_data<float>(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_data<float>(output_length); // -------- Step 9. Process reault -------- List<Rect> position_boxes = new List<Rect>(); List<int> class_ids = new List<int>(); List<float> confidences = new List<float>(); // Preprocessing output results for (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 opencvsharp int 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.GetArray<int>(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.cs 4. 项目运行与演示 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两种版本,供各位开发者使用。最后如果各位开发者在使用中有任何问题,欢迎大家与我联系。