简体   繁体   English

出现错误'System.IO.FileNotFoundException'

[英]Getting error 'System.IO.FileNotFoundException'

I am trying to follow this MLtutorial and trying to deploy ML model with asp.net core web app but when I run it I am getting this error我正在尝试遵循此MLtutorial并尝试使用 asp.net 核心 web 应用程序部署 ML model 但是当我运行它时出现此错误

An exception of type 'System.IO.FileNotFoundException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Could not find file '/Users/a10.12/Price Prediction/PricePrediction/..\SampleRegression\SampleRegression.Model\MLModel.zip'.'

my controller is this我的 controller 是这个

using Microsoft.AspNetCore.Mvc;  
using Microsoft.ML;  
using SampleRegression.Model;  

 // 
namespace Price_Prediction.Controllers  
{  
    public class PredictionController : Controller  
    {  
        public IActionResult Price(ModelInput input)  
        {  
            // Load the model  
            MLContext mlContext = new MLContext();  
            // Create predection engine related to the loaded train model  
            ITransformer mlModel = mlContext.Model.Load(@"..\SampleRegression\SampleRegression.Model\MLModel.zip", out var modelInputSchema);  
            var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel); 



        // Try model on sample data to predict fair price  
        ModelOutput result = predEngine.Predict(input);  

        ViewBag.Price = result.Score;  
        return View(input);  
    }  
}  

} }

and my project structure looks like this:我的项目结构如下所示:

PRICE PREDICTION
>.vscode
>PricePrediction
    >bin
    >Controllers
        PredictionController.cs
    >Models
    >obj
    >Properties
    >Views
       >Predicion
            Price.cshtml
        # other default files  
    >wwwroot
    # other default files   
>SampleRegression
     >SampleRegression.Console
     >SampleRegression.Model
        MLModel.zip
tax-fare-test.csv (dataset)

First I thought its because of permission issue and tried to run it as an admin but still getting the same error and also tried to change the controller like following首先,我认为这是因为权限问题并尝试以管理员身份运行它,但仍然遇到相同的错误,并且还尝试更改 controller,如下所示

(@"..\SampleRegression.Model\MLModel.zip", out var modelInputSchema);

but still no result, what am I doing wrong?但仍然没有结果,我做错了什么?

UPDATE: I tried to use absolute path like following:更新:我尝试使用如下绝对路径:

ITransformer mlModel = mlContext.Model.Load(@"/Users/a10.12/Price Prediction/SampleRegression/SampleRegression.ConsoleApp/ModelBuilder.cs", out var modelInputSchema);

and now getting error:现在得到错误:

An exception of type 'System.FormatException' occurred in Microsoft.ML.Core.dll but was not handled in user code: 'Failed to open a zip archive'

Even though its working with the absolute path, it would be good to point out that the reason that it wasn't working with the relative path is, when you run the code the compiled code resides in the bin folder of your application.尽管它使用绝对路径,但最好指出它不使用相对路径的原因是,当您运行代码时,编译的代码位于应用程序的 bin 文件夹中。 That means that when you do ..\SampleRegression.Model\MLModel.zip you are basically targeting the wrong folder.这意味着当您执行..\SampleRegression.Model\MLModel.zip时,您基本上是针对错误的文件夹。

Also a better solution to this would be, instead of using the absolute path, set the zip file to be copied on build to the output directory and load it from there.另外一个更好的解决方案是,而不是使用绝对路径,将 zip 文件设置为在构建时复制到 output 目录并从那里加载它。 This will make your code less error prone.这将使您的代码不易出错。

The way you set the file to be copied in the output directory is by right clicking the file in Visual Studio and set Copy to output directory to Copy always , or if you don't use visual studio then you need to set that in your csproj file在 output 目录中设置要复制的文件的方式是在 Visual Studio 中右键单击文件并将Copy to output directory设置为Copy always ,或者如果您不使用 Visual Studio,则需要在您的 csproj 中进行设置文件

<ItemGroup>
    <None Update="MLModel.zip">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
</ItemGroup>

Hope this makes things more clearer希望这能让事情更清楚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM