Skip to content

First look at Machine Learning using ML.NET

ML.NET is a cross-platform open-source machine learning framework that makes machine learning accessible to .NET developers. With ML.NET, you can create custom ML models using C# or F# without having to leave the .NET ecosystem. Oh yes it is cross-platform and works on Windows, Linux and macOS.

ML.NET lets you re-use all the knowledge, skills, code, and libraries you already have as a .NET developer so that you can easily integrate machine learning into your web, mobile, desktop, games, and IoT apps.

What is the difference between Artificial Intelligence and Machine Learning?

Artificial Intelligence (AI) is a branch of computing that involves training computers to do things that normally require human intelligence. Machine Learning (ML) is a subset of AI that involves computers learning from and finding patterns in data in order to then be able to make predictions on new data by themselves.

What is deep learning ?

Deep learning is a subset of machine learning. To train deep learning models, large quantities of data are required. Patterns in the data are represented by a series of layers. The relationships in the data are encoded as connections between the layers containing weights. The higher the weight, the stronger the relationship. Collectively, this series of layers and connections are known as artificial neural networks. The more layers in a network, the “deeper” it is, making it a deep neural network.

So how does ML.NET and Model Builder work?

As a .NET developer I am always excited to learn something new and ML.NET looks more interesting and futuristic. Though writing code for the different stages of machine learning is not an easy task, with the introduction of Model Builder it has become much easier.

ML.NET also works with pre-trained TensorFlow models as well as ONNX models.

Yes that means you can use the popular pre-trained models like YOLO in ML.NET

ML.NET follows the same basic steps for nearly every scenario; it combines data loading, transformations, and model training to make it easy for you to create machine learning models.

  1. Create ML.NET context
  2. Load data
  3. Transform data
  4. Choose algoritm
  5. Train model
  6. Evaluate model
  7. Deploy & consume model
Code Snippet below
//Step 1. Create a ML Context
var ctx = new MLContext();

//Step 2. Read in the input data for model training
IDataView dataReader = ctx.Data
    .LoadFromTextFile<MyInput>(dataPath, hasHeader: true);

//Step 3. Build your estimator
IEstimator<ITransformer> est = ctx.Transforms.Text
    .FeaturizeText("Features", nameof(SentimentIssue.Text))
        .LbfgsLogisticRegression("Label", "Features"));

//Step 4. Train your Model
ITransformer trainedModel = est.Fit(dataReader);

//Step 5. Make predictions using your model
var predictionEngine = ctx.Model
    .CreatePredictionEngine<MyInput, MyOutput>(trainedModel);

var sampleStatement = new MyInput { Text = "This is a horrible movie" };

var prediction = predictionEngine.Predict(sampleStatement);

But if you are using ML.NET Model Builder, several of the above steps become easy like

  1. Create ML.NET context is part of the final stage of the Model Builder and provides the code to integrate into your existing application.
  2. Load data is part of the 3rd step when you provide the data for the model to be trained
  3. For simple and straight forward cases, transform is also taken care of
  4. While training Model Builder uses AutoML (Automated ML) and picks the correct model to be used. Not just that, in several cases it also shows additional models and its performance for the user to pick the ones suitable.
  5. Training a model is time consuming and expensive with relatively larger data sets. Model Builder gives an option to run the Training of the model on Azure which is useful if its time/resource critical.
  6. Once the model is created, you are given an option to evaluate the model a sort of testing
  7. Model builder creates 2 projects, one for using the model for which the training was done and another Console app which you can use to test or evaluate the model.

Below you can see animation of how Model Builder is used for Image Classification (DNN + ResNet50) .

ML.NET Model Builder in action inside Visual Studio 2019

In the above example, I have provided 3 folders with product names and each folder containing several images of the said product. Once the training is done (which takes a bit of time depending on the number of images), at the evaluate stage I pick an image for the product which was not in the sample during the training to check if the model built can detect the right product name.

In this example the accuracy has been around 99-98%. In case the accuracy is low or detection is not accurate, you would need to give more images for the training stage.

An interesting project I came across using AI below. Very impressive!

Power of Maching Learning

Related resource links

Leave a Reply

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

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

%d bloggers like this: