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 GitHub

Key Features

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:

Clone the DirectML samples repository from GitHub and navigate to the ImageClassification folder to find the complete project.