Share via


LearningModelBinding.Bind メソッド

定義

オーバーロード

Bind(String, Object)

名前付き機能に値をバインドします。

Bind(String, Object, IPropertySet)

プロパティを使用して値を名前付き機能にバインドし、バインドを制御します。

Bind(String, Object)

名前付き機能に値をバインドします。

public:
 virtual void Bind(Platform::String ^ name, Platform::Object ^ value) = Bind;
/// [Windows.Foundation.Metadata.Overload("Bind")]
void Bind(winrt::hstring const& name, IInspectable const& value);
[Windows.Foundation.Metadata.Overload("Bind")]
public void Bind(string name, object value);
function bind(name, value)
Public Sub Bind (name As String, value As Object)

パラメーター

name
String

Platform::String

winrt::hstring

機能の名前。

value
Object

Platform::Object

IInspectable

バインドする値。

属性

次の例では、最初の入力フィーチャと出力フィーチャを取得し、出力フレームを作成し、入力フィーチャと出力フィーチャをバインドして、回避します。

private async Task EvaluateModelAsync(
    VideoFrame _inputFrame, 
    LearningModelSession _session, 
    IReadOnlyList<ILearningModelFeatureDescriptor> _inputFeatures, 
    IReadOnlyList<ILearningModelFeatureDescriptor> _outputFeatures,
    LearningModel _model)
{
    ImageFeatureDescriptor _inputImageDescription;
    TensorFeatureDescriptor _outputImageDescription;
    LearningModelBinding _binding = null;
    VideoFrame _outputFrame = null;

    try
    {
        // 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 はスレッド セーフです。

適用対象

Bind(String, Object, IPropertySet)

プロパティを使用して値を名前付き機能にバインドし、バインドを制御します。

public:
 virtual void Bind(Platform::String ^ name, Platform::Object ^ value, IPropertySet ^ props) = Bind;
/// [Windows.Foundation.Metadata.Overload("BindWithProperties")]
void Bind(winrt::hstring const& name, IInspectable const& value, IPropertySet const& props);
[Windows.Foundation.Metadata.Overload("BindWithProperties")]
public void Bind(string name, object value, IPropertySet props);
function bind(name, value, props)
Public Sub Bind (name As String, value As Object, props As IPropertySet)

パラメーター

name
String

Platform::String

winrt::hstring

バインド先のフィーチャーの名前。

value
Object

Platform::Object

IInspectable

フィーチャーにバインドする値。

props
IPropertySet

バインドのテンソル化とデテンソル化の動作を記述するキーと値のペアを持つプロパティ マップ。 詳細については、「解説」セクション 参照してください。

属性

注釈

props パラメーターの有効なキーと値のペアは次のとおりです。

名前 説明
BitmapBounds PropertyType.UInt32Array 入力をバインドする場合、BitmapBounds プロパティはトリミング境界を指定します。 トリミングされたイメージが抽出され、推論を実行するための入力として使用されます。

出力をバインドする場合、BitmapBounds プロパティはターゲット出力領域の境界を指定します。 推論の結果は、バインドされたイメージ内のターゲット出力領域に書き込まれます。

BitmapBounds プロパティは、値 [left, top, width, height]を持つ UInt32 配列として指定されます。

このプロパティは、 ImageFeatureValue をバインドする場合にのみ有効です。
BitmapPixelFormat PropertyType.Int32 入力または出力をバインドする場合、BitmapPixelFormat プロパティは、特定の機能値に対してモデル作成者が意図したピクセル形式を指定します。 Image.BitmapPixelFormat メタデータが ONNX モデルの Image メタデータにない場合は、実行時にこのプロパティを使用して指定できます。 バインドされた画像は、モデルで使用するために、指定されたピクセル形式に自動的に変換されます。

BitmapPixelFormat は、 Windows.Graphics.Imaging.BitmapPixelFormat 列挙の値に対応する Int32 値として指定する必要があります。 現在、次の値がサポートされています。
  • BitmapPixelFormat.Rgba8
  • BitmapPixelFormat.Bgra8
  • BitmapPixelFormat.Gray8
このプロパティは、 ImageFeatureValue をバインドする場合にのみ有効です。
DisableTensorCpuSync PropertyType.Boolean ID3D12Resource によってサポートされる出力テンソルをバインドする場合は、DisableTensorCpuSync プロパティを使用して、GPU/NPU 出力を CPU テンソルにコピーし直すのを防ぐことができます。 既定では、 LearningModelSession.Evaluate API 呼び出しはブロック呼び出しであり、推論結果が CPU 完了後に確実に使用できるようになります。

特定の GPU/NPU 評価シナリオでは、推論結果を GPU/NPU に保持することが望ましいです。結果を CPU にコピーし直す必要が無く、時間がかかります。 このコピーを回避するには、バインド中に DisableTensorCpuSync プロパティを有効にします。

このプロパティは、 ITensor またはその具象型 ( つまり TensorFloat) をバインドする場合にのみ有効です。

このプロパティは、Windows 11 バージョン 21H2 (10.0;ビルド 22000)。
PixelRange PropertyType.Int32 入力または出力をバインドする場合、PixelRange プロパティは、特定の特徴値に対してモデル作成者が意図した正規化範囲を指定します。 Image.NominalPixelRange メタデータが ONNX モデルの Image メタデータにない場合は、実行時にこのプロパティを使用して指定できます。 バインドされたイメージは、モデルで使用するために、指定された正規化された範囲に自動的に変換されます。

PixelRange は、 Windows.AI.MachineLearning.LearningModelPixelRange 列挙の値に対応する Int32 値として指定する必要があります。

このプロパティは、 ImageFeatureValue をバインドする場合にのみ有効です。

Windows Server

Windows Server でこの API を使用するには、Windows Server 2019 デスクトップ エクスペリエンスを使用する必要があります。

スレッド セーフ

この API はスレッド セーフです。

適用対象