Hi!
New version of Machine Learning.Net and in this version, we have the ability to use TensorFlow frozen models in ML.Net.
During the process of creating a pipeline, we can now use TensorFlow frozen models models and use them to train a model and make predictions. In a console application, at the time of adding the ML.Net packages we can see a new series of packages to work with TensorFlow

The following code is the best way to understand how a pipeline with a TF model works. While I’m building my Pipeline, I use a couple of trainers to load and modify images; and then on line 40 the TF model is added to the pipeline
| using System; | |
| using System.IO; | |
| using Microsoft.ML; | |
| using Microsoft.ML.Runtime.Api; | |
| using Microsoft.ML.Trainers; | |
| using Microsoft.ML.Transforms; | |
| namespace MLNetConsole10 | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| const int imageHeight = 32; | |
| const int imageWidth = 32; | |
| const string modelLocation = "cifar_model/frozen_model.pb"; | |
| const string dataFile = "images/images.tsv"; | |
| var imageFolder = Path.GetDirectoryName(dataFile); | |
| var pipeline = new LearningPipeline(); | |
| pipeline.Add(new Microsoft.ML.Data.TextLoader(dataFile).CreateFrom<CifarData>()); | |
| pipeline.Add(new ImageLoader(("ImagePath", "ImageReal")) | |
| { | |
| ImageFolder = imageFolder | |
| }); | |
| pipeline.Add(new ImageResizer(("ImageReal", "ImageCropped")) | |
| { | |
| ImageHeight = imageHeight, | |
| ImageWidth = imageWidth, | |
| Resizing = ImageResizerTransformResizingKind.IsoCrop | |
| }); | |
| pipeline.Add(new ImagePixelExtractor(("ImageCropped", "Input")) | |
| { | |
| UseAlpha = false, | |
| InterleaveArgb = true | |
| }); | |
| pipeline.Add(new TensorFlowScorer() | |
| { | |
| ModelFile = modelLocation, | |
| InputColumns = new[] { "Input" }, | |
| OutputColumn = "Output" | |
| }); | |
| pipeline.Add(new ColumnConcatenator("Features", "Output")); | |
| pipeline.Add(new TextToKeyConverter("Label")); | |
| pipeline.Add(new StochasticDualCoordinateAscentClassifier()); | |
| var model = pipeline.Train<CifarData, CifarPrediction>(); | |
| model.TryGetScoreLabelNames(out var scoreLabels); | |
| Console.WriteLine($"ScoreLabels.Length {scoreLabels.Length}"); | |
| Console.WriteLine($"banana {scoreLabels[0]}"); | |
| Console.WriteLine($"hotdog {scoreLabels[1]}"); | |
| Console.WriteLine($"tomato {scoreLabels[2]}"); | |
| var prediction = model.Predict(new CifarData() | |
| { | |
| ImagePath = "images/banana.jpg" | |
| }); | |
| Console.WriteLine($"{prediction.PredictedLabels[0]}"); | |
| Console.WriteLine($"{prediction.PredictedLabels[1]}"); | |
| Console.WriteLine($"{prediction.PredictedLabels[2]}"); | |
| } | |
| } | |
| public class CifarData | |
| { | |
| [Column("0")] | |
| public string ImagePath; | |
| [Column("1")] | |
| public string Label; | |
| } | |
| public class CifarPrediction | |
| { | |
| [ColumnName("Score")] | |
| public float[] PredictedLabels; | |
| } | |
| } |
At this time the file [Cifar_model/FrozenModel. PB] is not part of the ML.Net repository. If you want to try the code, you can download a trial version from https://github.com/deeplearning4j/dl4j-test-resources/tree/master/src/main/resources/tf_graphs/examples/yolov2_608x608

Happy Coding!
Greetings @ Toronto
El Bruno
References
My Posts
- New version 0.4, news Improvements in Text analysis using Word Embedding
- Error ‘Entry point ‘Trainers.LightGbmClassifier’ not found’ and how to fix it
- Machine Learning Glossary of terms
- Export Machine Learning.Net models to ONNX format
- 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 !
Leave a Reply