machinelearning-samples
machinelearning-samples copied to clipboard
Is this a bug, or is Microsoft ML incompatible with Windows Forms
Hello,
trying to integrate machine learnig into a Windows Forms app (even a very simple one that may only predict next value) is not successful. As soon as the ML modules are invoked, an error is thrown:

Details of the error message:
System.DllNotFoundException HResult=0x80131524 Nachricht = Die DLL "CpuMathNative": Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E) kann nicht geladen werden. Quelle = <Die Ausnahmequelle kann nicht ausgewertet werden.> Stapelüberwachung: <Die Ausnahmestapelüberwachung kann nicht ausgewertet werden.>
Obviously, a DLL named CpuMathNative is missing. Does it belong to original Windows code or to .NET Framework (Version???) Error message 0x8007007E of course has a little strong flavour :-( - Windows Update Error or damaged component store etc. Therefore I did the usual process:
- Windows Update Error Processing - Windows can't identify the error
- All known updates for the moment are successfully installed
- sfc /scannow and the well known DISM procedure without errors...
Can anybody help? Is there an incompatibility between Microsoft ML and .NET framework (GUI)? For first test, I implemented ML using the model generator within VS. The test program only tries to predict a new value for the trained model. Here's the code of the ConsumeModel class:
// This file was auto-generated by ML.NET Model Builder.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.ML; using ModelTrainerML.Model;
namespace ModelTrainerML.Model { public class ConsumeModel { // For more info on consuming ML.NET models, visit https://aka.ms/model-builder-consume // Method for consuming model in your app public static ModelOutput Predict(ModelInput input) {
// Create new MLContext
MLContext mlContext = new MLContext();
// Load model & create prediction engine
string modelPath = AppDomain.CurrentDomain.BaseDirectory + "MLModel.zip";
ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
// Use model to make prediction on input data
ModelOutput result = predEngine.Predict(input);
return result;
}
}
}
If I call the predict method from the ConsoleApp, everything works fine...
Update: Debugging more deeply: The error occurs in the line within ConsumeModel class:
ModelOutput result = predEngine.Predict(input);
I found a similar issue in Microsoft.ML NuGet package doesn't work with packages.config #93. Following this thread, I had a look at the packaging.config file. There I found that all included packages use .NET Framework 4.6.1 as target. On my computer version 4.8.3752.0 is installed. Could this be the reason? Should I change the target framework to the actual version?
It is compatible, you just need to select/run on x64. Error when running the explainability WinForms app from Visual Studio F5 If you get the following exception: DllNotFoundException: Unable to load DLL 'CpuMathNative': The specified module could not be found. Cause: ML.NET for this particular math operation only supports x64, and VS is attempting to use AnyCPU. Solution: Make sure you are running the app as x64 in VS, as shown in the following screenshot:

