Skip to content

Commit 25c17f6

Browse files
committed
added '.', formatted output.
1 parent 5a5be84 commit 25c17f6

File tree

4 files changed

+119
-131
lines changed

4 files changed

+119
-131
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/MulticlassClassification/ImageClassification/ImageClassificationDefault.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public class ImageClassificationDefault
1717
{
1818
public static void Example()
1919
{
20-
// Set the path for input images
20+
// Set the path for input images.
2121
string assetsRelativePath = @"../../../assets";
2222
string assetsPath = GetAbsolutePath(assetsRelativePath);
2323

2424
string imagesDownloadFolderPath = Path.Combine(assetsPath, "inputs",
2525
"images");
2626

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.
2828
string finalImagesFolderName = DownloadImageSet(
2929
imagesDownloadFolderPath);
3030
string fullImagesetFolderPath = Path.Combine(
@@ -34,12 +34,12 @@ public static void Example()
3434
{
3535

3636
MLContext mlContext = new MLContext(seed: 1);
37-
38-
// Load all the original images info
37+
38+
// Load all the original images info.
3939
IEnumerable<ImageData> images = LoadImagesFromDirectory(
4040
folder: fullImagesetFolderPath, useFolderNameAsLabel: true);
4141

42-
// Shuffle images
42+
// Shuffle images.
4343
IDataView shuffledFullImagesDataset = mlContext.Data.ShuffleRows(
4444
mlContext.Data.LoadFromEnumerable(images));
4545

@@ -53,7 +53,7 @@ public static void Example()
5353
.Fit(shuffledFullImagesDataset)
5454
.Transform(shuffledFullImagesDataset);
5555

56-
// Split the data 90:10 into train and test sets
56+
// Split the data 90:10 into train and test sets.
5757
TrainTestData trainTestData = mlContext.Data.TrainTestSplit(
5858
shuffledFullImagesDataset, testFraction: 0.1, seed: 1);
5959

@@ -73,10 +73,10 @@ public static void Example()
7373
"with DNN Transfer Learning on top of the selected " +
7474
"pre-trained model/architecture ***");
7575

76-
// Measuring training time
76+
// Measuring training time.
7777
var watch = System.Diagnostics.Stopwatch.StartNew();
7878

79-
// Train the model
79+
// Train the model.
8080
// This involves calculating the bottleneck values, and then
8181
// training the final layer.
8282
var trainedModel = pipeline.Fit(trainDataset);
@@ -87,17 +87,17 @@ public static void Example()
8787
Console.WriteLine("Training with transfer learning took: " +
8888
(elapsedMs / 1000).ToString() + " seconds");
8989

90-
// Save the trained model
90+
// Save the trained model.
9191
mlContext.Model.Save(trainedModel, shuffledFullImagesDataset.Schema,
9292
"model.zip");
9393

94-
// Load the trained and saved model for prediction
94+
// Load the trained and saved model for prediction.
9595
ITransformer loadedModel;
9696
DataViewSchema schema;
9797
using (var file = File.OpenRead("model.zip"))
9898
loadedModel = mlContext.Model.Load(file, out schema);
9999

100-
// Evaluate the model on the test dataset
100+
// Evaluate the model on the test dataset.
101101
// Sample output:
102102
// Making bulk predictions and evaluating model's quality...
103103
// Micro-accuracy: 0.888888888888889,macro-accuracy = 0.883333333333333
@@ -126,43 +126,43 @@ public static void Example()
126126
Console.ReadKey();
127127
}
128128

129-
// Predict on a single image
129+
// Predict on a single image.
130130
private static void TrySinglePrediction(string imagesForPredictions,
131131
MLContext mlContext, ITransformer trainedModel)
132132
{
133-
// Create prediction function to try one prediction
133+
// Create prediction function to try one prediction.
134134
var predictionEngine = mlContext.Model
135135
.CreatePredictionEngine<InMemoryImageData,
136136
ImagePrediction>(trainedModel);
137137

138-
// Load test images
138+
// Load test images.
139139
IEnumerable<InMemoryImageData> testImages =
140140
LoadInMemoryImagesFromDirectory(imagesForPredictions, false);
141141

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.
143143
InMemoryImageData imageToPredict = new InMemoryImageData
144144
{
145145
Image = testImages.First().Image
146146
};
147147

148-
// Predict on the single image
148+
// Predict on the single image.
149149
var prediction = predictionEngine.Predict(imageToPredict);
150150

151151
Console.WriteLine($"Scores : [{string.Join(",", prediction.Score)}], " +
152152
$"Predicted Label : {prediction.PredictedLabel}");
153153
}
154154

155-
// Evaluate the trained model on the passed test dataset
155+
// Evaluate the trained model on the passed test dataset.
156156
private static void EvaluateModel(MLContext mlContext,
157157
IDataView testDataset, ITransformer trainedModel)
158158
{
159159
Console.WriteLine("Making bulk predictions and evaluating model's " +
160160
"quality...");
161161

162-
// Measuring time to evaluate
162+
// Measuring time to evaluate.
163163
var watch2 = System.Diagnostics.Stopwatch.StartNew();
164164

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.
166166
IDataView predictions = trainedModel.Transform(testDataset);
167167
var metrics = mlContext.MulticlassClassification.Evaluate(predictions);
168168

@@ -176,7 +176,7 @@ private static void EvaluateModel(MLContext mlContext,
176176
(elapsed2Ms / 1000).ToString() + " seconds");
177177
}
178178

179-
//Load the Image Data from input directory
179+
//Load the Image Data from input directory.
180180
public static IEnumerable<ImageData> LoadImagesFromDirectory(string folder,
181181
bool useFolderNameAsLabel = true)
182182
{
@@ -211,7 +211,7 @@ public static IEnumerable<ImageData> LoadImagesFromDirectory(string folder,
211211
}
212212
}
213213

214-
// Load In memory raw images from directory
214+
// Load In memory raw images from directory.
215215
public static IEnumerable<InMemoryImageData>
216216
LoadInMemoryImagesFromDirectory(string folder,
217217
bool useFolderNameAsLabel = true)
@@ -247,7 +247,7 @@ public static IEnumerable<InMemoryImageData>
247247
}
248248
}
249249

