forked from dotnet/machinelearning
/
ImageClassificationBench.cs
242 lines (198 loc) · 9.04 KB
/
ImageClassificationBench.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms;
using BenchmarkDotNet.Attributes;
using static Microsoft.ML.DataOperationsCatalog;
using System.Net.Http;
using Microsoft.ML.Vision;
namespace Microsoft.ML.Benchmarks
{
[Config(typeof(TrainConfig))]
public class ImageClassificationBench
{
private MLContext mlContext;
private IDataView trainDataset;
private IDataView testDataset;
[GlobalSetup]
public void SetupData()
{
mlContext = new MLContext(seed: 1);
/*
* Running in benchmarks causes to create a new temporary dir for each run
* However this dir is deleted while still running, as such need to get one
* level up to prevent issues with saving data.
*/
string assetsRelativePath = @"../../../../assets";
string assetsPath = GetAbsolutePath(assetsRelativePath);
var outputMlNetModelFilePath = Path.Combine(assetsPath, "outputs",
"imageClassifier.zip");
string imagesDownloadFolderPath = Path.Combine(assetsPath, "inputs",
"images");
//Download the image set and unzip
string finalImagesFolderName = DownloadImageSet(
imagesDownloadFolderPath);
string fullImagesetFolderPath = Path.Combine(
imagesDownloadFolderPath, finalImagesFolderName);
//Load all the original images info
IEnumerable<ImageData> images = LoadImagesFromDirectory(
folder: fullImagesetFolderPath, useFolderNameAsLabel: true);
IDataView shuffledFullImagesDataset = mlContext.Data.ShuffleRows(
mlContext.Data.LoadFromEnumerable(images));
shuffledFullImagesDataset = mlContext.Transforms.Conversion
.MapValueToKey("Label")
.Append(mlContext.Transforms.LoadRawImageBytes("Image",
fullImagesetFolderPath, "ImagePath"))
.Fit(shuffledFullImagesDataset)
.Transform(shuffledFullImagesDataset);
// Split the data 90:10 into train and test sets, train and
// evaluate.
TrainTestData trainTestData = mlContext.Data.TrainTestSplit(
shuffledFullImagesDataset, testFraction: 0.1, seed: 1);
trainDataset = trainTestData.TrainSet;
testDataset = trainTestData.TestSet;
}
[Benchmark]
public TransformerChain<KeyToValueMappingTransformer> TrainResnetV250()
{
var options = new ImageClassificationTrainer.Options()
{
FeatureColumnName = "Image",
LabelColumnName = "Label",
Arch = ImageClassificationTrainer.Architecture.ResnetV250,
Epoch = 50,
BatchSize = 10,
LearningRate = 0.01f,
EarlyStoppingCriteria = new ImageClassificationTrainer.EarlyStopping(minDelta: 0.001f, patience: 20, metric: ImageClassificationTrainer.EarlyStoppingMetric.Loss),
ValidationSet = testDataset
};
var pipeline = mlContext.MulticlassClassification.Trainers.ImageClassification(options)
.Append(mlContext.Transforms.Conversion.MapKeyToValue(
outputColumnName: "PredictedLabel",
inputColumnName: "PredictedLabel"));
return pipeline.Fit(trainDataset);
}
public static IEnumerable<ImageData> LoadImagesFromDirectory(string folder,
bool useFolderNameAsLabel = true)
{
var files = Directory.GetFiles(folder, "*",
searchOption: SearchOption.AllDirectories);
foreach (var file in files)
{
if (Path.GetExtension(file) != ".jpg" &&
Path.GetExtension(file) != ".JPEG" &&
Path.GetExtension(file) != ".png")
continue;
var label = Path.GetFileName(file);
if (useFolderNameAsLabel)
label = Directory.GetParent(file).Name;
else
{
for (int index = 0; index < label.Length; index++)
{
if (!char.IsLetter(label[index]))
{
label = label.Substring(0, index);
break;
}
}
}
yield return new ImageData()
{
ImagePath = file,
Label = label
};
}
}
public static string DownloadImageSet(string imagesDownloadFolder)
{
// get a set of images to teach the network about the new classes
//SINGLE SMALL FLOWERS IMAGESET (200 files)
string fileName = "flower_photos_small_set.zip";
string url = $"https://aka.ms/mlnet-resources/datasets/flower_photos_small_set.zip/";
Download(url, imagesDownloadFolder, fileName);
UnZip(Path.Combine(imagesDownloadFolder, fileName), imagesDownloadFolder);
return Path.GetFileNameWithoutExtension(fileName);
}
public static bool Download(string url, string destDir, string destFileName)
{
if (destFileName == null)
destFileName = url.Split(Path.DirectorySeparatorChar).Last();
string relativeFilePath = Path.Combine(destDir, destFileName);
using (HttpClient client = new HttpClient())
{
if (File.Exists(relativeFilePath))
{
var headerResponse = client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead).Result;
var totalSizeInBytes = headerResponse.Content.Headers.ContentLength;
var currentSize = new FileInfo(relativeFilePath).Length;
//If current file size is not equal to expected file size, re-download file
if (currentSize != totalSizeInBytes)
{
File.Delete(relativeFilePath);
var response = client.GetAsync(url).Result;
using FileStream fileStream = new FileStream(relativeFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
using Stream contentStream = response.Content.ReadAsStreamAsync().Result;
contentStream.CopyTo(fileStream);
}
}
else
{
Directory.CreateDirectory(destDir);
var response = client.GetAsync(url).Result;
using FileStream fileStream = new FileStream(relativeFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
using Stream contentStream = response.Content.ReadAsStreamAsync().Result;
contentStream.CopyTo(fileStream);
}
}
return true;
}
public static void UnZip(String gzArchiveName, String destFolder)
{
var flag = gzArchiveName.Split(Path.DirectorySeparatorChar)
.Last()
.Split('.')
.First() + ".bin";
if (File.Exists(Path.Combine(destFolder, flag))) return;
ZipFile.ExtractToDirectory(gzArchiveName, destFolder);
File.Create(Path.Combine(destFolder, flag));
Console.WriteLine("");
Console.WriteLine("Extracting is completed.");
}
public static string GetAbsolutePath(string relativePath)
{
FileInfo dataRoot = new FileInfo(typeof(
ImageClassificationBench).Assembly.Location);
string assemblyFolderPath = dataRoot.Directory.FullName;
string fullPath = Path.Combine(assemblyFolderPath, relativePath);
return fullPath;
}
public class ImageData
{
[LoadColumn(0)]
public string ImagePath;
[LoadColumn(1)]
public string Label;
}
}
public static class HttpContentExtensions
{
public static async Task ReadAsFileAsync(this HttpContent content, string filename, bool overwrite)
{
string pathname = Path.GetFullPath(filename);
if (!overwrite && File.Exists(filename))
{
throw new InvalidOperationException(string.Format("File {0} already exists.", pathname));
}
using FileStream fileStream = new FileStream(pathname, FileMode.Create, FileAccess.Write, FileShare.None);
await content.CopyToAsync(fileStream);
}
}
}