@@ -17,14 +17,14 @@ public class ImageClassificationDefault
17
17
{
18
18
public static void Example ( )
19
19
{
20
- // Set the path for input images
20
+ // Set the path for input images.
21
21
string assetsRelativePath = @"../../../assets" ;
22
22
string assetsPath = GetAbsolutePath ( assetsRelativePath ) ;
23
23
24
24
string imagesDownloadFolderPath = Path . Combine ( assetsPath , "inputs" ,
25
25
"images" ) ;
26
26
27
- //Download the image set and unzip, set the path to image folder
27
+ //Download the image set and unzip, set the path to image folder.
28
28
string finalImagesFolderName = DownloadImageSet (
29
29
imagesDownloadFolderPath ) ;
30
30
string fullImagesetFolderPath = Path . Combine (
@@ -34,12 +34,12 @@ public static void Example()
34
34
{
35
35
36
36
MLContext mlContext = new MLContext ( seed : 1 ) ;
37
-
38
- // Load all the original images info
37
+
38
+ // Load all the original images info.
39
39
IEnumerable < ImageData > images = LoadImagesFromDirectory (
40
40
folder : fullImagesetFolderPath , useFolderNameAsLabel : true ) ;
41
41
42
- // Shuffle images
42
+ // Shuffle images.
43
43
IDataView shuffledFullImagesDataset = mlContext . Data . ShuffleRows (
44
44
mlContext . Data . LoadFromEnumerable ( images ) ) ;
45
45
@@ -53,7 +53,7 @@ public static void Example()
53
53
. Fit ( shuffledFullImagesDataset )
54
54
. Transform ( shuffledFullImagesDataset ) ;
55
55
56
- // Split the data 90:10 into train and test sets
56
+ // Split the data 90:10 into train and test sets.
57
57
TrainTestData trainTestData = mlContext . Data . TrainTestSplit (
58
58
shuffledFullImagesDataset , testFraction : 0.1 , seed : 1 ) ;
59
59
@@ -73,10 +73,10 @@ public static void Example()
73
73
"with DNN Transfer Learning on top of the selected " +
74
74
"pre-trained model/architecture ***" ) ;
75
75
76
- // Measuring training time
76
+ // Measuring training time.
77
77
var watch = System . Diagnostics . Stopwatch . StartNew ( ) ;
78
78
79
- // Train the model
79
+ // Train the model.
80
80
// This involves calculating the bottleneck values, and then
81
81
// training the final layer.
82
82
var trainedModel = pipeline . Fit ( trainDataset ) ;
@@ -87,17 +87,17 @@ public static void Example()
87
87
Console . WriteLine ( "Training with transfer learning took: " +
88
88
( elapsedMs / 1000 ) . ToString ( ) + " seconds" ) ;
89
89
90
- // Save the trained model
90
+ // Save the trained model.
91
91
mlContext . Model . Save ( trainedModel , shuffledFullImagesDataset . Schema ,
92
92
"model.zip" ) ;
93
93
94
- // Load the trained and saved model for prediction
94
+ // Load the trained and saved model for prediction.
95
95
ITransformer loadedModel ;
96
96
DataViewSchema schema ;
97
97
using ( var file = File . OpenRead ( "model.zip" ) )
98
98
loadedModel = mlContext . Model . Load ( file , out schema ) ;
99
99
100
- // Evaluate the model on the test dataset
100
+ // Evaluate the model on the test dataset.
101
101
// Sample output:
102
102
// Making bulk predictions and evaluating model's quality...
103
103
// Micro-accuracy: 0.888888888888889,macro-accuracy = 0.883333333333333
@@ -126,43 +126,43 @@ public static void Example()
126
126
Console . ReadKey ( ) ;
127
127
}
128
128
129
- // Predict on a single image
129
+ // Predict on a single image.
130
130
private static void TrySinglePrediction ( string imagesForPredictions ,
131
131
MLContext mlContext , ITransformer trainedModel )
132
132
{
133
- // Create prediction function to try one prediction
133
+ // Create prediction function to try one prediction.
134
134
var predictionEngine = mlContext . Model
135
135
. CreatePredictionEngine < InMemoryImageData ,
136
136
ImagePrediction > ( trainedModel ) ;
137
137
138
- // Load test images
138
+ // Load test images.
139
139
IEnumerable < InMemoryImageData > testImages =
140
140
LoadInMemoryImagesFromDirectory ( imagesForPredictions , false ) ;
141
141
142
- // Create an in-memory image object from the first image in the test data
142
+ // Create an in-memory image object from the first image in the test data.
143
143
InMemoryImageData imageToPredict = new InMemoryImageData
144
144
{
145
145
Image = testImages . First ( ) . Image
146
146
} ;
147
147
148
- // Predict on the single image
148
+ // Predict on the single image.
149
149
var prediction = predictionEngine . Predict ( imageToPredict ) ;
150
150
151
151
Console . WriteLine ( $ "Scores : [{ string . Join ( "," , prediction . Score ) } ], " +
152
152
$ "Predicted Label : { prediction . PredictedLabel } ") ;
153
153
}
154
154
155
- // Evaluate the trained model on the passed test dataset
155
+ // Evaluate the trained model on the passed test dataset.
156
156
private static void EvaluateModel ( MLContext mlContext ,
157
157
IDataView testDataset , ITransformer trainedModel )
158
158
{
159
159
Console . WriteLine ( "Making bulk predictions and evaluating model's " +
160
160
"quality..." ) ;
161
161
162
- // Measuring time to evaluate
162
+ // Measuring time to evaluate.
163
163
var watch2 = System . Diagnostics . Stopwatch . StartNew ( ) ;
164
164
165
- // Evaluate the model on the test data and get the evaluation metrics
165
+ // Evaluate the model on the test data and get the evaluation metrics.
166
166
IDataView predictions = trainedModel . Transform ( testDataset ) ;
167
167
var metrics = mlContext . MulticlassClassification . Evaluate ( predictions ) ;
168
168
@@ -176,7 +176,7 @@ private static void EvaluateModel(MLContext mlContext,
176
176
( elapsed2Ms / 1000 ) . ToString ( ) + " seconds" ) ;
177
177
}
178
178
179
- //Load the Image Data from input directory
179
+ //Load the Image Data from input directory.
180
180
public static IEnumerable < ImageData > LoadImagesFromDirectory ( string folder ,
181
181
bool useFolderNameAsLabel = true )
182
182
{
@@ -211,7 +211,7 @@ public static IEnumerable<ImageData> LoadImagesFromDirectory(string folder,
211
211
}
212
212
}
213
213
214
- // Load In memory raw images from directory
214
+ // Load In memory raw images from directory.
215
215
public static IEnumerable < InMemoryImageData >
216
216
LoadInMemoryImagesFromDirectory ( string folder ,
217
217
bool useFolderNameAsLabel = true )
@@ -247,7 +247,7 @@ public static IEnumerable<InMemoryImageData>
247
247
}
248
248
}
249
249
250
- // Download and unzip the image dataset
250
+ // Download and unzip the image dataset.
251
251
public static string DownloadImageSet ( string imagesDownloadFolder )
252
252
{
253
253
// get a set of images to teach the network about the new classes
@@ -265,7 +265,7 @@ public static string DownloadImageSet(string imagesDownloadFolder)
265
265
return Path . GetFileNameWithoutExtension ( fileName ) ;
266
266
}
267
267
268
- // Download file to destination directory from input URL
268
+ // Download file to destination directory from input URL.
269
269
public static bool Download ( string url , string destDir , string destFileName )
270
270
{
271
271
if ( destFileName == null )
@@ -295,7 +295,7 @@ public static bool Download(string url, string destDir, string destFileName)
295
295
return true ;
296
296
}
297
297
298
- // Unzip the file to destination folder
298
+ // Unzip the file to destination folder.
299
299
public static void UnZip ( String gzArchiveName , String destFolder )
300
300
{
301
301
var flag = gzArchiveName . Split ( Path . DirectorySeparatorChar )
@@ -313,7 +313,7 @@ public static void UnZip(String gzArchiveName, String destFolder)
313
313
Console . WriteLine ( "Extracting is completed." ) ;
314
314
}
315
315
316
- // Get absolute path from relative path
316
+ // Get absolute path from relative path.
317
317
public static string GetAbsolutePath ( string relativePath )
318
318
{
319
319
FileInfo _dataRoot = new FileInfo ( typeof (
@@ -326,7 +326,7 @@ public static string GetAbsolutePath(string relativePath)
326
326
return fullPath ;
327
327
}
328
328
329
- // InMemoryImageData class holding the raw image byte array and label
329
+ // InMemoryImageData class holding the raw image byte array and label.
330
330
public class InMemoryImageData
331
331
{
332
332
[ LoadColumn ( 0 ) ]
@@ -336,7 +336,7 @@ public class InMemoryImageData
336
336
public string Label ;
337
337
}
338
338
339
- // ImageData class holding the imagepath and label
339
+ // ImageData class holding the imagepath and label.
340
340
public class ImageData
341
341
{
342
342
[ LoadColumn ( 0 ) ]
@@ -346,7 +346,7 @@ public class ImageData
346
346
public string Label ;
347
347
}
348
348
349
- // ImagePrediction class holding the score and predicted label metrics
349
+ // ImagePrediction class holding the score and predicted label metrics.
350
350
public class ImagePrediction
351
351
{
352
352
[ ColumnName ( "Score" ) ]
0 commit comments