本次 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 主页,大家可以详细了解:GitHubOpen 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), )]
[Static(typeof(ILearningModelPreviewStatics), , "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 的介绍就完成了,因为目前官方提供的还是预览版,而且公开的内容还不够多,后续我们会继续跟进研究,欢迎大家一起讨论,谢谢!

Windows Developer Day - Windows AI Platform的更多相关文章

  1. Windows Developer Day Review

    北京时间 3 月 8 日凌晨 1 点钟,今年的第一次 Windows Developer Day 正式召开.    因为时间太晚看不了直播,我也是第二天早上在公司看的重播.整个会议过程有很多值得去研究 ...

  2. Windows Developer Day - Adaptive Cards

    概述 Windows Developer Day 在 Modern Application Experience 环节展示了一种可以让开发者以更通用和统一的方式来对卡片对展示和交互的方式,那就是:Ad ...

  3. Windows Developer Day - MSIX and Advanced Installer

    前面一篇我们介绍了 Adaptive Cards 的基础知识,而在 Windows Developer Day 的 Modern Application Experience 环节,还有一个需要划重点 ...

  4. Unity Game Starter Kit for Windows Store and Windows Phone Store games

    原地址:http://digitalerr0r.wordpress.com/2013/09/30/unity-game-starter-kit-for-windows-store-and-window ...

  5. 第一篇 Windows 8 开发Windows Metro style app环境配置

    半   饱问 题 到 我 这 里 为 止! 第一篇 Windows 8 开发Windows Metro style app环境配置 2012-09-24 08:24 by 半饱, 1289 阅读, 3 ...

  6. 玩转Windows服务系列——Windows服务小技巧

    伴随着研究Windows服务,逐渐掌握了一些小技巧,现在与大家分享一下. 将Windows服务转变为控制台程序 由于默认的Windows服务程序,编译后为Win32的窗口程序.我们在程序启动或运行过程 ...

  7. 玩转Windows服务系列——Windows服务启动超时时间

    最近有客户反映,机房出现断电情况,服务器的系统重新启动后,数据库服务自启动失败.第一次遇到这种情况,为了查看是不是断电情况导致数据库文件损坏,从客户的服务器拿到数据库的日志,进行分析. 数据库工作机制 ...

  8. 渗透杂记-2013-07-13 Windows XP SP2-SP3 / Windows Vista SP0 / IE 7

    Welcome to the Metasploit Web Console! | | _) | __ `__ \ _ \ __| _` | __| __ \ | _ \ | __| | | | __/ ...

  9. C# 编写Windows Service(windows服务程序)【转载】

    [转]http://www.cnblogs.com/bluestorm/p/3510398.html Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成 ...

随机推荐

  1. C++数据结构学习之顺序表

    顺序表是数据结构中最基本也是应用相当广泛的一种数据结构类型.它通常包含三个私有成分,即指向数据数组的头指针.当前表长以及表的实际容量.表的头指针通常指向数据数组的基地址,通过数组的形式进行访问数据数组 ...

  2. 树莓派3B上部署运行.net core 2程序

    针对Linxu arm处理器如何部署.net core 2的资料很少,网上找到几篇但都写得不够详细,按照他们教程来撞墙了,折磨了几天终于部署成功了,先上一张运行成功的图 1.windows系统中,在项 ...

  3. vue环境搭建与创建第一个vuejs文件

    我们在前端学习中,学会了HTML.CSS.JS之后一般会选择学习一些框架,比如Jquery.AngularJs等.这个系列的博文是针对于学习Vue.js的同学展开的. 1.如何简单地使用Vue.js ...

  4. 关闭sublime自动检测更新提示

    在使用sublime text 3的时候,有自动更新的话再打开的时候总是提醒更新,这让我这个强迫症重度患者非常难受,要取消自动检查更新,点击菜单栏"Preferences"=> ...

  5. 在CentOS 7中安装Jetty服务器

    Jetty 是一款纯Java的HTTP (Web) 服务器和Java Servlet容器. 通常在更大的网络框架中,Jetty经常用于设备间的通信,而其他Web服务器通常给"人类" ...

  6. PH日期格式化

    %M 月名字(January--December) %W 星期名字(Sunday--Saturday) %D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等.) %Y 年, 数字, 4 位 ...

  7. 微信小程序AES加密解密

    微信小程序  其实在调用一些第三方 js的时候 其实没有大家想的那么复杂,无非就是把原生js调用方式   改为微信小程序 js调用方式 废话不多说直接贴代码 其实就是将原生function 或者 对象 ...

  8. Python:main函数

    什么是函数? 一个程序可以包含多个模块,程序越复杂,包含的模块和功能就越多,模块细分里面包含多个类,类这个概念在任何一门面向对象语言里面都很重要,在类里面最主要的就是包含的函数,函数式实现某一个功能的 ...

  9. PowerDesigner Constraint name uniqueness 问题处理(转载)

    使用PowerDesigner生成数据库脚本时报 Constraint name uniqueness 错误: 双击每行错误,发现外键引用的名字有重复的: 惯性去网上找解决办法,找到的主要是两个方法: ...

  10. Visual Studio 2017 Enterprise 发布 15.3.3 版,附离线安装包百度网盘下载。

    Visual Studio 2017 Enterprise 发布 15.3.3 版,附离线安装包百度网盘下载. Visual Studio 2017 Enterprise 更新至 15.3.3 ,本安 ...