ImageEstimatorsCatalog.LoadImages 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立 ImageLoadingEstimator ,它會將資料從 中指定的 inputColumnName
資料行載入至新的資料行: outputColumnName
。
public static Microsoft.ML.Data.ImageLoadingEstimator LoadImages (this Microsoft.ML.TransformsCatalog catalog, string outputColumnName, string imageFolder, string inputColumnName = default);
static member LoadImages : Microsoft.ML.TransformsCatalog * string * string * string -> Microsoft.ML.Data.ImageLoadingEstimator
<Extension()>
Public Function LoadImages (catalog As TransformsCatalog, outputColumnName As String, imageFolder As String, Optional inputColumnName As String = Nothing) As ImageLoadingEstimator
參數
- catalog
- TransformsCatalog
轉換的目錄。
- imageFolder
- String
要在其中尋找影像的資料夾。
- inputColumnName
- String
具有要載入之影像路徑的資料行名稱。 此估算器會透過文字資料操作。
傳回
範例
using System;
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.LoadImages("ImageObject",
imagesFolder, "ImagePath");
var transformedData = pipeline.Fit(data).Transform(data);
PrintColumns(transformedData);
// Preview the transformedData.
// ImagePath Name ImageObject
// tomato.bmp tomato {Width=800, Height=534}
// banana.jpg banana {Width=800, Height=288}
// hotdog.jpg hotdog {Width=800, Height=391}
// tomato.jpg tomato {Width=800, Height=534}
}
private static void PrintColumns(IDataView transformedData)
{
// The transformedData IDataView contains the loaded images now.
Console.WriteLine("{0, -25} {1, -25} {2, -25}", "ImagePath", "Name",
"ImageObject");
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<char> imagePath = default;
ReadOnlyMemory<char> name = default;
MLImage imageObject = null;
var imagePathGetter = cursor.GetGetter<ReadOnlyMemory<char>>(cursor
.Schema["ImagePath"]);
var nameGetter = cursor.GetGetter<ReadOnlyMemory<char>>(cursor
.Schema["Name"]);
var imageObjectGetter = cursor.GetGetter<MLImage>(cursor.Schema[
"ImageObject"]);
while (cursor.MoveNext())
{
imagePathGetter(ref imagePath);
nameGetter(ref name);
imageObjectGetter(ref imageObject);
Console.WriteLine("{0, -25} {1, -25} {2, -25}",
imagePath, name,
$"Width={imageObject.Width}, Height={imageObject.Height}");
}
// Dispose the image.
imageObject.Dispose();
}
}
}
}