Tutorial: Create a Windows Machine Learning UWP application
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. Prerequisites
1. Download the sampleFirst,you‘ll need to download our MNIST Tutorial from GitHub. We‘ve provided a template with implemented XAML controls and events,including:
A completed MNIST sample is also available to download from GitHub. 2. Open the project in Visual StudioLaunch 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:
3. Build and run the projectIn 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 Min version to match your OS and SDK. 4. Download a modelNext,let‘s get a machine learning model to add to our application. For this tutorial,we‘ll use a pre-trained 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. 5. Add the modelAfter 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 Now,let‘s take a look at the newly generated code in the
We‘ll now use these classes to load,bind,and evaluate the model in our project. 6. Load,and evaluate the modelFor Windows ML applications,the pattern we want to follow is: Load > Bind > Evaluate.
We‘ll use the interface code generated in First,in
C#
namespace MNIST_Demo { public sealed partial class MainPage : Page { private mnistModel ModelGen; private mnistInput ModelInput = new mnistInput(); private mnistOutput ModelOutput; ... } }
Then,in
C#
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 To initialize the model‘s input object,call the Using our included helper functions in
C#
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 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.
C#
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.
C#
private void clearButton_Click(object sender,RoutedEventArgs e) { inkCanvas.InkPresenter.StrokeContainer.Clear(); numberLabel.Text = ""; }
7. Launch the applicationOnce 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:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows – UnKnown找不到:不存在的域名
- windows-server-2008 – Https可以在没有证书的情况下工作吗
- windows – Wix – 在安装结束时运行exe(带参数),当前MSI没
- windows-server-2012-r2 – 发布后安装问题 – 无法连接
- windows – 如何获取不同语言的管理员组名称
- windows-phone – 如何使用.NET代码从Windows Phone Market
- powershell – 您是否在Move-ClusterGroup之前或之后暂停Cl
- 如何在Windows上使用其他C编译器和CUDA?
- Windows 7中的UTF-8 CMD
- 在Windows窗体或WPF应用程序中使用“Wingdings”字体是否合