dotnet / dotnet/machinelearning

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

Open
#7,460 1 comment 0 reactions 0 assignees View on GitHub
area-TorchSharp untriaged
Dominant language
C#
Stars
9.4k
Forks
2k
Avg merge
2d 20h
Merged PRs (30d)
11

Description

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:

```csharp
public static readonly Lazy> PredictEngine = new Lazy>(() => CreatePredictEngine(), true);

private static PredictionEngineCreatePredictEngine()
{
var mlContext = new MLContext();
ITransformer mlModel = LoadModel();
return mlContext.Model.CreatePredictionEngine(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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.