Skip to content

Commit 61dad9b

Browse files
author
Sheil Kumar
committed
small cleanup refactor
1 parent 71f68bf commit 61dad9b

File tree

4 files changed

+8
-80
lines changed

4 files changed

+8
-80
lines changed

Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/EncryptedModel/EncryptedModel.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</StackPanel>
3636
</DataTemplate>
3737
</Page.Resources>
38-
<Grid Padding="0,20,0,0">
38+
<Grid Padding="0,10,0,0">
3939
<StackPanel>
4040
<StackPanel Orientation="Horizontal">
4141
<TextBlock Text="Enter the Key:" FontSize="16"
@@ -69,7 +69,7 @@
6969
Padding="7, 0, 0, 7"
7070
/>
7171
</StackPanel>
72-
<StackPanel Orientation="Horizontal" Padding="0,2,0,0">
72+
<StackPanel Orientation="Horizontal" Padding="0,10,0,0">
7373

7474
<Image x:Name="InputImage" Source="ms-appx:///InputData/hummingbird.jpg"
7575
Margin="0,1,0,0"

Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/ImageClassifier/ImageClassifier.xaml.cs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ public sealed partial class ImageClassifier : Page
6464
private Dictionary<Classifier, Func<LearningModel>> _preProcessorDictionary;
6565

6666
private static Dictionary<long, string> _labels;
67-
private static Dictionary<long, string> _imagenetLabels;
68-
private static Dictionary<long, string> _ilsvrc2013Labels;
6967

7068
private BitmapDecoder CurrentImageDecoder { get; set; }
7169

@@ -131,16 +129,6 @@ public ImageClassifier()
131129

