Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated constructor of ImageLoadingTransformer to accept empty imageFolder paths #4976

Merged
merged 3 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Microsoft.ML.ImageAnalytics/ImageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,16 @@ internal ImageLoadingTransformer(IHostEnvironment env, string imageFolder = null
internal ImageLoadingTransformer(IHostEnvironment env, string imageFolder = null, bool type = true, params (string outputColumnName, string inputColumnName)[] columns)
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(ImageLoadingTransformer)), columns)
{
// Throws ArgumentException if given imageFolder path is invalid or empty. Note: imageFolder may be null in this case.
if (imageFolder != null)
// Throws ArgumentException if given imageFolder path is invalid. Note: imageFolder may be null or empty in this case.
if (String.IsNullOrEmpty(imageFolder))
ImageFolder = null;
else
{
if (Directory.Exists(imageFolder))
ImageFolder = Path.GetFullPath(imageFolder);
else
throw new ArgumentException(String.Format("Directory \"{0}\" does not exist.", imageFolder));
else
ImageFolder = null;
}
_useImageType = type;
}

Expand Down
112 changes: 72 additions & 40 deletions test/Microsoft.ML.Tests/ImagesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public void TestEstimatorChain()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var invalidData = TextLoader.Create(env, new TextLoader.Options()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.Single, 0),
}
new TextLoader.Column("ImagePath", DataKind.Single, 0),
}
}, new MultiFileSource(dataFile));

var pipe = new ImageLoadingEstimator(env, imageFolder, ("ImageReal", "ImagePath"))
Expand All @@ -64,9 +64,9 @@ public void TestEstimatorSaveLoad()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));

var pipe = new ImageLoadingEstimator(env, imageFolder, ("ImageReal", "ImagePath"))
Expand Down Expand Up @@ -98,6 +98,38 @@ public void TestEstimatorSaveLoad()
Done();
}

[Fact]
public void TestLoadImages()
{
var env = new MLContext(1);
var dataFile = GetDataPath("images/images.tsv");
var correctImageFolder = Path.GetDirectoryName(dataFile);
var data = TextLoader.Create(env, new TextLoader.Options()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));

// Testing for invalid imageFolder path, should throw an ArgumentException
var incorrectImageFolder = correctImageFolder + "-nonExistantDirectory";
Assert.Throws<ArgumentException>(() => new ImageLoadingTransformer(env, incorrectImageFolder, ("ImageReal", "ImagePath")).Transform(data));

// Testing for empty imageFolder path, should not throw an exception
var emptyImageFolder = String.Empty;
var imagesEmptyImageFolder = new ImageLoadingTransformer(env, emptyImageFolder, ("ImageReal", "ImagePath")).Transform(data);

// Testing for null imageFolder path, should not throw an exception
var imagesNullImageFolder = new ImageLoadingTransformer(env, null, ("ImageReal", "ImagePath")).Transform(data);

// Testing for correct imageFolder path, should not throw an exception
var imagesCorrectImageFolder = new ImageLoadingTransformer(env, correctImageFolder, ("ImageReal", "ImagePath")).Transform(data);

Done();
}

[Fact]
public void TestSaveImages()
{
Expand All @@ -108,9 +140,9 @@ public void TestSaveImages()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var images = new ImageLoadingTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data);
var cropped = new ImageResizingTransformer(env, "ImageCropped", 100, 100, "ImageReal", ImageResizingEstimator.ResizingKind.IsoPad).Transform(images);
Expand Down Expand Up @@ -145,9 +177,9 @@ public void TestGreyscaleTransformImages()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var images = new ImageLoadingTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data);

Expand Down Expand Up @@ -279,9 +311,9 @@ public void TestBackAndForthConversionWithAlphaInterleave()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var images = new ImageLoadingTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data);
var cropped = new ImageResizingTransformer(env, "ImageCropped", imageWidth, imageHeight, "ImageReal").Transform(images);
Expand Down Expand Up @@ -336,9 +368,9 @@ public void TestBackAndForthConversionWithoutAlphaInterleave()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var images = new ImageLoadingTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data);
var cropped = new ImageResizingTransformer(env, "ImageCropped", imageWidth, imageHeight, "ImageReal").Transform(images);
Expand Down Expand Up @@ -393,9 +425,9 @@ public void TestBackAndForthConversionWithDifferentOrder()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var images = new ImageLoadingTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data);
var cropped = new ImageResizingTransformer(env, "ImageCropped", imageWidth, imageHeight, "ImageReal").Transform(images);
Expand Down Expand Up @@ -452,9 +484,9 @@ public void TestBackAndForthConversionWithAlphaNoInterleave()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var images = new ImageLoadingTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data);
var cropped = new ImageResizingTransformer(env, "ImageCropped", imageWidth, imageHeight, "ImageReal").Transform(images);
Expand Down Expand Up @@ -509,9 +541,9 @@ public void TestBackAndForthConversionWithoutAlphaNoInterleave()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var images = new ImageLoadingTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data);
var cropped = new ImageResizingTransformer(env, "ImageCropped", imageWidth, imageHeight, "ImageReal").Transform(images);
Expand Down Expand Up @@ -566,9 +598,9 @@ public void TestBackAndForthConversionWithAlphaInterleaveNoOffset()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var images = new ImageLoadingTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data);
var cropped = new ImageResizingTransformer(env, "ImageCropped", imageWidth, imageHeight, "ImageReal").Transform(images);
Expand Down Expand Up @@ -624,9 +656,9 @@ public void TestBackAndForthConversionWithoutAlphaInterleaveNoOffset()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var images = new ImageLoadingTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data);
var cropped = new ImageResizingTransformer(env, "ImageCropped", imageWidth, imageHeight, "ImageReal").Transform(images);
Expand Down Expand Up @@ -681,9 +713,9 @@ public void TestBackAndForthConversionWithAlphaNoInterleaveNoOffset()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var images = new ImageLoadingTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data);
var cropped = new ImageResizingTransformer(env, "ImageCropped", imageWidth, imageHeight, "ImageReal").Transform(images);
Expand Down Expand Up @@ -739,8 +771,8 @@ public void TestBackAndForthConversionWithoutAlphaNoInterleaveNoOffset()
{
Columns = new[]
{
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
new TextLoader.Column("ImagePath", DataKind.String, 0),
new TextLoader.Column("Name", DataKind.String, 1),
}
}, new MultiFileSource(dataFile));
var images = new ImageLoadingTransformer(env, imageFolder, ("ImageReal", "ImagePath")).Transform(data);
Expand Down