How to use Scikit-learn models within .NET Core

HOW TO USE SCIKIT-LEARN MODELS WITHIN .NET CORE

All the time appears new possibilities of deploying machine learning algorithms into the .NET environment. It can be quite tough to choose a certain way, especially for indecisive guys. 🙂 Unfortunately, I will make it even harder, because I would like to introduce you to ONNX. What is it? How it can be used within Scikit-learn models and .NET Core apps? You will find answers to those questions below.

ONNX

Let’s start with the explanation of ONXX – Open Neural Network Exchange. It is an open-source ecosystem that is used for establishing artificial intelligence algorithms. Thanks to this tool, we might convert our trained models between different machine learning frameworks or use those models in some devices, as you can see in the graphic. The authors, while creating the solution, had a beautiful mission to promote innovation and cooperation in the AI sector.

Example usage

I used the breast cancer dataset from UCI Repository. The analyzed dataset has 9 features and 2 classes. Classes indicate the stage of cancer (benign or malignant). For the experiment, I choose the Naive Bayes classifier. You might find some theory about that in another article.

Implementation

First of all, you need to install the skl2onnx library for Python:

pip install skl2onnx

After that, you can go to IDE (i.e. PyCharm) and add references for the mentioned library in your project (more precisely the method to convert the model from Scikit-learn and the type of our data):

from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType

Let me skip the code responsible for loading the dataset and learning the Naive Bayes algorithm. You can find it in my other blog post as well. Instead of that, let’s focus on the code that will create the ONNX format object from our model:

As you can see, the converting method has two arguments: a machine learning model and a type of that object (in our case there are NB (our Naive Bayes model) and shape of input where 9 equals the number of features).

Our ONNX file is ready, so it’s high time to use it! In our case, it will be a simple console application. It is worth taking a look at the structure of our ONNX model. We can do this in two ways:

  • Using ‘dotnet onnx info‘ at any command line:
  • Using Netron (it is a tool for inspecting your model’s inputs and outputs):

Of course, after creating your console app, remember about installing OnnxRuntime from NuGet Packages:

Now catch a glimpse of the code where all the “magic” happens and we are using the ONNX model we created earlier:

The first line is just about opening a session using the InterfaceSession class and passing in the file path to the model as a parameter.
Afterwards, we are preparing our input value (I know it could be done in a better way 😉 ), which means the next elements correspond to the features of our model.
Then we can execute queries using the Run method of the InterfaceSession object. This method gives us two important pieces of information: the predicted label(1) and the probability for each class(2).
In our case, we can consider that the patient with the given features has benign breast cancer and this result has a probability close to 1.

(1)
(2)

Summary

As you can see in this short introduction, ONNX is a great tool. Freedom in deploying various models in many devices or other frameworks is a big step in the development of systems based on AI & ML. I discovered this library relatively recently and I immediately thought that would like to share it with you. Was that a good idea? 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top