【问题标题】:How to use onnx model in mlnet c#, passing inputs and getting outputs如何在mlnet c#中使用onnx模型,传递输入和获取输出
【发布时间】:2021-11-14 15:28:07
【问题描述】:

我使用 pytorch 训练了一个模型 我将它导出为 onnx 格式并在 python 中测试它是否有效(确实如此)

我想知道如何在 c# 中的 ml.net 中使用它

python中的用法是这样的

netorn 中的模型看起来像

我找到了一个example,它使用 使用包 Microsoft.ML、Microsoft.ML.OnnxRuntime 和 Microsoft.ML.OnnxTransformer

并且能够使用 onnx 模型,但它适用于图像,因为我对此很陌生,所以我无法弄清楚如何加载模型并进行预测并获得 action 的值,即一个浮点数为 6 的数组。

【问题讨论】:

    标签: ml.net


    【解决方案1】:

    我发现 this 对此很有用

    您还必须定义模型架构。

    这是我为这个模型完成的课程

    using Microsoft.ML;
    using Microsoft.ML.Data;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Numerics;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace godot_net_server
    {
        class OnnxModelScorer
        {
            private readonly string modelLocation;
            private readonly MLContext mlContext;
    
    
            public OnnxModelScorer(string modelLocation, MLContext mlContext)
            {
                this.modelLocation = modelLocation;
                this.mlContext = mlContext;
            }
    
            public class ModelInput
            {
                [VectorType(10)]
                [ColumnName("input.1")]
                public float[] Features { get; set; }
            }
    
            private ITransformer LoadModel(string modelLocation)
            {
                Console.WriteLine("Read model");
                Console.WriteLine($"Model location: {modelLocation}");
                
                // Create IDataView from empty list to obtain input data schema
                var data = mlContext.Data.LoadFromEnumerable(new List<ModelInput>());
    
                // Define scoring pipeline
                var pipeline = mlContext.Transforms.ApplyOnnxModel(modelFile: modelLocation, outputColumnNames: new[] { "31","34" }, inputColumnNames: new[] { "input.1" });
    
                // Fit scoring pipeline
                var model = pipeline.Fit(data);
    
                return model;
            }
            public class Prediction
            {
                [VectorType(6)]
                [ColumnName("31")]
                public float[] action{ get; set; }
                [VectorType(1)]
                [ColumnName("34")]
                public float[]  state { get; set; }
            }
            private IEnumerable<float> PredictDataUsingModel(IDataView testData, ITransformer model)
            {
                Console.WriteLine("");
                Console.WriteLine("=====Identify the objects in the images=====");
                Console.WriteLine("");
    
                IDataView scoredData = model.Transform(testData);
    
                IEnumerable<float[]> probabilities = scoredData.GetColumn<float[]>("31");
                var a = probabilities.ToList();
                a.Count.ToString();
                return a[0];
            }
    
            public IEnumerable<float> Score(IDataView data)
            {
                var model = LoadModel(modelLocation);
    
                return PredictDataUsingModel(data, model);
            }
        }
    }
    
    

    这就是我用它来获得预测的方式

                MLContext mlContext = new MLContext();
    
                // Load trained model
    
                var modelScorer = new OnnxModelScorer("my_ppo_1_model.onnx", mlContext);
                List<ModelInput> input = new List<ModelInput>();
                input.Add(new ModelInput()
                {
                    Features = new[]
                    {
                        3.036393f,11.0f,2.958097f,0.0f,0.0f,0.0f,0.015607f,0.684984f,0.0f,0.0f
                    }
                });
                var action = modelScorer.Score(mlContext.Data.LoadFromEnumerable(input));
    

    结果如我所料

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 2022-01-05
      • 1970-01-01
      • 2017-11-21
      相关资源
      最近更新 更多