Skip to content

Commit 93590be

Browse files
author
Sheil Kumar
committed
Add Encrypted Models sample
1 parent f9affa0 commit 93590be

File tree

17 files changed

+492
-216
lines changed

17 files changed

+492
-216
lines changed

Samples/WinMLSamplesGallery/WinMLSamplesGallery/Pages/AllSamplesPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public sealed partial class AllSamplesPage : Page
1010
public AllSamplesPage()
1111
{
1212
this.InitializeComponent();
13-
SamplesGrid.Navigate(typeof(SamplesGrid));
13+
SamplesGrid.Navigate(typeof(SamplesGrid), typeof(AllSamplesPage));
1414
}
1515
}
1616
}

Samples/WinMLSamplesGallery/WinMLSamplesGallery/Pages/HomePage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public sealed partial class HomePage : Page
1010
public HomePage()
1111
{
1212
this.InitializeComponent();
13-
SamplesGrid.Navigate(typeof(SamplesGrid));
13+
SamplesGrid.Navigate(typeof(SamplesGrid), typeof(HomePage));
1414
}
1515
}
1616
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
3434
case "ImageSharpInterop":
3535
SampleFrame.Navigate(typeof(Samples.ImageSharpInterop));
3636
break;
37-
case "EncryptedResource":
38-
SampleFrame.Navigate(typeof(Samples.EncryptedResource));
37+
case "EncryptedModel":
38+
SampleFrame.Navigate(typeof(Samples.EncryptedModel));
3939
break;
4040
}
4141
if (sampleMetadata.Docs.Count > 0)

Samples/WinMLSamplesGallery/WinMLSamplesGallery/Pages/SamplesGrid.xaml.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
using System;
2+
using System.Linq;
23
using Microsoft.UI.Xaml.Controls;
3-
4+
using Microsoft.UI.Xaml.Navigation;
5+
46
namespace WinMLSamplesGallery
57
{
68
public sealed partial class SamplesGrid : Page
79
{
810
public SamplesGrid()
911
{
1012
this.InitializeComponent();
11-
this.SetSampleLinks();
12-
}
13-
14-
private async void SetSampleLinks()
15-
{
16-
StyledGrid.ItemsSource = await SampleMetadata.GetAllSampleMetadata();
1713
}
1814

15+
protected override async void OnNavigatedTo(NavigationEventArgs e)
16+
{
17+
var allSamples = await SampleMetadata.GetAllSampleMetadata();
18+
if (e.Parameter == typeof(HomePage))
19+
{
20+
var recentlyAddedSamples = allSamples.Where(metadata => metadata.IsRecentlyAdded).Reverse();
21+
StyledGrid.ItemsSource = recentlyAddedSamples;
22+
}
23+
else
24+
{
25+
allSamples.Sort((left, right) => left.Title.CompareTo(right.Title));
26+
StyledGrid.ItemsSource = allSamples;
27+
}
28+
}
29+
1930
private void NavigateToSample(object sender, SelectionChangedEventArgs e)
2031
{
2132
GridView gridView = sender as GridView;

Samples/WinMLSamplesGallery/WinMLSamplesGallery/SampleMetadata/SampleMetadata.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public sealed class SampleMetadata
2424
public string XAMLGithubLink { get; set; }
2525
public string CSharpGithubLink { get; set; }
2626
public List<SampleDoc> Docs { get; set; }
27+
public bool IsRecentlyAdded { get; set; }
2728

2829
// Grabs all the sample metadata from the SampleMetadata.json file
2930
// and returns it as a List<SampleMetadata>
@@ -59,7 +60,8 @@ public static async Task<List<SampleMetadata>> GetAllSampleMetadata()
5960
Tag = currentSampleMetadata["Tag"].GetString(),
6061
XAMLGithubLink = currentSampleMetadata["XAMLGithubLink"].GetString(),
6162
CSharpGithubLink = currentSampleMetadata["CSharpGithubLink"].GetString(),
62-
Docs = ConvertJsonArrayToSampleDocList(currentSampleMetadata["Docs"].GetArray())
63+
Docs = ConvertJsonArrayToSampleDocList(currentSampleMetadata["Docs"].GetArray()),
64+
IsRecentlyAdded = currentSampleMetadata["IsRecentlyAdded"].GetBoolean(),
6365
});
6466
}
6567
return allSampleMetadata;

