使用 Windows 机器学习 API 在 Windows 应用中部署 ML.NET 模型

在本教程的上一部分中,你已了解如何生成并以 ONNX 格式导出 ML.NET 模型。 有了这个模型之后,就可以将它嵌入到 Windows 应用程序中,并通过调用 WinML API 在设备上本地运行它。

完成操作后,你将有一个可正常运行的图像分类器 WinML UWP 应用 (C#)。

关于示例应用程序

通过使用模型,我们将创建一个可以对食物图像进行分类的应用。 这样,你可以从本地设备中选择一个图像,并通过在上一部分中生成和训练的本地存储的分类 ONNX 模型来处理它。 返回的标签显示在图像旁边,分类的置信概率也显示在这里。

如果到目前为止你一直在遵循本教程,那么你应已具备了应用程序开发的必要先决条件。 如果需要复习一下,请参阅本教程的第一部分

注意

如果希望下载完整的示例代码,可以克隆解决方案文件。 克隆存储库,导航到此示例,然后通过 Visual Studio 打开 classifierMLNETModel.sln 文件。 然后,可以跳到 [启动应用程序](#启动应用程序) 步骤。

创建 WinML UWP (C#)

下面,我们将展示如何从头开始创建应用和 WinML 代码。 将了解如何执行以下操作:

  • 加载机器学习模型。
  • 加载所需格式的图像。
  • 绑定模型的输入和输出。
  • 评估模型并显示有意义的结果。

你还将使用基本 XAML 创建简单的 GUI,以便测试图像分类器。

创建应用

  1. 打开 Visual Studio 并选择 create a new project

Create a new Visual Studio project

  1. 在搜索栏中,键入 UWP,然后选择 Blank APP (Universal Windows)。 这将为没有预定义控件或布局的单页通用 Windows 平台 (UWP) 应用程序打开一个新的 C# 项目。 选择 Next 以打开项目的配置窗口。

Create a UWP app

  1. 在配置窗口中:
  • 为项目选择一个名称。 此处,我们使用 classifierMLNETModel
  • 选择项目的位置。
  • 如果使用的是 VS 2019,请确保未选中 Place solution and project in the same directory
  • 如果使用的是 VS 2017,请确保已选中 Create directory for solution

Configure your app

create 创建项目。 可能会弹出最低目标版本窗口。 请确保将最低版本设置为“Windows 10 版本 17763”或更高版本

若要创建应用并部署具有 WinML 应用的模型,需要以下各项:

  1. 创建项目后,导航到项目文件夹,打开资产文件夹 [….\classifierMLNETModel\Assets],然后将 bestModel.onnx 文件复制到该位置。

探索项目解决方案

让我们探索项目解决方案。

Visual Studio 在解决方案资源管理器中自动创建了几个 cs 代码文件。 MainPage.xaml 包含 GUI 的 XAML 代码,MainPage.xaml.cs 包含应用程序代码。 如果之前已创建 UWP 应用,你应该对这些文件非常熟悉。

创建应用程序 GUI

首先,让我们为应用创建一个简单的 GUI。

  1. 双击 MainPage.xaml 文件。 在空白应用中,应用的 GUI 的 XAML模板为空,因此我们需要添加一些 UI 功能。

  2. 将代码 MainPage.xaml 替换为以下代码。

<Page
    x:Class="classifierMLNETModel.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:classifierMLNETModel"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

       <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

	
        <StackPanel Margin="1,0,-1,0">
            <TextBlock x:Name="Menu" 
                       FontWeight="Bold" 
                       TextWrapping="Wrap"
                       Margin="10,0,0,0"
                       Text="Image Classification"/>
            <TextBlock Name="space" />
            <Button Name="recognizeButton"
                    Content="Pick Image"
                    Click="OpenFileButton_Click" 
                    Width="110"
                    Height="40"
                    IsEnabled="True" 
                    HorizontalAlignment="Left"/>
            <TextBlock Name="space3" />
            <Button Name="Output"
                    Content="Result is:"
                    Width="110"
                    Height="40"
                    IsEnabled="True" 
                    HorizontalAlignment="Left" 
                    VerticalAlignment="Top">
            </Button>
            <!--Dispaly the Result-->
            <TextBlock Name="displayOutput" 
                       FontWeight="Bold" 
                       TextWrapping="Wrap"
                       Margin="25,0,0,0"
                       Text="" Width="1471" />
            <TextBlock Name="space2" />
            <!--Image preview -->
            <Image Name="UIPreviewImage" Stretch="Uniform" MaxWidth="300" MaxHeight="300"/>
        </StackPanel>
    </Grid> 
</Page>

使用 Windows 机器学习代码生成器将模型添加到项目

Windows 机器学习代码生成器 (mlgen) 是一项 Visual Studio 扩展,有助于你在 UWP 应用中开始使用 WinML API。 将经过训练的 ONNX 文件添加到 UWP 项目时,它会生成模板代码。

Windows 机器学习的代码生成器 mlgen 会创建一个接口(用于 C#、C++/WinRT 和 C++/CX),其中包含为你调用 Windows ML API 的包装类。 这样,你便可以轻松地在项目中加载、绑定和评估模型。 在本教程中,我们将使用它来处理其中许多函数。

代码生成器适用于 Visual Studio 2017 及更高版本。 请注意,在 Windows 10 版本 1903 和更高版本中,Windows 10 SDK 中不再包含 mlgen,因此必须下载并安装此扩展。 如果从简介开始就一直参加本教程,则已完成扩展的下载,否则,应下载适用于 VS 2019VS 2017 的扩展。

注意

若要详细了解 mlgen,请参阅 mlgen 文档

  1. 如果尚未安装 mlgen,请先安装。

  2. 右键单击 Visual Studio 解决方案资源管理器中的 Assets 文件夹,然后选择 Add > Existing Item

  3. 导航到 ImageClassifierAppUWP [….\ImageClassifierAppUWP\Assets] 中的资产文件夹,找到之前复制到该文件夹中的 ONNX 模型,然后选择 add

  4. 将 ONNX 模型(名称为“classifier”)添加到 VS 的解决方案资源管理器中的资产文件夹后,项目现在应该有两个新文件:

  • bestModel.onnx - 这是采用 ONNX 格式的模型。
  • bestModel.cs - 自动生成的 WinML 代码文件。

Project structure with ONNX model added

  1. 若要确保在编译应用程序时生成模型,请选择 bestModel.onnx 文件并选择 Properties。 对于 Build Action,请选择 Content

现在,让我们了解一下 bestModel.cs 文件中新生成的代码。

生成的代码包括三个类:

  • bestModelModel:此类包括用于模型实例化和模型评估的两种方法。 它有助于我们创建机器学习模型表示,在系统默认设备上创建一个会话,将特定输入和输出绑定到模型,并异步评估模型。
  • bestModelInput:此类初始化模型预期的输入类型。 模型输入取决于输入数据的模型要求。
  • bestModelOutput:此类初始化模型将输出的类型。 模型输出取决于模型对它的定义方式。

现在将使用这些类在项目中加载、绑定并评估模型。

张量转换

若要更轻松地处理张量化,请将输入 TensorFloat 类更改为 ImageFeatureValue

  1. bestModel.cs 文件中进行以下更改:

代码:

public sealed class bestModelInput
    {
        public TensorFloat input; // shape(-1,3,32,32)
    }

将成为:

public sealed class bestModelInput
    {
        public ImageFeatureValue input; // shape(-1,3,32,32)
    }

加载模型和输入

加载模型

  1. 双击 MainPage.xaml.cs 代码文件,打开应用程序代码。

  2. 将“using”语句替换为以下内容,以访问所需的所有 API。

// Specify all the using statements which give us the access to all the APIs that you'll need
using System;
using System.Threading.Tasks;
using Windows.AI.MachineLearning;
using Windows.Graphics.Imaging;
using Windows.Media;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
  1. MainPage 类中使用 using 语句后,在命名空间 classifierMLNETModel 下添加以下变量声明。
		// All the required fields declaration
		private bestModelModel modelGen;
		private bestModelInput image = new bestModelInput();
		private bestModelOutput results;
		private StorageFile selectedStorageFile;
		private string label = "";
		private float probability = 0;
		private Helper helper = new Helper();

		public enum Labels
		{
			desert,
			soup,
			vegetable_fruit,
		}

接下来,实现 LoadModel 方法。 该方法将访问 ONNX 模型,并将其存储在内存中。 然后,使用 CreateFromStreamAsync 方法将模型实例化为 LearningModel 对象。 LearningModel 类表示已训练的机器学习模型。 实例化后,LearningModel 是用于与 Windows ML 交互的初始对象。

若要加载模型,可使用 LearningModel 类中的多个静态方法。 在本例中,将使用 CreateFromStreamAsync 方法。

CreateFromStreamAsync 方法是使用 mlgen 自动创建的,因此无需实现此方法。 可通过双击 mlgen 生成的 bestModel.cs 文件来查看此方法。

若要详细了解 LearningModel 类,请查看 LearningModel 类文档

若要详细了解加载模型的其他方式,请查看加载模型文档

  1. 让我们来定义主要方法。
// The main page to initialize and execute the model.
public MainPage()
{
	this.InitializeComponent();
	loadModel();
}
  1. loadModel 方法的实现添加到 MainPage 类中的 MainPage.xaml.cs 代码文件。
private async Task loadModel()
{
// Get an access the ONNX model and save it in memory. 
	StorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/bestModel.onnx"));
// Instantiate the model. 
	modelGen = await bestModelModel.CreateFromStreamAsync(modelFile);
}

加载图像

  1. 我们需要定义一个 click 事件来启动模型执行的四个方法调用序列 - 转换、绑定和评估、输出提取以及结果显示。 将以下方法添加到 MainPage 类中的 MainPage.xaml.cs 代码文件。
        // Waiting for a click event to select a file 
        private async void OpenFileButton_Click(object sender, RoutedEventArgs e)
        {
            if (!await getImage())
            {
                return;
            }
            // After the click event happened and an input selected, begin the model execution. 
            // Bind the model input
            await imageBind();
            // Model evaluation
            await evaluate();
            // Extract the results
            extractResult();
            // Display the results  
            await displayResult();
        }
  1. 接下来,实现 getImage() 方法。 此方法将选择一个输入图像文件并将其保存在内存中。 将以下方法添加到 MainPage 类中的 MainPage.xaml.cs 代码文件。
        // A method to select an input image file
        private async Task<bool> getImage()
        {
            try
            {
                // Trigger file picker to select an image file
                FileOpenPicker fileOpenPicker = new FileOpenPicker();
                fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                fileOpenPicker.FileTypeFilter.Add(".jpg");
                fileOpenPicker.FileTypeFilter.Add(".png");
                fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
                selectedStorageFile = await fileOpenPicker.PickSingleFileAsync();
                if (selectedStorageFile == null)
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

接下来,你将实现图像 Bind() 方法,以获取位图 BGRA8 格式的文件表示。 但首先,你将创建帮助程序类来调整图像大小。

  1. 若要创建帮助程序文件,请右键单击解决方案名称 (ClassifierPyTorch),然后选择 Add a new item。 在打开的窗口中,选择 Class,然后为其指定一个名称。 此处我们将其命名为 Helper

Add a Helper file

  1. 你的项目中将出现一个新的类文件。 打开此类,并添加以下代码:
using System; 
using System.Threading.Tasks; 
using Windows.Graphics.Imaging; 
using Windows.Media; 

namespace classifierPyTorch 
{ 
    public class Helper 
    { 
        private const int SIZE = 32;  
        VideoFrame cropped_vf = null; 
 
        public async Task<VideoFrame> CropAndDisplayInputImageAsync(VideoFrame inputVideoFrame) 
        { 
            bool useDX = inputVideoFrame.SoftwareBitmap == null; 

            BitmapBounds cropBounds = new BitmapBounds(); 
            uint h = SIZE; 
            uint w = SIZE; 
            var frameHeight = useDX ? inputVideoFrame.Direct3DSurface.Description.Height : inputVideoFrame.SoftwareBitmap.PixelHeight; 
            var frameWidth = useDX ? inputVideoFrame.Direct3DSurface.Description.Width : inputVideoFrame.SoftwareBitmap.PixelWidth; 
 
            var requiredAR = ((float)SIZE / SIZE); 
            w = Math.Min((uint)(requiredAR * frameHeight), (uint)frameWidth); 
            h = Math.Min((uint)(frameWidth / requiredAR), (uint)frameHeight); 
            cropBounds.X = (uint)((frameWidth - w) / 2); 
            cropBounds.Y = 0; 
            cropBounds.Width = w; 
            cropBounds.Height = h; 
 
            cropped_vf = new VideoFrame(BitmapPixelFormat.Bgra8, SIZE, SIZE, BitmapAlphaMode.Ignore); 
 
            await inputVideoFrame.CopyToAsync(cropped_vf, cropBounds, null); 
            return cropped_vf; 
        } 
    } 
} 

现在,让我们将图像转换为适当的格式。

bestModelInput 类初始化模型预期的输入类型。 在本例中,我们将代码配置为预计 ImageFeatureValue

ImageFeatureValue 类描述用于传递到模型的图像的属性。 若要创建 ImageFeatureValue,请使用 CreateFromVideoFrame 方法。 有关这种情况的原因以及这些类和方法如何工作的更具体的详细信息,请参阅 ImageFeatureValue 类文档

注意

在本教程中,我们使用 ImageFeatureValue 类,而不是张量。 如果 Window ML 不支持模型的颜色格式,则不能使用此选项。 有关如何使用图像转换和张量化的示例,请参阅自定义张量化示例

  1. convert() 方法的实现添加到 MainPage 类中的 MainPage.xaml.cs 代码文件。 convert 方法将获取 BGRA8 格式的输入文件表示。
// A method to convert and bind the input image.  
        private async Task imageBind()
        {
            UIPreviewImage.Source = null;
            try
            {
                SoftwareBitmap softwareBitmap;
                using (IRandomAccessStream stream = await selectedStorageFile.OpenAsync(FileAccessMode.Read))
                {
                    // Create the decoder from the stream 
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
                    // Get the SoftwareBitmap representation of the file in BGRA8 format
                    softwareBitmap = await decoder.GetSoftwareBitmapAsync();
                    softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                }
                // Display the image
                SoftwareBitmapSource imageSource = new SoftwareBitmapSource();
                await imageSource.SetBitmapAsync(softwareBitmap);
                UIPreviewImage.Source = imageSource;

				// Encapsulate the image within a VideoFrame to be bound and evaluated
            	VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
              	// Resize the image size to 224x224 
              	inputImage=await helper.CropAndDisplayInputImageAsync(inputImage);
              	// Bind the model input with image
              	ImageFeatureValue imageTensor = ImageFeatureValue.CreateFromVideoFrame(inputImage);
				image.input1 = imageTensor;

				// Encapsulate the image within a VideoFrame to be bound and evaluated
				VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
				// Bind the input image
				ImageFeatureValue imageTensor = ImageFeatureValue.CreateFromVideoFrame(inputImage);
				image.modelInput = imageTensor;

            }
            catch (Exception e)
            {
            }
        }

绑定并评估模型

接下来,需要基于模型创建一个会话,绑定会话的输入和输出,并评估模型。

创建用于绑定模型的会话:

若要创建会话,请使用 LearningModelSession 类。 此类用于评估机器学习模型,并将该模型绑定到一个设备上,然后运行和评估该模型。 在创建会话时,可以选择一个设备,以便在计算机的特定设备上执行模型。 默认设备是 CPU。

注意

若要详细了解如何选择设备,请参阅创建会话文档。

绑定模型输入和输出:

若要绑定输入和输出,请使用 LearningModelBinding 类。 机器学习模型具有输入和输出特征,用于将信息传入和传出模型。 请注意,Window ML API 必须支持必需的功能。 在 LearningModelSession 上应用 LearningModelBinding 类以将值绑定到已命名的输入和输出特征。

绑定的实现由 mlgen 自动生成,因此你无需执行此操作。 绑定通过调用 LearningModelBinding 类的预定义方法来实现。 在我们的示例中,它使用 Bind 方法将值绑定到已命名的功能类型。

评估模型:

创建会话以将模型和已绑定的值绑定到模型的输入和输出后,可以评估模型的输入并获取其预测。 要运行模型执行,应在 LearningModelSession 上调用任意预定义的评估方法。 在本例中,我们将使用 EvaluateAsync 方法。

CreateFromStreamAsync 类似,EvaluateAsync 方法也由 WinML 代码生成器自动生成,因此你无需实现此方法。 可在 bestModel.cs 文件中查看此方法。

EvaluateAsync 方法将使用绑定中已绑定的特征值异步评估计算机学习模型。 它将通过 LearningModelSession 创建一个会话,通过 LearningModelBinding 绑定输入和输出,执行模型评估,并使用 LearningModelEvaluationResult 类获得模型的输出功能。

注意

要了解运行模型的其他评估方法,请通过查看 LearningModelSession 类文档,检查哪些方法可以在 LearningModelSession 上实现。

  1. 将以下方法添加到 MainPage 类内的 MainPage.xaml.cs 代码文件中,以创建会话、绑定和评估模型。
        // A method to evaluate the model
        private async Task evaluate()
        {
            results = await modelGen.EvaluateAsync(image);
        }

提取并显示结果

现在需要提取模型输出并显示正确的结果,这将通过 extractResultdisplayResult 方法来实现。 需要找出返回正确标签的最高概率。

  1. extractResult 方法添加到 MainPage 类中的 MainPage.xaml.cs 代码文件。
        // A method to extract output from the model 
        private void extractResult()
        {
            // Retrieve the results of evaluation
            var mResult = results.modelOutput as TensorFloat;
            // convert the result to vector format
            var resultVector = mResult.GetAsVectorView();
            
            probability = 0;
            int index = 0;
            // find the maximum probability
            for(int i=0; i<resultVector.Count; i++)
            {
                var elementProbability=resultVector[i];
                if (elementProbability > probability)
                {
                    index = i;
                }
            }
            label = ((Labels)index).ToString();
        }
  1. displayResult 方法添加到 MainPage 类中的 MainPage.xaml.cs 代码文件。
        private async Task displayResult() 
        {
            displayOutput.Text = label; 
        }

就是这样! 你已成功创建了包含基本 GUI 的 Windows 机器学习应用,以测试我们的分类模型。 下一步是启动应用程序并在 Windows 设备的本地运行该应用程序。

启动应用程序

完成应用程序接口、添加模型并生成 Windows ML 代码后,便可以测试应用程序了!

启用开发人员模式并从 Visual Studio 测试应用程序。 确保顶部工具栏中的下拉菜单设置为 Debug。 将解决方案平台更改为 x64(如果设备是 64 位的)或 x86(如果设备是 32 位的)以在你的本地计算机上运行该项目。

若要测试应用,请使用以下汤的图像。 让我们看看应用如何对图像内容进行分类。

Image for application testing

  1. 将此图像保存在本地设备上,以测试应用。 如果需要,将图像格式更改为 .jpg。 你还可以添加本地设备上任何格式为 .jpg.png 的其他相关图像。

  2. 要运行项目,请选择工具栏上的 Start Debugging 按钮,或按 F5

  3. 当应用程序启动时,按 Pick Image 并从本地设备中选择图像。

Application interface

结果会立即显示在屏幕上。 如你所见,Windows ML 应用成功地将图像归类为汤。

Successful classification in your app

总结

你刚刚完成了首个 Windows 机器学习应用从模型创建到成功执行的过程。

其他资源

若要详细了解本教程中所述的主题,请访问以下资源: