Search code examples
onnxml.net-model-builder

ML.NET export to ONNX


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.


Solution

  • 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);
    }