0

Hello i am trying to make the "is this a hot dog" app from silicon valley using ML.net image classification.

I am working in a Maui app and already got a trained model which i put the zipfile in the app and the model.consumption file.

I already got code for taking a picture.

private async void OnTakePhotoClicked(object sender, EventArgs e)
{
    var options = new StoreCameraMediaOptions();
    var result = await CrossMedia.Current.TakePhotoAsync(options);
    if (result is null) return;

    UploadedOrSelectedImage.Source = ImageSource.FromStream(() => result.GetStream());

    using var stream = result.GetStream();
    using var memoryStream = new MemoryStream();
    await stream.CopyToAsync(memoryStream);
    var imageBytes = memoryStream.ToArray();


    var modelInput = new MLModel2.ModelInput()
    {
        Label = "Image",
        ImageSource = imageBytes
    };

    var prediction = MLModel2.Predict(modelInput);

    ResultLabel.Text = prediction.PredictedLabel;
}

But it seems to always break when trying to find the zip file and trying to load the model.

    private static async Task<PredictionEngine<ModelInput, ModelOutput>> CreatePredictEngine()
    {
        const string filePath = "MLModel2.zip";
        using var stream = await FileSystem.OpenAppPackageFileAsync(filePath);
        var mlContext = new MLContext();
        ITransformer mlModel = mlContext.Model.Load(stream, out var _);
        return mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);

    }

i already tried different file paths, but none seems to work.

Jason
  • 86,222
  • 15
  • 131
  • 146
geralt420
  • 3
  • 1
  • "seems to always break " - what exactly does this mean? Do you get an error or exception? Where exactly are you putting the zip file and what is the build action? – Jason Aug 19 '23 at 21:28
  • Hey, i have put the zip file in the raw folder in the resources map, as for the exception i get this : System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.' Build action i got MAUIAsset, i have also tried with content – geralt420 Aug 19 '23 at 21:31
  • what is the InnerException? – Jason Aug 19 '23 at 21:35
  • It doesnt seem to show an innerexception, , it just goes to break mode no compatible code running on the thread, but i dont get an error – geralt420 Aug 19 '23 at 21:40
  • add some exception handling – Jason Aug 19 '23 at 21:47
  • i wrote a try catch but it seems it even then it doenst show an exception – geralt420 Aug 19 '23 at 21:55
  • after some more testing i saw this in the out put [ion] ioctl c0044901 failed with code -1: Invalid argument **System.NullReferenceException:** 'Object reference not set to an instance of an object.' – geralt420 Aug 19 '23 at 22:01
  • Have you set breakpoint, and stepped through code? Which line fails? Which value is null? This is basic debugging. – ToolmakerSteve Aug 19 '23 at 23:13
  • it seems to fail here ITransformer mlModel = mlContext.Model.Load(stream, out var _); – geralt420 Aug 19 '23 at 23:43
  • Is stream null? – Jason Aug 20 '23 at 00:05
  • stream is not null yet it goes into the catch with this exception Error loading ML.NET model: Specified method is not supported. – geralt420 Aug 20 '23 at 00:26

1 Answers1

0

Cant access ML model in my MAUI project ML.net

About it, you can see this case: Microsoft.ML and Xamarin. The answer said:

Unfortunately, ML.NET doesn't work directly in Xamarin just yet. I believe you would have to wait until MAUI comes out to be able to do that.

But MAUI does not seem to support ML.NET very well so far. I searched something about MAUI and ML.Net and found this on GitHub: ML.Net in MAUI. You can follow up.

In Addition, I also found this session: ML.NET: Machine learning from data to production in less than 30 minutes. It involves MAUI and ML.NET. Wish it can help you.

Jianwei Sun - MSFT
  • 2,289
  • 1
  • 3
  • 7