Windows.AI.MachineLearning 命名空間

可讓應用程式載入機器學習模型、系結功能,以及評估結果。

類別

ImageFeatureDescriptor

描述模型預期之影像的屬性。

ImageFeatureValue

描述用來傳入模型的影像屬性。

LearningModel

表示定型的機器學習模型。

LearningModelBinding

用來將值系結至具名輸入和輸出功能。

LearningModelDevice

用來評估機器學習模型的裝置。

LearningModelEvaluationResult

取得評估的結果。

LearningModelSession

用來評估機器學習模型。

LearningModelSessionOptions

描述 建立 LearningModelSession 物件期間所使用的推斷選項。

MapFeatureDescriptor

對應是 (索引鍵、值) 組的集合。

SequenceFeatureDescriptor

序列是元素的陣列。

TensorBoolean

布林 tensor 物件。

TensorDouble

64 位浮點數 tensor 物件。

TensorFeatureDescriptor

Tensors 是值的多維度陣列。

TensorFloat

32 位浮點數 tensor 物件。

TensorFloat16Bit

16 位浮點數 tensor 物件。

TensorInt16Bit

16 位帶正負號的整數 tensor 物件。

TensorInt32Bit

32 位帶正負號的整數 tensor 物件。

TensorInt64Bit

64 位帶正負號的整數 tensor 物件。

TensorInt8Bit

8 位帶正負號的整數 tensor 物件。

TensorString

字串 tensor 物件。

TensorUInt16Bit

16 位不帶正負號的整數 tensor 物件。

TensorUInt32Bit

32 位不帶正負號的整數 tensor 物件。

TensorUInt64Bit

64 位不帶正負號的整數 tensor 物件。

TensorUInt8Bit

8 位不帶正負號的整數 tensor 物件。

介面

ILearningModelFeatureDescriptor

描述所有功能都有的通用屬性。

ILearningModelFeatureValue

特徵的具現化值。

ILearningModelOperatorProvider

描述學習模型的操作程式。

ITensor

Tensors 是多維度值。

列舉

LearningModelDeviceKind

定義可評估機器學習模型的裝置種類清單。

LearningModelFeatureKind

機器學習模型的輸入和輸出功能種類。

LearningModelPixelRange

定義 Windows ML 所支援的影像標準像素範圍清單。 在機器學習模型的中繼資料中指定適當的值。

TensorKind

定義支援的 Tensor 資料類型清單。

範例

下列範例會載入模型、建立評估會話、取得模型的輸入和輸出功能、系結這些功能,以及評估。

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;
    }
}

備註

Windows Server

若要在 Windows Server 上使用此 API,您必須搭配桌面體驗使用 Windows Server 2019。

執行緒安全

此 API 是安全線程。

另請參閱