250-
// Download and unzip the image dataset
250+
// Download and unzip the image dataset.
251251
public static string DownloadImageSet(string imagesDownloadFolder)
252252
{
253253
// get a set of images to teach the network about the new classes
@@ -265,7 +265,7 @@ public static string DownloadImageSet(string imagesDownloadFolder)
265265
return Path.GetFileNameWithoutExtension(fileName);
266266
}
267267

268-
// Download file to destination directory from input URL
268+
// Download file to destination directory from input URL.
269269
public static bool Download(string url, string destDir, string destFileName)
270270
{
271271
if (destFileName == null)
@@ -295,7 +295,7 @@ public static bool Download(string url, string destDir, string destFileName)
295295
return true;
296296
}
297297

298-
// Unzip the file to destination folder
298+
// Unzip the file to destination folder.
299299
public static void UnZip(String gzArchiveName, String destFolder)
300300
{
301301
var flag = gzArchiveName.Split(Path.DirectorySeparatorChar)
@@ -313,7 +313,7 @@ public static void UnZip(String gzArchiveName, String destFolder)
313313
Console.WriteLine("Extracting is completed.");
314314
}
315315

316-
// Get absolute path from relative path
316+
// Get absolute path from relative path.
317317
public static string GetAbsolutePath(string relativePath)
318318
{
319319
FileInfo _dataRoot = new FileInfo(typeof(
@@ -326,7 +326,7 @@ public static string GetAbsolutePath(string relativePath)
326326
return fullPath;
327327
}
328328

329-
// InMemoryImageData class holding the raw image byte array and label
329+
// InMemoryImageData class holding the raw image byte array and label.
330330
public class InMemoryImageData
331331
{
332332
[LoadColumn(0)]
@@ -336,7 +336,7 @@ public class InMemoryImageData
336336
public string Label;
337337
}
338338

339-
// ImageData class holding the imagepath and label
339+
// ImageData class holding the imagepath and label.
340340
public class ImageData
341341
{
342342
[LoadColumn(0)]
@@ -346,7 +346,7 @@ public class ImageData
346346
public string Label;
347347
}
348348

349-
// ImagePrediction class holding the score and predicted label metrics
349+
// ImagePrediction class holding the score and predicted label metrics.
350350
public class ImagePrediction
351351
{
352352
[ColumnName("Score")]

0 commit comments

Comments
 (0)