2

NET GUI tools to train model. The model works great with C# application, but I want to have it in ONNX format. I have found tools which are converting between model formats, but couldn't find anything for ML.NET generated format. Apparently it's some zip file, and nothing more I know about it. does anyone know a tool to do conversion to ONNX. Thanks

Microsoft's ML.Net Model Builder generates code


    // Load Data
                IDataView trainingDataView = mlContext.Data.LoadFromTextFile<ModelInput>(
                                                path: TRAIN_DATA_FILEPATH,
                                                hasHeader: true,
                                                separatorChar: ',',
                                                allowQuoting: true,
                                                allowSparse: false);
    
                // Build training pipeline
                IEstimator<ITransformer> trainingPipeline = BuildTrainingPipeline(mlContext);
    
                // Train Model
                ITransformer mlModel = TrainModel(mlContext, trainingDataView, trainingPipeline);
    
                // Evaluate quality of Model
                Evaluate(mlContext, trainingDataView, trainingPipeline);
    
                // Save model
                SaveModel       (mlContext, mlModel, MODEL_FILE, trainingDataView.Schema);
    
    
                var path = GetAbsolutePath(MODEL_FILE);
                path = new FileInfo(path).Directory.FullName;
                path = Path.Combine(path, "mymodel.onnx");
    
                using (var onnx = File.Open(path, FileMode.OpenOrCreate))
                {
                    mlContext.Model.ConvertToOnnx(mlModel, trainingDataView, onnx);
                }

from

var path = GetAbsolutePath(MODEL_FILE);
                path = new FileInfo(path).Directory.FullName;
                path = Path.Combine(path, "mymodel.onnx");
    
                using (var onnx = File.Open(path, FileMode.OpenOrCreate))
                {
                    mlContext.Model.ConvertToOnnx(mlModel, trainingDataView, onnx);
                }

I have modified. I am getting onnx file, but I am not able to run it(inference). Likewise, I have tried to open it with WinML Dashboard, but it's also not able to run generated onnx. I wonder perhaps it's the version of the onnx it generates? the model is simple regression with all inputs float numbers and output one float as well.

r9guy
  • 65
  • 7
  • Have you got a solution to your problem? I have the same issue. I want to train a model using the model builder and then convert it to onnx, but have no success so far. The Onnx converter package does not work and throws an error. – Jojo71 Dec 28 '21 at 08:36

1 Answers1

3

Use the Microsoft.ML.OnnxConverter NuGet Package. Something like this:

var mlContext = new MLContext();

...

IDataView trainingData = mlContext.Data.LoadFromEnumerable(inputData);

var pipeline = mlContext.Transforms.Concatenate("Features", ... )
    .Append(...));

var model = pipeline.Fit(trainingData);

using (var onnx = File.Open("mymodel.onnx", FileMode.OpenOrCreate))
{
    mlContext.Model.ConvertToOnnx(model, trainingData, onnx);
}

ctacke
  • 66,480
  • 18
  • 94
  • 155