If you trained your model in a Python environment and it provides satisfying results – Congratulations! But what if you need to provide it to a .NET application? Let’s say an existing .NET application should be enhanced with an AI functionality that you created in Python.
The solution which we used for the model trained with Pytorch (Resnet34) is to export it to a ONNX format. ONNX comes from Open Neural Network Exchange.
The code in Python below saves the trained model as a .PT file (which can be later re-loaded in Python) and as a ONNX format (which can be exported and used in .NET):
If you had a model previously trained and saved in a .PT file then you could use this code:
Now on the .NET side Microsoft comes with a ML library to handle ONNX models, so using Nuget install Microsoft.ML.OnnxRuntime. As a target framework for the project use at least .NET Standard 2.0.
The code in C# looks like below:
1. It’s a good idea from the performance perspective to load the trained model once and keep it as long as we want to use it, because its initialization is a long process:
2. Assuming that we prepared an input tensor to be recognized in a variable named input we can now run the inference (in our case make the trained model recognize the image):
Be sure to name the input correctly. It should be the same as provided in the input_names parameter when exporting from Python. You can also check it using https://netron.app/ for your saved model; you will see it just at the beginning:
For other models it could look different, for example:
You can utilize 2 environments: Python – which could be the most practical to train a model,
and .NET – which could be desirable if you have an existing system where you want
to use such a model for predictions using the ONNX format.