Share via


OnnxCatalog.ApplyOnnxModel Yöntem

Tanım

Aşırı Yüklemeler

ApplyOnnxModel(TransformsCatalog, OnnxOptions)

Belirtilen OnnxOptionskullanarak bir OnnxScoringEstimator oluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

ApplyOnnxModel(TransformsCatalog, String, Nullable<Int32>, Boolean)

Giriş sütununa önceden eğitilmiş bir Onnx modeli uygulayan bir OnnxScoringEstimatoroluşturun. Giriş/çıkış sütunları, sağlanan ONNX modelinin giriş/çıkış sütunlarına göre belirlenir. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

ApplyOnnxModel(TransformsCatalog, String, IDictionary<String,Int32[]>, Nullable<Int32>, Boolean)

Giriş sütununa önceden eğitilmiş bir Onnx modeli uygulayan bir OnnxScoringEstimatoroluşturun. Giriş/çıkış sütunları, sağlanan ONNX modelinin giriş/çıkış sütunlarına göre belirlenir. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

ApplyOnnxModel(TransformsCatalog, String, String, String, Nullable<Int32>, Boolean)

Sütununa önceden eğitilmiş bir Onnx modeli inputColumnName uygulayan bir OnnxScoringEstimatoroluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

ApplyOnnxModel(TransformsCatalog, String[], String[], String, Nullable<Int32>, Boolean)

Sütunlara önceden eğitilmiş bir Onnx modeli inputColumnNames uygulayan bir OnnxScoringEstimatoroluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

ApplyOnnxModel(TransformsCatalog, String, String, String, IDictionary<String,Int32[]>, Nullable<Int32>, Boolean)

Sütununa önceden eğitilmiş bir Onnx modeli inputColumnName uygulayan bir OnnxScoringEstimatoroluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

ApplyOnnxModel(TransformsCatalog, String[], String[], String, IDictionary<String,Int32[]>, Nullable<Int32>, Boolean)

Sütunlara önceden eğitilmiş bir Onnx modeli inputColumnNames uygulayan bir OnnxScoringEstimatoroluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

ApplyOnnxModel(TransformsCatalog, String[], String[], String, IDictionary<String,Int32[]>, Nullable<Int32>, Boolean, Int32)

Sütunlara önceden eğitilmiş bir Onnx modeli inputColumnNames uygulayan bir OnnxScoringEstimatoroluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

ApplyOnnxModel(TransformsCatalog, OnnxOptions)

Belirtilen OnnxOptionskullanarak bir OnnxScoringEstimator oluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

public static Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator ApplyOnnxModel (this Microsoft.ML.TransformsCatalog catalog, Microsoft.ML.Transforms.Onnx.OnnxOptions options);
static member ApplyOnnxModel : Microsoft.ML.TransformsCatalog * Microsoft.ML.Transforms.Onnx.OnnxOptions -> Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator
<Extension()>
Public Function ApplyOnnxModel (catalog As TransformsCatalog, options As OnnxOptions) As OnnxScoringEstimator

Parametreler

catalog
TransformsCatalog

Dönüşümün kataloğu.

options
OnnxOptions

için OnnxScoringEstimatorseçenekler.

Döndürülenler

Açıklamalar

Seçenekler ise. GpuDeviceId değeri değilse kullanılacak değerdir nullMLContext.GpuDeviceIdnull.

Şunlara uygulanır

ApplyOnnxModel(TransformsCatalog, String, Nullable<Int32>, Boolean)

Giriş sütununa önceden eğitilmiş bir Onnx modeli uygulayan bir OnnxScoringEstimatoroluşturun. Giriş/çıkış sütunları, sağlanan ONNX modelinin giriş/çıkış sütunlarına göre belirlenir. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

public static Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator ApplyOnnxModel (this Microsoft.ML.TransformsCatalog catalog, string modelFile, int? gpuDeviceId = default, bool fallbackToCpu = false);
static member ApplyOnnxModel : Microsoft.ML.TransformsCatalog * string * Nullable<int> * bool -> Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator
<Extension()>
Public Function ApplyOnnxModel (catalog As TransformsCatalog, modelFile As String, Optional gpuDeviceId As Nullable(Of Integer) = Nothing, Optional fallbackToCpu As Boolean = false) As OnnxScoringEstimator

