Skip to content

ApplyOnnxModel sample #3349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,29 @@
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
{
/// <summary>
/// Example use of OnnxEstimator in an ML.NET pipeline
/// </summary>
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 use
// Microsoft.ML.Onnx.TestModels nuget.
var modelPath = @"squeezenet\00000001\model.onnx";
Copy link

@shmoradims shmoradims Apr 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please verify that this statement is true (you can double check with Jignesh):

  1. verify github link: Is the model.onnx in the squeezenet github link the same as squeezenet\00000001\model.onnx? I see manifest.json and 00000001 on our side which don't exist in the github link. Jignesh most likely has done an extra step to create squeezenet\00000001\model.onnx which users wouldn't know how to do.

  2. verify the nuget: Microsoft.ML.Onnx.TestModels was last updated 4 months ago on myget. It's also at version 0.0.4: https://dotnet.myget.org/feed/dotnet-core/package/nuget/Microsoft.ML.Onnx.TestModels

We also have this other similar nuget: https://dotnet.myget.org/feed/dotnet-core/package/nuget/Microsoft.ML.Onnx.TestModels

There has been a lot of reshuffling things. Please double check that we're naming the correct nuget package and also try it out locally to make sure it works.

If there are any nuances about using this API, please add it to it's xml documentation.

Thanks.

#Resolved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep its there


In reply to: 275814495 [](ancestors = 275814495)


// 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);

Expand All @@ -66,25 +50,18 @@ public static void Example()
// ----------
}

/// <summary>
/// inputSize is the overall dimensions of the model input tensor.
/// </summary>
// inputSize is the overall dimensions of the model input tensor.
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>
// 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; }
}

/// <summary>
/// Method to generate sample test data. Returns 2 sample rows.
/// </summary>
/// <returns></returns>
// Method to generate sample test data. Returns 2 sample rows.
public static TensorData[] GetTensorData()
{
// This can be any numerical data. Assume image pixel values.
Expand All @@ -93,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 to contain the output values from the transformation.
// This model generates a vector of 1000 floats.
class Prediction
{
[VectorType(1000)]
Expand Down