Skip to content

Commit 4c96067

Browse files
authored
Merge pull request #480 from microsoft/user/numform/ort_values
Add sample to showcase binding and inferencing on DX resources using ORT
2 parents 93e6abb + c77329b commit 4c96067

28 files changed

+5856
-8
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.onnx
22
!squeezenet1.1-7-batched.onnx
3-
!encrypted.onnx
3+
!encrypted.onnx
4+
!efficientnet-lite4-11.onnx
5+
!dx_preprocessor_efficient_net.onnx
Binary file not shown.

Samples/WinMLSamplesGallery/WinMLSamplesGallery/Pages/SampleBasePage.xaml.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
4242
case "AdapterSelection":
4343
SampleFrame.Navigate(typeof(Samples.AdapterSelection));
4444
break;
45+
case "DXResourceBindingORT":
46+
SampleFrame.Navigate(typeof(Samples.DXResourceBindingORT));
47+
break;
4548
}
4649
if (sampleMetadata.Docs.Count > 0)
4750
DocsHeader.Visibility = Visibility.Visible;
@@ -57,6 +60,12 @@ protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
5760
var page = (Samples.Batching)SampleFrame.Content;
5861
page.StopAllEvents();
5962
}
63+
else if(SampleFrame.SourcePageType == typeof(Samples.DXResourceBindingORT))
64+
{
65+
var page = (Samples.DXResourceBindingORT)SampleFrame.Content;
66+
page.StopAllEvents();
67+
68+
}
6069
}
6170

6271
public static void SetModelNameForTelemetry(String modelName, String sampleName, LearningModel model)

Samples/WinMLSamplesGallery/WinMLSamplesGallery/Pages/SamplesGrid.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Padding="5,1,5,1">
1212
<TextBlock FontFamily="Segoe MDL2 Assets" Text="{x:Bind Icon}" FontSize="40" Foreground="#919191" Padding="0,5,0,0"/>
1313
<StackPanel Orientation="Vertical" VerticalAlignment="Top" Padding="8,0,0,0">
14-
<TextBlock FontSize="20" Text="{x:Bind Title}" Foreground="#12bef6"
14+
<TextBlock FontSize="18" Text="{x:Bind Title}" Foreground="#12bef6"
1515
Typography.Capitals="AllSmallCaps"
1616
Typography.StylisticSet4="True"
1717
VerticalAlignment="Top"

