获取 ML NET 模型 zip 文件流时 Blazor 出错
Error In Blazor When Getting Stream of ML NET Model zip file
我对 Blazor 有疑问。
我尝试按照文档中关于从远程源加载现有模型的内容进行操作。
(https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/save-load-machine-learning-models-ml-net)
这是我想出的:
@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;
}
}
我初始化页面的时候出错了,我唯一理解的是第32行出错,
_predictionEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(trainedModel);
我找不到有同样问题的人。
这是错误代码:
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
我真的不知道该怎么办。
我用过:
- ASP.NET 核心 3.1
- ML.NET1.5
- .NET 标准 2.1
我发现有人似乎在类似类型的项目上取得了成功,我们的程序在结构上是相同的:https://www.luisquintanilla.me/2020/03/01/deploy-machine-learning-mlnet-models-blazor-webassembly
我在 DigitalOcean 和 wwwroot 中都托管了我的 ML 模型,但它有同样的错误。
如果这是一个 Blazor WASM 应用程序,那么不幸的是 ML NET 是不兼容的。 ML NET 需要 WebAssembly 中不存在的 x86。因此,您必须在服务器上处理您的 ML 模型并通过 HTTP 将结果发送到客户端。
从 .NET 5 RC1(.NET 5 或 .NET 6)开始,您现在可以在客户端 Blazor WASM 应用程序中托管 ML.NET 内存中的机器学习模型。
我的 link Blazor 服务器和 WASM 演示 ML.NET:
https://github.com/bartczernicki/Blazor-MachineIntelligence
我对 Blazor 有疑问。 我尝试按照文档中关于从远程源加载现有模型的内容进行操作。 (https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/save-load-machine-learning-models-ml-net)
这是我想出的:
@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;
}
}
我初始化页面的时候出错了,我唯一理解的是第32行出错,
_predictionEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(trainedModel);
我找不到有同样问题的人。 这是错误代码:
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
我真的不知道该怎么办。
我用过:
- ASP.NET 核心 3.1
- ML.NET1.5
- .NET 标准 2.1
我发现有人似乎在类似类型的项目上取得了成功,我们的程序在结构上是相同的:https://www.luisquintanilla.me/2020/03/01/deploy-machine-learning-mlnet-models-blazor-webassembly
我在 DigitalOcean 和 wwwroot 中都托管了我的 ML 模型,但它有同样的错误。
如果这是一个 Blazor WASM 应用程序,那么不幸的是 ML NET 是不兼容的。 ML NET 需要 WebAssembly 中不存在的 x86。因此,您必须在服务器上处理您的 ML 模型并通过 HTTP 将结果发送到客户端。
从 .NET 5 RC1(.NET 5 或 .NET 6)开始,您现在可以在客户端 Blazor WASM 应用程序中托管 ML.NET 内存中的机器学习模型。
我的 link Blazor 服务器和 WASM 演示 ML.NET: https://github.com/bartczernicki/Blazor-MachineIntelligence