dotnet / machinelearning-modelbuilder

Simple UI tool to build custom machine learning models.
Creative Commons Attribution 4.0 International
267 stars 59 forks source link

Could not load file from Consume Model even after changing to sdk-style #1102

Closed JoelShen123 closed 4 years ago

JoelShen123 commented 4 years ago

Versions Microsoft.ML - Version="1.5.0-preview2". Visual Studio 2019 Target Framework: Class Library ( .NET Framework 4.7)

Bug description "Could not load file" error thrown from "ModelOperationsCatalog.Load()".

Steps to Reproduce

  1. Follow the steps here: https://dotnet.microsoft.com/learn/ml-dotnet/get-started-tutorial/create
  2. Include the auto generated "MLModel.zip" into the project file as an "Embedded Resource".
  3. Read the "MLModel.zip" into Stream and pass that in into the mlContext.Model.Load(theMLModelZipStream, null)" string resourceName = "IfcPluginML.Model.MLModel.zip"; Stream modelFile = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName) ITransformer mlModel = mlContext.Model.Load(modelFile , out var modelInputSchema);
  4. Compile into .dll and run.
  5. Error is thrown.

Expected Experience File is read successfully.

Actual Experience Could not read file:

image

I have checked that the Stream modelFile = GetEmbeddedResourceStream(resourceName); // returns "System.IO.UnmanagedMemoryStream" so it is definitely not null.

Putting the full file path produces the same error too: string modelPath = @"C:\Users\User\AppData\Local\Temp\MLVSTools\IfcPluginML\IfcPluginML.Model\MLModel.zip"; ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);

Additional Context I have referenced this post: https://github.com/dotnet/machinelearning-modelbuilder/issues/274 However, even after changing the target framework of ProjectNameML.Model.csproj to an sdk style: image The error persists.

How my software works:

  1. There is a main project (Class Library .NET Framework 4.7).
  2. The main project calls ConsumeModel.Predict(input);
  3. ConsumeModel throws the error.

It works when:

  1. The auto generated Program.cs in ProjectNameML.ConsoleApp (.NET Core 3.1) is ran.
  2. Program.cs calls ConsumeModel.Predict(input);
  3. Runs perfectly.

Thanks for your help 💯

JoelShen123 commented 4 years ago

It's okay I've found a workaround. Instead of trying to run the machine learning with the .NET Framework, I created a standalone console application in .NET Core and export that as .exe, then run the .exe from the .NET Framework, and it works. :)