-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
ApplyOnnxModel sample #3349
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
||
// 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); | ||
|
||
|
@@ -66,25 +50,25 @@ public static void Example() | |
// ---------- | ||
} | ||
|
||
/// <summary> | ||
/// inputSize is the overall dimensions of the model input tensor. | ||
/// </summary> | ||
// <summary> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i mean let's delete the xml tags like and just keep the comment text:
|
||
// 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. | ||
|
@@ -93,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)] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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):
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.
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
There was a problem hiding this comment.
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)