-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
320 lines (272 loc) · 11.3 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
320 lines (272 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE.md in the repo root for license information.
using Microsoft.ML.OnnxRuntime;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media.Imaging;
using Microsoft.Windows.AI.MachineLearning;
using System.Reflection;
using System.Text;
using Windows.Storage;
using WindowsML.Shared;
using Microsoft.UI.Xaml.Controls;
namespace WindowsMLSample
{
public sealed partial class MainWindow : Window, IDisposable
{
private InferenceSession? _session;
private OrtEnv? _ortEnv;
private List<string> _labels = new();
private bool _disposed = false;
public MainWindow()
{
this.InitializeComponent();
this.Activated += MainWindow_Activated;
}
private bool _hasInitialized = false;
private async void MainWindow_Activated(object sender, WindowActivatedEventArgs e)
{
if (!_hasInitialized && e.WindowActivationState != WindowActivationState.Deactivated)
{
_hasInitialized = true;
try
{
await InitializeAndRunDemoAsync();
}
catch (Exception ex)
{
ResultsText.Text = $"Error: {ex.Message}";
}
}
}
private async Task InitializeAndRunDemoAsync()
{
try
{
ResultsText.Text = "Enumerating execution providers...\n";
var catalog = ExecutionProviderCatalog.GetDefault();
var providers = catalog.FindAllProviders();
var providerInfo = new StringBuilder();
providerInfo.AppendLine("Available Execution Providers:");
providerInfo.AppendLine("========================================");
if (providers is not null)
{
foreach (var provider in providers)
{
providerInfo.AppendLine($"Provider: {provider.Name}");
providerInfo.AppendLine($" Ready state: {provider.ReadyState}");
providerInfo.AppendLine();
}
}
bool allowDownload = AllowProviderDownloadCheckBox.IsChecked ?? false;
await ModelManager.InitializeExecutionProvidersAsync(allowDownload: allowDownload);
// Create the single OrtEnv instance for this application
_ortEnv ??= ModelManager.CreateEnvironment("WindowsMLWinUISample");
var devices = _ortEnv.GetEpDevices();
PopulateEpCombo(devices);
PopulateDeviceCombo(devices);
EpCombo.SelectionChanged += EpCombo_SelectionChanged;
providerInfo.AppendLine("========================================");
providerInfo.AppendLine("Select an execution provider (and device if required) then click 'Load / Reload Model'.");
ResultsText.Text = providerInfo.ToString();
}
catch (Exception ex)
{
ResultsText.Text = $"Initialization failed: {ex.Message}";
}
}
private async Task LoadModelAsync()
{
if (EpCombo.SelectedItem == null)
{
ResultsText.Text = "Select an execution provider first.";
return;
}
bool allowDownload = AllowProviderDownloadCheckBox.IsChecked ?? false;
_session?.Dispose();
_session = null;
// Create OrtEnv only once per application instance
_ortEnv ??= ModelManager.CreateEnvironment("WindowsMLWinUISample");
await ModelManager.InitializeExecutionProvidersAsync(allowDownload: allowDownload);
var selectedEp = EpCombo.SelectedItem?.ToString();
var selectedDeviceType = (DeviceCombo.IsEnabled ? DeviceCombo.SelectedItem?.ToString() : null);
if (DeviceCombo.IsEnabled && selectedDeviceType == null)
{
ResultsText.Text = "Select a device type for the selected execution provider.";
return;
}
var appDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
?? throw new InvalidOperationException("Could not determine application directory");
var options = new Options
{
ModelPath = "SqueezeNet.onnx",
EpName = selectedEp,
DeviceType = selectedDeviceType,
PerfMode = GetSelectedPerformanceMode()
};
var (modelPath, compiledModelPath, labelsPath) = await ModelManager.ResolvePaths(options, _ortEnv);
if (!File.Exists(modelPath)) throw new FileNotFoundException($"Model file not found: {modelPath}");
if (!File.Exists(labelsPath)) throw new FileNotFoundException($"Labels file not found: {labelsPath}");
_session = ModelManager.CreateSession(modelPath, options, _ortEnv);
_labels = ResultProcessor.LoadLabels(labelsPath).ToList();
}
private async Task LoadAndRunDemoAsync()
{
// Get application directory
var appDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
?? throw new InvalidOperationException("Could not determine application directory");
var imagePath = Path.Combine(appDirectory, "image.png");
if (!File.Exists(imagePath))
{
throw new FileNotFoundException($"Image file not found: {imagePath}");
}
// Load and display the image
await LoadAndDisplayImageAsync(imagePath);
// Run inference automatically
await RunInferenceAsync(imagePath);
}
private async void ReloadModelButton_Click(object sender, RoutedEventArgs e)
{
try
{
ResultsText.Text = "Loading / reloading model...";
await LoadModelAsync();
if (_session != null)
{
ResultsText.Text += "\nRunning demo inference...";
await LoadAndRunDemoAsync();
}
}
catch (Exception ex)
{
ResultsText.Text = $"Reload failed: {ex.Message}";
}
}
private async Task LoadAndDisplayImageAsync(string imagePath)
{
var bitmap = new BitmapImage();
var imageFile = await StorageFile.GetFileFromPathAsync(imagePath);
using (var stream = await imageFile.OpenAsync(FileAccessMode.Read))
{
await bitmap.SetSourceAsync(stream);
}
SelectedImage.Source = bitmap;
}
private async Task RunInferenceAsync(string imagePath)
{
if (_session == null)
{
ResultsText.Text = "Model not loaded.";
return;
}
try
{
ResultsText.Text = "Running inference...";
// Load and preprocess image using shared helper
using var videoFrame = await ImageProcessor.LoadImageFileAsync(imagePath);
var inputTensor = await ImageProcessor.PreprocessImageAsync(videoFrame);
// Run inference using shared helper
using var results = InferenceEngine.RunInference(_session, inputTensor);
var output = InferenceEngine.ExtractResults(_session, results);
// Process and display results using shared helper
var topResults = ResultProcessor.GetTopPredictions(output, _labels, 5);
var resultText = new StringBuilder();
resultText.AppendLine("Top 5 Predictions:");
resultText.AppendLine();
for (int i = 0; i < topResults.Count; i++)
{
var (label, confidence) = topResults[i];
resultText.AppendLine($"{i + 1}. {label}: {confidence:P2}");
}
ResultsText.Text = resultText.ToString();
}
catch (Exception ex)
{
ResultsText.Text = $"Inference failed: {ex.Message}";
}
}
private void PopulateEpCombo(IEnumerable<OrtEpDevice> devices)
{
EpCombo.Items.Clear();
var epGroups = devices
.GroupBy(d => d.EpName)
.OrderBy(g => g.Key, StringComparer.OrdinalIgnoreCase);
foreach (var g in epGroups)
{
EpCombo.Items.Add(g.Key);
}
if (EpCombo.Items.Count > 0)
{
EpCombo.SelectedIndex = 0;
}
}
private void PopulateDeviceCombo(IEnumerable<OrtEpDevice> devices)
{
DeviceCombo.Items.Clear();
DeviceCombo.IsEnabled = false;
var selectedEp = EpCombo.SelectedItem?.ToString();
if (string.IsNullOrEmpty(selectedEp))
{
return;
}
var epDevices = devices.Where(d => d.EpName == selectedEp).ToList();
// Generalized: any EP with multiple distinct hardware device types
var types = epDevices
.Select(d => d.HardwareDevice.Type.ToString())
.Distinct(StringComparer.OrdinalIgnoreCase)
.OrderBy(s => s, StringComparer.OrdinalIgnoreCase)
.ToList();
if (types.Count == 1)
{
DeviceCombo.Items.Add(types[0]);
DeviceCombo.SelectedIndex = 0;
}
else if (types.Count > 1)
{
DeviceCombo.IsEnabled = true;
foreach (var t in types)
{
DeviceCombo.Items.Add(t);
}
DeviceCombo.SelectedIndex = 0;
}
else
{
// Log an error – no devices for selected EP
ResultsText.Text = $"No devices found for selected execution provider: {selectedEp}";
}
}
private void EpCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
// Use the shared OrtEnv instance for device refresh
if (_ortEnv != null)
{
var devices = _ortEnv.GetEpDevices();
PopulateDeviceCombo(devices);
}
}
catch (Exception ex)
{
ResultsText.Text = $"Device refresh failed: {ex.Message}";
}
}
private PerformanceMode GetSelectedPerformanceMode()
{
if (PerfModeMaxPerfRadio.IsChecked == true)
return PerformanceMode.MaxPerformance;
if (PerfModeMaxEffRadio.IsChecked == true)
return PerformanceMode.MaxEfficiency;
return PerformanceMode.Default;
}
public void Dispose()
{
if (!_disposed)
{
_session?.Dispose();
_ortEnv?.Dispose();
_disposed = true;
}
}
}
}