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,最近將會和你們見面

相關文章
相關標籤/搜索