Skip to content

Update to use OnnxRuntime library instead of Sonoma #1717

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 8 commits into from
Dec 1, 2018
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
2 changes: 1 addition & 1 deletion build/Dependencies.props
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
<PropertyGroup>
<GoogleProtobufPackageVersion>3.5.1</GoogleProtobufPackageVersion>
<LightGBMPackageVersion>2.2.1.1</LightGBMPackageVersion>
<MicrosoftMLScoring>1.1.0</MicrosoftMLScoring>
<MicrosoftMLOnnxRuntimePackageVersion>0.1.5</MicrosoftMLOnnxRuntimePackageVersion>
<MlNetMklDepsPackageVersion>0.0.0.7</MlNetMklDepsPackageVersion>
<ParquetDotNetPackageVersion>2.1.3</ParquetDotNetPackageVersion>
<SystemDrawingCommonPackageVersion>4.5.0</SystemDrawingCommonPackageVersion>
110 changes: 110 additions & 0 deletions docs/samples/Microsoft.ML.Samples/Dynamic/OnnxTransform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.Runtime.Api;
using Microsoft.ML.Runtime.Data;
using Microsoft.ML.Transforms;
using System;
using System.Linq;

namespace Microsoft.ML.Samples.Dynamic
{
class OnnxTransformExample
{
/// <summary>
/// Example use of OnnxEstimator in an ML.NET pipeline
/// </summary>
public static void OnnxTransformSample()
{
// Download the squeeznet image model from ONNX model zoo, version 1.2
// https://github.com/onnx/models/tree/master/squeezenet
var modelPath = @"squeezenet\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.CreateStreamingDataView(data);
var pipeline = new OnnxScoringEstimator(mlContext, modelPath, new[] { inputInfo.Key }, new[] { outputInfo.Key });

// Run the pipeline and get the transformed values
var transformedValues = pipeline.Fit(idv).Transform(idv);

// Retrieve model scores into Prediction class
var predictions = transformedValues.AsEnumerable<Prediction>(mlContext, 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
// ----------
}

/// <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.
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 } };
}

/// <summary>
/// Class to contain the output values from the transformation.
/// This model generates a vector of 1000 floats.
/// </summary>
class Prediction
{
[VectorType(1000)]
public float[] softmaxout_1 { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
<ProjectReference Include="..\..\..\src\Microsoft.ML.LightGBM\Microsoft.ML.LightGBM.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.ML.Recommender\Microsoft.ML.Recommender.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.ML.TimeSeries\Microsoft.ML.TimeSeries.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.ML.OnnxTransform\Microsoft.ML.OnnxTransform.csproj" />

<NativeAssemblyReference Include="CpuMathNative" />
<NativeAssemblyReference Include="FastTreeNative" />
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

<ItemGroup>
<ProjectReference Include="../Microsoft.ML/Microsoft.ML.nupkgproj" />
<PackageReference Include="Microsoft.ML.Scoring" Version="$(MicrosoftMLScoring)"/>
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="$(MicrosoftMLOnnxRuntimePackageVersion)"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
<ItemGroup>
<ProjectReference Include="..\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
<ProjectReference Include="..\Microsoft.ML.Data\Microsoft.ML.Data.csproj" />
<PackageReference Include="Microsoft.ML.Scoring" Version="$(MicrosoftMLScoring)" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="$(MicrosoftMLOnnxRuntimePackageVersion)" />
</ItemGroup>

</Project>
Loading