MrCSharp
MrCSharp

Reputation: 1143

ML.Net fails to load a model from storage in MVC project

I have been playing around with ML.Net and wanted to integrate it in an ASP.NET MVC project.

I have a console app that is responsible for training the model and then saving it to a folder on the server which the MVC project will then load it and use it to predict results.

In my initial test, the console app was able to train and save the model to a file, then loading the model from that file and predicting a few results.

However, the MVC project fails to load the same model file with the following exception:

"Message": "An error has occurred.",
"ExceptionMessage": "Couldn't load model: 'DataLoaderModel\Transform_005\SchemaBindableMapper\InnerMapper\Predictor'",
"ExceptionType": "System.FormatException",
"StackTrace": " at Microsoft.ML.Runtime.Model.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, Entry ent, String dir, Object[] extra)\r\n at Microsoft.ML.Runtime.Model.ModelLoadContext.LoadModelOrNull[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, String dir, Object[] extra)\r\n at Microsoft.ML.Runtime.Model.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, String name, Object[] extra)\r\n at Microsoft.ML.Runtime.Data.SchemaBindablePredictorWrapperBase..ctor(IHostEnvironment env, ModelLoadContext ctx)\r\n at Microsoft.ML.Runtime.Data.SchemaBindablePredictorWrapper.Create(IHostEnvironment env, ModelLoadContext ctx)"

Anyone experienced this? It is weird that the same model would load as expected in the console app but fails in the MVC project.

Both the console app and the mvc project are running on .Net Framework 4.6.1 (.Net standard 2.0 as required by ML.NET). The console app is targeting x64 while the MVC project is set to AnyCPU but the local IIS server loads the site in a x64 process.

Any help would be appreciated.

Thanks

Edit As requested in the comments, here are some code snippets:

//Loading the model
    public Task<PredictionModel<Data, PredictionResult>> LoadModelAsync()
        {
            return PredictionModel.ReadAsync<SlateData, ResourcePrediction>(modelFilePath);  // this line throws the exception above
        }

//The above method is called like this
var predictionModel = await LoadModelAsync();

EDIT ML.Net version 0.3.0 (from Nuget package)

Upvotes: 0

Views: 933

Answers (1)

MrCSharp
MrCSharp

Reputation: 1143

So I've managed to solve this. The exception i was getting had many internal exceptions inside and i focused mainly on the inner most one. But the outer most exception was complaining about failure to create an instance of some object by reflection.

The MVC project was referencing another project that had all the code for loading and predicting.

The fix to this was to add a reference to the Microsoft.ML nuget library in the main MVC project too which fixed the reflection exception and allowed the model to be loaded as expected.

Upvotes: 2

Related Questions