This sample demonstrates how to use Windows ML in a Windows Forms desktop application for real-time image classification. The application provides a familiar Windows Forms interface for selecting images, configuring execution providers, and viewing classification results.
- Image File Selection - Browse and select images for classification using standard OpenFileDialog
- Execution Provider Configuration - View available providers (NPU, GPU, CPU) and configure download settings
- Real-time Inference - Classify images using the SqueezeNet model with immediate results
- Results Display - Show top 5 predictions with confidence percentages in formatted output
- Image Preview - Display selected image in PictureBox control
- Provider Information - Display detailed execution provider status and capabilities
- Windows Forms - Windows .NET-based desktop UI framework
- Windows ML - ONNX Runtime integration via Windows App SDK
- Shared Helpers - Uses
WindowsML.Sharedlibrary for consistent ML operations
- Open the main solution:
WindowsML-Samples.sln - Set the
WindowsMLWinFormsSampleproject as the startup project - Build and run (F5)
- Launch Application - The app loads and displays available execution providers
- Configure Providers - Optionally check "Allow Provider Download" to enable automatic EP downloads
- Select Image - Click "Select Image" to browse for an image file (.jpg, .jpeg, .png)
- Preview Image - Selected image appears in the picture box
- Run Classification - Click "Run Inference" to classify the selected image
- View Results - See the top 5 predictions with confidence percentages in the text area
Top 5 Predictions:
-------------------------------------------
Label Confidence
-------------------------------------------
French bulldog 45.07%
bull mastiff 35.05%
boxer 2.85%
pug 1.01%
American Staffordshire terrier 0.57%
-------------------------------------------
The sample uses the shared WindowsML.Shared library for consistency across samples:
// Initialize execution providers
var catalog = ExecutionProviderCatalog.GetDefault();
await ModelManager.InitializeExecutionProvidersAsync(allowDownload: allowDownload);
// Load and preprocess image
using var videoFrame = await ImageProcessor.LoadImageFileAsync(_selectedImagePath);
var inputTensor = await ImageProcessor.PreprocessImageAsync(videoFrame);
// Run inference
using var results = InferenceEngine.RunInference(_session, inputTensor);
var resultTensor = InferenceEngine.ExtractResults(_session, results);
// Process results
var topPredictions = ResultProcessor.GetTopPredictions(resultTensor, _labels, 5);- WPF Version: cs-wpf - Same functionality with WPF UI framework
- WinUI 3 Version: cs-winui - WinUI 3 packaged application
- Console Version: cs/CSharpConsoleDesktop - Command-line interface
This sample uses the SqueezeNet model:
- Purpose: Image classification with 1000 categories
- Input: 224x224 RGB images
- Output: Probability distribution over ImageNet classes
- Size: Lightweight model optimized for performance