#MLNET – ML.Net 0.5 initial support for #TensorFlow

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

01 ms ml tensor flow

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

view raw
MLNetTensorFlow.cs
hosted with ❤ by GitHub

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

02 mlnet cifar pb

Happy Coding!

Greetings @ Toronto

El Bruno

References

My Posts

8 thoughts on “#MLNET – ML.Net 0.5 initial support for #TensorFlow

Leave a Reply

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 )

Google photo

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

Twitter picture

You are commenting using your Twitter 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.