DirectML Samples

Explore the power of DirectML with these Windows development examples.

Style Transfer Sample

This sample demonstrates how to implement a neural style transfer model using DirectML. Style transfer allows you to apply the artistic style of one image (e.g., a painting) to the content of another image (e.g., a photograph).

Overview

The neural style transfer algorithm typically involves a pre-trained convolutional neural network (CNN). This sample leverages a popular network architecture and modifies it to separate content and style representations.

  • Content Representation: Captures the semantic meaning of an image.
  • Style Representation: Captures the textures, colors, and patterns of an image.

By minimizing a loss function that combines content loss and style loss, we can generate a new image that possesses the content of one image and the style of another.

Key Components

  • DirectML API Integration: Utilizes the DirectML API for high-performance hardware acceleration of neural network operations.
  • Model Loading: Demonstrates how to load a pre-trained neural network model suitable for style transfer.
  • Tensor Operations: Shows examples of tensor manipulation, convolution, and activation functions as part of the inference pipeline.
  • Loss Function Calculation: Implements the core logic for calculating content and style losses.
  • Optimization: Includes an iterative process to optimize the output image based on the calculated losses.

Prerequisites

  • Windows 10 version 1903 or later.
  • DirectX 12 compatible GPU.
  • Visual Studio 2019 or later with the Universal Windows Platform development workload.
  • DirectML NuGet package installed in your project.

Code Snippet Example (Conceptual)

Here's a conceptual snippet illustrating the core idea of defining a loss function:

#include <dml.h> // ... (other includes and setup) DML_INPUT_DESC contentLossInputDesc = { /* ... */ }; DML_INPUT_DESC styleLossInputDesc = { /* ... */ }; DML_OPERATOR_DESC styleTransferOperatorDesc = { /* ... */ }; // Create and execute the style transfer operator // ... // Calculate content loss // ... // Calculate style loss // ... // Combine losses and backpropagate (simplified concept) // ...
Download Sample Source Code

Related Samples

Image Classification with DirectML

Learn how to classify images using pre-trained models accelerated by DirectML.

View Sample

Object Detection with DirectML

Implement object detection to identify and locate objects within images.

View Sample

Custom Operator Implementation

Explore how to define and integrate custom operators into your DirectML graphs.

View Sample