From 771e933df28821a608b908fd450bb74c54fedfcc Mon Sep 17 00:00:00 2001 From: Gani Nazirov <ganaziro@microsoft.com> Date: Mon, 15 Apr 2019 21:23:20 -0700 Subject: [PATCH 1/4] Rename to ApplyOnnxModel --- .../{OnnxTransform.cs => Transforms/ApplyOnnxModel.cs} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename docs/samples/Microsoft.ML.Samples/Dynamic/{OnnxTransform.cs => Transforms/ApplyOnnxModel.cs} (97%) diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/OnnxTransform.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs similarity index 97% rename from docs/samples/Microsoft.ML.Samples/Dynamic/OnnxTransform.cs rename to docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs index 62a7802ec1..73e56160c6 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/OnnxTransform.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs @@ -6,10 +6,10 @@ namespace Samples.Dynamic { - public static class OnnxTransformExample + public static class ApplyOnnxModel { /// <summary> - /// Example use of OnnxEstimator in an ML.NET pipeline + /// Example use of ApplyOnnxModel in an ML.NET pipeline /// </summary> public static void Example() { From 10110131fae1590c6043ca3613c84fb3c2a29347 Mon Sep 17 00:00:00 2001 From: Gani Nazirov <ganaziro@microsoft.com> Date: Mon, 15 Apr 2019 21:34:41 -0700 Subject: [PATCH 2/4] simplify --- .../Dynamic/Transforms/ApplyOnnxModel.cs | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs index 73e56160c6..deb5dfd21d 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs @@ -18,29 +18,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<Prediction>(transformedValues, reuseRowObject: false); From f7b57d7a9513f333dce312cf20795f8b9e06190f Mon Sep 17 00:00:00 2001 From: Gani Nazirov <ganaziro@microsoft.com> Date: Tue, 16 Apr 2019 10:28:48 -0700 Subject: [PATCH 3/4] fix comments --- .../Dynamic/Transforms/ApplyOnnxModel.cs | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs index deb5dfd21d..edb2b073e1 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.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 ApplyOnnxModel { - /// <summary> - /// Example use of ApplyOnnxModel in an ML.NET pipeline - /// </summary> public static void Example() { // Download the squeeznet image model from ONNX model zoo, version 1.2 @@ -54,25 +50,25 @@ public static void Example() // ---------- } - /// <summary> - /// inputSize is the overall dimensions of the model input tensor. - /// </summary> + // <summary> + // inputSize is the overall dimensions of the model input tensor. + // </summary> private const int inputSize = 224 * 224 * 3; - /// <summary> - /// A class to hold sample tensor data. Member name should match - /// the inputs that the model expects (in this case, data_0) - /// </summary> + // <summary> + // A class to hold sample tensor data. Member name should match + // the inputs that the model expects (in this case, data_0) + // </summary> public class TensorData { [VectorType(inputSize)] public float[] data_0 { get; set; } } - /// <summary> - /// Method to generate sample test data. Returns 2 sample rows. - /// </summary> - /// <returns></returns> + // <summary> + // Method to generate sample test data. Returns 2 sample rows. + // </summary> + // <returns></returns> public static TensorData[] GetTensorData() { // This can be any numerical data. Assume image pixel values. @@ -81,10 +77,10 @@ public static TensorData[] GetTensorData() return new TensorData[] { new TensorData() { data_0 = image1 }, new TensorData() { data_0 = image2 } }; } - /// <summary> - /// Class to contain the output values from the transformation. - /// This model generates a vector of 1000 floats. - /// </summary> + // <summary> + // Class to contain the output values from the transformation. + // This model generates a vector of 1000 floats. + // </summary> class Prediction { [VectorType(1000)] From 47e7176c9cd4c51f1a368519675157ac1778db05 Mon Sep 17 00:00:00 2001 From: Gani Nazirov <ganaziro@microsoft.com> Date: Wed, 17 Apr 2019 12:39:50 -0700 Subject: [PATCH 4/4] fix comments --- .../Dynamic/Transforms/ApplyOnnxModel.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs index edb2b073e1..abff3a339d 100644 --- a/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs +++ b/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ApplyOnnxModel.cs @@ -50,25 +50,18 @@ public static void Example() // ---------- } - // <summary> // inputSize is the overall dimensions of the model input tensor. - // </summary> private const int inputSize = 224 * 224 * 3; - // <summary> // A class to hold sample tensor data. Member name should match // the inputs that the model expects (in this case, data_0) - // </summary> public class TensorData { [VectorType(inputSize)] public float[] data_0 { get; set; } } - // <summary> // Method to generate sample test data. Returns 2 sample rows. - // </summary> - // <returns></returns> public static TensorData[] GetTensorData() { // This can be any numerical data. Assume image pixel values. @@ -77,10 +70,8 @@ public static TensorData[] GetTensorData() return new TensorData[] { new TensorData() { data_0 = image1 }, new TensorData() { data_0 = image2 } }; } - // <summary> // Class to contain the output values from the transformation. // This model generates a vector of 1000 floats. - // </summary> class Prediction { [VectorType(1000)]