2
2
using System . Linq ;
3
3
using Microsoft . ML ;
4
4
using Microsoft . ML . Data ;
5
- using Microsoft . ML . OnnxRuntime ;
6
5
7
6
namespace Samples . Dynamic
8
7
{
9
- public static class OnnxTransformExample
8
+ public static class ApplyOnnxModel
10
9
{
11
- /// <summary>
12
- /// Example use of OnnxEstimator in an ML.NET pipeline
13
- /// </summary>
14
10
public static void Example ( )
15
11
{
16
12
// Download the squeeznet image model from ONNX model zoo, version 1.2
17
13
// https://github.com/onnx/models/tree/master/squeezenet or use
18
14
// Microsoft.ML.Onnx.TestModels nuget.
19
15
var modelPath = @"squeezenet\00000001\model.onnx" ;
20
16
21
- // Inspect the model's inputs and outputs
22
- var session = new InferenceSession ( modelPath ) ;
23
- var inputInfo = session . InputMetadata . First ( ) ;
24
- var outputInfo = session . OutputMetadata . First ( ) ;
25
- Console . WriteLine ( $ "Input Name is { String . Join ( "," , inputInfo . Key ) } ") ;
26
- Console . WriteLine ( $ "Input Dimensions are { String . Join ( "," , inputInfo . Value . Dimensions ) } ") ;
27
- Console . WriteLine ( $ "Output Name is { String . Join ( "," , outputInfo . Key ) } ") ;
28
- Console . WriteLine ( $ "Output Dimensions are { String . Join ( "," , outputInfo . Value . Dimensions ) } ") ;
29
- // Results..
30
- // Input Name is data_0
31
- // Input Dimensions are 1,3,224,224
32
- // Output Name is softmaxout_1
33
- // Output Dimensions are 1,1000,1,1
34
-
35
17
// Create ML pipeline to score the data using OnnxScoringEstimator
36
18
var mlContext = new MLContext ( ) ;
37
- var data = GetTensorData ( ) ;
38
- var idv = mlContext . Data . LoadFromEnumerable ( data ) ;
39
- var pipeline = mlContext . Transforms . ApplyOnnxModel ( new [ ] { outputInfo . Key } , new [ ] { inputInfo . Key } , modelPath ) ;
40
-
41
- // Run the pipeline and get the transformed values
42
- var transformedValues = pipeline . Fit ( idv ) . Transform ( idv ) ;
43
19
20
+ // Generate sample test data.
21
+ var samples = GetTensorData ( ) ;
22
+ // Convert training data to IDataView, the general data type used in ML.NET.
23
+ var data = mlContext . Data . LoadFromEnumerable ( samples ) ;
24
+ // Create the pipeline to score using provided onnx model.
25
+ var pipeline = mlContext . Transforms . ApplyOnnxModel ( modelPath ) ;
26
+ // Fit the pipeline and get the transformed values
27
+ var transformedValues = pipeline . Fit ( data ) . Transform ( data ) ;
44
28
// Retrieve model scores into Prediction class
45
29
var predictions = mlContext . Data . CreateEnumerable < Prediction > ( transformedValues , reuseRowObject : false ) ;
46
30
@@ -66,25 +50,18 @@ public static void Example()
66
50
// ----------
67
51
}
68
52
69
- /// <summary>
70
- /// inputSize is the overall dimensions of the model input tensor.
71
- /// </summary>
53
+ // inputSize is the overall dimensions of the model input tensor.
72
54
private const int inputSize = 224 * 224 * 3 ;
73
55
74
- /// <summary>
75
- /// A class to hold sample tensor data. Member name should match
76
- /// the inputs that the model expects (in this case, data_0)
77
- /// </summary>
56
+ // A class to hold sample tensor data. Member name should match
57
+ // the inputs that the model expects (in this case, data_0)
78
58
public class TensorData
79
59
{
80
60
[ VectorType ( inputSize ) ]
81
61
public float [ ] data_0 { get ; set ; }
82
62
}
83
63
84
- /// <summary>
85
- /// Method to generate sample test data. Returns 2 sample rows.
86
- /// </summary>
87
- /// <returns></returns>
64
+ // Method to generate sample test data. Returns 2 sample rows.
88
65
public static TensorData [ ] GetTensorData ( )
89
66
{
90
67
// This can be any numerical data. Assume image pixel values.
@@ -93,10 +70,8 @@ public static TensorData[] GetTensorData()
93
70
return new TensorData [ ] { new TensorData ( ) { data_0 = image1 } , new TensorData ( ) { data_0 = image2 } } ;
94
71
}
95
72
96
- /// <summary>
97
- /// Class to contain the output values from the transformation.
98
- /// This model generates a vector of 1000 floats.
99
- /// </summary>
73
+ // Class to contain the output values from the transformation.
74
+ // This model generates a vector of 1000 floats.
100
75
class Prediction
101
76
{
102
77
[ VectorType ( 1000 ) ]
0 commit comments