Reputation: 137
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
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
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
(@"..\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'
Upvotes: 1
Views: 3087
Reputation: 803
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. That means that when you do ..\SampleRegression.Model\MLModel.zip
you are basically targeting the wrong folder.
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. 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
<ItemGroup>
<None Update="MLModel.zip">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
Hope this makes things more clearer
Upvotes: 1