【问题标题】:ML.net - error when consuming ONNX in ML.netML.net - 在 ML.net 中使用 ONNX 时出错
【发布时间】:2020-05-04 13:01:29
【问题描述】:

在我的 .NET 核心应用程序中使用 ML.net 我正在尝试使用导出到 ONNX 文件的 KERAS LSTM 模型。这是我的代码:

using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms.Onnx;

public void getmodel(mydata[] data1)
{
    string modelPath = "C:\\MyStuff\\ONNXtest.onnx";
    MLContext mlContext = new MLContext();

    IDataView data = mlContext.Data.LoadFromEnumerable<mydata>(data1);

    OnnxScoringEstimator pipeline = mlContext.Transforms.ApplyOnnxModel(new[] { "output" }, new[] { "input" }, modelPath);

    IEnumerable<mydata> testdata = mlContext.Data.CreateEnumerable<mydata>(data, reuseRowObject: true);
    foreach (mydata row in testdata)
    {
        System.Diagnostics.Debug.WriteLine(row.myval[0]);
        System.Diagnostics.Debug.WriteLine(row.myval[1]);
        System.Diagnostics.Debug.WriteLine(row.myval[2]);
        System.Diagnostics.Debug.WriteLine(row.myval[3]);
        System.Diagnostics.Debug.WriteLine(row.myval[4]);
    }

    OnnxTransformer test = pipeline.Fit(data);

    IDataView transformedValues = test.Transform(data);

    IEnumerable<float[]> results = transformedValues.GetColumn<float[]>("output");

    double result = Convert.ToDouble(results.ElementAtOrDefault(0).GetValue(0));

}

这是 mydata 类的样子:

public class mydata
{
     [VectorType(1,5,1)]
     [ColumnName("input")]   
     public float[] myval { get; set; }
}

我想在模型中输入 5 个值并查看“System.Diagnostics.Debug.WriteLine”输出,似乎一切正常,并且 IDataView 数据包含 5 个输入模型的值。但是,pipeline.Fit(data) 行会导致 Microsoft.ML.OnnxTransformer.dll 中出现 "System.ArgumentOutOfRangeException" 错误。

这里也是用python训练和导出LSTM的代码:

regressor = Sequential()
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1),name ='input'))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))

regressor.add(Dense(units = 1,name ='output'))
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
regressor.fit(X_train, y_train, epochs = 100, batch_size = 32)

from winmltools import convert_keras
model_onnx = convert_keras(regressor, 7, name='sequential_7')
from winmltools.utils import save_model
save_model(model_onnx, 'C:\\MyStuff\\ONNXtest.onnx')

这是导出的网络摘要:

Model: "sequential_7"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input (LSTM)                 (None, 5, 50)             10400     
_________________________________________________________________
dropout_25 (Dropout)         (None, 5, 50)             0         
_________________________________________________________________
lstm_21 (LSTM)               (None, 5, 50)             20200     
_________________________________________________________________
dropout_26 (Dropout)         (None, 5, 50)             0         
_________________________________________________________________
lstm_22 (LSTM)               (None, 5, 50)             20200     
_________________________________________________________________
dropout_27 (Dropout)         (None, 5, 50)             0         
_________________________________________________________________
lstm_23 (LSTM)               (None, 50)                20200     
_________________________________________________________________
dropout_28 (Dropout)         (None, 50)                0         
_________________________________________________________________
output (Dense)               (None, 1)                 51        
=================================================================
Total params: 71,051
Trainable params: 71,051
Non-trainable params: 0

如果我只是在 Python 中使用该模型,它可以完美运行,并且前段时间非常相似的设置(实际上我认为它是相同的代码)在 ML.net 中完美运行。但现在我什至不确定我的错误是在 python 端还是在 c# 端。谁能帮我弄清楚如何处理这个错误?

【问题讨论】:

    标签: python c# keras ml.net onnx


    【解决方案1】:

    据我了解,这是 C# 端的错误,因为 ML.NET 不允许多维输入数组。 VectorTypeAttribute(dims) 的文档在这一点上尚不清楚,但似乎是一个很快就会解决的问题。见:https://github.com/dotnet/machinelearning/issues/5273

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      相关资源
      最近更新 更多