目录
介绍
效果
耗时
项目
代码
下载
介绍
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。该工具包的初衷就是实现一处编码后,能在任何地方部署的机器学习推理的解决方案。
效果
耗时
class id=brown_bear, score=0.86
preprocess time: 0.00ms
infer time: 2.72ms
postprocess time: 0.02ms
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<byte>((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();
using OpenCvSharp;
using Sdcb.OpenVINO;
using Sdcb.OpenVINO.Natives;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.XPath;
namespace OpenVINO_Cls_图像分类
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
string image_path = "";
string startupPath;
string model_path;
Mat src;
string[] dicts;
StringBuilder sb = new StringBuilder();
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null;
image_path = ofd.FileName;
pictureBox1.Image = new Bitmap(image_path);
textBox1.Text = "";
src = new Mat(image_path);
pictureBox2.Image = null;
}
unsafe private void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null)
{
return;
}
pictureBox2.Image = null;
textBox1.Text = "";
sb.Clear();
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<byte>((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();
using (Tensor output = ir.Outputs.Primary)
{
ReadOnlySpan<float> data = output.GetData<float>();
int maxIndex = Common.MaxIndexOfSpan(data);
double postProcessTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Stop();
sb.AppendLine($"class id={dicts[maxIndex]}, score={data[maxIndex]:F2}");
double totalTime = preprocessTime + inferTime + postProcessTime;
sb.AppendLine($"preprocess time: {preprocessTime:F2}ms");
sb.AppendLine($"infer time: {inferTime:F2}ms");
sb.AppendLine($"postprocess time: {postProcessTime:F2}ms");
sb.AppendLine($"Total time: {totalTime:F2}ms");
Mat result_image = src.Clone();
Cv2.PutText(result_image
, $"class id={dicts[maxIndex]}, score={data[maxIndex]:F2}"
, new OpenCvSharp.Point(10, 30)
, HersheyFonts.HersheySimplex
, 1
, new Scalar(0, 0, 255)
, 2);
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
textBox1.Text = sb.ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
startupPath = Application.StartupPath;
model_path = startupPath + "\\yolov8n-cls.xml";
dicts = XDocument.Load(model_path)
.XPathSelectElement(@"/net/rt_info/model_info/labels").Attribute("value").Value
.Split(' ');
}
}
}
下载
可执行程序exe下载
源码下载