Consume machine learning models
After you train a machine learning model, it's time to consume it so that you can make predictions.
ML.NET models are serialized and saved to a file. You can load the model file into any .NET application and use it make predictions through ML.NET APIs.
Model Builder makes it easy for you to consume your model in existing applications by using code snippets and in new applications by using templates.
Code snippet
If you have an existing application where you want to use your model to make predictions, a code snippet simplifies the process. To use a code snippet in your application:
- Add a reference to the class library that contains your model from the project where you'll use the model.
- Add the code snippet to your application.
The code snippet creates an instance of your model input. It then calls the Predict
method in the .consumption.cs file to make predictions by using the input data that you've provided.
Project templates
Like the training and consumption code that's autogenerated during the training process, Model Builder provides the following templates to autogenerate new .NET applications to consume your model.
Console application
The console application template is a C# .NET application that uses your model to make predictions. It contains the following files:
- Program.cs: The entry point of your application. Similar to the code snippet, this file creates an instance of your model's input, uses the
Predict
method in the .consumption.cs file, and displays the result in the console. - <MODEL-NAME>.mbconfig: The .mbconfig file for your model and generated training and consumption code. These files are copied over from the class library project where you originally added the machine learning project.
Web API
The Web API template is an ASP.NET Core project built via the ASP.NET Minimal API application model to simplify hosting your model as a web service. Web APIs give you the flexibility of making predictions with your model over HTTP web requests from various clients, like desktop, web, and mobile applications.
The Web API template contains the following files:
Program.cs: The entry point of your application. In this file, your application configures the
PredictionEnginePool
service by using dependency injection, defines a single/predict
endpoint, and starts your application to listen for incoming HTTP requests.As part of the
predict
endpoint definition, a handler is also defined. The handler uses thePredictionEnginePool
service to make predictions on incoming JSON requests that contain your model input data. The handler then returns the results of those predictions back to the client.<MODEL-NAME>.mbconfig: The .mbconfig file for your model and generated training and consumption code. These files are copied over from the class library project where you originally added the machine learning project.
Important
The Web API project does not use the Predict
method in the .consumption.cs file. Instead, it registers PredictionEnginePool
as a service by using dependency injection. PredictionEngine
is not thread-safe. You also have to create an instance of it everywhere it's needed within your application. As your application grows, this process can become unmanageable.
For improved performance and thread safety, use a combination of dependency injection and the PredictionEnginePool
service, which creates an ObjectPool
of PredictionEngine
objects for use throughout your application.
To learn more about dependency injection, see Dependency injection in ASP.NET Core.
In the next unit, you'll consume the model that you trained to predict machine failures in a .NET console application.