• C# OpenVINO Cls 图像分类


    目录

    介绍

    效果

    耗时

    项目

    代码

    下载


    介绍

    OpenVINO.NET github地址:https://github.com/sdcb/OpenVINO.NET

    High quality .NET wrapper for OpenVINO™ toolkit.

    在AI的应用越来越广泛的今天,优化深度学习模型并进行推理部署已经成为了一门必要的技术。Intel开发的OpenVINO工具包(Open Visual Inference and Neural network Optimization)就是这样一款强大的工具。作为一个开源的工具包,OpenVINO为开发者提供了强大的深度学习模型优化和推理功能,支持跨不同的Intel硬件平台进行部署,包括CPU, 集成GPU, Intel Movidius VPU, 和FPGAs。该工具包的初衷就是实现一处编码后,能在任何地方部署的机器学习推理的解决方案。

    效果

    耗时

    1. class id=brown_bear, score=0.86
    2. preprocess time: 0.00ms
    3. infer time: 2.72ms
    4. postprocess time: 0.02ms
    5. Total time: 2.74ms

    项目

    VS2022

    .net framework 4.8

    OpenCvSharp 4.8

    Sdcb.OpenVINO

    代码

    Model rawModel = OVCore.Shared.ReadModel(model_path);
    PrePostProcessor pp = rawModel.CreatePrePostProcessor();
    PreProcessInputInfo inputInfo = pp.Inputs.Primary;

    inputInfo.TensorInfo.Layout = Sdcb.OpenVINO.Layout.NHWC;
    inputInfo.ModelInfo.Layout = Sdcb.OpenVINO.Layout.NCHW;

    Model m = pp.BuildModel();
    CompiledModel cm = OVCore.Shared.CompileModel(m, "CPU");
    InferRequest ir = cm.CreateInferRequest();

    Shape inputShape = m.Inputs.Primary.Shape;

    Stopwatch stopwatch = new Stopwatch();
    Mat resized = src.Resize(new OpenCvSharp.Size(inputShape[2], inputShape[1]));
    Mat f32 = new Mat();
    resized.ConvertTo(f32, MatType.CV_32FC3, 1.0 / 255);

    using (Tensor input = Tensor.FromRaw(
         new ReadOnlySpan((void*)f32.Data, (int)((long)f32.DataEnd - (long)f32.DataStart)),
        new Shape(1, f32.Rows, f32.Cols, 3),
        ov_element_type_e.F32))
    {
        ir.Inputs.Primary = input;
    }
    double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
    stopwatch.Restart();

    ir.Run();
    double inferTime = stopwatch.Elapsed.TotalMilliseconds;
    stopwatch.Restart();

    1. using OpenCvSharp;
    2. using Sdcb.OpenVINO;
    3. using Sdcb.OpenVINO.Natives;
    4. using System;
    5. using System.Diagnostics;
    6. using System.Drawing;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using System.Xml.Linq;
    10. using System.Xml.XPath;
    11. namespace OpenVINO_Cls_图像分类
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    20. string image_path = "";
    21. string startupPath;
    22. string model_path;
    23. Mat src;
    24. string[] dicts;
    25. StringBuilder sb = new StringBuilder();
    26. private void button1_Click(object sender, EventArgs e)
    27. {
    28. OpenFileDialog ofd = new OpenFileDialog();
    29. ofd.Filter = fileFilter;
    30. if (ofd.ShowDialog() != DialogResult.OK) return;
    31. pictureBox1.Image = null;
    32. image_path = ofd.FileName;
    33. pictureBox1.Image = new Bitmap(image_path);
    34. textBox1.Text = "";
    35. src = new Mat(image_path);
    36. pictureBox2.Image = null;
    37. }
    38. unsafe private void button2_Click(object sender, EventArgs e)
    39. {
    40. if (pictureBox1.Image == null)
    41. {
    42. return;
    43. }
    44. pictureBox2.Image = null;
    45. textBox1.Text = "";
    46. sb.Clear();
    47. Model rawModel = OVCore.Shared.ReadModel(model_path);
    48. PrePostProcessor pp = rawModel.CreatePrePostProcessor();
    49. PreProcessInputInfo inputInfo = pp.Inputs.Primary;
    50. inputInfo.TensorInfo.Layout = Sdcb.OpenVINO.Layout.NHWC;
    51. inputInfo.ModelInfo.Layout = Sdcb.OpenVINO.Layout.NCHW;
    52. Model m = pp.BuildModel();
    53. CompiledModel cm = OVCore.Shared.CompileModel(m, "CPU");
    54. InferRequest ir = cm.CreateInferRequest();
    55. Shape inputShape = m.Inputs.Primary.Shape;
    56. Stopwatch stopwatch = new Stopwatch();
    57. Mat resized = src.Resize(new OpenCvSharp.Size(inputShape[2], inputShape[1]));
    58. Mat f32 = new Mat();
    59. resized.ConvertTo(f32, MatType.CV_32FC3, 1.0 / 255);
    60. using (Tensor input = Tensor.FromRaw(
    61. new ReadOnlySpan<byte>((void*)f32.Data, (int)((long)f32.DataEnd - (long)f32.DataStart)),
    62. new Shape(1, f32.Rows, f32.Cols, 3),
    63. ov_element_type_e.F32))
    64. {
    65. ir.Inputs.Primary = input;
    66. }
    67. double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
    68. stopwatch.Restart();
    69. ir.Run();
    70. double inferTime = stopwatch.Elapsed.TotalMilliseconds;
    71. stopwatch.Restart();
    72. using (Tensor output = ir.Outputs.Primary)
    73. {
    74. ReadOnlySpan<float> data = output.GetData<float>();
    75. int maxIndex = Common.MaxIndexOfSpan(data);
    76. double postProcessTime = stopwatch.Elapsed.TotalMilliseconds;
    77. stopwatch.Stop();
    78. sb.AppendLine($"class id={dicts[maxIndex]}, score={data[maxIndex]:F2}");
    79. double totalTime = preprocessTime + inferTime + postProcessTime;
    80. sb.AppendLine($"preprocess time: {preprocessTime:F2}ms");
    81. sb.AppendLine($"infer time: {inferTime:F2}ms");
    82. sb.AppendLine($"postprocess time: {postProcessTime:F2}ms");
    83. sb.AppendLine($"Total time: {totalTime:F2}ms");
    84. Mat result_image = src.Clone();
    85. Cv2.PutText(result_image
    86. , $"class id={dicts[maxIndex]}, score={data[maxIndex]:F2}"
    87. , new OpenCvSharp.Point(10, 30)
    88. , HersheyFonts.HersheySimplex
    89. , 1
    90. , new Scalar(0, 0, 255)
    91. , 2);
    92. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
    93. textBox1.Text = sb.ToString();
    94. }
    95. }
    96. private void Form1_Load(object sender, EventArgs e)
    97. {
    98. startupPath = Application.StartupPath;
    99. model_path = startupPath + "\\yolov8n-cls.xml";
    100. dicts = XDocument.Load(model_path)
    101. .XPathSelectElement(@"/net/rt_info/model_info/labels").Attribute("value").Value
    102. .Split(' ');
    103. }
    104. }
    105. }

    下载

    可执行程序exe下载

    源码下载

  • 相关阅读:
    《腾讯精选练习50题》专题
    Vue的computed和watch的区别是什么?
    网站性能测试软件工具【安装教程】
    [ Shell ] 通过 Shell 脚本导出 GDSII/OASIS 文件
    【java实战】项目经验_04
    使用 JPA、Hibernate 和 Spring Data JPA 进行审计
    flex弹性盒模型与阿里图标的使用
    【SpringMvc】SpringMvc +MyBatis整理
    如何使用“Search quesries“报表数据
    Perl中捕获警告信息、异常信息并写入日志详解
  • 原文地址:https://blog.csdn.net/lw112190/article/details/133823813