#MLNET – Export Machine Learning.Net models to #ONNX format


Machine Learning.Net has a new release. In this version 0.3, there are several very interesting features. Cesar De la Torre explains in a very detailed way all these new features (see references). I’ve had some extra time these days and I’ve been trying something that I find very interesting

Export Machine Learning.Net models to #ONNX format

As always the best thing describe this it with a couple of lines of code

  • Up to Line 28, the Console APP creates a pipeline, and trains it to have a ML.Net model.
  • In the following lines, using a OnnxConverter, I export the model to ONNX.

using System.IO;
using System.Text.RegularExpressions;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Models;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms;
using MLNetConsole05;
namespace MLNetConsole06
class Program
const string FileName = "AgeRangeData.csv";
const string OnnxPath = "SaveModelToOnnxTest.onnx";
const string OnnxAsJsonPath = "SaveModelToOnnxTest.json";
static void Main()
var pipeline = new LearningPipeline
new TextLoader(FileName).CreateFrom<AgeRange>(separator: ',', useHeader: true),
new Dictionarizer("Label"),
new TextFeaturizer("Sex", "Sex"),
new ColumnConcatenator("Features", "Age", "Sex"),
new StochasticDualCoordinateAscentClassifier(),
new PredictedLabelColumnOriginalValueConverter() {PredictedLabelColumn = "PredictedLabel"}
var model = pipeline.Train<AgeRange, AgeRangePrediction>();
var converter = new OnnxConverter()
Onnx = OnnxPath,
Json = OnnxAsJsonPath,
Domain = "com.elbruno"
// Strip the version.
var fileText = File.ReadAllText(OnnxAsJsonPath);
fileText = Regex.Replace(fileText, "\"producerVersion\": \"([^\"]+)\"", "\"producerVersion\": \"##VERSION##\"");
File.WriteAllText(OnnxAsJsonPath, fileText);

The documentation of OnnxConverter and examples of ML.Net detail more complex scenarios where for example you define which columns are included or excluded. If we open the file Onnx, it’s interesting to see how a simple pipeline is represented as this example


Happy Coding!

Saludos @ Toronto

El Bruno


My Posts


  1. Cada vez tienes menos vergüenza a la hora de escribir posts.. Haz algo y déjate de fusilar a la gente.. en fin, todo por el MVP.


    1. Siento que hayas perdido el tiempo leyendo esto …
      Espero que tengas la capacidad suficiente para elegir mejor algo que leer next time.
      Parece que no aprecias la ventaja de poder exportar a ONNX desde ML.Net !

      Un Saludo


Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: