DirectML Image Classification Sample
Overview
This sample demonstrates how to perform image classification using DirectML on Windows. It leverages a pre-trained convolutional neural network (CNN) model to classify input images into predefined categories. The sample highlights the performance benefits of hardware-accelerated machine learning inference on compatible hardware.
Image classification is a fundamental task in computer vision, enabling machines to identify and categorize objects or scenes within an image. This sample provides a practical implementation showcasing the integration of AI models with the Windows ecosystem via DirectML.
View on GitHubKey Features
- Hardware Acceleration: Utilizes DirectML for fast, hardware-accelerated inference on GPUs.
- Pre-trained Model: Integrates a popular pre-trained image classification model (e.g., ResNet, MobileNet).
- Cross-platform Compatibility: Runs on any Windows device with a DirectML-compatible GPU.
- Ease of Integration: Provides a clear structure for integrating machine learning models into Windows applications.
- Input/Output Handling: Demonstrates how to load images, preprocess them for the model, and interpret the classification output.
Technical Details
Programming Language
C++
DirectML API
Uses the DirectML C++ API for model execution.
Model Format
Supports ONNX (Open Neural Network Exchange) format.
Dependencies
Windows SDK, DirectML, DirectX Graphics Infrastructure (DXGI).
Code Snippet Example
Here's a glimpse into how model inference might be initiated:
// Initialize DirectML Device and Operator Session
auto device = dxaml::CreateDevice(); // Assuming a helper for DirectML device creation
auto session = dxaml::CreateOperatorSession(device, modelPath);
// Prepare Input Tensor
DML_BUFFER_BINDING inputBinding = { inputBuffer.Get(), nullptr };
DML_INPUT_DATA_PARALLELIZATION_COLUMNS inputParallelization = { 0 };
// Create Input Tensor Descriptor
DML_BUFFER_ARRAY_REFERENCE inputRefs[] = { { inputBinding } };
DML_TENSOR_DESC inputTensorDesc = {
DML_TENSOR_DATA_TYPE_FLOAT32,
DML_TENSOR_LAYOUT_NCHW,
{ 1, 3, 224, 224 }, // Example dimensions (batch, channels, height, width)
0 // Placeholder for size in bytes, calculated later
};
// Prepare Output Tensor
DML_BUFFER_BINDING outputBinding = { outputBuffer.Get(), nullptr };
// Create Output Tensor Descriptor
DML_TENSOR_DESC outputTensorDesc = {
DML_TENSOR_DATA_TYPE_FLOAT32,
DML_TENSOR_LAYOUT_NCHW,
{ 1, 1000, 1, 1 }, // Example dimensions for 1000 classes
0
};
// Create Binding Table
std::vector bindings;
bindings.push_back({ DML_BINDING_TYPE_INPUT_DATA, &inputDesc });
bindings.push_back({ DML_BINDING_TYPE_OUTPUT_DATA, &outputDesc });
// Submit Batch and Execute
session->Execute(bindings.data(), bindings.size(), nullptr);
// Process Output
// ... interpret classification results ...
Getting Started
To build and run this sample, you will need:
- A Windows 10 (version 1903 or later) or Windows 11 PC.
- A compatible GPU with drivers that support DirectML.
- Visual Studio 2019 or later with the "Desktop development with C++" workload installed.
Clone the DirectML samples repository from GitHub and navigate to the ImageClassification
folder to find the complete project.