Windows ML,系统内置的机器学习平台初探

人工智能如今很火,虽然最近风头隐隐有被区块链盖过,但还是将来技术转型的首选方向之一。做为AI核心的机器学习,目前也进化到了能够基于平台自动训练模型的地步,例如Azure Machine Learning Service和Google AutoML Service。这使得训练模型的难度大大下降,开发人员能够分出更多精力关注在训练好的模型应用上。git

在这种背景下,各个操做系统平台纷纷推出内置的机器学习框架/运行环境,iOS有CoreML,Android有TensorFlow。Windows在最近的RS4(build 1803)更新以后,也正式内置了机器学习平台- Windows MLgithub

 

Windows ML是什么?


 

Windows ML是Windows全新的内置机器学习平台,用于本机执行预训练的机器学习模型,并提供了API容许咱们快速集成到应用中。web

它的亮点以下:windows

  • 支持硬件加速

在兼容DirectX 12的设备上能够直接使用GPU加速运算,确保机器学习模型能够被高效执行。网络

  • 本机执行

不依赖于任何远程服务,不受任何网络链接限制,本机便可达到低延迟高性能的执行效果。框架

  • 图像处理优化

针对计算机视觉场景,对视频、图像和相机数据统一预处理为VideoFrame形式,简化图像处理流程。机器学习

 

模型要求


 

 

 

Windows ML目前仅支持执行ONNX格式模型,其余格式须要预先转换后再使用。async

ONNX是由微软、Facebook和英特尔等公司推出的一个通用开放的机器学习模型格式,官方支持现有机器学习框架对其转换。ONNX项目地址ide

支持转换的现有模型来源:工具

  • Core ML
  • Scikit-Learn
  • XGBoost
  • LibSVM

使用的转换工具为微软提供的WinMLTools:https://pypi.org/project/winmltools/

转换工具使用教程请参考官方文档:https://docs.microsoft.com/en-us/windows/uwp/machine-learning/conversion-samples 

 

代码生成


 

在安装了Windows SDK Build 17110或更新版本后,默认会为Visual Studio 2017项目添加模型代码生成工具mlgen.exe。它能够根据添加的ONNX模型文件,Visual Studio 2017 Preview自动生成C#/CX的定义文件,方便代码直接调用。

这里以FNS-La-Muse模型为例,这是一个能够将图像转为特定风格的模型。

 

 

生成的代码以下:

 

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Media;
using Windows.Storage;
using Windows.AI.MachineLearning.Preview;

// FNSLaMuse

namespace Demo
{
    public sealed class FNSLaMuseModelInput
    {
        public VideoFrame inputImage { get; set; }
    }

    public sealed class FNSLaMuseModelOutput
    {
        public VideoFrame outputImage { get; set; }
        public FNSLaMuseModelOutput()
        {
            this.outputImage = VideoFrame.CreateWithSoftwareBitmap(new Windows.Graphics.Imaging.SoftwareBitmap(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, 720, 720));
        }
    }

    public sealed class FNSLaMuseModel
    {
        private LearningModelPreview learningModel;
        public static async Task<FNSLaMuseModel> CreateFNSLaMuseModel(StorageFile file)
        {
            LearningModelPreview learningModel = await LearningModelPreview.LoadModelFromStorageFileAsync(file);
            FNSLaMuseModel model = new FNSLaMuseModel();
            model.learningModel = learningModel;
            return model;
        }
        public async Task<FNSLaMuseModelOutput> EvaluateAsync(FNSLaMuseModelInput input) {
            FNSLaMuseModelOutput output = new FNSLaMuseModelOutput();
            LearningModelBindingPreview binding = new LearningModelBindingPreview(learningModel);
            binding.Bind("inputImage", input.inputImage);
            binding.Bind("outputImage", output.outputImage);
            LearningModelEvaluationResultPreview evalResult = await learningModel.EvaluateAsync(binding, string.Empty);
            return output;
        }
    }
}
View Code

 

 目前因为SDK仍在预览中,因此Visual Studio正式版并不会自动调用mlgen工具生成定义文件,须要手动执行以下命令:

mlgen -i INPUT-FILE -l LANGUAGE -n NAMESPACE [-o OUTPUT-FILE]
  • INPUT-FILE: ONNX模型文件
  • LANGUAGE: C++或者C#
  • NAMESPACE: 命名空间
  • OUTPUT-FILE: 输出路径,可缺省

 

 总结


 

有了Windows ML后咱们能够实现之前难以实现的机器学习特性,同时不用依赖外部web service,不少创新的体验能够实现,不单单是在PC,甚至在HoloLens上一样能够运用机器学习的能力。

最后给你们安利下个人开源项目- Awesome WindowsML ONNX Models ,这个项目除了提供我已经验证过的模型外,还提供了CoreML模型的快速转换工具。

 

 

 同时我也在开发为HoloLens编写的Demo,最近将会和你们见面

相关文章
相关标签/搜索