Hi!
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
}; | |
converter.Convert(model); | |
// 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
References
My Posts
- Loading Data In our Learning Pipeline With List (Lists for ever!)
- What’s new in version 0.2.0
- What’s a Machine Learning model? A 7 minute video as the best possible explanation
- Write and Load models using Machine Learning .Net
- Understanding the step by step of Hello World
- Hello World in ML.Net, Machine Learning for .Net !
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.
LikeLike
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
-Bruno
LikeLike