Loading

AI ML.NET

How to Start with ML.NET Model Builder for Using in ASP.NET Core?. The Complete AI ML.NET Developer Course 2023 [Videos].

ML.NET Model builder makes our work easier while developing an ML.NET application. It helps build, train and deploy the models more easily for all kinds of ML .NET supporting models, as well us helps us develop the custom models. If you are new to ML.NET, please read my previous articles related to ML.NET. The latest version of ML.NET is also established to develop easy custom ML using AutoML and with ML.NET Model builders.

Before getting started with the ML.NET, lets understand the basic concept of ML.NET to develop our machine learning applications.
 
Load Data
 
For the perfect prediction of results, we need to give a lot of data to train the model. In ML.NET, we can give the data for both training and testing with a Text (CSV/TSV, Relational Database.
 
Train
 
We need to select the right algorithm to train the model. Depending on our need, we need to pick the correct algorithm to train and predict the results.
 
Evaluate
 
Select the Machine learning type for our model training and prediction. If you need to work with a segment, then you can select the Clustering model. If you need to find the price of stock prediction, you can select Regression, and if you need to find the sentiment analysis, then can select the Classification model.
 
Predicted Results
 
Based on the train and test data with the trained model, the final prediction will be displayed using the ML.NET application. The trained model will be saved as the binary format which can also be integrated with our other .NET applications.
  
 
While using ML.NET model builder, its easy to load the data either from CSV, Excel or from the Database. Select and train our preferred available ML.NET models, or go with custom model training. Evaluate the model and finally predict and display the trained model result to any of our .NET applications. Now in this Video, lets see trained and predicted results. We will be displaying results in our ASP.NET Core application.
 
In ML.NET Mode builder we can see the following scenario. (Note: Microsoft can add more scenario, as it might be different from each version)
 
 
In this sample program, we will be using ML.NET Model builder to predict the Item Stock using the Custom Scenario.
 
In my previous Video that I wrote on April 2018, I explained the same Item Stock Prediction using the console application. I used the same Item Stock Data as CSV file and used the data to train the model and predict the result. Finally, I bound the result to the ASP.NET Core application.
 
The item stock data will look like this. Here, I have the columns Item Code, Location Code, Warehouse in Quantity, Warehouse Out Quantity, Item Type and finally Remaining Item total Stock quantity of each item in each location. We will use the below dataset to train, evaluate, and predict the result using the ML.NET Model builder.
 
 
Prerequisites
 
 
Code Part
 
Step 1 - Create an ASP.NET Core Application
 
(Note: I used Visual Studio 2019)
 
After installing the prerequisites, click Start >> Programs >> Visual Studio 2019 >> Visual Studio 2019 on your desktop. >> Click Create a Project. Click continue.
 
 
Select ASP.NET Core Web Application and click Next.
 
 
Select the Project Folder and check the Place solutions in the same directory and on the create button.
 
 
Now select Web Application (Model-View-Controller) and click on the Create button.
 
 
We can see our ASP.NET Project has been created with Controllers/Models and Views.
  
 
Step 2 - Working with ML.NET Model Builder
 
Right click on the project, select Add and click on Machine Learning.
  
 
We can see the ML.NET Model Builder
 
 
In ML.NET Model Builder, on the  left side we can see the menu, which displays Scenario, Data, Train, Evaluate and Code
 
Scenario
 
From the Scenario, you can see the available ML.NET Models for the development example like Binary classification, Image Classification, etc. Also, you can see the Custom Scenario in order to build your own custom models. In our demo, we will be using the custom model.
 
Data
 
You can load the data for training from the file or from the Database.
 
Train
 
Select the model to be trained
 
Evaluate
 
The evaluation will be performed after the model training is completed. While evaluating, the trained model will be compared with the test data to compare and predict the final result to be produced.
 
Code
 
Finally, the code will be used to create the trained model and use it in any application. We will be using the final trained and evaluated model code for our ASP.NET Core application to predict and display the result for Total Item stock Quantity available per location.
 
Custom Scenario
 
Here, we have selected the Custom scenario of our demo application in order to predict the Stock quantity.
 
 
Data
 
After selecting our Scenario, we can see the next menu will be displayed. Here, we have selected the Custom Scenario. Next, the Data menu has been displayed. From here, we have the option to load the data to be trained from the file or the Database. Here we will be using the file to load the data.
 
Click on the Select a file and load the file. We can see here that we have loaded the file to be trained.
 
 
Next, we need to select the column to predict (In Machine Learning, the prediction column is called the Label). In our demo, we are going to predict the TotalStockQty. Also, we need to select the Input column given by us to predict the result. For our demo, we need to predict the Stock Qty based on the Item and by the Location. Therefore we have selected the Input (Feature) columns as ItemID and Loccode.
 
 
Train
 
After that, click on the train button to train the model with the loaded data.

In the train screen, we can see the Machine Learning tasks to be selected for training our model. We also can see the time to train the model in seconds to be selected.
 
 
Machine Learning Task
 
We can see that in the custom scenario, the task is available for ML.NET is multiclass-classification, binary-classification, and regression. Here for our demo, we selected multiclass-classification.
 
 
Our file size is less than 10mb data size, so we can select approximately 10 seconds to train the model.
 
After selecting the Machine learning task and training time, click on the Start training button and wait for few seconds to complete the training. 
 
Now we can see the training is completed and the best algorithm has been used. Next, we need to click on the Evaluate button to evaluate our training model.
 
Evaluate
 
In the evaluation screen, we can evaluate the trained model and finally click on the code button to get the code.
 
 
Code
 
We can see that with the ML.NET Model, training and testing project can be added to our solutions. We will add this model to our ASP.NET Code project solution. For that, we need to click on the Add to Projects button.
 
 
We can see both Model and the console project have been added to our ASP.NET Core solution. Now lets see how to import the model to our ASP.NET core and display the predicted result to the users.
 
 
Step 3 - Add the ML.NET to ASP.Net Core project for prediction
 
Select Tools from the menu, and select NuGet Package Manager, then click on Manage NuGet Packages for the Solution.
 
 
Select Browse and enter ML.NET in search. Select Microsoft.ML. Also, on the right side, we can see our ML.NET model projects displayed. Select the project and click on Install.
 
 
Step 4 - Create new ASP.NET Core Controller

Right click on the Controller Class and click New Empty control. Give the controller name StockPrediction and add the controller to the controller folder.
 
 
Step 5 - Create new ASP.NET Core View

Next, create a new folder inside the View folder as StockPrediction. Also, add a view with the name StockPrediction.cshtml
 
 
Controller Code
 
In the StockPredictionController Controller add the below code:
 
In the import part, add the model
  1. using ShanuASPMLNETML.Model;  
After importing that, add the below code. Here, we change the default view with our new View name. Also, we have created one more post Action result and returned the predicted result to the view to bind the results.
  1. public class StockPredictionController : Controller  
  2.     {  
  3.         [HttpGet]  
  4.         public IActionResult StockPrediction()  
  5.         {  
  6.             return View();  
  7.         }  
  8.   
  9.         [HttpPost]  
  10.         public ActionResult StockPrediction(ModelInput input)  
  11.         {  
  12.             ViewBag.Result = "";  
  13.             var stockPredictions = ConsumeModel.Predict(input);  
  14.             ViewBag.Result = stockPredictions;  
  15.   
  16.             ViewData["ItemID"] = input.ItemID;  
  17.             ViewData["Loccode"] = input.Loccode;  
  18.             return View();  
  19.   
  20.         }  
  21.     }  
View Code
 
In the StockPrediction View, add the below code. Here, we added 2 textboxes, one for the item Code input and one for the Location code input. We have set the same name as our model name so that the data will be passed to our Trained model and return the predicted results. Finally, we bind the predicted results to the users.
  1.  @model ShanuASPMLNETML.Model.ModelInput  
  2. @{  
  3.     ViewData["Title"] = "item Stock Predicti0n";  
  4.     Layout = "~/Views/Shared/_Layout.cshtml";  
  5. }  
  6.   
  7. Welcome to Shanu Stock Prediction for the Item by Warehouse Location

      
  8. using ML.NET in ASP.NET Core

      
  9.   
  10.   
  11.   
  12. class="row">  
  13.     class="col-md-12">  
  14.         "StockPrediction">  
  15.             class="row">  
  16.                 class="form-group col-md-4">  
  17.                     for="ItemID" class="control-label">Item ID  
  18.                     for="ItemID" class="form-control" />  
  19.                 
  
  •                 class="form-group col-md-4">  
  •                     for="Loccode" class="control-label">Warehouse Locatoin Code:  
  •                     for="Loccode" class="form-control" />  
  •                 
  •   
  •   
  •               
  •             class="form-group col-md-4 text-left">  
  •                 class="control-label">  
  •                 "submit" value="Stock Prediction" class="btn btn-primary" />  
  •               
  •           
  •           
  •       
  •   
  •   
  •   
  •   
  • @if (ViewBag.Result != null)  
  • {  
  •     class="row">  
  •         class="col-md-12">  
  •             "color:#103f62">Here is the Test result for Item : "color:#a50f0f"> @ViewData["ItemID"]   for the Warehouse location "color:#a50f0f">@ViewData["Loccode"    
  •             "color:#a50f0f">Final Predicted Stock Quantity is :@ViewBag.Result.Prediction    
  •           
  •       
  •       
  • }  
  • Build and run the project
     
    We can enter the Item code and Location code to be predicted and display the Stock Quantity.
     
    Here, we have enter the Item code as Item001 and Location code as 1.
     
    Getting Started With ML.NET Model Builder For Using In ASP.NET Core 
     
    We can see the Predicted result as :90
     
    In the Excel file, we can also cross check the result or our original data.
     
     
    Same as above, we test for one more data for prediction.
     
    Here, we enter the Item code as Item003 and Location code as 2. 
     
    The Excel file data for the Item003 is added in the file as shown below.
     
     
    From both results, we can find that the prediction is completed 100%, and the result is as expected.
     

    Conclusion

     
    ML.NET (Machine Learning DotNet) is a great framework for all the .NET lovers who are all looking to work with machine learning. Now, only the preview version of ML.NET is available and I cant wait until the release of the public version. If you are .NET lovers, unaware of Machine Learning and are looking forward to working with machine learning, then ML.Net is for you all. Its a great framework to get started with ML.NET. Hopefully, you all enjoyed reading this Video and see you all soon with another post1

      See All

      Comments (335 Comments)

      Submit Your Comment

      See All Posts

      Related Posts

      AI ML.NET / Blog

      How to start with Machine Learning .NET (ML.NET)?

      In Build 2018, Microsoft introduced the preview of ML.NET (Machine Learning .NET) which is a cross-platform, open-source Machine Learning Framework. Yes, now it is easy to develop our own Machine Learning application or develop custom modules using Machine Learning Framework. ML.NET is a Machine Learning framework that was mainly developed for .NET developers. We can use C# or F# to develop ML.NET applications. ML.NET is open source and cross-platform and can run on Windows, Linux, and macOS. ML.NET is still in development and now we can use the preview version to work and play with ML.NET.
      7-Feb-2022 /25 /335

      AI ML.NET / Blog

      What Is Web3?

      Web3, or Web 3.0, is the next generation of the Web. Tim Barners-Lee called it the Semantic Web. Co-founder of Ethereum, Gavin Wood, who believes decentralized technologies will be the next generation of the Web, coined the term Web3 in 2014. And recently, Elon Musk asked on Twitter if anyone has seen web3. Twitter founder Jack Dorsey replied, "Its somewhere between a and z."
      7-Feb-2022 /25 /335

      AI ML.NET / Blog

      What Is Metaverse?

      The metaverse is a new buzzword that everyone is talking about today. If you pay attention to any trending technologies, Im sure you have heard of the metaverse by now. Some experts are calling it the next internet or the next computing technology. What is this metaverse? Why is it a hot topic today? How will the metaverse change the computing world? What role will the metaverse play in the future? Lets get all these answers right here, right now.
      7-Feb-2022 /25 /335