Samples/WinMLSamplesGallery/WinMLSamplesGallery/SampleMetadata/SampleMetadata.json

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"Tag": "ImageClassifier",
99
"XAMLGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/ImageClassifier/ImageClassifier.xaml",
1010
"CSharpGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/ImageClassifier/ImageClassifier.xaml.cs",
11-
"Docs": []
11+
"Docs": [],
12+
"IsRecentlyAdded": true
1213
},
1314
{
1415
"Title": "Image Effects",
@@ -18,7 +19,8 @@
1819
"Tag": "ImageEffects",
1920
"XAMLGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/ImageEffects/ImageEffects.xaml",
2021
"CSharpGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/ImageEffects/ImageEffects.xaml.cs",
21-
"Docs": []
22+
"Docs": [],
23+
"IsRecentlyAdded": true
2224
},
2325
{
2426
"Title": "Batched Inputs",
@@ -33,7 +35,8 @@
3335
"name": "BatchSizeOverride - API",
3436
"link": "https://docs.microsoft.com/en-us/uwp/api/windows.ai.machinelearning.learningmodelsessionoptions.batchsizeoverride?view=winrt-20348"
3537
}
36-
]
38+
],
39+
"IsRecentlyAdded": true
3740
},
3841
{
3942
"Title": "OpenCV",
@@ -43,7 +46,8 @@
4346
"Tag": "OpenCVInterop",
4447
"XAMLGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/OpenCVInterop/OpenCVInterop.xaml",
4548
"CSharpGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/OpenCVInterop/OpenCVInterop.xaml.cs",
46-
"Docs": []
49+
"Docs": [],
50+
"IsRecentlyAdded": true
4751
},
4852
{
4953
"Title": "ImageSharp",
@@ -53,17 +57,20 @@
5357
"Tag": "ImageSharpInterop",
5458
"XAMLGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/ImageSharpInterop/ImageSharpInterop.xaml",
5559
"CSharpGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/ImageSharpInterop/ImageSharpInterop.xaml.cs",
56-
"Docs": []
60+
"Docs": [],
61+
"IsRecentlyAdded": true
62+
5763
},
5864
{
5965
"Title": "Encrypted Models",
6066
"DescriptionShort": "The sample shows how to use Windows ML to load encrypted models from embedded resources.",
61-
"Description": "The sample shows how to use Windows ML to load encrypted models from embedded resources.",
67+
"Description": "The sample shows how to use Windows ML to load encrypted models from embedded resources. Enter the correct key to decrypt the model and classify the hummingbird.",
6268
"Icon": "\uE155",
63-
"Tag": "EncryptedResource",
64-
"XAMLGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/ImageSharpInterop/EncryptedResource.xaml",
65-
"CSharpGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/ImageSharpInterop/EncryptedResource.xaml.cs",
66-
"Docs": []
69+
"Tag": "EncryptedModel",
70+
"XAMLGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/EncryptedModel/EncryptedModel.xaml",
71+
"CSharpGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/EncryptedModel/EncryptedModel.xaml.cs",
72+
"Docs": [],
73+
"IsRecentlyAdded": true
6774
}
6875
]
6976
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Windows.Storage;
4+
5+
namespace WinMLSamplesGallery.Samples
6+
{
7+
public static class ClassificationLabels
8+
{
9+
private static Dictionary<long, string> _imagenetLabels = null;
10+
public static Dictionary<long, string> ImageNet {
11+
get {
12+
if (_imagenetLabels == null)
13+
{
14+
_imagenetLabels = LoadLabels("ms-appx:///InputData/sysnet.txt");
15+
}
16+
return _imagenetLabels;
17+
}
18+
}
19+
20+
private static Dictionary<long, string> _ilsvrc2013Labels = null;
21+
public static Dictionary<long, string> ILSVRC2013
22+
{
23+
get
24+
{
25+
if (_ilsvrc2013Labels == null)
26+
{
27+
_ilsvrc2013Labels = LoadLabels("ms-appx:///InputData/ilsvrc2013.txt");
28+
}
29+
return _ilsvrc2013Labels;
30+
}
31+
}
32+
33+
private static Dictionary<long, string> LoadLabels(string csvFile)
34+
{
35+
var file = StorageFile.GetFileFromApplicationUriAsync(new Uri(csvFile)).GetAwaiter().GetResult();
36+
var text = Windows.Storage.FileIO.ReadTextAsync(file).GetAwaiter().GetResult();
37+
var labels = new Dictionary<long, string>();
38+
var records = text.Split(Environment.NewLine);
39+
foreach (var record in records)
40+
{
41+
var fields = record.Split(",", 2);
42+
if (fields.Length == 2)
43+
{
44+
var index = long.Parse(fields[0]);
45+
labels[index] = fields[1];
46+
}
47+
}
48+
return labels;
49+
}
50+
}
51+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<Page
2+
x:Class="WinMLSamplesGallery.Samples.EncryptedModel"
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+
11+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
12+
13+
<Page.Resources>
14+
<DataTemplate x:Name="InferenceResultsTemplate" x:DataType="local_controls:Prediction">
15+
<StackPanel Orientation="Horizontal">
16+
<TextBlock Width="420"
17+
FontSize="14"
18+
Foreground="Black"
19+
Padding="0,1,1,1"
20+
Typography.Capitals="AllSmallCaps"
21+
Typography.StylisticSet4="True"
22+
TextTrimming="CharacterEllipsis">
23+
<Run Text="[" />
24+
<Run Text="{Binding Index}" />
25+
<Run Text="] " />
26+
<Run Text="{Binding Name}" />
27+
</TextBlock>
28+
<TextBlock Width="120"
29+
FontSize="14"
30+
Foreground="Black"
31+
Padding="0,1,1,1"
32+
Typography.Capitals="AllSmallCaps"
33+
Typography.StylisticSet4="True">
34+
<Run Text="p =" />
35+
<Run Text="{Binding Probability}" />
36+
</TextBlock>
37+
</StackPanel>
38+
</DataTemplate>
39+
</Page.Resources>
40+
<Grid>
41+
<StackPanel>
42+
<StackPanel Orientation="Horizontal" Margin="0,20,0,0">
43+
<PasswordBox x:Name="DecryptionKey" Header="Enter the key:" Width="228" Password="WindowsMLSamples" PasswordChar="#"/>
44+
<Button Content="Decrypt" x:Name="DecryptButton"
45+
Margin="2,0,0,0" Click="OnDecryptClick" VerticalAlignment="Bottom"
46+
Style="{StaticResource AccentButtonStyle}"
47+
Width="100" />
48+
<Button Content="View key..." Margin="2,0,0,0" VerticalAlignment="Bottom">
49+
<Button.Flyout>
50+
<Flyout>
51+
<StackPanel Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right">
52+
<TextBlock FontFamily="Segoe UI Light" FontSize="12" Margin="0,0,0,5">WindowsMLSamples</TextBlock>
53+
</StackPanel>
54+
</Flyout>
55+
</Button.Flyout>
56+
</Button>
57+
<TextBlock x:Name="Fail" FontSize="16" FontWeight="Bold"
58+
Visibility="Collapsed"
59+
Typography.Capitals="AllSmallCaps" Typography.StylisticSet4="True"
60+
Foreground="Red" FontFamily="Segoe UI Light" Text="Invalid key!" VerticalAlignment="Bottom"
61+
Padding="7, 0, 0, 7"
62+
/>
63+
<TextBlock x:Name="Succeed" FontSize="16" FontWeight="Bold"
64+
Visibility="Collapsed"
65+
Typography.Capitals="AllSmallCaps" Typography.StylisticSet4="True"
66+
Foreground="Green" FontFamily="Segoe UI Light" Text="Model decrypted!" VerticalAlignment="Bottom"
67+
Padding="7, 0, 0, 7"
68+
/>
69+
</StackPanel>
70+
<StackPanel Orientation="Horizontal" Padding="0,2,0,0">
71+
<Image x:Name="InputImage" Source="ms-appx:///InputData/hummingbird.jpg"
72+
Margin="0,1,0,0"
73+
Stretch="UniformToFill" Height="228" Width="228" VerticalAlignment="Top" HorizontalAlignment="Left"/>
74+
75+
<ListView Grid.Row="2"
76+
Padding="1,0,0,0"
77+
x:Name="InferenceResults"
78+
HorizontalAlignment="Stretch"
79+
VerticalAlignment="Top"
80+
ItemTemplate="{StaticResource InferenceResultsTemplate}"
81+
IsItemClickEnabled="False"
82+
SingleSelectionFollowsFocus="False">
83+
<ListView.ItemContainerStyle>
84+
<Style TargetType="ListViewItem">
85+
<Setter Property="Margin" Value="1,1,1,1"/>
86+
<Setter Property="MinHeight" Value="0"/>
87+
</Style>
88+
</ListView.ItemContainerStyle>
89+
90+
<ListView.ItemsPanel>
91+
<ItemsPanelTemplate>
92+
<ItemsWrapGrid x:Name="MaxItemsWrapGrid" Orientation="Vertical" HorizontalAlignment="Stretch"/>
93+
</ItemsPanelTemplate>
94+
</ListView.ItemsPanel>
95+
</ListView>
96+
</StackPanel>
97+
</StackPanel>
98+
</Grid>
99+
</Page>

0 commit comments

Comments
 (0)