@@ -89,7 +89,7 @@ public static void Example()
89
89
90
90
// Create the ImageClassification pipeline.
91
91
var pipeline = mlContext . Transforms . LoadRawImageBytes (
92
- "Image" , fullImagesetFolderPath , "ImagePath" )
92
+ "Image" , fullImagesetFolderPath , "ImagePath" )
93
93
. Append ( mlContext . MulticlassClassification . Trainers .
94
94
ImageClassification ( options ) ) ;
95
95
@@ -135,13 +135,17 @@ public static void Example()
135
135
// Micro-accuracy: 0.851851851851852,macro-accuracy = 0.85
136
136
EvaluateModel ( mlContext , testDataset , loadedModel ) ;
137
137
138
+ VBuffer < ReadOnlyMemory < char > > keys = default ;
139
+ loadedModel . GetOutputSchema ( schema ) [ "Label" ] . GetKeyValues ( ref keys ) ;
140
+
138
141
watch = System . Diagnostics . Stopwatch . StartNew ( ) ;
139
142
140
143
// Predict on a single image class using an in-memory image.
141
144
// Sample output:
142
145
// Scores : [0.09683081,0.0002645972,0.007213613,0.8912219,0.004469037],
143
146
// Predicted Label : daisy
144
- TrySinglePrediction ( fullImagesetFolderPath , mlContext , loadedModel ) ;
147
+ TrySinglePrediction ( fullImagesetFolderPath , mlContext , loadedModel ,
148
+ keys . DenseValues ( ) . ToArray ( ) ) ;
145
149
146
150
watch . Stop ( ) ;
147
151
elapsedMs = watch . ElapsedMilliseconds ;
@@ -160,28 +164,31 @@ public static void Example()
160
164
161
165
// Predict on a single image.
162
166
private static void TrySinglePrediction ( string imagesForPredictions ,
163
- MLContext mlContext , ITransformer trainedModel )
167
+ MLContext mlContext , ITransformer trainedModel ,
168
+ ReadOnlyMemory < char > [ ] originalLabels )
164
169
{
165
170
// Create prediction function to try one prediction.
166
171
var predictionEngine = mlContext . Model
167
- . CreatePredictionEngine < InMemoryImageData ,
168
- ImagePrediction > ( trainedModel ) ;
172
+ . CreatePredictionEngine < ImageData , ImagePrediction > ( trainedModel ) ;
169
173
170
174
// Load test images
171
- IEnumerable < InMemoryImageData > testImages =
172
- LoadInMemoryImagesFromDirectory ( imagesForPredictions , false ) ;
175
+ IEnumerable < ImageData > testImages = LoadImagesFromDirectory (
176
+ imagesForPredictions , false ) ;
173
177
174
178
// Create an in-memory image object from the first image in the test data.
175
- InMemoryImageData imageToPredict = new InMemoryImageData
179
+ ImageData imageToPredict = new ImageData
176
180
{
177
- Image = testImages . First ( ) . Image
181
+ ImagePath = testImages . First ( ) . ImagePath
178
182
} ;
179
183
180
184
// Predict on the single image.
181
185
var prediction = predictionEngine . Predict ( imageToPredict ) ;
186
+ var index = prediction . PredictedLabel ;
182
187
183
- Console . WriteLine ( $ "Scores : [{ string . Join ( "," , prediction . Score ) } ], " +
184
- $ "Predicted Label : { prediction . PredictedLabel } ") ;
188
+ Console . WriteLine ( $ "ImageFile : " +
189
+ $ "[{ Path . GetFileName ( imageToPredict . ImagePath ) } ], " +
190
+ $ "Scores : [{ string . Join ( "," , prediction . Score ) } ], " +
191
+ $ "Predicted Label : { originalLabels [ index ] } ") ;
185
192
}
186
193
187
194
// Evaluate the trained model on the passed test dataset.
@@ -243,42 +250,6 @@ public static IEnumerable<ImageData> LoadImagesFromDirectory(string folder,
243
250
}
244
251
}
245
252
246
- // Load In memory raw images from directory.
247
- public static IEnumerable < InMemoryImageData >
248
- LoadInMemoryImagesFromDirectory ( string folder ,
249
- bool useFolderNameAsLabel = true )
250
- {
251
- var files = Directory . GetFiles ( folder , "*" ,
252
- searchOption : SearchOption . AllDirectories ) ;
253
- foreach ( var file in files )
254
- {
255
- if ( Path . GetExtension ( file ) != ".jpg" )
256
- continue ;
257
-
258
- var label = Path . GetFileName ( file ) ;
259
- if ( useFolderNameAsLabel )
260
- label = Directory . GetParent ( file ) . Name ;
261
- else
262
- {
263
- for ( int index = 0 ; index < label . Length ; index ++ )
264
- {
265
- if ( ! char . IsLetter ( label [ index ] ) )
266
- {
267
- label = label . Substring ( 0 , index ) ;
268
- break ;
269
- }
270
- }
271
- }
272
-
273
- yield return new InMemoryImageData ( )
274
- {
275
- Image = File . ReadAllBytes ( file ) ,
276
- Label = label
277
- } ;
278
-
279
- }
280
- }
281
-
282
253
// Download and unzip the image dataset.
283
254
public static string DownloadImageSet ( string imagesDownloadFolder )
284
255
{
@@ -367,16 +338,6 @@ public static string GetAbsolutePath(string relativePath)
367
338
return fullPath ;
368
339
}
369
340
370
- // InMemoryImageData class holding the raw image byte array and label.
371
- public class InMemoryImageData
372
- {
373
- [ LoadColumn ( 0 ) ]
374
- public byte [ ] Image ;
375
-
376
- [ LoadColumn ( 1 ) ]
377
- public string Label ;
378
- }
379
-
380
341
// ImageData class holding the imagepath and label.
381
342
public class ImageData
382
343
{
@@ -394,7 +355,7 @@ public class ImagePrediction
394
355
public float [ ] Score ;
395
356
396
357
[ ColumnName ( "PredictedLabel" ) ]
397
- public string PredictedLabel ;
358
+ public UInt32 PredictedLabel ;
398
359
}
399
360
}
400
361
}
0 commit comments