保姆级教程:在C# WinForms里用ONNX Runtime跑通Detic模型(附完整源码与避坑指南)
实战指南在C# WinForms中部署Detic模型实现21K类物体检测1. 环境准备与项目配置在开始集成Detic模型之前我们需要搭建完整的开发环境。以下是详细的配置步骤1.1 开发工具与SDK安装首先确保已安装Visual Studio 2022社区版或专业版均可在安装时需要勾选以下工作负载.NET桌面开发工作负载使用C的桌面开发用于OpenCV的本地依赖1.2 项目依赖配置创建新的Windows窗体应用(.NET Framework)项目后通过NuGet包管理器安装以下关键组件Install-Package Microsoft.ML.OnnxRuntime -Version 1.16.2 Install-Package OpenCvSharp4 -Version 4.8.0 Install-Package OpenCvSharp4.runtime.win -Version 4.8.0这些组件分别提供ONNX Runtime推理引擎支持OpenCV的.NET封装OpenCV本地运行时库1.3 模型文件准备从Facebook Research官方仓库下载Detic模型推荐使用Detic_C2_SwinB_896_4x_IN-21KCOCO_in21k.onnx同时下载对应的21K类别标签文件imagenet_21k_class_names.txt。建议在项目目录下创建model子文件夹存放这些资源。提示模型文件较大(约500MB)首次运行时会进行初始化优化可能导致启动稍慢2. ONNX Runtime集成核心逻辑2.1 初始化推理会话在窗体类中声明以下成员变量private SessionOptions options; private InferenceSession onnx_session; private Tensorfloat input_tensor; private ListNamedOnnxValue input_ontainer; private string[] class_names;初始化代码应放在窗体加载事件中private void Form1_Load(object sender, EventArgs e) { string startupPath Application.StartupPath \\model\\; string model_path startupPath Detic_C2_SwinB_896_4x_IN-21KCOCO_in21k.onnx; string classer_path startupPath imagenet_21k_class_names.txt; // 读取类别标签 class_names File.ReadAllLines(classer_path); // 配置ONNX Runtime options new SessionOptions(); options.LogSeverityLevel OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO; options.AppendExecutionProvider_CPU(0); // 使用CPU推理 // 创建输入Tensor (1,3,640,640) input_tensor new DenseTensorfloat(new[] { 1, 3, 640, 640 }); input_ontainer new ListNamedOnnxValue(); // 加载模型首次加载较慢 onnx_session new InferenceSession(model_path, options); }2.2 图像预处理流水线Detic模型需要特定的输入预处理以下是关键步骤Mat PrepareInputImage(Mat srcImage, out float[] scaleFactors) { // 计算缩放因子 int max_dim Math.Max(srcImage.Cols, srcImage.Rows); scaleFactors new float[] { max_dim / 640f, max_dim / 640f }; // 创建正方形画布 Mat padded Mat.Zeros(new Size(max_dim, max_dim), MatType.CV_8UC3); Rect roi new Rect(0, 0, srcImage.Cols, srcImage.Rows); srcImage.CopyTo(new Mat(padded, roi)); // 转换颜色空间并缩放 Mat rgb new Mat(); Cv2.CvtColor(padded, rgb, ColorConversionCodes.BGR2RGB); Mat resized new Mat(); Cv2.Resize(rgb, resized, new Size(640, 640)); return resized; }3. 推理执行与结果解析3.1 执行模型推理在按钮点击事件中添加推理逻辑private void btnDetect_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(image_path)) return; // 准备输入图像 Mat image new Mat(image_path); Mat processed PrepareInputImage(image, out float[] scales); // 填充输入Tensor for (int y 0; y processed.Height; y) { for (int x 0; x processed.Width; x) { var pixel processed.AtVec3b(y, x); input_tensor[0, 0, y, x] pixel[0]; // R input_tensor[0, 1, y, x] pixel[1]; // G input_tensor[0, 2, y, x] pixel[2]; // B } } // 执行推理 input_ontainer.Clear(); input_ontainer.Add(NamedOnnxValue.CreateFromTensor(img, input_tensor)); var stopwatch Stopwatch.StartNew(); using var results onnx_session.Run(input_ontainer); stopwatch.Stop(); // 解析输出 ParseResults(results, scales); }3.2 输出结果解析定义结果解析方法class DetectionResult { public float XMin { get; set; } public float YMin { get; set; } public float XMax { get; set; } public float YMax { get; set; } public float Score { get; set; } public string Label { get; set; } } void ParseResults(IDisposableReadOnlyCollectionDisposableNamedOnnxValue results, float[] scales) { var boxes results[0].AsTensorfloat().ToArray(); var scores results[1].AsTensorfloat().ToArray(); var classes results[2].AsTensorlong().ToArray(); int num_detections results[0].AsTensorfloat().Dimensions[0]; ListDetectionResult detections new ListDetectionResult(); for (int i 0; i num_detections; i) { float score scores[i]; if (score 0.5f) continue; // 置信度阈值 float xmin boxes[i * 4] * scales[0]; float ymin boxes[i * 4 1] * scales[1]; float xmax boxes[i * 4 2] * scales[0]; float ymax boxes[i * 4 3] * scales[1]; detections.Add(new DetectionResult { XMin xmin, YMin ymin, XMax xmax, YMax ymax, Score score, Label class_names[classes[i]] }); } // 更新UI显示 UpdateDetectionResults(detections); }4. 可视化与性能优化4.1 检测结果可视化void UpdateDetectionResults(ListDetectionResult detections) { Mat resultImage originalImage.Clone(); foreach (var det in detections.OrderByDescending(d d.Score)) { // 绘制边界框 Cv2.Rectangle(resultImage, new Point((int)det.XMin, (int)det.YMin), new Point((int)det.XMax, (int)det.YMax), Scalar.Green, 2); // 绘制标签背景 string label ${det.Label} ({det.Score:0.00}); int baseline 0; var textSize Cv2.GetTextSize(label, HersheyFonts.HersheySimplex, 0.6, 1, out baseline); Cv2.Rectangle(resultImage, new Point((int)det.XMin, (int)det.YMin - textSize.Height - 5), new Point((int)det.XMin textSize.Width, (int)det.YMin), Scalar.Green, -1); // 绘制文本 Cv2.PutText(resultImage, label, new Point((int)det.XMin, (int)det.YMin - 5), HersheyFonts.HersheySimplex, 0.6, Scalar.White, 1); } // 显示结果 pictureBoxResult.Image BitmapConverter.ToBitmap(resultImage); }4.2 性能优化技巧通过以下方法可以显著提升推理性能模型优化// 在SessionOptions中添加图优化 options.GraphOptimizationLevel GraphOptimizationLevel.ORT_ENABLE_ALL;内存复用// 复用输入Tensor避免重复分配 private Tensorfloat reusableTensor new DenseTensorfloat(new[] { 1, 3, 640, 640 });异步处理private async void btnDetect_Click(object sender, EventArgs e) { btnDetect.Enabled false; await Task.Run(() RunInference()); btnDetect.Enabled true; }批处理支持// 修改输入Tensor维度支持批处理 input_tensor new DenseTensorfloat(new[] { batchSize, 3, 640, 640 });5. 高级功能扩展5.1 掩码预测支持Detic模型支持实例分割掩码输出扩展结果解析方法void ParseMaskResults(IDisposableReadOnlyCollectionDisposableNamedOnnxValue results) { var masks results[3].AsTensorfloat(); // 获取掩码维度信息 int num_instances masks.Dimensions[0]; int mask_height masks.Dimensions[2]; int mask_width masks.Dimensions[3]; for (int i 0; i num_instances; i) { Mat mask new Mat(mask_height, mask_width, MatType.CV_32FC1); // 填充掩码数据 for (int y 0; y mask_height; y) { for (int x 0; x mask_width; x) { mask.Set(y, x, masks[0, i, y, x]); } } // 缩放掩码到原图尺寸 Mat resizedMask new Mat(); Cv2.Resize(mask, resizedMask, new Size(originalWidth, originalHeight)); // 应用阈值生成二值掩码 Mat binaryMask new Mat(); Cv2.Threshold(resizedMask, binaryMask, 0.5, 255, ThresholdTypes.Binary); // 可视化处理 VisualizeMask(binaryMask, i); } }5.2 多线程处理管道实现高效的图像处理管道private BlockingCollectionMat imageQueue new BlockingCollectionMat(boundedCapacity: 3); // 生产者线程 void StartCameraCapture() { VideoCapture capture new VideoCapture(0); while (true) { Mat frame new Mat(); capture.Read(frame); if (!frame.Empty()) { imageQueue.Add(frame.Clone()); } } } // 消费者线程 void ProcessFrames() { foreach (var frame in imageQueue.GetConsumingEnumerable()) { var results RunInference(frame); BeginInvoke((Action)(() UpdateUI(results))); } }5.3 模型量化加速使用ONNX Runtime的量化功能提升性能// 量化模型需提前准备校准数据集 var quantizeOptions new QuantizationOptions { CalibrationData GetCalibrationData(), ActivationType QuantizationType.QUInt8, WeightType QuantizationType.QUInt8 }; var quantizedModel OnnxRuntime.QuantizeModel(modelPath, quantizeOptions); File.WriteAllBytes(quantized_model.onnx, quantizedModel); // 使用量化模型 options new SessionOptions(); options.GraphOptimizationLevel GraphOptimizationLevel.ORT_ENABLE_ALL; options.EnableCpuMemArena true; onnx_session new InferenceSession(quantized_model.onnx, options);
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2585379.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!