Share via


Windows.AI.MachineLearning Namespace

Enables apps to load machine learning models, bind features, and evaluate the results.

Classes

ImageFeatureDescriptor

Describes the properties of the image the model is expecting.

ImageFeatureValue

Describes the properties of the image used to pass into a model.

LearningModel

Represents a trained machine learning model.

LearningModelBinding

Used to bind values to named input and output features.

LearningModelDevice

The device used to evaluate the machine learning model.

LearningModelEvaluationResult

Get the results of the evaluation.

LearningModelSession

Used to evaluate machine learning models.

LearningModelSessionOptions

Describes inference options that are used during the creation of LearningModelSession objects.

MapFeatureDescriptor

A map is a collection of (key, value) pairs.

SequenceFeatureDescriptor

A sequence is an array of elements.

TensorBoolean

A boolean tensor object.

TensorDouble

A 64-bit float tensor object.

TensorFeatureDescriptor

Tensors are multi-dimensional arrays of values.

TensorFloat

A 32-bit float tensor object.

TensorFloat16Bit

A 16-bit float tensor object.

TensorInt16Bit

A 16-bit signed integer tensor object.

TensorInt32Bit

A 32-bit signed integer tensor object.

TensorInt64Bit

A 64-bit signed integer tensor object.

TensorInt8Bit

An 8-bit signed integer tensor object.

TensorString

A string tensor object.

TensorUInt16Bit

A 16-bit unsigned integer tensor object.

TensorUInt32Bit

A 32-bit unsigned integer tensor object.

TensorUInt64Bit

A 64-bit unsigned integer tensor object.

TensorUInt8Bit

A 8-bit unsigned integer tensor object.

Interfaces

ILearningModelFeatureDescriptor

Describes the common properties that all features have.

ILearningModelFeatureValue

The instantiated value for a feature.

ILearningModelOperatorProvider

Describes the operaators for a learning model.

ITensor

Tensors are multi-dimensional values.

Enums

LearningModelDeviceKind

Defines the list of device kinds that can evaluate a machine learning model.

LearningModelFeatureKind

Input and output feature kinds for a machine learning model.

LearningModelPixelRange

Defines the list of image nominal pixel range suppored by Windows ML. The proper value is specified in a machine learning model's metadata.

TensorKind

Defines the list of supported tensor data types.

Examples

The following example loads a model, creates an evaluation session, gets the input and output features of the model, binds those features, and evaluates.

private async Task LoadAndEvaluateModelAsync(VideoFrame _inputFrame, string _modelFileName)
{
    LearningModel _model;
    ImageFeatureDescriptor _inputImageDescription;
    TensorFeatureDescriptor _outputImageDescription;
    LearningModelBinding _binding = null;
    VideoFrame _outputFrame = null;
    LearningModelSession _session;

    try
    {
        // Load and create the model
        var modelFile = 
            await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_modelFileName}"));
        _model = await LearningModel.LoadFromStorageFileAsync(modelFile);

        // Create the evaluation session with the model
        _session = new LearningModelSession(_model);

        //Get input and output features of the model
        List<ILearningModelFeatureDescriptor> inputFeatures = _model.InputFeatures.ToList();
        List<ILearningModelFeatureDescriptor> outputFeatures = _model.OutputFeatures.ToList();

        // Retrieve the first input feature which is an image
        _inputImageDescription =
                inputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Image)
                as ImageFeatureDescriptor;

        // Retrieve the first output feature which is a tensor
        _outputImageDescription =
                        outputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Tensor)
                        as TensorFeatureDescriptor;

        //Create output frame based on expected image width and height
        _outputFrame = new VideoFrame(
            BitmapPixelFormat.Bgra8, 
            (int)_inputImageDescription.Width, 
            (int)_inputImageDescription.Height);

        //Create binding and then bind input/output features
        _binding = new LearningModelBinding(_session);

        _binding.Bind(_inputImageDescription.Name, _inputFrame);
        _binding.Bind(_outputImageDescription.Name, _outputFrame);

        //Evaluate and get the results
        var results = await _session.EvaluateAsync(_binding, "test");
    }
    catch (Exception ex)
    {
        StatusBlock.Text = $"error: {ex.Message}";
        _model = null;
    }
}

Remarks

Windows Server

To use this API on Windows Server, you must use Windows Server 2019 with Desktop Experience.

Thread safety

This API is thread-safe.

See also