首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在mlnet c#中使用onnx模型,传递输入和获取输出

如何在mlnet c#中使用onnx模型,传递输入和获取输出
EN

Stack Overflow用户
提问于 2021-11-14 15:28:07
回答 1查看 124关注 0票数 0

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

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

python中的用法如下所示

netorn中的模型如下所示

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

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

EN

回答 1

Stack Overflow用户

发布于 2021-11-14 16:39:32

我发现this在这方面很有用

您还必须定义您的模型架构。

下面是我在这个模型中最终得到的类

代码语言:javascript
复制
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);
        }
    }
}

这就是我如何使用它来获得预测

代码语言:javascript
复制
            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));

结果正如我所期望的那样

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69964461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档