dotnet / machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
9.05k stars 1.89k forks source link

Error In Blazor When Creating Prediction Engine #5393

Closed johnrickabe closed 4 years ago

johnrickabe commented 4 years ago

I have a problem with Blazor. I tried to do what's said in the documentation about loading existing model from remote source. (https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/save-load-machine-learning-models-ml-net)

This is what I came up with:

@page "/analyzer"
@inject HttpClient _client
<h1>Analyzer</h1>

<input class="form-control" @bind:event="oninput" @bind="InputText"/>

@if (InputText != null)
{
    <h1>@InputText</h1>
}

@code {
    private string _inputText, mlPrediction;
    DataViewSchema modelSchema;
    MLContext mlContext = new MLContext();
    public string InputText
    {
        get
        {
            return _inputText;
        }
        set
        {
            _inputText = value;
            GetPrediction();
        }
    }
    private PredictionEngine<ModelInput, ModelOutput> _predictionEngine;
    protected override async Task OnInitializedAsync()
    {
        Stream modelFile = await _client.GetStreamAsync("<MODEL.ZIP ENDPOINT>");
        ITransformer trainedModel = mlContext.Model.Load(modelFile, out modelSchema);
        _predictionEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(trainedModel);
    }
    private void GetPrediction()
    {
        ModelInput mlInput = new ModelInput();
        mlInput.Sentiment = InputText;
        ModelOutput mlOutput = _predictionEngine.Predict(mlInput);
        mlPrediction = mlOutput.Prediction;
    }
}

When I initialize the page, there was an error, and the only thing I understood is there is an error in line 32: _predictionEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(trainedModel);

Here's the error message:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Exception has been thrown by the target of an invocation.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not load the file 'Microsoft.ML.Transforms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) <0x296c470 + 0x000ce> in <filename unknown>:0 
   --- End of inner exception stack trace ---
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) <0x296c470 + 0x000ce> in <filename unknown>:0 
   --- End of inner exception stack trace ---
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) <0x296c470 + 0x000ce> in <filename unknown>:0 
   --- End of inner exception stack trace ---
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) <0x296c470 + 0x000f6> in <filename unknown>:0 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) <0x296bd98 + 0x00014> in <filename unknown>:0 
  at Microsoft.ML.Runtime.ComponentCatalog+LoadableClassInfo.CreateInstanceCore (System.Object[] ctorArgs) <0x545e428 + 0x000dc> in <filename unknown>:0 
  at Microsoft.ML.Runtime.ComponentCatalog+LoadableClassInfo.CreateInstance (Microsoft.ML.Runtime.IHostEnvironment env, System.Object args, System.Object[] extra) <0x545e070 + 0x000c4> in <filename unknown>:0 
  at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes] (Microsoft.ML.Runtime.IHostEnvironment env, System.Type signatureType, TRes& result, System.String name, System.String options, System.Object[] extra) <0x545d848 + 0x001c8> in <filename unknown>:0 
  at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes,TSig] (Microsoft.ML.Runtime.IHostEnvironment env, TRes& result, System.String name, System.String options, System.Object[] extra) <0x545d488 + 0x00018> in <filename unknown>:0 
  at Microsoft.ML.ModelLoadContext.TryLoadModelCore[TRes,TSig] (Microsoft.ML.Runtime.IHostEnvironment env, TRes& result, System.Object[] extra) <0x5365f30 + 0x00068> in <filename unknown>:0 
  at Microsoft.ML.ModelLoadContext.TryLoadModel[TRes,TSig] (Microsoft.ML.Runtime.IHostEnvironment env, TRes& result, Microsoft.ML.RepositoryReader rep, Microsoft.ML.Repository+Entry ent, System.String dir, System.Object[] extra) <0x535cb38 + 0x00068> in <filename unknown>:0 
  at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig] (Microsoft.ML.Runtime.IHostEnvironment env, TRes& result, Microsoft.ML.RepositoryReader rep, Microsoft.ML.Repository+Entry ent, System.String dir, System.Object[] extra) <0x535c980 + 0x00038> in <filename unknown>:0 
  at Microsoft.ML.ModelLoadContext.LoadModelOrNull[TRes,TSig] (Microsoft.ML.Runtime.IHostEnvironment env, TRes& result, Microsoft.ML.RepositoryReader rep, System.String dir, System.Object[] extra) <0x5351628 + 0x00064> in <filename unknown>:0 
  at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig] (Microsoft.ML.Runtime.IHostEnvironment env, TRes& result, Microsoft.ML.RepositoryReader rep, System.String dir, System.Object[] extra) <0x5351438 + 0x00034> in <filename unknown>:0 
  at Microsoft.ML.ModelOperationsCatalog.Load (System.IO.Stream stream, Microsoft.ML.DataViewSchema& inputSchema) <0x5243828 + 0x000a0> in <filename unknown>:0 
  at Deployments.Pages.SentimentAnalyzer2.OnInitializedAsync () [0x00096] in <PROJECTDIRECTORY>\Pages\SentimentAnalyzer2.razor:32 
  at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2ecb9b0 + 0x0013a> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x31219d8 + 0x000b6> in <filename unknown>:0

It seems like the real problem is: System.IO.FileNotFoundException: Could not load the file 'Microsoft.ML.Transforms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

What workaround can I do to fix this?

I tried getting the model from a cloud host and from wwwroot using GetStreamAsync. Both of their errors has to do with that line.

Lynx1820 commented 4 years ago

Hi @JohnWasTakenWasTaken,

It seems like you're missing that nuget package. Can you make sure you add it? What does your .csproj file look like? Do you have something like <PackageReference Include="Microsoft.ML" Version=1.0.0.0 /> in there?

johnrickabe commented 4 years ago

Yep. I added the Microsoft.ML Nuget Package.

Is ML.NET compatible with Blazor Wasm? I heard Wasm is not 86x or 64x but I can't be so sure.

andersson09 commented 4 years ago

As soon as you upgrade blazer wasm to .net5 ml.net will not work. https://github.com/dotnet/machinelearning/issues/5375

luisquintanilla commented 4 years ago

@andersson09 @JohnWasTakenWasTaken .NET 5 RC1 just released. Do you run into the same issues with that version?

https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-5-release-candidate-1/

johnrickabe commented 4 years ago

@luisquintanilla Sweet. Looks like there's a lot of things for Blazor. I'm gonna try it out.

luisquintanilla commented 4 years ago

@JohnWasTakenWasTaken @andersson09 you can take a look at this project for reference that uses a regression model (SDCAMaximumEntropy). Looks like things work with .NET 5 RC1! ๐Ÿ˜ƒ

andersson09 commented 4 years ago

Hey @luisquintanilla thanks will take a look!

johnrickabe commented 4 years ago

@luisquintanilla image

I made a sentiment analysis model using Imdb dataset with Model Builder and added a reference to it on wasm. Apparently, String doesn't exist now or I'm doing something wrong. ๐Ÿ˜„

It also has errors with types on the generated console app.

EDIT: Fixed by restarting. Might have just bugged.

luisquintanilla commented 4 years ago

๐Ÿ˜† might help if I would've included the project I was referencing.

https://github.com/luisquintanilla/NET5RC1BlazorWASMMLTest

antoniovs1029 commented 4 years ago

I'll close this issue since it seems the problem has been resolved. Thanks, @luisquintanilla for your help ๐Ÿ˜„

Feel free to reopen if you're still facing this problem.