132130
private void EnsureInitialized()
133131
{
134-
if (_imagenetLabels == null)
135-
{
136-
_imagenetLabels = LoadLabels("ms-appx:///InputData/sysnet.txt");
137-
}
138-
139-
if (_ilsvrc2013Labels == null)
140-
{
141-
_ilsvrc2013Labels = LoadLabels("ms-appx:///InputData/ilsvrc2013.txt");
142-
}
143-
144132
if (_modelDictionary == null)
145133
{
146134
_modelDictionary = new Dictionary<Classifier, string>{
@@ -168,10 +156,10 @@ private void EnsureInitialized()
168156
if (_postProcessorDictionary == null)
169157
{
170158
_postProcessorDictionary = new Dictionary<Classifier, Func<LearningModel>>{
171-
{ Classifier.DenseNet121, () => TensorizationModels.ReshapeThenSoftmaxThenTopK(new long[] { BatchSize, _imagenetLabels.Count, 1, 1 },
159+
{ Classifier.DenseNet121, () => TensorizationModels.ReshapeThenSoftmaxThenTopK(new long[] { BatchSize, ClassificationLabels.ImageNet.Count, 1, 1 },
172160
TopK,
173161
BatchSize,
174-
_imagenetLabels.Count) },
162+
ClassificationLabels.ImageNet.Count) },
175163
{ Classifier.EfficientNetLite4, () => TensorizationModels.SoftMaxThenTopK(TopK) },
176164
{ Classifier.ShuffleNet_V1, () => TensorizationModels.TopK(TopK) },
177165
{ Classifier.SqueezeNet, () => TensorizationModels.SoftMaxThenTopK(TopK) },
@@ -257,11 +245,11 @@ private void InitializeWindowsMachineLearning()
257245

258246
if (model == Classifier.RCNN_ILSVRC13)
259247
{
260-
_labels = _ilsvrc2013Labels;
248+
_labels = ClassificationLabels.ILSVRC2013;
261249
}
262250
else
263251
{
264-
_labels = _imagenetLabels;
252+
_labels = ClassificationLabels.ImageNet;
265253
}
266254

267255
CurrentModel = model;
@@ -396,24 +384,6 @@ private static LearningModel CreateLearningModel(string modelPath)
396384
}
397385
#pragma warning restore CA1416 // Validate platform compatibility
398386

399-
private static Dictionary<long, string> LoadLabels(string csvFile)
400-
{
401-
var file = StorageFile.GetFileFromApplicationUriAsync(new Uri(csvFile)).GetAwaiter().GetResult();
402-
var text = Windows.Storage.FileIO.ReadTextAsync(file).GetAwaiter().GetResult();
403-
var labels = new Dictionary<long, string>();
404-
var records = text.Split(Environment.NewLine);
405-
foreach (var record in records)
406-
{
407-
var fields = record.Split(",", 2);
408-
if (fields.Length == 2)
409-
{
410-
var index = long.Parse(fields[0]);
411-
labels[index] = fields[1];
412-
}
413-
}
414-
return labels;
415-
}
416-
417387
private void TryPerformInference()
418388
{
419389
if (CurrentImageDecoder != null)

Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/ImageSharpInterop/ImageSharpInterop.xaml.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public sealed partial class ImageSharpInterop : Page
3838
private LearningModelSession _tensorizationSession;
3939
private LearningModelSession _postProcessingSession;
4040

41-
private static Dictionary<long, string> _imagenetLabels;
42-
4341
private Image<Bgra32> CurrentImage { get; set; }
4442

4543
#pragma warning disable CA1416 // Validate platform compatibility
@@ -58,7 +56,6 @@ public ImageSharpInterop()
5856
{
5957
this.InitializeComponent();
6058

61-
_imagenetLabels = LoadLabels("ms-appx:///InputData/sysnet.txt");
6259
var tensorizationModel = TensorizationModels.BasicTensorization(Height, Width, BatchSize, Channels, Height, Width, "nearest");
6360
_tensorizationSession = CreateLearningModelSession(tensorizationModel, SelectedDeviceKind);
6461
_inferenceSession = CreateLearningModelSession("ms-appx:///Models/squeezenet1.1-7.onnx");
@@ -104,7 +101,7 @@ public ImageSharpInterop()
104101
// Return results
105102
var probabilities = topKValues.GetAsVectorView();
106103
var indices = topKIndices.GetAsVectorView();
107-
var labels = indices.Select((index) => _imagenetLabels[index]);
104+
var labels = indices.Select((index) => ClassificationLabels.ImageNet[index]);
108105
stop = HighResolutionClock.UtcNow();
109106
var postProcessDuration = HighResolutionClock.DurationInMs(start, stop);
110107

@@ -168,24 +165,6 @@ private static LearningModel CreateLearningModel(string modelPath)
168165
}
169166
#pragma warning restore CA1416 // Validate platform compatibility
170167

171-
private static Dictionary<long, string> LoadLabels(string csvFile)
172-
{
173-
var file = StorageFile.GetFileFromApplicationUriAsync(new Uri(csvFile)).GetAwaiter().GetResult();
174-
var text = Windows.Storage.FileIO.ReadTextAsync(file).GetAwaiter().GetResult();
175-
var labels = new Dictionary<long, string>();
176-
var records = text.Split(Environment.NewLine);
177-
foreach (var record in records)
178-
{
179-
var fields = record.Split(",", 2);
180-
if (fields.Length == 2)
181-
{
182-
var index = long.Parse(fields[0]);
183-
labels[index] = fields[1];
184-
}
185-
}
186-
return labels;
187-
}
188-
189168
private void TryPerformInference(bool reloadImages = true)
190169
{
191170
if (CurrentImage != null)

Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/OpenCVInterop/OpenCVInterop.xaml.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ enum ClassifyChoice {
4141
private LearningModelSession _tensorizationSession;
4242
private LearningModelSession _postProcessingSession;
4343

44-
private static Dictionary<long, string> _imagenetLabels;
45-
4644
private WinMLSamplesGalleryNative.OpenCVImage Original { get; set; }
4745
private WinMLSamplesGalleryNative.OpenCVImage Noisy { get; set; }
4846
private WinMLSamplesGalleryNative.OpenCVImage Denoised { get; set; }
@@ -115,7 +113,6 @@ public OpenCVInterop()
115113
this.InitializeComponent();
116114
CurrentImagePath = null;
117115
InferenceChoice = ClassifyChoice.Denoised;
118-
_imagenetLabels = LoadLabels("ms-appx:///InputData/sysnet.txt");
119116
_inferenceSession = CreateLearningModelSession("ms-appx:///Models/squeezenet1.1-7.onnx");
120117
_postProcessingSession = CreateLearningModelSession(TensorizationModels.SoftMaxThenTopK(TopK));
121118

@@ -157,7 +154,7 @@ public OpenCVInterop()
157154
// Return results
158155
var probabilities = topKValues.GetAsVectorView();
159156
var indices = topKIndices.GetAsVectorView();
160-
var labels = indices.Select((index) => _imagenetLabels[index]);
157+
var labels = indices.Select((index) => ClassificationLabels.ImageNet[index]);
161158
stop = HighResolutionClock.UtcNow();
162159
var postProcessDuration = HighResolutionClock.DurationInMs(start, stop);
163160

@@ -219,24 +216,6 @@ private static LearningModel CreateLearningModel(string modelPath)
219216
}
220217
#pragma warning restore CA1416 // Validate platform compatibility
221218

222-
private static Dictionary<long, string> LoadLabels(string csvFile)
223-
{
224-
var file = StorageFile.GetFileFromApplicationUriAsync(new Uri(csvFile)).GetAwaiter().GetResult();
225-
var text = Windows.Storage.FileIO.ReadTextAsync(file).GetAwaiter().GetResult();
226-
var labels = new Dictionary<long, string>();
227-
var records = text.Split(Environment.NewLine);
228-
foreach (var record in records)
229-
{
230-
var fields = record.Split(",", 2);
231-
if (fields.Length == 2)
232-
{
233-
var index = long.Parse(fields[0]);
234-
labels[index] = fields[1];
235-
}
236-
}
237-
return labels;
238-
}
239-
240219
private void TryPerformInference(bool reloadImages = true)
241220
{
242221
if (CurrentImagePath != null)

0 commit comments

Comments
 (0)