DirectML Samples

Image Classification with DirectML

This sample demonstrates how to perform image classification using DirectML, leveraging modern neural network architectures for high-performance inference on Windows. It showcases the power and flexibility of DirectML for accelerating machine learning tasks directly on the GPU.

GPU Acceleration

Utilizes DirectML for high-speed inference, offloading computation to the GPU for significantly faster results.

Modern Architectures

Supports popular convolutional neural network (CNN) models like ResNet, MobileNet, and custom architectures.

Cross-Platform Ready

Built with Windows in mind, but the DirectML API enables portability across various hardware vendors.

Flexible Deployment

Integrates seamlessly into C++ applications, enabling deployment in diverse scenarios.

Key Components

The sample is structured to provide a clear understanding of the DirectML inference pipeline:

  • Model Loading: Demonstrates loading pre-trained ONNX models or custom DirectML graph definitions.
  • Input Preparation: Covers preprocessing input images (resizing, normalization) to match model requirements.
  • DirectML Operator Execution: Shows how to bind input/output tensors and execute the DirectML inference graph.
  • Output Interpretation: Explains how to parse the model's output to determine the classified image category.

Getting Started

To run this sample, you will need:

  • Windows 10 version 1903 or later.
  • A compatible GPU with WDDM 2.5 or later.
  • Visual Studio 2019 or later.
  • DirectX 12 Agility SDK (recommended).

Follow the detailed setup instructions in the accompanying project README file.

Sample Code Snippet (C++)

#include <DirectML.h>
#include <dxgi1_6.h>
#include <wrl/client.h>

using namespace Microsoft::WRL;

void RunImageClassification(const char* modelPath, const char* imagePath) {

// 1. Initialize DirectML Device and Command Queue
ComPtr<IDMLDevice> dmlDevice;
ComPtr<ID3D12CommandQueue> commandQueue;
// ... Device and Queue Initialization ...

// 2. Load ONNX Model
ComPtr<IDMLOperatorInitializer> initializer;
ComPtr<IDMLCompiledOperator> compiledOperator;
// ... Model Loading and Compilation ...

// 3. Prepare Input Tensor from Image
ComPtr<ID3D12Resource> inputTensor;
// ... Image Preprocessing and Tensor Upload ...

// 4. Prepare Output Tensor
ComPtr<ID3D12Resource> outputTensor;
// ... Output Tensor Allocation ...

// 5. Create Operator Graph and Dispatch
ComPtr<IDMLCommandRecorder> commandRecorder;
ComPtr<ID3D12CommandList> commandList;
// ... Dispatch Operator and Execute Command List ...

// 6. Read Back Output and Interpret Results
// ... Read Output Tensor and Map to Classes ...
printf("Classification Result: Dog\n");
}
Download Sample Project