大佬教程收集整理的这篇文章主要介绍了Tutorial: Create a Windows Machine Learning UWP application (C#),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
In this tutorial,we‘ll build a simple Universal Windows Platform application that uses a Trained machine learning model to recognize a numeric digit drawn by the user. This tutorial primarily focuses on how to load and use Windows ML in your UWP application.
The following video walks through the sample that this tutorial is based on.
First,you‘ll need to download our MNIST Tutorial from GitHub. We‘ve provided a template with implemented XAML controls and events,including:
A @L_616_7@ is also available to download from GitHub.
Launch Visual studio,and open the MNIST sample application. (If the solution is shown as unavailable,you‘ll need to right-click the project in the Solution Explorer and SELEct Reload Project.)
Inside the Solution Explorer,the project has three main code files:
Helper.cs
- Helper routInes to crop and convert image formats.In the Visual studio toolbar,change the Solution Platform to x64 to run the project on your local machine.
To run the project,click the Start Debugging button on the toolbar,or press F5. The application should show an InkCanvas where users can write a digit,a Recognize button to interpret the number,an empty label field where thE interpreted digit will be displayed as text,and a Clear Digit button to clear the InkCanvas.
Note
If the project won‘t build,you might need to change the project‘s deployment target version. Right-click the project in the Solution Explorer and SELEct Properties. In the Application tab,set the Target version and @H_173_42@min version to match your OS and SDK.
Next,let‘s get a machine learning model to add to our application. For this tutorial,we‘ll use a pre-Trained @H_173_42@mNIST model that was Trained with the Microsoft Cognitive Toolkit (CNTK) and exported to ONNX format.
If you are using the MNIST Tutorial sample from GitHub,the MNIST model has already been included in your Assets folder,and you will need to add it to your application as an exisTing item. You can also download the pre-Trained model from the ONNX Model Zoo on GitHub.
After downloading the MNIST model,right click on the Assets folder in the Solution Explorer,and SELEct Add > ExisTing Item. Point the file picker to the LOCATIOn of your ONNX model,and click Add.
The project should Now have two new files:
To make sure the model builds when we compile our application,right click on the @H_577_62@mnist.onnx file,and SELEct Properties. For Build Action,SELEct Content.
Now,let‘s take a look at the newly generated code in the @H_577_62@mnist.cs file. We have three classes:
Plus214_Output_0
of type TensorFloat.We‘ll Now use these classes to load,bind,and evaluate the model in our project.
For Windows ML applications,the pattern we want to follow is: Load > Bind > Evaluate.
We‘ll use thE interface code generated in @H_577_62@mnist.cs to load,and evaluate the model in our application.
First,in @H_577_62@mainPage.xaml.cs,let‘s instantiate the model,inputs,and outputs.
namespace @H_793_235@mNIST_Demo { public sealed partial class @H_793_235@mainPage : Page { private mnistModel ModelGen; private mnisTinput ModelInput = new mnistinput(); private mnistOutput ModelOutput; ... } }
Then,in LoadModelAsync
,we‘ll load the model. This method should be called before we use any of the models methods (i.e. on the Page‘s Loaded event,at a OnNavigatedTo override,or anywhere before recognizeButton_Click
is called). The @H_577_62@mnistModel class represents the MNIST model and creates the session on the system default device. To load the model,we call the CreateFromStreamAsync
method,passing in the ONNX file as the parameter.
private async Task LoadModelAsync() { //Load a machine learning model StorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/mnist.onnx")); ModelGen = await mnistModel.CreateFromStreamAsync(modelFile as IRandomAccessStreamReferencE); }
Next,we want to bind our inputs and outputs to the model. The generated code also includes @H_577_62@mnisTinput and @H_577_62@mnistOutput wrapper classes. The @H_577_62@mnisTinput class represents the model‘s expected inputs,and the @H_577_62@mnistOutput class represents the model‘s expected outputs.
To initialize the model‘s input object,call the @H_577_62@mnisTinput class constructor,passing in your application data,and make sure that your input data matches the input type that your model expects. The @H_577_62@mnisTinput class expects an ImageFeatureValue,so we use a Helper method to get an ImageFeatureValue for the input.
Using our included Helper functions in Helper.cs
,we will copy the contents of the InkCanvas,convert it to type ImageFeatureValue,and bind it to our model.
private async void recognizeButton_Click(object sender,RoutedEventArgs E) { //Bind model input with contents from InkCanvas VideoFrame vf = await Helper.GetHandWrittenImage(inkGrid); ModelInput.Input3 = ImageFeatureValue.CreateFromVideoFrame(vf); }
For output,we simply call EvaluateAsync
with the specified input. Once your inputs are initialized,call the model‘s EvaluateAsync
method to evaluate your model on the input data. EvaluateAsync
binds your inputs and outputs to the model object and evaluates the model on the inputs.
Since the model returns an output tensor,we‘ll first want to convert it to a friendly datatype,and then parse the returned list to determine which digit had the highest probability and display that one.
private async void recognizeButton_Click(object sender,RoutedEventArgs E) { //Bind model input with contents from InkCanvas VideoFrame vf = await Helper.GetHandWrittenImage(inkGrid); ModelInput.Input3 = ImageFeatureValue.CreateFromVideoFrame(vf); //Evaluate the model ModelOutput = await ModelGen.EvaluateAsync(ModelInput); //Convert output to datatype IReadOnlyList<float> vectorImage = ModelOutput.Plus214_Output_0.GetAsVectorView(); IList<float> imageList = vectorImage.ToList(); //Query to check for highest probability digit var maxIndex = imageList.IndexOf(imageList.Max()); //Display the results numberLabel.Text = maxIndex.ToString(); }
Finally,we‘ll want to clear out the InkCanvas to allow users to draw another number.
private void clearButton_Click(object sender,RoutedEventArgs E) { inkCanvas.InkPresenter.strokeContainer.Clear(); numberLabel.Text = ""; }
Once we build and launch the application,we‘ll be able to recognize a number drawn on the InkCanvas.
That‘s it - you‘ve made your first Windows ML application! For more samples that demonstrate how to use Windows ML,check out our Windows-Machine-Learning repo on GitHub.
Note
Use the following resources for Help with Windows ML:
以上是大佬教程为你收集整理的Tutorial: Create a Windows Machine Learning UWP application (C#)全部内容,希望文章能够帮你解决Tutorial: Create a Windows Machine Learning UWP application (C#)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。