ImageEstimatorsCatalog.ConvertToImage メソッド

定義

VectorToImageConvertingEstimatorを作成し、inputColumnNameで指定された列のデータから新しい列 (outputColumnName) に画像を作成します。

public static Microsoft.ML.Transforms.Image.VectorToImageConvertingEstimator ConvertToImage(this Microsoft.ML.TransformsCatalog catalog, int imageHeight, int imageWidth, string outputColumnName, string inputColumnName = default, Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator.ColorBits colorsPresent = Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator+ColorBits.Rgb, Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator.ColorsOrder orderOfColors = Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator+ColorsOrder.ARGB, bool interleavedColors = false, float scaleImage = 1, float offsetImage = 0, int defaultAlpha = 255, int defaultRed = 0, int defaultGreen = 0, int defaultBlue = 0);
static member ConvertToImage : Microsoft.ML.TransformsCatalog * int * int * string * string * Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator.ColorBits * Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator.ColorsOrder * bool * single * single * int * int * int * int -> Microsoft.ML.Transforms.Image.VectorToImageConvertingEstimator
<Extension()>
Public Function ConvertToImage (catalog As TransformsCatalog, imageHeight As Integer, imageWidth As Integer, outputColumnName As String, Optional inputColumnName As String = Nothing, Optional colorsPresent As ImagePixelExtractingEstimator.ColorBits = Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator+ColorBits.Rgb, Optional orderOfColors As ImagePixelExtractingEstimator.ColorsOrder = Microsoft.ML.Transforms.Image.ImagePixelExtractingEstimator+ColorsOrder.ARGB, Optional interleavedColors As Boolean = false, Optional scaleImage As Single = 1, Optional offsetImage As Single = 0, Optional defaultAlpha As Integer = 255, Optional defaultRed As Integer = 0, Optional defaultGreen As Integer = 0, Optional defaultBlue As Integer = 0) As VectorToImageConvertingEstimator

パラメーター

catalog
TransformsCatalog

変換のカタログ。

imageHeight
Int32

出力イメージの高さ。

imageWidth
Int32

出力イメージの幅。

outputColumnName
String

inputColumnNameの変換に起因する列の名前。 この列のデータ型は MLImageされます。

inputColumnName
String

イメージに変換するデータを含む列の名前。 この推定器は、 SingleDouble 、および Byteの既知のサイズのベクトルに対して動作します。

colorsPresent
ImagePixelExtractingEstimator.ColorBits

入力ピクセル ベクトルに存在する ImagePixelExtractingEstimator.ColorBits を指定します。 色の順序は、 orderOfColorsで指定します。

orderOfColors
ImagePixelExtractingEstimator.ColorsOrder

入力ベクターに色が表示される順序。

interleavedColors
Boolean

ピクセルがインターリーブされているかどうか。つまり、ピクセルが orderOfColors 順序であるか、平面形式で区切られているかを意味します。すべてのピクセルの 1 つの色のすべての値、その後、別の色のすべての値などです。

scaleImage
Single

値は、ピクセルに変換される前に、この値によってスケーリングされます。 offsetImageする前にベクター値に適用されます。

offsetImage
Single

オフセットは、値をピクセルに変換する前に減算されます。 scaleImage後のベクター値に適用されます。

defaultAlpha
Int32

アルファ カラーの既定値は、 colorsPresentAlphaが含まれている場合にオーバーライドされます。

defaultRed
Int32

赤の既定値は、 colorsPresentRedが含まれている場合にオーバーライドされます。

defaultGreen
Int32

緑色の既定値は、 colorsPresentGreenが含まれている場合にオーバーライドされます。

defaultBlue
Int32

青色の既定値は、 colorsPresentBlueが含まれている場合にオーバーライドされます。

返品

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Samples.Dynamic
{
    public static class ConvertToImage
    {
        private const int imageHeight = 224;
        private const int imageWidth = 224;
        private const int numberOfChannels = 3;
        private const int inputSize = imageHeight * imageWidth * numberOfChannels;

        // Sample that shows how an input array (of doubles) can be used to interop
        // with image related estimators in ML.NET.
        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();

            // Create a list of training data points.
            var dataPoints = GenerateRandomDataPoints(4);

            // Convert the list of data points to an IDataView object, which is
            // consumable by ML.NET API.
            var data = mlContext.Data.LoadFromEnumerable(dataPoints);

            // Image loading pipeline.
            var pipeline = mlContext.Transforms.ConvertToImage(imageHeight,
                imageWidth, "Image", "Features")
                .Append(mlContext.Transforms.ExtractPixels("Pixels", "Image"));

            var transformedData = pipeline.Fit(data).Transform(data);

            // Preview the transformedData.
            PrintColumns(transformedData);

            // Features                 Image                    Pixels
            // 185,209,196,142,52...    {Width=224, Height=224}  185,209,196,142,52...
            // 182,235,84,23,87...      {Width=224, Height=224}  182,235,84,23,87...
            // 192,214,247,22,38...     {Width=224, Height=224}  192,214,247,22,38...
            // 242,161,141,223,192...   {Width=224, Height=224}  242,161,141,223,192...
        }

        private static void PrintColumns(IDataView transformedData)
        {
            Console.WriteLine("{0, -25} {1, -25} {2, -25}", "Features", "Image",
                "Pixels");

            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.
                VBuffer<float> features = default;
                VBuffer<float> pixels = default;
                MLImage imageObject = null;

                var featuresGetter = cursor.GetGetter<VBuffer<float>>(cursor.Schema[
                    "Features"]);

                var pixelsGetter = cursor.GetGetter<VBuffer<float>>(cursor.Schema[
                    "Pixels"]);

                var imageGetter = cursor.GetGetter<MLImage>(cursor.Schema["Image"]);
                while (cursor.MoveNext())
                {

                    featuresGetter(ref features);
                    pixelsGetter(ref pixels);
                    imageGetter(ref imageObject);

                    Console.WriteLine("{0, -25} {1, -25} {2, -25}", string.Join(",",
                        features.DenseValues().Take(5)) + "...",
                        $"Width={imageObject.Width}, Height={imageObject.Height}",
                        string.Join(",", pixels.DenseValues().Take(5)) + "...");
                }

                // Dispose the image.
                imageObject.Dispose();
            }
        }

        private class DataPoint
        {
            [VectorType(inputSize)]
            public float[] Features { get; set; }
        }

        private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
            int seed = 0)
        {
            var random = new Random(seed);

            for (int i = 0; i < count; i++)
                yield return new DataPoint
                {
                    Features = Enumerable.Repeat(0,
                    inputSize).Select(x => (float)random.Next(0, 256)).ToArray()
                };
        }
    }
}

適用対象