发布于 2021-11-14 16:39:32
我发现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));结果正如我所期望的那样

https://stackoverflow.com/questions/69964461
复制相似问题