Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Developing Universal Windows app using WinML model exported from Custom Vision

[日本語版はこちら]

ONNX-based WinML format enables to add ML model into your Windows Universal (UWP) app without using Rest API call.

To create WinML-type model in Custom Vision Service, select "compact" type domain when create new project. Or just change domain type to "compact" from settings and re-train model.

Once compact-type model trained, it should be downloadable from "export" button. Select "WinML" and download "*.onnx" file.

Visual Studio tools for AI (for free) with Visual Studio 2017 is also great tool for ONNX support, it automatically generate class libraries only to add ONNX file to Visual Studio project. Better to be visible to replace long Custom vision model name to friendly name ...

Auto-generated library uses Windows.AI.MachineLearning library. It works as to create LearningModel (Create_*modelname*_Model method), evaluate ModelInput and return ModelOutput. ModelInput is defined as VideoFrame.

Here is my sample code how to call my onnx model named as "FruitDetection.onnx";

private async Task EvaluateVideoFrameAsync(VideoFrame inputFrame)
{
    if (inputFrame != null)
    {
        try
        {
            // Get ONNX file
            string modelPath
                = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "FruitDetection.onnx");
            var modelFile = await StorageFile.GetFileFromPathAsync(modelPath);

            // Create WinML Model
            var model = new FruitDetectionModel();
            var learninModel = await model.CreateFruitDetectionModel(modelFile);

            // Set image(VideoFrame)
            var input = new FruitDetectionModelInput
            {
                data = inputFrame
            };

            // Detect image
            var output = await learninModel.EvaluateAsync(input);

            ResultText.Text = output.classLabel[0];
            ResultText.Visibility = Visibility.Visible;
        }
        catch(Exception ex)
        {
        }
    }
}

This "FruitDetection.onnx" is fruits detection model created by Custom Vision Service as below;

Here is sample UWP app (whole source code) using this onnx model to detect photo selected from local files.

Share the post

Developing Universal Windows app using WinML model exported from Custom Vision

×

Subscribe to Msdn Blogs | Get The Latest Information, Insights, Announcements, And News From Microsoft Experts And Developers In The Msdn Blogs.

Get updates delivered right to your inbox!

Thank you for your subscription

×