From c6ed5b12850b913037390b8bc9ee2f8b996e675a Mon Sep 17 00:00:00 2001 From: Keren Fuentes Date: Tue, 27 Oct 2020 11:59:58 -0700 Subject: [PATCH 1/2] adding sample --- .../ImageAnalytics/LoadRawImages.cs | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ImageAnalytics/LoadRawImages.cs diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ImageAnalytics/LoadRawImages.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ImageAnalytics/LoadRawImages.cs new file mode 100644 index 0000000000..2dbf5a70f2 --- /dev/null +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ImageAnalytics/LoadRawImages.cs @@ -0,0 +1,90 @@ +using System; +using System.Drawing; +using System.IO; +using Microsoft.ML; +using Microsoft.ML.Data; + +namespace Samples.Dynamic +{ + public static class LoadImages + { + // Loads the images of the imagesFolder into an IDataView. + public static void Example() + { + // Create a new ML context, for ML.NET operations. It can be used for + // exception tracking and logging, as well as the source of randomness. + var mlContext = new MLContext(); + + // Downloading a few images, and an images.tsv file, which contains a + // list of the files from the dotnet/machinelearning/test/data/images/. + // If you inspect the fileSystem, after running this line, an "images" + // folder will be created, containing 4 images, and a .tsv file + // enumerating the images. + var imagesDataFile = Microsoft.ML.SamplesUtils.DatasetUtils + .GetSampleImages(); + + // Preview of the content of the images.tsv file + // + // imagePath imageType + // tomato.bmp tomato + // banana.jpg banana + // hotdog.jpg hotdog + // tomato.jpg tomato + + var data = mlContext.Data.CreateTextLoader(new TextLoader.Options() + { + Columns = new[] + { + new TextLoader.Column("ImagePath", DataKind.String, 0), + new TextLoader.Column("Name", DataKind.String, 1), + } + }).Load(imagesDataFile); + + var imagesFolder = Path.GetDirectoryName(imagesDataFile); + // Image loading pipeline. + var pipeline = mlContext.Transforms.LoadRawImageBytes("Image", + imagesFolder, "ImagePath"); + + var transformedData = pipeline.Fit(data).Transform(data); + + PrintColumns(transformedData); + // ImagePath Name + // tomato.bmp tomato + // banana.jpg banana + // hotdog.jpg hotdog + // tomato.jpg tomato + } + + private static void PrintColumns(IDataView transformedData) + { + // The transformedData IDataView contains the loaded images now. + Console.WriteLine("{0, -25} {1, -25} {2, -25}", "ImagePath", "Name", + "Image"); + + using (var cursor = transformedData.GetRowCursor(transformedData + .Schema)) + { + // Note that it is best to get the getters and values *before* + // iteration, so as to facilitate buffer sharing (if applicable), + // and column-type validation once, rather than many times. + ReadOnlyMemory imagePath = default; + ReadOnlyMemory name = default; + + var imagePathGetter = cursor.GetGetter>(cursor + .Schema["ImagePath"]); + + var nameGetter = cursor.GetGetter>(cursor + .Schema["Name"]); + + while (cursor.MoveNext()) + { + + imagePathGetter(ref imagePath); + nameGetter(ref name); + + Console.WriteLine("{0, -25} {1, -25}", imagePath, name); + } + } + } + } +} \ No newline at end of file From ef9c18426b7be87419d3c605824ffb604c02c2d8 Mon Sep 17 00:00:00 2001 From: Keren Fuentes Date: Fri, 30 Oct 2020 17:15:28 -0700 Subject: [PATCH 2/2] removing file and replacing example --- .../ImageAnalytics/LoadRawImages.cs | 90 ------------------- .../ExtensionsCatalog.cs | 2 +- 2 files changed, 1 insertion(+), 91 deletions(-) delete mode 100644 docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ImageAnalytics/LoadRawImages.cs diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ImageAnalytics/LoadRawImages.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ImageAnalytics/LoadRawImages.cs deleted file mode 100644 index 2dbf5a70f2..0000000000 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ImageAnalytics/LoadRawImages.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Drawing; -using System.IO; -using Microsoft.ML; -using Microsoft.ML.Data; - -namespace Samples.Dynamic -{ - public static class LoadImages - { - // Loads the images of the imagesFolder into an IDataView. - public static void Example() - { - // Create a new ML context, for ML.NET operations. It can be used for - // exception tracking and logging, as well as the source of randomness. - var mlContext = new MLContext(); - - // Downloading a few images, and an images.tsv file, which contains a - // list of the files from the dotnet/machinelearning/test/data/images/. - // If you inspect the fileSystem, after running this line, an "images" - // folder will be created, containing 4 images, and a .tsv file - // enumerating the images. - var imagesDataFile = Microsoft.ML.SamplesUtils.DatasetUtils - .GetSampleImages(); - - // Preview of the content of the images.tsv file - // - // imagePath imageType - // tomato.bmp tomato - // banana.jpg banana - // hotdog.jpg hotdog - // tomato.jpg tomato - - var data = mlContext.Data.CreateTextLoader(new TextLoader.Options() - { - Columns = new[] - { - new TextLoader.Column("ImagePath", DataKind.String, 0), - new TextLoader.Column("Name", DataKind.String, 1), - } - }).Load(imagesDataFile); - - var imagesFolder = Path.GetDirectoryName(imagesDataFile); - // Image loading pipeline. - var pipeline = mlContext.Transforms.LoadRawImageBytes("Image", - imagesFolder, "ImagePath"); - - var transformedData = pipeline.Fit(data).Transform(data); - - PrintColumns(transformedData); - // ImagePath Name - // tomato.bmp tomato - // banana.jpg banana - // hotdog.jpg hotdog - // tomato.jpg tomato - } - - private static void PrintColumns(IDataView transformedData) - { - // The transformedData IDataView contains the loaded images now. - Console.WriteLine("{0, -25} {1, -25} {2, -25}", "ImagePath", "Name", - "Image"); - - using (var cursor = transformedData.GetRowCursor(transformedData - .Schema)) - { - // Note that it is best to get the getters and values *before* - // iteration, so as to facilitate buffer sharing (if applicable), - // and column-type validation once, rather than many times. - ReadOnlyMemory imagePath = default; - ReadOnlyMemory name = default; - - var imagePathGetter = cursor.GetGetter>(cursor - .Schema["ImagePath"]); - - var nameGetter = cursor.GetGetter>(cursor - .Schema["Name"]); - - while (cursor.MoveNext()) - { - - imagePathGetter(ref imagePath); - nameGetter(ref name); - - Console.WriteLine("{0, -25} {1, -25}", imagePath, name); - } - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.ML.ImageAnalytics/ExtensionsCatalog.cs b/src/Microsoft.ML.ImageAnalytics/ExtensionsCatalog.cs index aa67b12187..4ba2aed578 100644 --- a/src/Microsoft.ML.ImageAnalytics/ExtensionsCatalog.cs +++ b/src/Microsoft.ML.ImageAnalytics/ExtensionsCatalog.cs @@ -84,7 +84,7 @@ public static ImageLoadingEstimator LoadImages(this TransformsCatalog catalog, s /// /// /// /// public static ImageLoadingEstimator LoadRawImageBytes(this TransformsCatalog catalog, string outputColumnName, string imageFolder, string inputColumnName = null)