本次 Windows Developer Day,最值得期待的莫過於 Windows AI Platform 了,可以說是千呼萬喚始出來。觀看直播的開發者們,留言最多的也是 Windows AI Platform。
下面結合微軟提供的展示過程,文檔和 Git Sample 來詳細分析一下。
基礎概念
基礎認知
眾所周知,目前 AI(Artificial Intelligence)的主要實現方式就是機器學習(Machine Learning),而 Windows AI Platform 對應的就是 Windows Machine Learning。
微軟官方對於它的描述如下:
Windows Machine Learning (ML) evaluates trained machine learning models locally on Windows 10 devices, allowing developers to use pre-trained models within their applications. The platform provides hardware-accelerated performance by leveraging the device's CPU or GPU to compute evaluations for both classical Machine Learning algorithms and Deep Learning.
結合這一描述,我們可以簡單總結出 Windows ML 的幾個特點:
- 硬件加速 在支持 DirectX12 的硬件設備上,Windows ML 可以利用 GPU 對模型的評估實現加速。
- 本地評估 Windows ML 可以利用本地硬件進行模型評估,減少了模型上傳到雲端造成的服務端流量成本和服務端壓力。可以更快速便捷的得到結果。
- 圖像處理 在機器視覺場景,Windows ML 簡化並優化了圖像、視頻文件和視頻流的處理,對輸入源做預處理和攝像頭管道處理。
模型格式
Windows ML 的模型格式是 ONNX,Open Neural Network Exchange,是 Microsoft 和 Facebook、Amazon 等公司制定的機器學習模型文件格式標准。在目前很多主流模型訓練框架中,都有 ONNX 的原生支持,或者可以支持其他格式轉換為 ONNX 格式。 這里是 ONNX 的 Git 主頁,大家可以詳細了解:GitHub Open Neural Network Exchange
另外大家可以通過 WinMLTools 來把其他格式的模型文件轉換為 ONNX 格式,這里是 WinMLTools 地址:Python WinMLTools 0.1.0.5072. 可以轉換的格式有 Core ML/Scikit-Learn/XGBoost/LibSVM。
另外 ONNX 支持超過 100 種運算符,針對 CPU 或 GPU 有不同的運算符支持,這里是運算符列表:https://github.com/onnx/onnx/blob/rel-1.0/docs/Operators.md
技術架構
從這張架構圖來看:
- 底層是 Direct 層的 DirectML API/Direct3D/CPU/GPU,DirectX 的版本支持是 DX12
- 上面一層是推斷引擎,包括了 Win32 和 WinRT 部分,主要負責模型和設備資源管理,負責加載和編輯核心操作符,執行數據流圖
- 最上層是應用程序層,同樣包括了 Win32 和 WinRT 部分;令人欣喜的是,它在所有 2018 年的 Windows 版本上都可用
開發過程
概述
目前 Windows AI Platform 還是預覽版內容,所以需要預覽版的 Windows OS 和 WIndows 10 SDK,下面是下載地址:
Windows Insider Preview Downloads
其中 Visual Studio 的版本要求是 Community、Professional 或 Enterprise,Community 版本的獲取最為簡單,建議實驗性需求時使用這個版本。
先來看一張發布會的展示圖:
從上圖中可以看出整個 Windows ML 的使用過程:
- 首先在雲端或者本地服務器上訓練模型,生成 ONNX 模型文件
- 把 ONNX 添加到本地開發環境,如 Visual Studio 中
- 在本地程序中通過 Windows 10 SDK 使用和評估 ONNX 模型的性能和學習結果
- 把集成了 ONNX 的本地程序發布到 Windows 序列的全平台各種設備中
示例分析
Windows ML 的示例 Git 地址:GitHub Windows-Machine-Learning
上面的鏈接中也提供了 Windows Insider Preview 17110 OS、Windows 10 SDK 17110 和 Visual Studio 2017 的下載地址,按照指示我下載安裝好了開發環境。
來看第一個示例:MNIST_Demo,是一個手寫數字識別的 UWP 程序,大家都知道,手寫數字識別是 Machine Learning 的基礎和入門課題,就像每種編程語言的 Hello World 一樣,我們借這個示例來看一下 Windows ML 對於 ONNX 模型和 Windows 10 SDK 的使用過程。
首先來看一下示例在 Visual Studio 中的工程結構:
這里我們可以看到:
- Universal Windows,也就是 Windows 10 SDK 的引用版本是:10.0.17110.0,也就是 Windows ML 支持的最低版本預覽版 SDK
- mnist.onnx,也就是前面說明的 Windows ML 模型支持格式,被直接添加到了解決方案中的 Assets 文件夾中,Build Action 為 “Content”
而在 mnist.cs 文件中
using System; using System.Collections.Generic; using System.Threading.Tasks; using Windows.Media; using Windows.Storage; using Windows.AI.MachineLearning.Preview; ...
...
public sealed class MNISTModel
{
private LearningModelPreview learningModel;
...
我們可以看到,Windows ML 的命名空間是:Windows.AI.MachineLearning.Preview
可以看得出,目前因為還是預覽版本,所有命名空間包含了 Preview 的字樣,但 Windows.AI.MachineLearning 這個命名空間應該可以確定。
來看看 Windows ML winmd 的結構:
而模型的名稱是 LearningModelPreview,來看一下類的定義:
#region 程序集 Windows.AI.MachineLearning.Preview.MachineLearningPreviewContract, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime // C:\Program Files (x86)\Windows Kits\10\References\10.0.17110.0\Windows.AI.MachineLearning.Preview.MachineLearningPreviewContract\1.0.0.0\Windows.AI.MachineLearning.Preview.MachineLearningPreviewContract.winmd #endregion using System.Collections.Generic; using Windows.Foundation; using Windows.Foundation.Metadata; using Windows.Storage; using Windows.Storage.Streams; namespace Windows.AI.MachineLearning.Preview { [ContractVersion(typeof(MachineLearningPreviewContract), 65536)] [Static(typeof(ILearningModelPreviewStatics), 65536, "Windows.AI.MachineLearning.Preview.MachineLearningPreviewContract")] public sealed class LearningModelPreview : ILearningModelPreview { [RemoteAsync] public IAsyncOperation<LearningModelEvaluationResultPreview> EvaluateAsync(LearningModelBindingPreview binding, string correlationId); [RemoteAsync] public IAsyncOperation<LearningModelEvaluationResultPreview> EvaluateFeaturesAsync(IDictionary<string, object> features, string correlationId); [RemoteAsync] public static IAsyncOperation<LearningModelPreview> LoadModelFromStorageFileAsync(IStorageFile modelFile); [RemoteAsync] public static IAsyncOperation<LearningModelPreview> LoadModelFromStreamAsync(IRandomAccessStreamReference modelStream); public InferencingOptionsPreview InferencingOptions { get; set; } public LearningModelDescriptionPreview Description { get; } } }
這個類包含了推斷選項、模型的兩種加載方式和模型評估方法。
接下來看看界面代碼中模型實際的加載方式:
private async void LoadModel() { //Load a machine learning model StorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/MNIST.onnx")); ModelGen = await MNISTModel.CreateMNISTModel(modelFile); }
public static async Task<MNISTModel> CreateMNISTModel(StorageFile file) { LearningModelPreview learningModel = await LearningModelPreview.LoadModelFromStorageFileAsync(file); MNISTModel model = new MNISTModel(); model.learningModel = learningModel; return model; }
mnist.onnx 模型文件被作為一個項目文件被加載到 StorageFile 中,使用 mnist 類的 CreateMNISTModel 方法,具體說是 LearningModelPreview 類的 LoadModelFromStorageFileAsync 方法完成模型加載。
整個 Sample 完成的事情就是使用 InkCanvas 獲取用戶的手寫輸入,輸入給 Windows ML 進行檢測,輸出檢測結果。來看看運行結果:
另外發布會的展示過程中還展示了其他的 Sample,這里暫不詳細介紹,大家可以看看它完成的效果:
這是一個圖片藝術化風格轉換的 Sample,類似 Prisma 的實現方式。尤其是第二張,是從攝像頭采集圖像的實時轉換,攝像頭圖像流的幀率應該在 30 幀以上,依然能在本地運行模型的情況下,完成實時轉換。這也讓我們對本地程序完成視頻風格轉換很有信心。
到這里,對於 Windows AI Platform 和 Windows ML 的介紹就完成了,因為目前官方提供的還是預覽版,而且公開的內容還不夠多,后續我們會繼續跟進研究,歡迎大家一起討論,謝謝!