Windows Developer Day - Windows AI Platform
本次 Windows Developer Day,最值得期待的莫過(guò)于 Windows AI Platform 了,可以說(shuō)是千呼萬(wàn)喚始出來(lái)。觀看直播的開(kāi)發(fā)者們,留言最多的也是 Windows AI Platform。
下面結(jié)合微軟提供的展示過(guò)程,文檔和 Git Sample 來(lái)詳細(xì)分析一下。
基礎(chǔ)概念
基礎(chǔ)認(rèn)知
眾所周知,目前 AI(Artificial Intelligence)的主要實(shí)現(xiàn)方式就是機(jī)器學(xué)習(xí)(Machine Learning),而 Windows AI Platform 對(duì)應(yīng)的就是 Windows Machine Learning。
微軟官方對(duì)于它的描述如下:
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.
結(jié)合這一描述,我們可以簡(jiǎn)單總結(jié)出 Windows ML 的幾個(gè)特點(diǎn):
硬件加速? 在支持 DirectX12 的硬件設(shè)備上,Windows ML 可以利用 GPU 對(duì)模型的評(píng)估實(shí)現(xiàn)加速。
本地評(píng)估? Windows ML 可以利用本地硬件進(jìn)行模型評(píng)估,減少了模型上傳到云端造成的服務(wù)端流量成本和服務(wù)端壓力。可以更快速便捷的得到結(jié)果。
圖像處理? 在機(jī)器視覺(jué)場(chǎng)景,Windows ML 簡(jiǎn)化并優(yōu)化了圖像、視頻文件和視頻流的處理,對(duì)輸入源做預(yù)處理和攝像頭管道處理。??
?
模型格式
Windows ML 的模型格式是 ONNX,Open Neural Network Exchange,是 Microsoft 和 Facebook、Amazon 等公司制定的機(jī)器學(xué)習(xí)模型文件格式標(biāo)準(zhǔn)。在目前很多主流模型訓(xùn)練框架中,都有 ONNX 的原生支持,或者可以支持其他格式轉(zhuǎn)換為 ONNX 格式。?這里是 ONNX 的 Git 主頁(yè),大家可以詳細(xì)了解:GitHub?Open Neural Network Exchange
另外大家可以通過(guò) WinMLTools 來(lái)把其他格式的模型文件轉(zhuǎn)換為 ONNX 格式,這里是 WinMLTools 地址:Python WinMLTools 0.1.0.5072.?可以轉(zhuǎn)換的格式有 Core ML/Scikit-Learn/XGBoost/LibSVM。
另外 ONNX 支持超過(guò) 100 種運(yùn)算符,針對(duì) CPU 或 GPU 有不同的運(yùn)算符支持,這里是運(yùn)算符列表:https://github.com/onnx/onnx/blob/rel-1.0/docs/Operators.md
?
技術(shù)架構(gòu)
從這張架構(gòu)圖來(lái)看:
底層是 Direct 層的 DirectML API/Direct3D/CPU/GPU,DirectX 的版本支持是 DX12
上面一層是推斷引擎,包括了 Win32 和 WinRT 部分,主要負(fù)責(zé)模型和設(shè)備資源管理,負(fù)責(zé)加載和編輯核心操作符,執(zhí)行數(shù)據(jù)流圖
最上層是應(yīng)用程序?qū)?#xff0c;同樣包括了 Win32 和 WinRT 部分;令人欣喜的是,它在所有 2018 年的 Windows 版本上都可用
?
開(kāi)發(fā)過(guò)程
概述
目前 Windows AI Platform 還是預(yù)覽版內(nèi)容,所以需要預(yù)覽版的 Windows OS 和 WIndows 10 SDK,下面是下載地址:
Windows Insider Preview Downloads
其中 Visual Studio 的版本要求是 Community、Professional 或 Enterprise,Community 版本的獲取最為簡(jiǎn)單,建議實(shí)驗(yàn)性需求時(shí)使用這個(gè)版本。
先來(lái)看一張發(fā)布會(huì)的展示圖:
?
從上圖中可以看出整個(gè) Windows ML 的使用過(guò)程:
首先在云端或者本地服務(wù)器上訓(xùn)練模型,生成 ONNX 模型文件
把 ONNX 添加到本地開(kāi)發(fā)環(huán)境,如 Visual Studio 中
在本地程序中通過(guò) Windows 10 SDK 使用和評(píng)估 ONNX 模型的性能和學(xué)習(xí)結(jié)果
把集成了 ONNX 的本地程序發(fā)布到 Windows 序列的全平臺(tái)各種設(shè)備中
?
示例分析
Windows ML 的示例 Git 地址:GitHub Windows-Machine-Learning
上面的鏈接中也提供了 Windows Insider Preview 17110 OS、Windows 10 SDK 17110 和 Visual Studio 2017 的下載地址,按照指示我下載安裝好了開(kāi)發(fā)環(huán)境。
來(lái)看第一個(gè)示例:MNIST_Demo,是一個(gè)手寫數(shù)字識(shí)別的 UWP 程序,大家都知道,手寫數(shù)字識(shí)別是 Machine Learning 的基礎(chǔ)和入門課題,就像每種編程語(yǔ)言的 Hello World 一樣,我們借這個(gè)示例來(lái)看一下 Windows ML 對(duì)于 ONNX 模型和 Windows 10 SDK 的使用過(guò)程。
首先來(lái)看一下示例在 Visual Studio 中的工程結(jié)構(gòu):
這里我們可以看到:
Universal Windows,也就是 Windows 10 SDK 的引用版本是:10.0.17110.0,也就是 Windows ML 支持的最低版本預(yù)覽版 SDK
mnist.onnx,也就是前面說(shuō)明的 Windows ML 模型支持格式,被直接添加到了解決方案中的 Assets 文件夾中,Build Action 為 “Content”
而在?mnist.cs?文件中
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
可以看得出,目前因?yàn)檫€是預(yù)覽版本,所有命名空間包含了 Preview 的字樣,但?Windows.AI.MachineLearning?這個(gè)命名空間應(yīng)該可以確定。
來(lái)看看 Windows ML winmd 的結(jié)構(gòu):
而模型的名稱是 LearningModelPreview,來(lái)看一下類的定義:
#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#endregionusing 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; }} }
這個(gè)類包含了推斷選項(xiàng)、模型的兩種加載方式和模型評(píng)估方法。
接下來(lái)看看界面代碼中模型實(shí)際的加載方式:
private async void LoadModel() { ? ?//Load a machine learning modelStorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/MNIST.onnx"));ModelGen = await MNISTModel.CreateMNISTModel(modelFile); }??return model; }
mnist.onnx 模型文件被作為一個(gè)項(xiàng)目文件被加載到 StorageFile 中,使用?mnist 類的 CreateMNISTModel 方法,具體說(shuō)是?LearningModelPreview 類的 LoadModelFromStorageFileAsync 方法完成模型加載。
整個(gè) Sample 完成的事情就是使用 InkCanvas 獲取用戶的手寫輸入,輸入給 Windows ML 進(jìn)行檢測(cè),輸出檢測(cè)結(jié)果。來(lái)看看運(yùn)行結(jié)果:
另外發(fā)布會(huì)的展示過(guò)程中還展示了其他的 Sample,這里暫不詳細(xì)介紹,大家可以看看它完成的效果:
?
這是一個(gè)圖片藝術(shù)化風(fēng)格轉(zhuǎn)換的 Sample,類似 Prisma 的實(shí)現(xiàn)方式。尤其是第二張,是從攝像頭采集圖像的實(shí)時(shí)轉(zhuǎn)換,攝像頭圖像流的幀率應(yīng)該在 30 幀以上,依然能在本地運(yùn)行模型的情況下,完成實(shí)時(shí)轉(zhuǎn)換。這也讓我們對(duì)本地程序完成視頻風(fēng)格轉(zhuǎn)換很有信心。
?
到這里,對(duì)于 Windows AI Platform 和 Windows ML 的介紹就完成了,因?yàn)槟壳肮俜教峁┑倪€是預(yù)覽版,而且公開(kāi)的內(nèi)容還不夠多,后續(xù)我們會(huì)繼續(xù)跟進(jìn)研究,歡迎大
原文:https://www.cnblogs.com/shaomeng/p/8540860.html
.NET社區(qū)新聞,深度好文,歡迎訪問(wèn)公眾號(hào)文章匯總 http://www.csharpkit.com
總結(jié)
以上是生活随笔為你收集整理的Windows Developer Day - Windows AI Platform的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 谈谈在.NET Core中使用Redis
- 下一篇: 微软将人工智能嵌入Windows 10更