The code that accompanies this article can be downloaded here.
In the previous articles, we explored how we can use Microsoft’s new framework for machine learning – ML.NET. We used different datasets for different purposes and explored how to solve real-world problems. First, we used the Iris dataset to explore machine learning concepts and get up to speed with this field in general. This dataset is sort of Hello World example, but we wanted to learn how to deal with more complex datasets too. Because of that, we used the Bike Sharing Demand dataset to break down one real-world regression problem.
Finally, in the previous article, we used the Wine Quality dataset for solving real-world classification problem. We didn’t run through specific algorithms, but we have made certain mechanisms that helped us find the best algorithm very quickly. However, we didn’t really explore how to utilize this model that we created. We just created it and evaluated it. Now, let’s see how we can save the model, and then use it in a standard web application.
It is not an unusual process to create and train machine learning model in one application, export it an use it another application. Training and evaluation of this model can be time-consuming and requires a lot of resources. That is what we are going to do here. Since we had pretty cool results with Bike Sharing Demand regression solution, we will make an ASP.NET application that will use this model. So, let’s start with exporting the model.
Exporting the Model
As you can remember we used different algorithms to solve the regression problem and come to the conclusion that the best results are achieved with the FastTreeTweedieRegressor model. So, now we are going to extend the Main method in that example with the additional call. Essentially, we just want to store that model somewhere and use it in another application that we are going to develop with ASP.NET. Here is the new implementation of the Main method:
The only difference is that at the end of the method, where we called WriteAsync method on FastTreeTweedieRegressor model. Using this method on any PredictionModel object you can save the model into either Stream or into the file. In this example, we are saving the machine learning model into the Model.zip file. This way we can copy this file over into our new web application, or to any other application in a nutshell, and then use it from there.
Now, we are not making some sort of sophisticated web application here. Basically, what we want to achieve is to give a user options to enter the values for the features and then feed this data to the machine learning model. After that, we want to present this prediction to the user. So, nothing too complex, the emphasis is on properly importing the model and using it out of the box. For this purpose, we created two views. Here is the first one which is used to gather information from the user:
And here is the second view that is used to display prediction results:
To sum it up, nothing complicated: getting information from the user, using machine learning model to get the prediction and displaying those predictions back to the user. But, let’s start at the beginning. During the creation the new project we used the .NET Core option in the left pane of the window and then used the ASP.NET Core Web Application. After filling the name and the location information about our app, we pressed the Next button.
In the next creation window, we used Model-Controler-View (MVC) option, because we want to create this kind of ASP.NET application.
As you probably know, this creates default views and controllers. So, in order to achieve the examples from above, we modified _Layout to remove unnecessary links from the header. Also, we rewrote the Index view from the Home folder completely. Here is what we added there:
Nothing too fancy, just a bunch of input fields. We expect these to be filled with by the user and passed to the controller. Apart from that, we implemented the second, very simple vier – Predict view:
As you can see, we will expect from our controller to fill the ViewData with the Prediction property when this view is returned. Of course for generating this value we will have to use the machine learning model. All of this is handled in the controller, so let’s examine its implementation.
Importing the Model into Controller
You remember how we saved the machine learning model from the previous application into Model.zip file? We have to include that file in our web application. That is why we created new folder MachineLeartningModel and copied over that file there:
Don’t forget to set configure this file in the way that it is always copied over to the bin output folder. Now, what we want to do is import this model into our controller and use it on the input data provided by a user. We can achieve that by using the ReadAsync function of the PredictionModel class. This is done in the constructor of the controller like this:
From this moment on, we can use _machineLearningModel as we have used it in previous applications. HomeController has yet two methods implemented: Index and Predict. The first one is used on application start to display the Index view and to initiate gathering of the information from the user.
Again, nothing too fancy, we are just returning the view in this method. However, once the Submit button from the form on the Index view has been clicked, the HTTP POST request will be issued. That request is processed inside of Predict method on HomeController. Here is how it is implemented:
Input data is gathered and it is used to create a new object of BikeSharingDemandSample class. This class is the same as in the previous application, just like the BikeSharingDemandPrediction class. We had to move these classes over from the machine learning model training application as well. Just as a reminder, here is their implementation:
It is crucial to install the Microsoft.ML NuGet package in this web application as well, so don’t forget on that step when you are building your own app. We already mentioned that we can use the object of the PredictionModel class (_machineLearningModel) just like in the machine learning model training application. That is why we call the Predict method that returns us an object of the BikeSharingDemandPrediction class and we are using this information to fill in the ViewData and return the view. This way we used the model and gave back the information that the user required.
In this post, we examined how to export machine learning model that we created and use it in another application. Of course, for this, we used Microsoft technologies ML.NET and ASP.NET. Training and evaluation of the model can take a lot of time and resources, and that is why these processes are actually separated from the moment that the model is used. In this scenario, we used it in another web ASP.NET application.
However, we could have used it anywhere else not just in simple web applications. Using the same principles of storing and loading the machine learning model form the file we are able to incorporate it into any other workflow we want to. This gives us enormous possibilities of using the machine learning, from big enterprise systems to simpler web apps.
Thank you for reading!
Read more posts from the author at Rubik’s Code.