ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# 部署onnx模型

2021-10-29 16:03:14  阅读:560  来源: 互联网

标签:input c# onnx 模型 OnnxRuntime using new 224 image


1.环境

gpu3060+cuda11.1+vs2019

+Microsoft.ML.OnnxRuntime

+SixLabors.ImageSharp

 2.代码

using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.ML.OnnxRuntime.Tensors;  // DenseTensor

using SixLabors.ImageSharp;  // Image, Size
using SixLabors.ImageSharp.PixelFormats;  // Rgb24
using SixLabors.ImageSharp.Processing;  // image.Mutate

namespace Microsoft.ML.OnnxRuntime.ResNet50v2Sample
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Read paths
            string modelFilePath = @"E:\code\Csharp\onnxruntime-master\csharp\sample\Microsoft.ML.OnnxRuntime.ResNet50v2Sample\resnet50-v2-7.onnx";
            string imageFilePath = @"E:\code\Csharp\onnxruntime-master\csharp\sample\Microsoft.ML.OnnxRuntime.ResNet50v2Sample\dog.jpeg";

            // Read image
            // Rgb24:Pixel type containing three 8-bit unsigned normalized values ranging from 0 to
            //        255. The color components are stored in red, green, blue order
            // SixLabors.ImageSharp.Image
            using Image<Rgb24> image = Image.Load<Rgb24>(imageFilePath);  // 以rgb形式读取图片

            // Resize image
            image.Mutate(x =>
            {
                x.Resize(new ResizeOptions
                {
                    Size = new Size(224, 224),
                    Mode = ResizeMode.Crop
                });
            });

            //image.Mutate(x =>
            //        x.Resize(224, 224)
            //);


            // Preprocess image
            Tensor<float> input = new DenseTensor<float>(new[] { 1, 3, 224, 224 });  // 声明4维变量:(b, c, h, w)
            var mean = new[] { 0.485f, 0.456f, 0.406f };
            var stddev = new[] { 0.229f, 0.224f, 0.225f };
            for (int y = 0; y < image.Height; y++)
            {
                Span<Rgb24> pixelSpan = image.GetPixelRowSpan(y);
                for (int x = 0; x < image.Width; x++)  // 先行后列
                {
                    input[0, 0, y, x] = ((pixelSpan[x].R / 255f) - mean[0]) / stddev[0];
                    input[0, 1, y, x] = ((pixelSpan[x].G / 255f) - mean[1]) / stddev[1];
                    input[0, 2, y, x] = ((pixelSpan[x].B / 255f) - mean[2]) / stddev[2];
                }
            }

            // Setup inputs
            var inputs = new List<NamedOnnxValue>
            {
                NamedOnnxValue.CreateFromTensor("data", input)
            };

            // Run inference
            using var session = new InferenceSession(modelFilePath);
            using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = session.Run(inputs);

            // Postprocess to get softmax vector
            IEnumerable<float> output = results.First().AsEnumerable<float>();  // First(): The first element in the specified sequence. AsEnumerable:
            float sum = output.Sum(x => (float)Math.Exp(x));   // sum(e^x)
            IEnumerable<float> softmax = output.Select(x => (float)Math.Exp(x) / sum);  // e^x / sum

            // Extract top 10 predicted classes
            IEnumerable<Prediction> top10 = softmax.Select((x, i) => new Prediction { Label = LabelMap.Labels[i], Confidence = x })
                               .OrderByDescending(x => x.Confidence)
                               .Take(10);

            // Print results to console
            Console.WriteLine("Top 10 predictions for ResNet50 v2...");
            Console.WriteLine("--------------------------------------------------------------");
            foreach (var t in top10)
            {
                Console.WriteLine($"Label: {t.Label}, Confidence: {t.Confidence}");
            }
        }
    }
}

3.效果

输入图片:

网络输出:

 

 第一个是金毛猎犬。

标签:input,c#,onnx,模型,OnnxRuntime,using,new,224,image
来源: https://blog.csdn.net/jizhidexiaoming/article/details/121036458

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有