Introduction to ONNX

04 October 2021 at 10:00 by ParTech Media - Post a comment

Let’s time travel back to the 20th century where there was a massive growth in the oil and gas industry. During this time, the development of many other industries was driven by it.

In this 21st century, a new field is slowly replicating the success of oil and gas and many other industries are largely dependent on it. Yes, we are talking about the famous data industry.

The data industry involves a wide array of important activities such as -

  • Collecting data from various sources
  • Cleaning and filtering out the required data
  • Applying machine learning algorithms to create and train a model
  • Use it to find insights and make decisions out of it.

All these processes apply to various industries with their own set of use cases. For example, it can be applied in the manufacturing industry for collecting data from the plant and finding out the ways to improve efficiency. Similarly, in the banking industry, you can use it to detect fraudulent transactions.

In the entire process of finding insights from raw data, creating and training models are key steps and there are popular machine learning frameworks available to achieve the same. But there will also be cases where there arises a need to switch between frameworks to get better insights. In both these cases, the ONNX framework comes in handy.

In this post, let's see in detail everything about ONNX and how to use it with .Net.

Table of contents

  1. Introduction to ONNX
  2. Implementing ONNX with .Net
  3. Conclusion

Introduction to ONNX

ONNX (Open Neural Network Exchange) is an intermediary framework used for converting models between different machine learning frameworks. It is developed by Microsoft, Facebook, and AWS. It gives the ability to use the same model across different platforms. In other words, it enables the exchange of models between different frameworks. It is a high-performance inference engine for both machine learning and deep neural networks. This allows the users to develop a model using Scikit in Python and then import the resulting model in the C#.

It also gives us the option to develop the models with desired frameworks without worrying about compatibility with the downstream platforms and frameworks. When using ONNX, users can train a model with one framework, then export the model in ONNX format, which is a serialized representation of the model. And later on, using the ML.NET, it can be imported into the .Net application.

ML.Net runs on both .Net Core and .Net frameworks. Though its 64-bit version supports all frameworks, the 32-bit does not support the ONNX related functionalities.

Implementing ONNX with .Net

In this section, we are going to implement the ONNX runtime in the C# application. We are then going to export it to an ONNX model from the raw data.

To achieve this task, we are first going to create a console-based application (by choosing the project template) with .Net 5 and providing a valid name for the solution.

The solution gets created with the following code structure.

Before we get into the code, let's import the packages that are required for the execution.

To install NuGet packages, right-click the project and click on Manage NuGet packages. Then go to the browse tab and look for the packages below and install them.

  • Microsoft.ML
  • Microsoft.ML.OnnxConverter
  • Microsoft.ML.OnnxTransformer

MLContext, training the test data are part of Microsoft.ML header. OnnxConverter is used to convert the model to an ONNX format file. The OnnxTransformer is used to create estimations based on the exported model, which is later imported into the code.

Once the packages are installed, go to the Program.cs file to implement the code that needs to do the model export. Below are some of the ‘using’ statements that will be necessary to export the ONNX model -

using Microsoft.ML;

using Microsoft.ML.Data;

using System.IO;

namespace PARTECH_ONNX

  class Program
​    private static readonly string MODEL_NAME = "partech_model.onnx";
​    static void Main(string[] args)
​    {
​      var context = new MLContext();
​      var textLoader = context.Data.CreateTextLoader(new[]
​      {
​        new TextLoader.Column("LaptopBrand", DataKind.Single, 0),
​        new TextLoader.Column("YearsOfUsage", DataKind.Single, 1),
​      },

​      hasHeader: true,
​      separatorChar: ',');

​      var data = textLoader.Load("./LaptopData.csv"); 
​      var trainTestData = context.Data.TrainTestSplit(data);
​      var pipeline = context.Transforms.Concatenate("Features", "LaptopBrand")

​        .Append(context.Regression.Trainers.Sdca(labelColumnName: "YearsOfUsage"));

​      ITransformer model = pipeline.Fit(trainTestData.TrainSet);

​      using (FileStream stream = File.Create(MODEL_NAME))

​      {
​        context.Model.ConvertToOnnx(model, data, stream);
​      }

​      var estimator = context.Transforms.ApplyOnnxModel(MODEL_NAME);
​      var newModel = estimator.Fit(trainTestData.TestSet);

​    }



The above code takes a CSV file as input which has the sample data for a laptop brand and the number of years it works without any major issues. The data is in the below format -


1, 2.5

2, 2.6

1, 5

1, 6

…… and so on.

This data is now imported into the code and the model has been trained using the test data. The model is then converted to an ONNX model and saved in the local storage. And with the exported ONNX model, the estimation is done.

Similarly, the ONNX model can be used for image processing. And just like the above conversion, ONNX models can be used to convert a wide range of ML frameworks which can then later be imported into .Net code. Thus, the required functionalities can be achieved and vice versa is also possible. The partech_model.onnx can be imported into any other ML framework and predictions can be made.


ML frameworks play a crucial role in testing and predicting the data by creating models. But, when users need multiple ML frameworks to be used for prediction, then ONNX acts as the bridge between the ML frameworks.