Visual studio Azure(C#)System.IO.FileNotFoundException:找不到文件
我已经用VisualStudio创建了一个ML模型。我也用Visual Studio将web应用程序上载到Azure。然而,当我填写我的ML模型的字段并在网站上单击“运行”时,我得到了这个错误,我直接从Azure应用程序服务编辑器复制了这个错误 我只是在尝试在Azure网站上运行ML模型时出现此错误,如果我在我的计算机上运行web应用程序,我一点错误都没有 谢谢:)Visual studio Azure(C#)System.IO.FileNotFoundException:找不到文件,visual-studio,azure,machine-learning,error-handling,azure-web-app-service,Visual Studio,Azure,Machine Learning,Error Handling,Azure Web App Service,我已经用VisualStudio创建了一个ML模型。我也用Visual Studio将web应用程序上载到Azure。然而,当我填写我的ML模型的字段并在网站上单击“运行”时,我得到了这个错误,我直接从Azure应用程序服务编辑器复制了这个错误 我只是在尝试在Azure网站上运行ML模型时出现此错误,如果我在我的计算机上运行web应用程序,我一点错误都没有 谢谢:) 错误: 2020-07-18 01:12:59.138+00:00[错误]Microsoft.AspNetCore.Diagnos
错误:
2020-07-18 01:12:59.138+00:00[错误]Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware:执行请求时发生未经处理的异常。
System.IO.FileNotFoundException:找不到文件“C:\Users\X\X\fileML.Model\MLModel.zip”。
文件名:“C:\Users\X\X\fileML.Model\MLModel.zip”
____________________
我的代码:
//此文件由ML.NET模型生成器自动生成。
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用Microsoft.ML;
使用fileML.Model;
名称空间fileML.Model
{
公共类消费模型
{
private static readonly Lazy PredictionEngine=new Lazy(CreatePredictionEngine);
//有关使用ML.NET模型的更多信息,请访问https://aka.ms/mlnet-consume
//在应用程序中使用模型的方法
公共静态模型输出预测(模型输入)
{
ModelOutput结果=PredictionEngine.Value.Predict(输入);
返回结果;
}
公共静态PredictionEngine CreatePredictionEngine()
{
//创建新的MLContext
MLContext MLContext=新的MLContext();
//负载模型和创建预测引擎
字符串modelPath=@“C:\Users\X\X\fileML.Model\MLModel.zip”;
ITransformer mlModel=mlContext.Model.Load(modelPath,out u);
var predEngine=mlContext.Model.CreatePredictionEngine(mlModel);
返回引擎;
}
}
}
您正试图从C:\Users\X\X\X\fileML.Model加载文件MLModel.zip。这就是您的本地计算机路径。Azure Web应用程序中不存在该路径
如果确实要存储在本地目录中,有两种方法:
您正试图从C:\Users\X\X\X\fileML.Model加载文件MLModel.zip。这就是您的本地计算机路径。Azure Web应用程序中不存在该路径 如果确实要存储在本地目录中,有两种方法:
Nathan,欢迎来到stackoverflow。
这里是您缺少的东西:
您正试图从您的计算机访问本地路径,但Azure上没有本地计算机,因此每当您编写代码试图访问硬编码的同一路径时,都会导致错误。
我的建议是将zip文件添加到项目中,一旦添加,右键单击该文件并标记Copy to Output Directory-Copy always
请参见下文 这将有助于从输出目录获取本地文件路径。 现在是时候更改代码以动态获取文件了
您可以使用
string directoryPath = Directory.GetCurrentDirectory();
string modelPath= Path.Combine(directoryPath ,"MLModel.zip");
这将获得文件路径。只需在本地服务器上测试代码并部署应用程序好的是,现在模型文件将与代码一起部署。每次更改模型时,只需替换文件并再次部署代码。
提示使其更具动态性:-您还可以使用Azure Blob存储来保存zip文件,通过使用它,您无需反复部署代码。只需替换侧块中的文件。内森,欢迎来到stackoverflow。
这里是您缺少的东西:
public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
{
// Create new MLContext
MLContext mlContext = new MLContext();
// Load model & create prediction engine
string directoryPath = Directory.GetCurrentDirectory();
string modelPath = Path.Combine(@"C:\Users\Admin\source\repos\ShanuASPMLNETML.Model\MLModel.zip","MLModel.zip");
ITransformer mlModel = mlContext.Model.Load(modelPath, out _);
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
return predEngine;
}
}
您正试图从您的计算机访问本地路径,但Azure上没有本地计算机,因此每当您编写代码试图访问硬编码的同一路径时,都会导致错误。我的建议是将zip文件添加到项目中,一旦添加,右键单击该文件并标记Copy to Output Directory-Copy always
请参见下文 这将有助于从输出目录获取本地文件路径。 现在是时候更改代码以动态获取文件了
您可以使用
string directoryPath = Directory.GetCurrentDirectory();
string modelPath= Path.Combine(directoryPath ,"MLModel.zip");
这将获得文件路径。只需在本地服务器上测试代码并部署应用程序好的是,现在模型文件将与代码一起部署。每次更改模型时,只需替换文件并再次部署代码。
提示使其更具动态性:-您还可以使用Azure Blob存储来保存zip文件,通过使用它,您无需反复部署代码。只需替换side blob中的文件。
public static PredictionEngine CreatePredictionEngine()
public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
{
// Create new MLContext
MLContext mlContext = new MLContext();
// Load model & create prediction engine
string directoryPath = Directory.GetCurrentDirectory();
string modelPath = Path.Combine(@"C:\Users\Admin\source\repos\ShanuASPMLNETML.Model\MLModel.zip","MLModel.zip");
ITransformer mlModel = mlContext.Model.Load(modelPath, out _);
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
return predEngine;
}
}
{
//创建新的MLContext
MLContext MLContext=新的MLContext();
//负载模型和创建预测引擎
字符串directoryPath=Directory.GetCurrentDirectory();
字符串modelPath=Path.Combine(@“C:\Users\Admin\source\repos\ShanuASPMLNETML.Model\MLModel.zip”,“MLModel.zip”);
ITransformer mlModel=mlContext.Model.Load(modelPath,out u);
var predEngine=mlContext.Model.CreatePredictionEngine(mlModel);
返回引擎;
}
}
公共静态预测引擎CreatePredictionEngine()
{
//创建新的MLContext
毫升