C#使用ONNX模型

C#使用ONNX模型

            // 读取模型文件到会话对象中
            var onnxModelPath = @"F:\D\AI_Model\LZ\weights\best.onnx";
            using var session = new InferenceSession(onnxModelPath);

            string _inputName = session.InputMetadata.Keys.First();

            string imageFilePath = @"D:\\dataset\\LZ\\images\\train\\20230710152804042_6.jpg";


            // 读取图片
            // Read image
            using Image<Rgb24> image = Image.Load<Rgb24>(imageFilePath);

            // 改变图片大小至模型运算指定的大小
            // Resize image
            float ratio = 512f / Math.Min(image.Width, image.Height);
            image.Mutate(x => x.Resize((int)(512), (int)(512)));

            // Preprocess image
            var paddedHeight = (int)(Math.Ceiling(image.Height / 32f) * 32f);
            var paddedWidth = (int)(Math.Ceiling(image.Width / 32f) * 32f);
            Tensor<float> input = new DenseTensor<float>(new[] { 1,3, paddedHeight, paddedWidth });
            var mean = new[] { 102.9801f, 115.9465f, 122.7717f };
            for (int y = paddedHeight - image.Height; y < image.Height; y++)
            {
                image.ProcessPixelRows(im =>
                {
                    var pixelSpan = im.GetRowSpan(y);
                    for (int x = paddedWidth - image.Width; x < image.Width; x++)
                    {
                        input[0,0, y, x] = pixelSpan[x].B - mean[0];
                        input[0,1, y, x] = pixelSpan[x].G - mean[1];
                        input[0,2, y, x] = pixelSpan[x].R - mean[2];
                    }
                });

            }


            var inputs = new List<NamedOnnxValue>
            {
                    NamedOnnxValue.CreateFromTensor(_inputName, input)
            };

            // 运行模型得到结果
            using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = session.Run(inputs);

            // 对运行结果解析
            // Postprocess to get predictions
            var resultsArray = results.ToArray();
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C#调用ONNX模型,可以使用ONNX Runtime来实现。下面是一个简单的示例代码,展示了如何加载ONNX模型使用它进行预测: ```csharp using System; using System.Linq; using Microsoft.ML.OnnxRuntime; using Microsoft.ML.OnnxRuntime.Tensors; class Program { static void Main() { // 加载ONNX模型 var session = new InferenceSession("path/to/your/model.onnx"); // 准备输入数据 var inputMeta = session.InputMetadata; var inputName = inputMeta.Keys.First(); var shape = inputMeta[inputName].Dimensions.ToArray(); var inputTensor = new DenseTensor<float>(shape); // 设置输入数据 // 注意:这里需要根据模型的输入要求,将数据填充到inputTensor中 // 这里只是个示例,实际需要根据模型的输入要求来处理数据 float[] inputData = new float[shape.Length]; // 填充inputData数组 inputTensor.CopyFrom(inputData); // 准备输出数据 var outputMeta = session.OutputMetadata; var outputName = outputMeta.Keys.First(); var outputShape = outputMeta[outputName].Dimensions.ToArray(); var outputTensor = new DenseTensor<float>(outputShape); // 运行预测 session.Run(new[] { inputName }, new[] { inputTensor }, new[] { outputName }, new[] { outputTensor }); // 处理输出结果 var outputData = outputTensor.ToArray(); // 输出结果 foreach (var value in outputData) { Console.WriteLine(value); } } } ``` 请确保已经安装了ONNX Runtime NuGet包,并将路径 "path/to/your/model.onnx" 替换为你自己的ONNX模型文件路径。在代码中,你需要根据模型的输入要求,将数据填充到inputTensor中,并根据模型的输出要求来处理输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值