forked from andrewleader/WinMLLabDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
330 lines (284 loc) · 11.5 KB
/
MainWindow.xaml.cs
File metadata and controls
330 lines (284 loc) · 11.5 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
321
322
323
324
325
326
327
328
329
330
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using Microsoft.Win32;
using Microsoft.Windows.AI.MachineLearning;
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Xml.Linq;
using Windows.Graphics.Imaging;
using Windows.Media;
using Windows.Storage;
using Windows.Storage.Streams;
using IOPath = System.IO.Path;
namespace WinMLLabDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private InferenceSession? _loadedSession;
public ObservableCollection<OrtEpDevice> ExecutionProviders { get; set; }
private string selectedImagePath = string.Empty;
private OrtEpDevice? selectedExecutionProvider = null;
private const string ModelName = "SqueezeNet";
private const string ModelExtension = ".onnx";
public MainWindow()
{
InitializeComponent();
ExecutionProviders = new ObservableCollection<OrtEpDevice>();
ExecutionProvidersGrid.ItemsSource = ExecutionProviders;
// Set up EP selection event
ExecutionProvidersGrid.SelectionChanged += ExecutionProvidersGrid_SelectionChanged;
// Initialize with some sample data
LoadExecutionProviders();
WriteToConsole("WinML Demo Application initialized.");
// Select the default image
SelectImage(IOPath.Combine(AppDomain.CurrentDomain.BaseDirectory, "image.jpg"));
}
private void LoadExecutionProviders()
{
ExecutionProviders.Clear();
var eps = ExecutionLogic.LoadExecutionProviders();
foreach (var ep in eps)
{
ExecutionProviders.Add(ep);
}
WriteToConsole("Loaded execution providers.");
}
private void ExecutionProvidersGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ExecutionProvidersGrid.SelectedItem is OrtEpDevice selectedEP)
{
selectedExecutionProvider = selectedEP;
_loadedSession = null;
WriteToConsole($"Selected execution provider: {selectedEP.EpName}");
}
else
{
selectedExecutionProvider = null;
_loadedSession = null;
}
// Update button states
UpdateButtonStates();
}
private void UpdateButtonStates()
{
if (selectedExecutionProvider == null)
{
CompileModelButton.IsEnabled = false;
LoadModelButton.IsEnabled = false;
RunButton.IsEnabled = false;
}
CompileModelButton.IsEnabled = true;
string compiledModelPath = ModelHelpers.GetCompiledModelPath(selectedExecutionProvider!);
LoadModelButton.IsEnabled = File.Exists(compiledModelPath);
RunButton.IsEnabled = _loadedSession != null;
}
private void RefreshEPButton_Click(object sender, RoutedEventArgs e)
{
LoadExecutionProviders();
// Reset selection state
selectedExecutionProvider = null;
_loadedSession = null;
CompileModelButton.IsEnabled = false;
RunButton.IsEnabled = false;
}
private async void InitializeWinMLEPsButton_Click(object sender, RoutedEventArgs e)
{
InitializeWinMLEPsButton.IsEnabled = false;
try
{
WriteToConsole("WinML: Downloading and registering EPs...");
var now = DateTime.Now;
// Download and register the Execution Providers for our device
await ExecutionLogic.InitializeWinMLEPsAsync();
var elapsed = DateTime.Now - now;
WriteToConsole($"WinML: EPs downloaded and registered in {elapsed.TotalMilliseconds} ms.");
LoadExecutionProviders();
}
catch (Exception ex)
{
WriteToConsole($"Error downloading execution providers: {ex.Message}");
}
finally
{
InitializeWinMLEPsButton.IsEnabled = true;
}
}
private async void CompileModelButton_Click(object sender, RoutedEventArgs e)
{
if (selectedExecutionProvider == null)
{
WriteToConsole("No execution provider selected.");
return;
}
switch (selectedExecutionProvider.EpName)
{
case "CPUExecutionProvider":
case "DmlExecutionProvider":
WriteToConsole("Compiling isn't necessary for CPU or DML EPs");
return;
}
CompileModelButton.IsEnabled = false;
try
{
WriteToConsole($"Compiling model for {selectedExecutionProvider.EpName}...");
var now = DateTime.Now;
string compiledModelPath = await Task.Run(() => ExecutionLogic.CompileModelForExecutionProvider(selectedExecutionProvider));
var elapsed = DateTime.Now - now;
WriteToConsole($"Model compiled successfully in {elapsed.TotalMilliseconds} ms: {compiledModelPath}");
// Update Run button state
UpdateButtonStates();
}
catch (Exception ex)
{
WriteToConsole($"Error compiling model: {ex.Message}");
}
finally
{
CompileModelButton.IsEnabled = true;
}
}
private void BrowseImageButton_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new OpenFileDialog
{
Title = "Select an image file",
Filter = "Image files (*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp|All files (*.*)|*.*",
InitialDirectory = IOPath.Combine(AppDomain.CurrentDomain.BaseDirectory)
};
if (openFileDialog.ShowDialog() == true)
{
SelectImage(openFileDialog.FileName);
}
}
private void SelectImage(string filePath)
{
selectedImagePath = filePath;
ImagePathTextBox.Text = selectedImagePath;
try
{
// Load and display the selected image
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(selectedImagePath);
bitmap.DecodePixelWidth = 300; // Limit size for preview
bitmap.EndInit();
SelectedImage.Source = bitmap;
WriteToConsole($"Selected image: {IOPath.GetFileName(selectedImagePath)}");
}
catch (Exception ex)
{
WriteToConsole($"Error loading image: {ex.Message}");
}
}
private async void RunButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(selectedImagePath))
{
WriteToConsole("Please select an image first.");
return;
}
if (selectedExecutionProvider == null)
{
WriteToConsole("Please select an execution provider first.");
return;
}
string compiledModelPath = ModelHelpers.GetCompiledModelPath(selectedExecutionProvider);
if (!File.Exists(compiledModelPath))
{
WriteToConsole("Compiled model not found. Please compile the model first.");
return;
}
WriteToConsole($"Running classification on: {IOPath.GetFileName(selectedImagePath)}");
WriteToConsole($"Using execution provider: {selectedExecutionProvider.EpName}");
WriteToConsole($"Using compiled model: {IOPath.GetFileName(compiledModelPath)}");
// Disable all buttons during inference
CompileModelButton.IsEnabled = false;
LoadModelButton.IsEnabled = false;
RunButton.IsEnabled = false;
try
{
ResultsTextBlock.Text = "Running classification ...";
DateTime start = DateTime.Now;
var results = await Task.Run(() => ExecutionLogic.RunModelAsync(_loadedSession, selectedImagePath, compiledModelPath, selectedExecutionProvider));
ResultsTextBlock.Text = results;
var time = DateTime.Now - start;
WriteToConsole($"Classification completed successfully in {time.TotalMilliseconds} ms.");
}
catch (Exception ex)
{
WriteToConsole($"Error during classification: {ex.Message}");
ResultsTextBlock.Text = $"Error during classification: {ex.Message}";
}
finally
{
// Re-enable the button
CompileModelButton.IsEnabled = true;
LoadModelButton.IsEnabled = true;
RunButton.IsEnabled = true;
}
}
private void ClearConsoleButton_Click(object sender, RoutedEventArgs e)
{
ConsoleTextBlock.Text = string.Empty;
}
public void WriteToConsole(string message)
{
var timestamp = DateTime.Now.ToString("HH:mm:ss.fff");
var logEntry = $"[{timestamp}] {message}\n";
Dispatcher.Invoke(() =>
{
ConsoleTextBlock.Text += logEntry;
// Auto-scroll to bottom
if (ConsoleTextBlock.Parent is ScrollViewer scrollViewer)
{
scrollViewer.ScrollToEnd();
}
});
}
private async void LoadModelButton_Click(object sender, RoutedEventArgs e)
{
if (selectedExecutionProvider == null)
{
WriteToConsole("Please select an execution provider first.");
return;
}
var path = ModelHelpers.GetCompiledModelPath(selectedExecutionProvider);
if (!File.Exists(path))
{
WriteToConsole($"Compiled model not found: {path}");
return;
}
try
{
WriteToConsole($"Loading model for execution provider: {selectedExecutionProvider.EpName}");
DateTime start = DateTime.Now;
_loadedSession = await Task.Run(() => ExecutionLogic.LoadModel(ModelHelpers.GetCompiledModelPath(selectedExecutionProvider), selectedExecutionProvider));
var elapsed = DateTime.Now - start;
WriteToConsole($"Model loaded successfully in {elapsed.TotalMilliseconds} ms.");
RunButton.IsEnabled = true;
}
catch (Exception ex)
{
WriteToConsole($"Error loading model: {ex.Message}");
}
}
}
}