Onnxruntime does not work on real IOS device

104 Views Asked by At

I have aplication in .net Maui where im trying to use onnx model.

All is ok on android real device and iOS simulator (made on macos with intel procesor) but when I try to start InferenceSession app is beeing termined.

thats code Im using

using Microsoft.ML.OnnxRuntime;

namespace Test {

public partial class MainPage : ContentPage
{
    int count = 0;


    List<string> classNames;
    InferenceSession session;
    Task initTask;
    byte[] model;



    public MainPage()
    {
        InitializeComponent();
    }

    private void OnCounterClicked(object sender, EventArgs e)
    {
        InitTask();
        classNames = LoadLabels();
    }


    public void InitTask()
    {
        var assembly = GetType().Assembly;
        var resourceNames = assembly.GetManifestResourceNames();
        foreach (var resourceName in resourceNames)
        {
            Console.WriteLine(resourceName);
        }

        var modelStream = assembly.GetManifestResourceStream("Test.model2.onnx");
        var modelMemoryStream = new MemoryStream();

        modelStream.CopyTo(modelMemoryStream);
        model = modelMemoryStream.ToArray();

        try
        {
            session = new InferenceSession(model: model);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error creating InferenceSession: " + ex.Message);
            Console.WriteLine("Stack Trace: " + ex.StackTrace);
            if (ex.InnerException != null)
            {
                Console.WriteLine("Inner Exception: " + ex.InnerException.Message);
                Console.WriteLine("Inner Stack Trace: " + ex.InnerException.StackTrace);
            }
        }
    }
    private List<string> LoadLabels()
    {
        using var stream = FileSystem.OpenAppPackageFileAsync("cclabel.txt").Result;
        using var reader = new StreamReader(stream);

        List<string> labels = new List<string>();
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            labels.Add(line);
        }
        return labels;
    }
}

}

and here ale logs

2023-11-24 12:43:50.712 Xamarin.PreBuilt.iOS[4423:2205361] Error creating InferenceSession: The type initializer for 'Microsoft.ML.OnnxRuntime.NativeMethods' threw an exception.

2023-11-24 12:43:50.716 Xamarin.PreBuilt.iOS[4423:2205361] Inner Stack Trace: at Microsoft.ML.OnnxRuntime.NativeMethods..cctor()

2023-11-24 12:43:50.716 Xamarin.PreBuilt.iOS[4423:2205361] Inner Exception: __Internal

2023-11-24 12:43:50.715 Xamarin.PreBuilt.iOS[4423:2205361] Stack Trace: at Microsoft.ML.OnnxRuntime.SessionOptions..ctor() at Microsoft.ML.OnnxRuntime.InferenceSession..ctor(Byte[] model) at Test.MainPage.InitTask() in D:\Git\Test\MainPage.xaml.cs:line 56

The app has been terminated.

I tried few models with different size and type. Tried to load model on few way also. Tried few vesions of onnxruntime.

0

There are 0 best solutions below