If you migrate the WinForms app to .NET Core 3.0, you won't have this issue. :)
We have a couple of samples using WinForms: https://github.com/dotnet/machinelearning-samples/tree/master/samples/csharp/end-to-end-apps/Model-Explainability
https://github.com/dotnet/machinelearning-samples/tree/master/samples/csharp/end-to-end-apps/AnomalyDetection-Sales
Feel free to close this issue if working for you, ok? 👍
Sorry, but solution is not that simple. Please have a look at the screenshot I sent yesterday... :-(

Be sure I wouldn't bother you with such newbie's errors!
Aren’t the WinForms samples I mentioned above not working for you?
I will try, but it is unlikely that it is a programming error of mine. The error occurs in code produced automatically by Microsoft Visual Studio! In fact, I opened a minimal Forms window and added "Machine learning" from the VS menu. But I will try the examples. I think it depends upon the fact which ML Trainer is used etc.
The Taxi Fare example I tried out long ago, since it is part of the basic tutorials from Microsoft. Of course, it works. But it is a console application, not windows forms. The Spike Detection example seems to be damaged in the GitHub repository because it is impossible to compile within Visual Studio. First of all this project doesn't allow to install nuget packages, for there is an "invalid version number" whose value is "". Additionally this project seems to be incompatible with .NET version 2.2 and higher since it forces to downgrade to .NET 2.1 I will not invest much work in this. Even if this sample compiles and works, that doesn't prove anything. Machine Learning is such a wide field with so many objects, methods, parameters, pre-trained models etc., i. e. so many boundary conditions that the fact that one program is running doesn't tell us anything about another program.
Summarizing once more my problem:
- I opened a new VS project with a windows forms app.
- I used ML.NET model builder tool to implement machine learning to the project (in fact I could implement the model builder myself, but it is a nice feature that the ML.NET model builder pre-trains given data and tries to detect which trainer would be the best). In my case the model builder selected Ols as a favourite. But I tried other trainers too, resulting in the same errors.
- Usually ML.NET model builder adds two new projects to the project folder: a console app (containing the model builder for additional training) and a second project that contains the class definitions for the dataview and a ConsumeModel method for predictions.
- If I enter the using directive to the Model project in the forms windows app, first of all the project can't compile without an explicit using Microsoft.ML directive in the Forms class. This is strange because this class doesn't call any methods from Microsoft.ML but only the ConsumeModel method where all necessary packages are referenced correctly.
- After adding Microsoft.ML to the package list of the forms app, the program builds without errors. But if I want to predict a new value using the ConsumeModel method, further packages are demanded during runtime. First the program dies because Microsoft.ML.Mkl.Components package is missing. If I install this, next run of the program dies because "CpuMathNative.dll" is missing, and this dll can't be found anywhere.
- If I call the ConsumeModel method via the console app instead of the windows forms, everything works fine,
- On the web there are suggestions that to resolve this one should install Microsoft Visual C++ Redistributable 2017. On my computer version 2015-2019 is installed, including 2017.
- My test program doesn't contain any handwritten code of mine but was completely built by Microsoft VS. So, even if the user program would be erraneous (I don't think it is), it would be a bug.
Hi Issuer,
plz check it for inspect more details:
https://docs.microsoft.com/en-us/dotnet/machine-learning/how-does-mldotnet-work#mlnet-architecture
On Sun, Nov 3, 2019, 16:58 FranzBl [email protected] wrote:
The Taxi Fare example I tried out long ago, since it is part of the basic tutorials from Microsoft. Of course, it works. But it is a console application, not windows forms. The Spike Detection example seems to be damaged in the GitHub repository because it is impossible to compile within Visual Studio. First of all this project doesn't allow to install nuget packages, for there is an "invalid version number" whose value is "". Additionally this project seems to be incompatible with .NET version 2.2 and higher since it forces to downgrade to .NET 2.1 I will not invest much work in this. Even if this sample compiles and works, that doesn't prove anything. Machine Learning is such a wide field with so many objects, methods, parameters, pre-trained models etc., i. e. so many boundary conditions that the fact that one program is running doesn't tell us anything about another program.
Summarizing once more my problem:
- I opened a new VS project with a windows forms app.
- I used ML.NET model builder tool to implement machine learning to the project (in fact I could implement the model builder myself, but it is a nice feature that the ML.NET model builder pre-trains given data and tries to detect which trainer would be the best). In my case the model builder selected Ols as a favourite. But I tried other trainers too, resulting in the same errors.
- Usually ML.NET model builder adds two new projects to the project folder: a console app (containing the model builder for additional training) and a second project that contains the class definitions for the dataview and a ConsumeModel method for predictions.
- If I enter the using directive to the Model project in the forms windows app, first of all the project can't compile without an explicit using Microsoft.ML directive in the Forms class. This is strange because this class doesn't call any methods from Microsoft.ML but only the ConsumeModel method where all necessary packages are referenced correctly.
- After adding Microsoft.ML to the package list of the forms app, the program builds without errors. But if I want to predict a new value using the ConsumeModel method, further packages are demanded during runtime. First the program dies because Microsoft.ML.Mkl.Components package is missing. If I install this, next run of the program dies because "CpuMathNative.dll" is missing, and this dll can't be found anywhere.
- If I call the ConsumeModel method via the console app instead of the windows forms, everything works fine,
- On the web there are suggestions that to resolve this one should install Microsoft Visual C++ Redistributable 2017. On my computer version 2015-2019 is installed, including 2017.
- My test program doesn't contain any handwritten code of mine but was completely built by Microsoft VS. So, even if the user program would be erraneous (I don't think it is), it would be a bug.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dotnet/machinelearning-samples/issues/733?email_source=notifications&email_token=AEYAMLYSNC54CA76IBAAMM3QR2OC3A5CNFSM4JHH4AV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEC5OY5A#issuecomment-549121140, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEYAML6DI6G2DYOXNOC3MDLQR2OC3ANCNFSM4JHH4AVQ .
The Winforms samples using ML.NET at this repo work properly. You can see some screenshots below:
https://github.com/dotnet/machinelearning-samples/tree/master/samples/csharp/end-to-end-apps/Model-Explainability

https://github.com/dotnet/machinelearning-samples/tree/master/samples/csharp/end-to-end-apps/AnomalyDetection-Sales

If this issue is not related to the samples here but related to MODEL BUILDER, please, open an issue here:
https://github.com/dotnet/machinelearning-modelbuilder/issues
Thanks,
I had the same issue. The problem was in target framework which needs to be core 3.1 and up on windows forms project. So please make a new project. They now released this: https://devblogs.microsoft.com/dotnet/windows-forms-designer-for-net-core-released/