Parametreler

catalog
TransformsCatalog

Dönüşümün kataloğu.

modelFile
String

ONNX modelini içeren dosyanın yolu.

gpuDeviceId
Nullable<Int32>

CPU üzerinde çalıştırılacak, null üzerinde yürütmenin çalıştırılacak isteğe bağlı GPU cihaz kimliği.

fallbackToCpu
Boolean

GPU hatası olursa, özel durum veya CPU'ya geri dönüş sağlayın.

Döndürülenler

Örnekler

using System;
using System.IO;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Samples.Dynamic
{
    public static class ApplyOnnxModel
    {
        public static void Example()
        {
            // Download the squeeznet image model from ONNX model zoo, version 1.2
            // https://github.com/onnx/models/tree/master/squeezenet or
            // https://s3.amazonaws.com/download.onnx/models/opset_8/squeezenet.tar.gz
            // or use Microsoft.ML.Onnx.TestModels nuget.
            var modelPath = @"squeezenet\00000001\model.onnx";

            // Create ML pipeline to score the data using OnnxScoringEstimator
            var mlContext = new MLContext();

            // Generate sample test data.
            var samples = GetTensorData();
            // Convert training data to IDataView, the general data type used in
            // ML.NET.
            var data = mlContext.Data.LoadFromEnumerable(samples);
            // Create the pipeline to score using provided onnx model.
            var pipeline = mlContext.Transforms.ApplyOnnxModel(modelPath);
            // Fit the pipeline and get the transformed values
            var transformedValues = pipeline.Fit(data).Transform(data);
            // Retrieve model scores into Prediction class
            var predictions = mlContext.Data.CreateEnumerable<Prediction>(
                transformedValues, reuseRowObject: false);

            // Iterate rows
            foreach (var prediction in predictions)
            {
                int numClasses = 0;
                foreach (var classScore in prediction.softmaxout_1.Take(3))
                {
                    Console.WriteLine("Class #" + numClasses++ + " score = " +
                        classScore);
                }
                Console.WriteLine(new string('-', 10));
            }

            // Results look like below...
            // Class #0 score = 4.544065E-05
            // Class #1 score = 0.003845858
            // Class #2 score = 0.0001249467
            // ----------
            // Class #0 score = 4.491953E-05
            // Class #1 score = 0.003848222
            // Class #2 score = 0.0001245592
            // ----------
        }

        // inputSize is the overall dimensions of the model input tensor.
        private const int inputSize = 224 * 224 * 3;

        // A class to hold sample tensor data. Member name should match
        // the inputs that the model expects (in this case, data_0)
        public class TensorData
        {
            [VectorType(inputSize)]
            public float[] data_0 { get; set; }
        }

        // Method to generate sample test data. Returns 2 sample rows.
        public static TensorData[] GetTensorData()
        {
            // This can be any numerical data. Assume image pixel values.
            var image1 = Enumerable.Range(0, inputSize).Select(x => (float)x /
                inputSize).ToArray();

            var image2 = Enumerable.Range(0, inputSize).Select(x => (float)(x +
                10000) / inputSize).ToArray();

            return new TensorData[] { new TensorData() { data_0 = image1 }, new
                TensorData() { data_0 = image2 } };
        }

        // Class to contain the output values from the transformation.
        // This model generates a vector of 1000 floats.
        class Prediction
        {
            [VectorType(1000)]
            public float[] softmaxout_1 { get; set; }
        }
    }
}

Açıklamalar

Giriş sütunlarının adı/türü, ONNX modeli girişlerinin adı/türüyle tam olarak eşleşmelidir. Oluşturulan çıkış sütunlarının adı/türü, ONNX modeli çıkışlarının adı/türüyle eşleşecektir. gpuDeviceId değeri ise nullMLContext.GpuDeviceId değeri değilse nullkullanılır.

Şunlara uygulanır

ApplyOnnxModel(TransformsCatalog, String, IDictionary<String,Int32[]>, Nullable<Int32>, Boolean)

Giriş sütununa önceden eğitilmiş bir Onnx modeli uygulayan bir OnnxScoringEstimatoroluşturun. Giriş/çıkış sütunları, sağlanan ONNX modelinin giriş/çıkış sütunlarına göre belirlenir. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

public static Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator ApplyOnnxModel (this Microsoft.ML.TransformsCatalog catalog, string modelFile, System.Collections.Generic.IDictionary<string,int[]> shapeDictionary, int? gpuDeviceId = default, bool fallbackToCpu = false);
static member ApplyOnnxModel : Microsoft.ML.TransformsCatalog * string * System.Collections.Generic.IDictionary<string, int[]> * Nullable<int> * bool -> Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator
<Extension()>
Public Function ApplyOnnxModel (catalog As TransformsCatalog, modelFile As String, shapeDictionary As IDictionary(Of String, Integer()), Optional gpuDeviceId As Nullable(Of Integer) = Nothing, Optional fallbackToCpu As Boolean = false) As OnnxScoringEstimator

Parametreler

catalog
TransformsCatalog

Dönüşümün kataloğu.

modelFile
String

ONNX modelini içeren dosyanın yolu.

shapeDictionary
IDictionary<String,Int32[]>

'den modelFileyüklenenler üzerinde kullanılacak ONNX şekilleri. Anahtarlar için, ONNX modelinde belirtildiği gibi adları kullanın, örneğin "giriş". Bu parametreye sahip şekillerin belirtilmesi, değişken boyut girişleri ve çıkışlarıyla çalışmak için özellikle yararlıdır.

gpuDeviceId
Nullable<Int32>

CPU üzerinde çalıştırılacak, null üzerinde yürütmenin çalıştırılacak isteğe bağlı GPU cihaz kimliği.

fallbackToCpu
Boolean

GPU hatası olursa, özel durum veya CPU'ya geri dönüş sağlayın.

Döndürülenler

Örnekler

using System;
using System.IO;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Samples.Dynamic
{
    public static class ApplyOnnxModel
    {
        public static void Example()
        {
            // Download the squeeznet image model from ONNX model zoo, version 1.2
            // https://github.com/onnx/models/tree/master/squeezenet or
            // https://s3.amazonaws.com/download.onnx/models/opset_8/squeezenet.tar.gz
            // or use Microsoft.ML.Onnx.TestModels nuget.
            var modelPath = @"squeezenet\00000001\model.onnx";

            // Create ML pipeline to score the data using OnnxScoringEstimator
            var mlContext = new MLContext();

            // Generate sample test data.
            var samples = GetTensorData();
            // Convert training data to IDataView, the general data type used in
            // ML.NET.
            var data = mlContext.Data.LoadFromEnumerable(samples);
            // Create the pipeline to score using provided onnx model.
            var pipeline = mlContext.Transforms.ApplyOnnxModel(modelPath);
            // Fit the pipeline and get the transformed values
            var transformedValues = pipeline.Fit(data).Transform(data);
            // Retrieve model scores into Prediction class
            var predictions = mlContext.Data.CreateEnumerable<Prediction>(
                transformedValues, reuseRowObject: false);

            // Iterate rows
            foreach (var prediction in predictions)
            {
                int numClasses = 0;
                foreach (var classScore in prediction.softmaxout_1.Take(3))
                {
                    Console.WriteLine("Class #" + numClasses++ + " score = " +
                        classScore);
                }
                Console.WriteLine(new string('-', 10));
            }

            // Results look like below...
            // Class #0 score = 4.544065E-05
            // Class #1 score = 0.003845858
            // Class #2 score = 0.0001249467
            // ----------
            // Class #0 score = 4.491953E-05
            // Class #1 score = 0.003848222
            // Class #2 score = 0.0001245592
            // ----------
        }

        // inputSize is the overall dimensions of the model input tensor.
        private const int inputSize = 224 * 224 * 3;

        // A class to hold sample tensor data. Member name should match
        // the inputs that the model expects (in this case, data_0)
        public class TensorData
        {
            [VectorType(inputSize)]
            public float[] data_0 { get; set; }
        }

        // Method to generate sample test data. Returns 2 sample rows.
        public static TensorData[] GetTensorData()
        {
            // This can be any numerical data. Assume image pixel values.
            var image1 = Enumerable.Range(0, inputSize).Select(x => (float)x /
                inputSize).ToArray();

            var image2 = Enumerable.Range(0, inputSize).Select(x => (float)(x +
                10000) / inputSize).ToArray();

            return new TensorData[] { new TensorData() { data_0 = image1 }, new
                TensorData() { data_0 = image2 } };
        }

        // Class to contain the output values from the transformation.
        // This model generates a vector of 1000 floats.
        class Prediction
        {
            [VectorType(1000)]
            public float[] softmaxout_1 { get; set; }
        }
    }
}

Açıklamalar

Giriş sütunlarının adı/türü, ONNX modeli girişlerinin adı/türüyle tam olarak eşleşmelidir. Oluşturulan çıkış sütunlarının adı/türü, ONNX modeli çıkışlarının adı/türüyle eşleşecektir. gpuDeviceId değeri ise nullMLContext.GpuDeviceId değeri değilse nullkullanılır.

Şunlara uygulanır

ApplyOnnxModel(TransformsCatalog, String, String, String, Nullable<Int32>, Boolean)

Sütununa önceden eğitilmiş bir Onnx modeli inputColumnName uygulayan bir OnnxScoringEstimatoroluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

public static Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator ApplyOnnxModel (this Microsoft.ML.TransformsCatalog catalog, string outputColumnName, string inputColumnName, string modelFile, int? gpuDeviceId = default, bool fallbackToCpu = false);
static member ApplyOnnxModel : Microsoft.ML.TransformsCatalog * string * string * string * Nullable<int> * bool -> Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator
<Extension()>
Public Function ApplyOnnxModel (catalog As TransformsCatalog, outputColumnName As String, inputColumnName As String, modelFile As String, Optional gpuDeviceId As Nullable(Of Integer) = Nothing, Optional fallbackToCpu As Boolean = false) As OnnxScoringEstimator

Parametreler

catalog
TransformsCatalog

Dönüşümün kataloğu.

outputColumnName
String

Dönüştürme sonucunda elde edilen çıkış sütunu.

inputColumnName
String

Giriş sütunu.

modelFile
String

ONNX modelini içeren dosyanın yolu.

gpuDeviceId
Nullable<Int32>

CPU üzerinde çalıştırılacak, null üzerinde yürütmenin çalıştırılacak isteğe bağlı GPU cihaz kimliği.

fallbackToCpu
Boolean

GPU hatası olursa, özel durum veya CPU'ya geri dönüş sağlayın.

Döndürülenler

Örnekler

using System;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms.Image;

namespace Samples.Dynamic
{
    public static class ApplyOnnxModelWithInMemoryImages
    {
        // Example of applying ONNX transform on in-memory images.
        public static void Example()
        {
            // Download the squeeznet image model from ONNX model zoo, version 1.2
            // https://github.com/onnx/models/tree/master/vision/classification/squeezenet or use
            // Microsoft.ML.Onnx.TestModels nuget.
            // It's a multiclass classifier. It consumes an input "data_0" and
            // produces an output "softmaxout_1".
            var modelPath = @"squeezenet\00000001\model.onnx";

            // Create ML pipeline to score the data using OnnxScoringEstimator
            var mlContext = new MLContext();

            // Create in-memory data points. Its Image/Scores field is the
            // input /output of the used ONNX model.
            var dataPoints = new ImageDataPoint[]
            {
                new ImageDataPoint(red: 255, green: 0, blue: 0), // Red color
                new ImageDataPoint(red: 0, green: 128, blue: 0)  // Green color
            };

            // Convert training data to IDataView, the general data type used in
            // ML.NET.
            var dataView = mlContext.Data.LoadFromEnumerable(dataPoints);

            // Create a ML.NET pipeline which contains two steps. First,
            // ExtractPixle is used to convert the 224x224 image to a 3x224x224
            // float tensor. Then the float tensor is fed into a ONNX model with an
            // input called "data_0" and an output called "softmaxout_1". Note that
            // "data_0" and "softmaxout_1" are model input and output names stored
            // in the used ONNX model file. Users may need to inspect their own
            // models to get the right input and output column names.
            // Map column "Image" to column "data_0"
            // Map column "data_0" to column "softmaxout_1"
            var pipeline = mlContext.Transforms.ExtractPixels("data_0", "Image")
                .Append(mlContext.Transforms.ApplyOnnxModel("softmaxout_1",
                "data_0", modelPath));

            var model = pipeline.Fit(dataView);
            var onnx = model.Transform(dataView);

            // Convert IDataView back to IEnumerable<ImageDataPoint> so that user
            // can inspect the output, column "softmaxout_1", of the ONNX transform.
            // Note that Column "softmaxout_1" would be stored in ImageDataPont
            //.Scores because the added attributed [ColumnName("softmaxout_1")]
            // tells that ImageDataPont.Scores is equivalent to column
            // "softmaxout_1".
            var transformedDataPoints = mlContext.Data.CreateEnumerable<
                ImageDataPoint>(onnx, false).ToList();

            // The scores are probabilities of all possible classes, so they should
            // all be positive.
            foreach (var dataPoint in transformedDataPoints)
            {
                var firstClassProb = dataPoint.Scores.First();
                var lastClassProb = dataPoint.Scores.Last();
                Console.WriteLine("The probability of being the first class is " +
                    (firstClassProb * 100) + "%.");

                Console.WriteLine($"The probability of being the last class is " +
                    (lastClassProb * 100) + "%.");
            }

            // Expected output:
            //  The probability of being the first class is 0.002542659%.
            //  The probability of being the last class is 0.0292684%.
            //  The probability of being the first class is 0.02258059%.
            //  The probability of being the last class is 0.394428%.
        }

        // This class is used in Example() to describe data points which will be
        // consumed by ML.NET pipeline.
        private class ImageDataPoint
        {
            // Height of Image.
            private const int height = 224;

            // Width of Image.
            private const int width = 224;

            // Image will be consumed by ONNX image multiclass classification model.
            [ImageType(height, width)]
            public MLImage Image { get; set; }

            // Expected output of ONNX model. It contains probabilities of all
            // classes. Note that the ColumnName below should match the output name
            // in the used ONNX model file.
            [ColumnName("softmaxout_1")]
            public float[] Scores { get; set; }

            public ImageDataPoint()
            {
                Image = null;
            }

            public ImageDataPoint(byte red, byte green, byte blue)
            {
                byte[] imageData = new byte[width * height * 4]; // 4 for the red, green, blue and alpha colors
                for (int i = 0; i < imageData.Length; i += 4)
                {
                    // Fill the buffer with the Bgra32 format
                    imageData[i] = blue;
                    imageData[i + 1] = green;
                    imageData[i + 2] = red;
                    imageData[i + 3] = 255;
                }

                Image = MLImage.CreateFromPixels(width, height, MLPixelFormat.Bgra32, imageData);
            }
        }
    }
}

Açıklamalar

gpuDeviceId değeri ise nullMLContext.GpuDeviceId değeri değilse nullkullanılır.

Şunlara uygulanır

ApplyOnnxModel(TransformsCatalog, String[], String[], String, Nullable<Int32>, Boolean)

Sütunlara önceden eğitilmiş bir Onnx modeli inputColumnNames uygulayan bir OnnxScoringEstimatoroluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

public static Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator ApplyOnnxModel (this Microsoft.ML.TransformsCatalog catalog, string[] outputColumnNames, string[] inputColumnNames, string modelFile, int? gpuDeviceId = default, bool fallbackToCpu = false);
static member ApplyOnnxModel : Microsoft.ML.TransformsCatalog * string[] * string[] * string * Nullable<int> * bool -> Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator
<Extension()>
Public Function ApplyOnnxModel (catalog As TransformsCatalog, outputColumnNames As String(), inputColumnNames As String(), modelFile As String, Optional gpuDeviceId As Nullable(Of Integer) = Nothing, Optional fallbackToCpu As Boolean = false) As OnnxScoringEstimator

Parametreler

catalog
TransformsCatalog

Dönüşümün kataloğu.

outputColumnNames
String[]

Dönüştürme sonucunda elde edilen çıkış sütunları.

inputColumnNames
String[]

Giriş sütunları.

modelFile
String

ONNX modelini içeren dosyanın yolu.

gpuDeviceId
Nullable<Int32>

CPU üzerinde çalıştırılacak, null üzerinde yürütmenin çalıştırılacak isteğe bağlı GPU cihaz kimliği.

fallbackToCpu
Boolean

GPU hatası olursa, özel durum veya CPU'ya geri dönüş sağlayın.

Döndürülenler

Açıklamalar

gpuDeviceId değeri ise nullMLContext.GpuDeviceId değeri değilse nullkullanılır.

Şunlara uygulanır

ApplyOnnxModel(TransformsCatalog, String, String, String, IDictionary<String,Int32[]>, Nullable<Int32>, Boolean)

Sütununa önceden eğitilmiş bir Onnx modeli inputColumnName uygulayan bir OnnxScoringEstimatoroluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

public static Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator ApplyOnnxModel (this Microsoft.ML.TransformsCatalog catalog, string outputColumnName, string inputColumnName, string modelFile, System.Collections.Generic.IDictionary<string,int[]> shapeDictionary, int? gpuDeviceId = default, bool fallbackToCpu = false);
static member ApplyOnnxModel : Microsoft.ML.TransformsCatalog * string * string * string * System.Collections.Generic.IDictionary<string, int[]> * Nullable<int> * bool -> Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator
<Extension()>
Public Function ApplyOnnxModel (catalog As TransformsCatalog, outputColumnName As String, inputColumnName As String, modelFile As String, shapeDictionary As IDictionary(Of String, Integer()), Optional gpuDeviceId As Nullable(Of Integer) = Nothing, Optional fallbackToCpu As Boolean = false) As OnnxScoringEstimator

Parametreler

catalog
TransformsCatalog

Dönüşümün kataloğu.

outputColumnName
String

Dönüştürme sonucunda elde edilen çıkış sütunu.

inputColumnName
String

Giriş sütunu.

modelFile
String

ONNX modelini içeren dosyanın yolu.

shapeDictionary
IDictionary<String,Int32[]>

'den modelFileyüklenenler üzerinde kullanılacak ONNX şekilleri. Anahtarlar için, ONNX modelinde belirtildiği gibi adları kullanın, örneğin "giriş". Bu parametreye sahip şekillerin belirtilmesi, değişken boyut girişleri ve çıkışlarıyla çalışmak için özellikle yararlıdır.

gpuDeviceId
Nullable<Int32>

CPU üzerinde çalıştırılacak, null üzerinde yürütmenin çalıştırılacak isteğe bağlı GPU cihaz kimliği.

fallbackToCpu
Boolean

GPU hatası olursa, özel durum veya CPU'ya geri dönüş sağlayın.

Döndürülenler

Örnekler

using System;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms.Image;

namespace Samples.Dynamic
{
    public static class ApplyOnnxModelWithInMemoryImages
    {
        // Example of applying ONNX transform on in-memory images.
        public static void Example()
        {
            // Download the squeeznet image model from ONNX model zoo, version 1.2
            // https://github.com/onnx/models/tree/master/vision/classification/squeezenet or use
            // Microsoft.ML.Onnx.TestModels nuget.
            // It's a multiclass classifier. It consumes an input "data_0" and
            // produces an output "softmaxout_1".
            var modelPath = @"squeezenet\00000001\model.onnx";

            // Create ML pipeline to score the data using OnnxScoringEstimator
            var mlContext = new MLContext();

            // Create in-memory data points. Its Image/Scores field is the
            // input /output of the used ONNX model.
            var dataPoints = new ImageDataPoint[]
            {
                new ImageDataPoint(red: 255, green: 0, blue: 0), // Red color
                new ImageDataPoint(red: 0, green: 128, blue: 0)  // Green color
            };

            // Convert training data to IDataView, the general data type used in
            // ML.NET.
            var dataView = mlContext.Data.LoadFromEnumerable(dataPoints);

            // Create a ML.NET pipeline which contains two steps. First,
            // ExtractPixle is used to convert the 224x224 image to a 3x224x224
            // float tensor. Then the float tensor is fed into a ONNX model with an
            // input called "data_0" and an output called "softmaxout_1". Note that
            // "data_0" and "softmaxout_1" are model input and output names stored
            // in the used ONNX model file. Users may need to inspect their own
            // models to get the right input and output column names.
            // Map column "Image" to column "data_0"
            // Map column "data_0" to column "softmaxout_1"
            var pipeline = mlContext.Transforms.ExtractPixels("data_0", "Image")
                .Append(mlContext.Transforms.ApplyOnnxModel("softmaxout_1",
                "data_0", modelPath));

            var model = pipeline.Fit(dataView);
            var onnx = model.Transform(dataView);

            // Convert IDataView back to IEnumerable<ImageDataPoint> so that user
            // can inspect the output, column "softmaxout_1", of the ONNX transform.
            // Note that Column "softmaxout_1" would be stored in ImageDataPont
            //.Scores because the added attributed [ColumnName("softmaxout_1")]
            // tells that ImageDataPont.Scores is equivalent to column
            // "softmaxout_1".
            var transformedDataPoints = mlContext.Data.CreateEnumerable<
                ImageDataPoint>(onnx, false).ToList();

            // The scores are probabilities of all possible classes, so they should
            // all be positive.
            foreach (var dataPoint in transformedDataPoints)
            {
                var firstClassProb = dataPoint.Scores.First();
                var lastClassProb = dataPoint.Scores.Last();
                Console.WriteLine("The probability of being the first class is " +
                    (firstClassProb * 100) + "%.");

                Console.WriteLine($"The probability of being the last class is " +
                    (lastClassProb * 100) + "%.");
            }

            // Expected output:
            //  The probability of being the first class is 0.002542659%.
            //  The probability of being the last class is 0.0292684%.
            //  The probability of being the first class is 0.02258059%.
            //  The probability of being the last class is 0.394428%.
        }

        // This class is used in Example() to describe data points which will be
        // consumed by ML.NET pipeline.
        private class ImageDataPoint
        {
            // Height of Image.
            private const int height = 224;

            // Width of Image.
            private const int width = 224;

            // Image will be consumed by ONNX image multiclass classification model.
            [ImageType(height, width)]
            public MLImage Image { get; set; }

            // Expected output of ONNX model. It contains probabilities of all
            // classes. Note that the ColumnName below should match the output name
            // in the used ONNX model file.
            [ColumnName("softmaxout_1")]
            public float[] Scores { get; set; }

            public ImageDataPoint()
            {
                Image = null;
            }

            public ImageDataPoint(byte red, byte green, byte blue)
            {
                byte[] imageData = new byte[width * height * 4]; // 4 for the red, green, blue and alpha colors
                for (int i = 0; i < imageData.Length; i += 4)
                {
                    // Fill the buffer with the Bgra32 format
                    imageData[i] = blue;
                    imageData[i + 1] = green;
                    imageData[i + 2] = red;
                    imageData[i + 3] = 255;
                }

                Image = MLImage.CreateFromPixels(width, height, MLPixelFormat.Bgra32, imageData);
            }
        }
    }
}

Açıklamalar

gpuDeviceId değeri ise nullMLContext.GpuDeviceId değeri değilse nullkullanılır.

Şunlara uygulanır

ApplyOnnxModel(TransformsCatalog, String[], String[], String, IDictionary<String,Int32[]>, Nullable<Int32>, Boolean)

Sütunlara önceden eğitilmiş bir Onnx modeli inputColumnNames uygulayan bir OnnxScoringEstimatoroluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

public static Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator ApplyOnnxModel (this Microsoft.ML.TransformsCatalog catalog, string[] outputColumnNames, string[] inputColumnNames, string modelFile, System.Collections.Generic.IDictionary<string,int[]> shapeDictionary, int? gpuDeviceId = default, bool fallbackToCpu = false);
static member ApplyOnnxModel : Microsoft.ML.TransformsCatalog * string[] * string[] * string * System.Collections.Generic.IDictionary<string, int[]> * Nullable<int> * bool -> Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator
<Extension()>
Public Function ApplyOnnxModel (catalog As TransformsCatalog, outputColumnNames As String(), inputColumnNames As String(), modelFile As String, shapeDictionary As IDictionary(Of String, Integer()), Optional gpuDeviceId As Nullable(Of Integer) = Nothing, Optional fallbackToCpu As Boolean = false) As OnnxScoringEstimator

Parametreler

catalog
TransformsCatalog

Dönüşümün kataloğu.

outputColumnNames
String[]

Dönüştürme sonucunda elde edilen çıkış sütunları.

inputColumnNames
String[]

Giriş sütunları.

modelFile
String

ONNX modelini içeren dosyanın yolu.

shapeDictionary
IDictionary<String,Int32[]>

'den modelFileyüklenenler üzerinde kullanılacak ONNX şekilleri. Anahtarlar için, ONNX modelinde belirtildiği gibi adları kullanın, örneğin "giriş". Bu parametreye sahip şekillerin belirtilmesi, değişken boyut girişleri ve çıkışlarıyla çalışmak için özellikle yararlıdır.

gpuDeviceId
Nullable<Int32>

CPU üzerinde null çalıştırılacak, yürütmenin çalıştırılacak isteğe bağlı GPU cihaz kimliği.

fallbackToCpu
Boolean

GPU hatası olursa, cpu'ya özel durum veya geri dönüş sağlayın.

Döndürülenler

Açıklamalar

gpuDeviceId değeri değilse nullMLContext.GpuDeviceId değer kullanılır null.

Şunlara uygulanır

ApplyOnnxModel(TransformsCatalog, String[], String[], String, IDictionary<String,Int32[]>, Nullable<Int32>, Boolean, Int32)

Sütunlara önceden eğitilmiş bir Onnx modeli inputColumnNames uygulayan bir OnnxScoringEstimatoroluşturun. Gerekli bağımlılıklar ve gpu üzerinde çalıştırma hakkında daha fazla bilgi edinmek için lütfen bakın OnnxScoringEstimator .

public static Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator ApplyOnnxModel (this Microsoft.ML.TransformsCatalog catalog, string[] outputColumnNames, string[] inputColumnNames, string modelFile, System.Collections.Generic.IDictionary<string,int[]> shapeDictionary, int? gpuDeviceId = default, bool fallbackToCpu = false, int recursionLimit = 100);
static member ApplyOnnxModel : Microsoft.ML.TransformsCatalog * string[] * string[] * string * System.Collections.Generic.IDictionary<string, int[]> * Nullable<int> * bool * int -> Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator
<Extension()>
Public Function ApplyOnnxModel (catalog As TransformsCatalog, outputColumnNames As String(), inputColumnNames As String(), modelFile As String, shapeDictionary As IDictionary(Of String, Integer()), Optional gpuDeviceId As Nullable(Of Integer) = Nothing, Optional fallbackToCpu As Boolean = false, Optional recursionLimit As Integer = 100) As OnnxScoringEstimator

Parametreler

catalog
TransformsCatalog

Dönüşümün kataloğu.

outputColumnNames
String[]

Dönüştürmeden kaynaklanan çıkış sütunları.

inputColumnNames
String[]

Giriş sütunları.

modelFile
String

ONNX modelini içeren dosyanın yolu.

shapeDictionary
IDictionary<String,Int32[]>

'den modelFileyüklenenler üzerinde kullanılacak ONNX şekilleri. Anahtarlar için, ONNX modelinde belirtildiği gibi adları kullanın, örneğin "giriş". Bu parametreye sahip şekillerin belirtilmesi, değişken boyut girişleri ve çıkışlarıyla çalışmak için özellikle yararlıdır.

gpuDeviceId
Nullable<Int32>

CPU üzerinde null çalıştırılacak, yürütmenin çalıştırılacak isteğe bağlı GPU cihaz kimliği.

fallbackToCpu
Boolean

GPU hatası olursa, cpu'ya özel durum veya geri dönüş sağlayın.

recursionLimit
Int32

İsteğe bağlı olarak, Protobuf CodedInputStream özyineleme sınırını belirtir. Varsayılan değer 100'dür.

Döndürülenler

Açıklamalar

gpuDeviceId değeri değilse nullMLContext.GpuDeviceId değer kullanılır null.

Şunlara uygulanır