Samples/WinMLSamplesGallery/WinMLSamplesGallery/SampleMetadata/SampleMetadata.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@
8282
"CSharpGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/AdapterSelection/AdapterSelection.xaml.cs",
8383
"Docs": [],
8484
"IsRecentlyAdded": true
85+
},
86+
{
87+
"Title": "DX Resource Binding in ORT",
88+
"DescriptionShort": "Learn how to bind and evaluate DirectX Resources using ONNX Runtime.",
89+
"Description": "The sample showcases how to create and bind OrtValue objects from ID3D12 Resources using the ONNX Runtime C-API.",
90+
"Icon": "\uE155",
91+
"Tag": "DXResourceBindingORT",
92+
"XAMLGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/DXResourceBindingORT/DXResourceBindingORT.xaml",
93+
"CSharpGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/DXResourceBindingORT/DXResourceBindingORT.xaml.cs",
94+
"Docs": [],
95+
"IsRecentlyAdded": true
8596
}
8697
]
8798
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<Page
2+
x:Class="WinMLSamplesGallery.Samples.DXResourceBindingORT"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:local_controls="using:WinMLSamplesGallery.Controls"
8+
xmlns:local_samples="using:WinMLSamplesGallery.Samples"
9+
mc:Ignorable="d"
10+
FontFamily="Arial">
11+
12+
<Page.Resources>
13+
<DataTemplate x:Key="ContactListViewTemplate" x:DataType="local_samples:DXClassifierResult">
14+
<TextBlock Margin="0,5,0,5">
15+
<Span>
16+
<Run Text="{Binding index}" />
17+
</Span>
18+
<Span>
19+
<Run Text="{Binding label}" />
20+
</Span>
21+
</TextBlock>
22+
</DataTemplate>
23+
</Page.Resources>
24+
25+
<StackPanel>
26+
<Button
27+
x:Name="LaunchWindowBtn"
28+
Click="LaunchWindow"
29+
Margin="0,10,0,0">
30+
Launch Window
31+
</Button>
32+
33+
<TextBlock
34+
x:Name="LoadingTxt"
35+
Margin="0,15,0,0"
36+
FontWeight="Bold"
37+
Visibility="Collapsed">
38+
Loading Window and ONNX Runtime Sessions...
39+
</TextBlock>
40+
41+
<ListView
42+
x:Name="Top10Results"
43+
ItemTemplate="{StaticResource ContactListViewTemplate}"
44+
BorderThickness="1"
45+
BorderBrush="{ThemeResource SystemControlForegroundBaseMediumLowBrush}"
46+
Width="350"
47+
Height="400"
48+
HorizontalAlignment="Left"
49+
Margin="0,15,0,0"/>
50+
51+
</StackPanel>
52+
</Page>
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using Microsoft.AI.MachineLearning;
2+
using Microsoft.UI.Xaml;
3+
using Microsoft.UI.Xaml.Controls;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Threading.Tasks;
8+
using Windows.Graphics.Imaging;
9+
using Windows.Media;
10+
using Windows.Storage;
11+
using WinMLSamplesGalleryNative;
12+
using System.Linq;
13+
14+
namespace WinMLSamplesGallery.Samples
15+
{
16+
public class DXClassifierResult
17+
{
18+
public int index { get; private set; }
19+
public string label { get; private set; }
20+
public DXClassifierResult(int i, string lab)
21+
{
22+
index = i;
23+
label = lab;
24+
}
25+
}
26+
27+
public sealed partial class DXResourceBindingORT : Page
28+
{
29+
private bool pageExited = false;
30+
31+
public DXResourceBindingORT()
32+
{
33+
this.InitializeComponent();
34+
35+
// Run in background thread to avoid blocking UI on sample launch
36+
Task.Run(() => SetModelNameForTelemetry());
37+
}
38+
39+
private async void LaunchWindow(object sender, RoutedEventArgs e)
40+
{
41+
ToggleLaunchWindowButton();
42+
ToggleLoadingTxtVisibility();
43+
44+
await Task.Run(() => WinMLSamplesGalleryNative.DXResourceBinding.LaunchWindow());
45+
46+
ToggleLoadingTxtVisibility();
47+
ClassifyCube();
48+
}
49+
50+
private async void ClassifyCube()
51+
{
52+
int i = 0;
53+
float[] prev_results = { };
54+
while (true)
55+
{
56+
if (pageExited)
57+
{
58+
WinMLSamplesGalleryNative.DXResourceBinding.CloseWindow();
59+
break;
60+
}
61+
float[] results = await Task.Run(() => WinMLSamplesGalleryNative.DXResourceBinding.EvalORT());
62+
if (i == 0)
63+
prev_results = results;
64+
// The first evaluation may return null so move to the next iteration
65+
if (results == null)
66+
continue;
67+
UpdateClassificationResults(prev_results);
68+
prev_results = results;
69+
System.Threading.Thread.Sleep(1000);
70+
i++;
71+
}
72+
ToggleLaunchWindowButton();
73+
pageExited = false;
74+
}
75+
76+
private void UpdateClassificationResults(float[] results)
77+
{
78+
var results_lst = new List<float>(results);
79+
List<int> top_1k_imagenet_indices = Enumerable.Range(0, 1000).ToList();
80+
top_1k_imagenet_indices.Sort((x, y) => results_lst[y].CompareTo(results_lst[x]));
81+
// The top 10 results represented by their indices in the list of ImageNet labels
82+
List<int> top_10_imagenet_indices = top_1k_imagenet_indices.Take(10).ToList();
83+
84+
List<DXClassifierResult> dx_classifier_results = new List<DXClassifierResult>();
85+
for (int i = 0; i < top_10_imagenet_indices.Count; i++)
86+
{
87+
dx_classifier_results.Add(new DXClassifierResult(i+1,
88+
ClassificationLabels.ImageNet[top_10_imagenet_indices[i]]));
89+
}
90+
91+
Top10Results.ItemsSource = dx_classifier_results;
92+
}
93+
94+
private void ToggleLaunchWindowButton()
95+
{
96+
LaunchWindowBtn.IsEnabled = !LaunchWindowBtn.IsEnabled;
97+
}
98+
99+
private void ToggleLoadingTxtVisibility()
100+
{
101+
if (LoadingTxt.Visibility == Visibility.Visible)
102+
LoadingTxt.Visibility = Visibility.Collapsed;
103+
else
104+
LoadingTxt.Visibility = Visibility.Visible;
105+
}
106+
107+
public void StopAllEvents()
108+
{
109+
pageExited = true;
110+
}
111+
112+
// Session must be created for telemety to be sent
113+
private void SetModelNameForTelemetry()
114+
{
115+
var modelName = "squeezenet1.1-7.onnx";
116+
var modelPath = Path.Join(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Models", modelName);
117+
var model = LearningModel.LoadFromFilePath(modelPath);
118+
var device = new LearningModelDevice(LearningModelDeviceKind.Cpu);
119+
var options = new LearningModelSessionOptions();
120+
SampleBasePage.SetModelNameForTelemetry("SqueezeNet", "DXResourceBinding", model);
121+
new LearningModelSession(model, device, options);
122+
}
123+
}
124+
}

Samples/WinMLSamplesGallery/WinMLSamplesGallery/WinMLSamplesGallery.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<None Remove="SamplesGrid.xaml" />
3636
<None Remove="SampleBasePage.xaml" />
3737
<None Remove="Image.xaml" />
38+
<None Remove="Samples\DXResourceBindingORT\DXResourceBindingORT.xaml" />
3839
<None Remove="Samples\ImageClassifier.xaml" />
3940
<None Remove="Samples\ImageEffects.xaml" />
4041
<None Remove="Samples\ObjectDetector\ObjectDetector.xaml" />
@@ -163,6 +164,12 @@
163164
<Folder Include="Samples\AdapterSelection\Docs\" />
164165
</ItemGroup>
165166

167+
<ItemGroup>
168+
<Page Update="Samples\DXResourceBindingORT\DXResourceBindingORT.xaml">
169+
<Generator>MSBuild:Compile</Generator>
170+
</Page>
171+
</ItemGroup>
172+
166173
<Target Name="DownloadContentFiles" BeforeTargets="BeforeBuild">
167174
<!-- Small models. These models get shipped in the store app by default. -->
168175
<DownloadFile Condition="!Exists('$(MSBuildProjectDirectory)\Models\densenet-9.onnx')" SourceUrl="https://github.com/onnx/models/blob/main/vision/classification/densenet-121/model/densenet-9.onnx?raw=true" DestinationFolder="$(MSBuildProjectDirectory)\Models">

0 commit comments

Comments
 (0)