Implementation of Machine Learning in Xamarin.Forms: What You Should Know?

Over the past few years, the trajectory of Artificial Intelligence has changed at a fast pace. It came as a response after companies like Microsoft and Google realized the effectiveness of this revolutionized technology.

Today, we will talk about how to implement Machine Learning (ML) in Xamarin mobile apps. It is an essential component of Artificial Intelligence that helps mobile applications be more intelligent at predicting outcomes. Given its exceptional capabilities, nearly 49% of companies are planning to adopt Machine learning.

If you own Xamarin mobile app, you should know that it is not compatible with Machine Learning. It means Xamarin does not have ML abilities. But do not worry! You can still implement this cutting-edge technology in your mobile app using Xamarin.Forms and ONNX Runtime.

Do you want to boost the capabilities and user interface of your app? If yes, hire a reliable Xamarin app development services provider working with ONNX Runtime. Below, we have explained how to implement ML in Xamarin.Forms using ONNX Runtime. But first, let us learn about this runtime.

  • ONNX Runtime: Everything you need to know

    ONNX Runtime is an open-source tool that ML capabilities across various frameworks, hardware platforms, and operating systems. It has recently added support for Xamarin developers in the 1.10 NuGet package. As a result, they can create iOS and Android ML-enabled mobile apps in C#.

    ONNX, Open Neural Network Exchange Runtime, empowers ML models in several products and services of Microsoft. You can access it across Azure, Office, Bing, and other community projects. If you want to build ONNX models, leverage services like Convert existing models to ONNX format or Azure Custom Vision. With this, you can perform cross-platform on-device inference. It offers numerous perks to your mobile app, such as:

    • Offline availability
    • Ultimate privacy to your device data
    • Faster performance
    • Affordable

    Now, you know what ONNX Runtime is and how does it work. The next step is to understand how to use ONNX Runtime in Xamarin.Forms to ensure on-device inference.

  • On-device inference with ONNX Runtime

    The process is categorized into two classes, such as:

    1. MobileNetImageClassifier
    2. MainPage
  • MobileNetImageClassifier – It involves using the model through ONNX Runtime. Of course, depending on the model documentation. With this, you can experience the representation of the network architecture of the model. It reveals two public methods like GetSampleImageAsync and GetClassificationAsync. Let us look at the step-by-step breakdown of how these methods work.
    • Initialization – The first step of this class is initialization that helps in loading embedded resource files. They represent the labels, model, and sample image. The process uses the asynchronous initialization pattern for two reasons. First, to make its use downstream simple. Plus, to prevent the use of resources before completing initialization. The syntax of the asynchronous initialization is InitAsync. GetSampleImageAsync helps load a sample image conveniently, and GetClassificationAsync executes the inferencing on the image.
    • Preprocessing – In this step, developers transform raw images depending on the model’s requirements. After this, the image data will be stored in an adjacent sequential block memory. The process is characterized by a Tensor object. It involves two steps:
      • Resizing the original image to height and width at least 224.
      • Normalizing the pixels of a result image and storing it in a flat array. You can use this to craft the Tensor object.
    • Inference – The ONNX model uses an InferenceSession as the runtime representation. With this input, the model results in the computed output values. When it comes to the input and output values, they are a group of NamedOnnxValue objects.
    • Postprocessing – The resulted outputs have a score for every classification. The code settles the Tensor by name to ensure simplicity. Consequently, it obtains the highest score value. After this, developers will use an equivalent label item for the GetClassificationAsync method as a return value.
  • MainPage – The MainPage XAML uses a single Button to run the inference. All this is done through the MobileNetImageClassifier. It helps developers solve the sample image. Before displaying the final result, it passes through the GetClassificationAsync.

You just learned basic inference with ONNX Runtime while using the default options. You can also optimize the ONNX Runtime documentation in several ways listed below. Please remember, it is beneficial for mobile devices.

  • Optimization tips for ONNX Runtime documentation
    • Reusing InferenceSession objects – The best way to boost inference speed is by reusing InferenceSession throughout multiple inference runs. It helps you avoid unwanted allocation overhead.
    • Creating values using an existing array or directly on Tensor – Numerous ways create the Tensor object before setting values on it directly. It is a convenient and hassle-free method because it does not involve performing offset calculations. But if speed is your priority, prepare a primary array followed by creating the tensor object.
    • Leverage different Execution Providers – ONNX Runtime comes with CPU Execution Provider by default. It uses them to execute models. For example, it uses the Core ML EP (iOS) and the NNAPI EP (Android) when it comes to ORT format models. To ensure better performance for your model, test it with and without EPs.

    In closing

    By following these steps, you can use ONNX Runtime to implement Machine learning in Xamarin.Forms apps. With this, you can perform cross-platform on-device inference, which offers countless advantages. If you want to incorporate ML into your mobile app, hire a Xamarin app development company experienced in ONNX Runtime.

    If you are looking for a reliable Xamarin mobile app development company, get in touch with SoftProdigy. We house a team of Xamarin experts who stay abreast with the latest updates and trends in the field.



    1. What makes Xamarin an ideal choice for app development?

    Xamarin comes with numerous features and development capabilities. However, many enterprises choose it because of its code-sharing ability. It offers 60% to 95% of reusable code, along with Native performance, UI, and controls.

    2. What is the future of Xamarin?

    Xamarin has a bright future. The core team is continuously working to improve it. In November 2022, it will have a new API “.NET MAUI,” which will further enhance the capabilities of Xamarin.

    3. What is the difference between Xamarin and Xamarin.Forms?

    Most people live with this misconception that Xamarin and Xamarin.Forms are the same. But this is not the truth. Xamarin is a cross-platform app development tool. On the other hand, Xamari.Forms is a UI toolkit.

    4. Is Xamarin still a popular cross-platform tool?

    Xamarin is one of the oldest frameworks available in the market. However, it is still popular with a large user base across the globe.

    5. Is it easy to find a Xamarin developer?

    Yes, you can hire a Xamarin developer easily and quickly. Currently, there are nearly 1.6 million Xamarin developers across the globe. Hence, you choose a developer that meets your needs and budget.

    6. Which is faster between Xamarin and React Native?

    When it comes to Xamarin and React Native, both offer native-like performance. But in comparison to React Native, Xamarin runs the code faster.