Windows ML では、GPU パスを慎重に最適化することで、モデル チェーンの高パフォーマンスの負荷と実行がサポートされます。 モデル チェーンは、順番に実行される 2 つ以上のモデルによって定義されます。ここで、1 つのモデルの出力がチェーンの次のモデルへの入力になります。
Windows ML でモデルを効率的にチェーンする方法を説明するために、FNS-Candy スタイル転送 ONNX モデルをおもちゃの例として使用してみましょう。 この種類のモデルは、 GitHub の FNS-Candy スタイル転送サンプル フォルダーにあります。
ここでは 、mosaic.onnx と呼ばれる、同じ FNS-Candy モデルの 2 つのインスタンスで構成されるチェーンを実行するとします。 アプリケーション コードは、チェーン内の最初のモデルにイメージを渡し、出力を計算し、その変換されたイメージを FNS-Candy の別のインスタンスに渡して、最終的なイメージを生成します。
次の手順は、Windows ML を使用してこれを実現する方法を示しています。
注
実際のシナリオでは、2 つの異なるモデルを使用する可能性が最も高くなりますが、概念を説明するにはこれで十分です。
- まず、 mosaic.onnx モデルを読み込んで使用できるようにします。
std::wstring filePath = L"path\\to\\mosaic.onnx";
LearningModel model = LearningModel::LoadFromFilePath(filePath);
string filePath = "path\\to\\mosaic.onnx";
LearningModel model = LearningModel.LoadFromFilePath(filePath);
- 次に、入力パラメーターと同じモデルを使用して、デバイスの既定の GPU に 2 つの同じセッションを作成してみましょう。
LearningModelSession session1(model, LearningModelDevice(LearningModelDeviceKind::DirectX));
LearningModelSession session2(model, LearningModelDevice(LearningModelDeviceKind::DirectX));
LearningModelSession session1 =
new LearningModelSession(model, new LearningModelDevice(LearningModelDeviceKind.DirectX));
LearningModelSession session2 =
new LearningModelSession(model, new LearningModelDevice(LearningModelDeviceKind.DirectX));
注
チェーンのパフォーマンス上の利点を得るには、すべてのモデルに対して同じ GPU セッションを作成する必要があります。 これを行わないと、GPU から CPU にデータが移動し、パフォーマンスが低下します。
- 次のコード行は、セッションごとにバインドを作成します。
LearningModelBinding binding1(session1);
LearningModelBinding binding2(session2);
LearningModelBinding binding1 = new LearningModelBinding(session1);
LearningModelBinding binding2 = new LearningModelBinding(session2);
- 次に、最初のモデルの入力をバインドします。 このモデルと同じパスにある画像を渡します。 この例では、イメージは "fish_720.png" と呼ばれます。
//get the input descriptor
ILearningModelFeatureDescriptor input = model.InputFeatures().GetAt(0);
//load a SoftwareBitmap
hstring imagePath = L"path\\to\\fish_720.png";
// Get the image and bind it to the model's input
try
{
StorageFile file = StorageFile::GetFileFromPathAsync(imagePath).get();
IRandomAccessStream stream = file.OpenAsync(FileAccessMode::Read).get();
BitmapDecoder decoder = BitmapDecoder::CreateAsync(stream).get();
SoftwareBitmap softwareBitmap = decoder.GetSoftwareBitmapAsync().get();
VideoFrame videoFrame = VideoFrame::CreateWithSoftwareBitmap(softwareBitmap);
ImageFeatureValue image = ImageFeatureValue::CreateFromVideoFrame(videoFrame);
binding1.Bind(input.Name(), image);
}
catch (...)
{
printf("Failed to load/bind image\n");
}
//get the input descriptor
ILearningModelFeatureDescriptor input = model.InputFeatures[0];
//load a SoftwareBitmap
string imagePath = "path\\to\\fish_720.png";
// Get the image and bind it to the model's input
try
{
StorageFile file = await StorageFile.GetFileFromPathAsync(imagePath);
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
VideoFrame videoFrame = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
ImageFeatureValue image = ImageFeatureValue.CreateFromVideoFrame(videoFrame);
binding1.Bind(input.Name, image);
}
catch
{
Console.WriteLine("Failed to load/bind image");
}
- 最初のモデルの評価の出力をチェーン内の次のモデルで使用するには、チェーンのマーカーを設定するために、空の出力テンソルを作成してその出力をバインドする必要があります。
//get the output descriptor
ILearningModelFeatureDescriptor output = model.OutputFeatures().GetAt(0);
//create an empty output tensor
std::vector<int64_t> shape = {1, 3, 720, 720};
TensorFloat outputValue = TensorFloat::Create(shape);
//bind the (empty) output
binding1.Bind(output.Name(), outputValue);
//get the output descriptor
ILearningModelFeatureDescriptor output = model.OutputFeatures[0];
//create an empty output tensor
List<long> shape = new List<long> { 1, 3, 720, 720 };
TensorFloat outputValue = TensorFloat.Create(shape);
//bind the (empty) output
binding1.Bind(output.Name, outputValue);
注
出力をバインドするときは、 TensorFloat データ型を使用する必要があります。 これにより、最初のモデルの評価が完了すると、テンソル化解除が行われなくなり、2 つ目のモデルの読み込みとバインド操作に対する追加の GPU キューも回避されます。
- 次に、最初のモデルの評価を実行し、その出力を次のモデルの入力にバインドします。
//run session1 evaluation
session1.EvaluateAsync(binding1, L"");
//bind the output to the next model input
binding2.Bind(input.Name(), outputValue);
//run session2 evaluation
auto session2AsyncOp = session2.EvaluateAsync(binding2, L"");
//run session1 evaluation
await session1.EvaluateAsync(binding1, "");
//bind the output to the next model input
binding2.Bind(input.Name, outputValue);
//run session2 evaluation
LearningModelEvaluationResult results = await session2.EvaluateAsync(binding2, "");
- 最後に、次のコード行を使用して、両方のモデルを実行した後に生成された最終的な出力を取得してみましょう。
auto finalOutput = session2AsyncOp.get().Outputs().First().Current().Value();
var finalOutput = results.Outputs.First().Value;
それです! どちらのモデルも、使用可能な GPU リソースを最大限に活用することで順番に実行できるようになりました。
注
Windows ML のヘルプについては、次のリソースを使用してください。
- Windows ML に関する技術的な質問や回答を行うには、Stack Overflowの windows-machine-learning タグを使用してください。
- バグを報告するには、GitHubで問題を報告してください。