Skip to content

When an object detection model created with ML.net Model builder is run on Maui (iOS), the model fails to load. #7460

@ysanada12

Description

@ysanada12

Description: I have created a machine learning model for object detection using ML.net Model Builder (MLModel1.zip). The model was successfully tested using the automatically generated project by ML.net Model Builder.

I then added the model as an embedded resource in a .NET MAUI project and implemented the model loading. Below is the source code:

        public static readonly Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(() => CreatePredictEngine(), true);

        private static PredictionEngine<ModelInput, ModelOutput>CreatePredictEngine()
        {
            var mlContext = new MLContext();
            ITransformer mlModel = LoadModel();
            return mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
        }
        public static ITransformer LoadModel()
        {
            var assembly = typeof(MLModel1).GetTypeInfo().Assembly;
            System.IO.Stream stream = assembly.GetManifestResourceStream("MauiProject.MLModel1.zip");

            if (stream == null)
            {
                throw new FileNotFoundException("Resource not found");
            }

            using (var fileStream = new MemoryStream())
            {
                stream.CopyTo(fileStream);
                fileStream.Position = 0;

                var mlContext = new MLContext();
                return mlContext.Model.Load(fileStream, out _);
            }
        }

If you build this code in the Ios simulator, you will get an error that libtorch-cpu-any does not exist when you reach mlContext.Model.Load.
libtorch-cpuのパッケージを確認したところ、この名称のdllが存在しないようです。
https://www.nuget.org/packages?q=libtorch-cpu

Error Message
This application or script uses TorchSharp but doesn't contain a reference to libtorch-cpu-any, Version=2.2.1.1.

Consider referencing one of the combination packages TorchSharp-cpu, TorchSharp-cuda-linux, TorchSharp-cuda-windows or call System.Runtime.InteropServices.NativeLibrary.Load(path-to-libLibTorchSharp.so) explicitly for a Python install of pytorch. See https://github.com/dotnet/TorchSharp/issues/169.".

For CUDA, you may need to call 'TorchSharp.torch.InitializeDeviceType(TorchSharp.DeviceType.CUDA)' before any use of TorchSharp CUDA packages from scripts or notebooks.

Trace from LoadNativeBackend:

TorchSharp: LoadNativeBackend: Initialising native backend, useCudaBackend = False

Step 1 - First try regular load of native libtorch binaries.

    Trying to load native component torch_cpu relative to /Users/devadmin/Library/Developer/CoreSimulator/Devices/2027A329-FB06-4053-AB75-3F1DB97EE874/data/Containers/Bundle/Application/74E75385-2642-4E2C-8105-E9C1107CECCF/calouselTest.app/TorchSharp.dll
    Failed to load native component torch_cpu relative to /Users/devadmin/Library/Developer/CoreSimulator/Devices/2027A329-FB06-4053-AB75-3F1DB97EE874/data/Containers/Bundle/Application/74E75385-2642-4E2C-8105-E9C1107CECCF/calouselTest.app/TorchSharp.dll
    Trying to load native component LibTorchSharp relative to /Users/devadmin/Library/Developer/CoreSimulator/Devices/2027A329-FB06-4053-AB75-3F1DB97EE874/data/Containers/Bundle/Application/74E75385-2642-4E2C-8105-E9C1107CECCF/calouselTest.app/TorchSharp.dll
    Failed to load native component LibTorchSharp relative to /Users/devadmin/Library/Developer/CoreSimulator/Devices/2027A329-FB06-4053-AB75-3F1DB97EE874/data/Containers/Bundle/Application/74E75385-2642-4E2C-8105-E9C1107CECCF/calouselTest.app/TorchSharp.dll
    Result from regular native load of LibTorchSharp is False

Step 3 - Alternative load from consolidated directory of native binaries from nuget packages

    torchsharpLoc = /Users/devadmin/Library/Developer/CoreSimulator/Devices/2027A329-FB06-4053-AB75-3F1DB97EE874/data/Containers/Bundle/Application/74E75385-2642-4E2C-8105-E9C1107CECCF/calouselTest.app
    packagesDir = /Users/devadmin/Library/Developer/CoreSimulator/Devices/2027A329-FB06-4053-AB75-3F1DB97EE874/data/Containers
    torchsharpHome = /Users/devadmin/Library/Developer/CoreSimulator/Devices/2027A329-FB06-4053-AB75-3F1DB97EE874/data/Containers/Bundle/Application
    Giving up, TorchSharp.dll does not appear to have been loaded from package directories
Could anyone help me resolve this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    untriagedNew issue has not been triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions