diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/OnnxTransform.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs similarity index 53% rename from docs/samples/Microsoft.ML.Samples/Dynamic/OnnxTransform.cs rename to docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs index 62a7802ec1..abff3a339d 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/OnnxTransform.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs @@ -2,15 +2,11 @@ using System.Linq; using Microsoft.ML; using Microsoft.ML.Data; -using Microsoft.ML.OnnxRuntime; namespace Samples.Dynamic { - public static class OnnxTransformExample + public static class ApplyOnnxModel { - /// - /// Example use of OnnxEstimator in an ML.NET pipeline - /// public static void Example() { // Download the squeeznet image model from ONNX model zoo, version 1.2 @@ -18,29 +14,17 @@ public static void Example() // Microsoft.ML.Onnx.TestModels nuget. var modelPath = @"squeezenet\00000001\model.onnx"; - // Inspect the model's inputs and outputs - var session = new InferenceSession(modelPath); - var inputInfo = session.InputMetadata.First(); - var outputInfo = session.OutputMetadata.First(); - Console.WriteLine($"Input Name is {String.Join(",", inputInfo.Key)}"); - Console.WriteLine($"Input Dimensions are {String.Join(",", inputInfo.Value.Dimensions)}"); - Console.WriteLine($"Output Name is {String.Join(",", outputInfo.Key)}"); - Console.WriteLine($"Output Dimensions are {String.Join(",", outputInfo.Value.Dimensions)}"); - // Results.. - // Input Name is data_0 - // Input Dimensions are 1,3,224,224 - // Output Name is softmaxout_1 - // Output Dimensions are 1,1000,1,1 - // Create ML pipeline to score the data using OnnxScoringEstimator var mlContext = new MLContext(); - var data = GetTensorData(); - var idv = mlContext.Data.LoadFromEnumerable(data); - var pipeline = mlContext.Transforms.ApplyOnnxModel(new[] { outputInfo.Key }, new[] { inputInfo.Key }, modelPath); - - // Run the pipeline and get the transformed values - var transformedValues = pipeline.Fit(idv).Transform(idv); + // 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(transformedValues, reuseRowObject: false); @@ -66,25 +50,18 @@ public static void Example() // ---------- } - /// - /// inputSize is the overall dimensions of the model input tensor. - /// + // 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) - /// + // 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. - /// - /// + // Method to generate sample test data. Returns 2 sample rows. public static TensorData[] GetTensorData() { // This can be any numerical data. Assume image pixel values. @@ -93,10 +70,8 @@ public static TensorData[] GetTensorData() 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 to contain the output values from the transformation. + // This model generates a vector of 1000 floats. class Prediction { [VectorType(1000)]