Skip to content

Commit b1e5739

Browse files
author
Harshitha Parnandi Venkata
committed
Added unit test for the change
1 parent acf985d commit b1e5739

3 files changed

Lines changed: 43 additions & 17 deletions

File tree

docs/samples/Microsoft.ML.Samples/Dynamic/ImageClassification/ResnetV2101TransferLearningTrainTestSplit.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ public static void Example()
2828
"images");
2929

3030
//Download the image set and unzip
31-
//string finalImagesFolderName = DownloadImageSet(
32-
// imagesDownloadFolderPath);
33-
string finalImagesFolderName = "flower_photos";
31+
string finalImagesFolderName = DownloadImageSet(
32+
imagesDownloadFolderPath);
33+
// Use this for testing on big flower datasets:
34+
//string finalImagesFolderName = "flower_photos";
3435
string fullImagesetFolderPath = Path.Combine(
3536
imagesDownloadFolderPath, finalImagesFolderName);
3637

@@ -192,15 +193,7 @@ public static int LoadDataIntoBuffer(string path, ref VBuffer<Byte> imgData)
192193

193194
count = (int)fileLength;
194195
var editor = VBufferEditor.Create(ref imgData, count);
195-
//var buffer = File.ReadAllBytes(path);
196-
fs.Read(editor.Values);
197-
/*
198-
for (int i = 0; i < count; i++)
199-
{
200-
//editor.Values[i] = (byte) fs.ReadByte();
201-
editor.Values[i] = buffer[i];
202-
}
203-
*/
196+
fs.Read(editor.Values);
204197
imgData = editor.Commit();
205198

206199
return count;

src/Microsoft.ML.Dnn/ImageClassificationTransform.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ private void CheckTrainingParameters(ImageClassificationEstimator.Options option
189189
return (jpegData, resizedImage);
190190
}
191191

192-
193192
private static Tensor Encode(VBuffer<byte> buffer, int length)
194193
{
195194
var size = c_api.TF_StringEncodedSize((UIntPtr)length);
@@ -251,7 +250,6 @@ private void CacheFeaturizedImagesToDisk(IDataView input, string labelColumnName
251250
var imageBufGetter = cursor.GetGetter<VBuffer<byte>>(imageBufColumn);
252251
UInt32 label = UInt32.MaxValue;
253252
VBuffer<byte> imageBuf = default;
254-
ReadOnlyMemory<char> imagePath = default;
255253
runner.AddInput(inputTensorName);
256254
ImageClassificationMetrics metrics = new ImageClassificationMetrics();
257255
metrics.Bottleneck = new BottleneckMetrics();

test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ public void TensorFlowImageClassification()
12431243
IDataView testDataset = trainTestData.TestSet;
12441244

12451245
var pipeline = mlContext.Model.ImageClassification(
1246-
"ImagePath", "Label",
1246+
"ImageVBuf", "Label",
12471247
arch: ImageClassificationEstimator.Architecture.ResnetV2101,
12481248
epoch: 5,
12491249
batchSize: 5,
@@ -1281,12 +1281,41 @@ public void TensorFlowImageClassification()
12811281
}
12821282
}
12831283

1284+
public static int LoadDataIntoBuffer(string path, ref VBuffer<Byte> imgData)
1285+
{
1286+
int count = -1;
1287+
// bufferSize == 1 used to avoid unnecessary buffer in FileStream
1288+
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1))
1289+
{
1290+
long fileLength = fs.Length;
1291+
if (fileLength > int.MaxValue)
1292+
throw new IOException($"File {path} too big to open.");
1293+
else if (fileLength == 0)
1294+
{
1295+
byte[] _imageBuffer;
1296+
1297+
// Some file systems (e.g. procfs on Linux) return 0 for length even when there's content.
1298+
// Thus we need to assume 0 doesn't mean empty.
1299+
_imageBuffer = File.ReadAllBytes(path);
1300+
count = _imageBuffer.Length;
1301+
Console.WriteLine("File length is zero");
1302+
}
1303+
1304+
count = (int)fileLength;
1305+
var editor = VBufferEditor.Create(ref imgData, count);
1306+
fs.Read(editor.Values);
1307+
imgData = editor.Commit();
1308+
1309+
return count;
1310+
}
1311+
}
1312+
12841313
public static IEnumerable<ImageData> LoadImagesFromDirectory(string folder,
12851314
bool useFolderNameAsLabel = true)
12861315
{
12871316
var files = Directory.GetFiles(folder, "*",
12881317
searchOption: SearchOption.AllDirectories);
1289-
1318+
VBuffer<Byte> imgData = new VBuffer<byte>();
12901319
foreach (var file in files)
12911320
{
12921321
if (Path.GetExtension(file) != ".jpg")
@@ -1307,10 +1336,14 @@ public static IEnumerable<ImageData> LoadImagesFromDirectory(string folder,
13071336
}
13081337
}
13091338

1339+
// Get the buffer of bytes
1340+
int imgSize = LoadDataIntoBuffer(file, ref imgData);
1341+
13101342
yield return new ImageData()
13111343
{
13121344
ImagePath = file,
1313-
Label = label
1345+
Label = label,
1346+
ImageVBuf = imgData
13141347
};
13151348

13161349
}
@@ -1372,6 +1405,8 @@ public class ImageData
13721405

13731406
[LoadColumn(1)]
13741407
public string Label;
1408+
1409+
public VBuffer<byte> ImageVBuf;
13751410
}
13761411

13771412
public class ImagePrediction

0 commit comments

Comments
 (0)