@@ -15,44 +15,61 @@ public static void Example()
15
15
// Download the squeeznet image model from ONNX model zoo, version 1.2
16
16
// https://github.com/onnx/models/tree/master/squeezenet or use
17
17
// Microsoft.ML.Onnx.TestModels nuget.
18
- // It's a multiclass classifier. It consumes an input "data_0" and produces
19
- // an output "softmaxout_1".
18
+ // It's a multiclass classifier. It consumes an input "data_0" and
19
+ // produces an output "softmaxout_1".
20
20
var modelPath = @"squeezenet\00000001\model.onnx" ;
21
21
22
22
// Create ML pipeline to score the data using OnnxScoringEstimator
23
23
var mlContext = new MLContext ( ) ;
24
24
25
- // Create in-memory data points. Its Image/Scores field is the input/output of the used ONNX model.
25
+ // Create in-memory data points. Its Image/Scores field is the
26
+ // input /output of the used ONNX model.
26
27
var dataPoints = new ImageDataPoint [ ]
27
28
{
28
29
new ImageDataPoint ( Color . Red ) ,
29
30
new ImageDataPoint ( Color . Green )
30
31
} ;
31
32
32
- // Convert training data to IDataView, the general data type used in ML.NET.
33
+ // Convert training data to IDataView, the general data type used in
34
+ // ML.NET.
33
35
var dataView = mlContext . Data . LoadFromEnumerable ( dataPoints ) ;
34
36
35
- // Create a ML.NET pipeline which contains two steps. First, ExtractPixle is used to convert the 224x224 image to a 3x224x224 float tensor.
36
- // Then the float tensor is fed into a ONNX model with an input called "data_0" and an output called "softmaxout_1". Note that "data_0" and
37
- // "softmaxout_1" are model input and output names stored in the used ONNX model file. Users may need to inspect their own models to
38
- // get the right input and output column names.
39
- var pipeline = mlContext . Transforms . ExtractPixels ( "data_0" , "Image" ) // Map column "Image" to column "data_0"
40
- . Append ( mlContext . Transforms . ApplyOnnxModel ( "softmaxout_1" , "data_0" , modelPath ) ) ; // Map column "data_0" to column "softmaxout_1"
37
+ // Create a ML.NET pipeline which contains two steps. First,
38
+ // ExtractPixle is used to convert the 224x224 image to a 3x224x224
39
+ // float tensor. Then the float tensor is fed into a ONNX model with an
40
+ // input called "data_0" and an output called "softmaxout_1". Note that
41
+ // "data_0" and "softmaxout_1" are model input and output names stored
42
+ // in the used ONNX model file. Users may need to inspect their own
43
+ // models to get the right input and output column names.
44
+ // Map column "Image" to column "data_0"
45
+ // Map column "data_0" to column "softmaxout_1"
46
+ var pipeline = mlContext . Transforms . ExtractPixels ( "data_0" , "Image" )
47
+ . Append ( mlContext . Transforms . ApplyOnnxModel ( "softmaxout_1" ,
48
+ "data_0" , modelPath ) ) ;
49
+
41
50
var model = pipeline . Fit ( dataView ) ;
42
51
var onnx = model . Transform ( dataView ) ;
43
52
44
- // Convert IDataView back to IEnumerable<ImageDataPoint> so that user can inspect the output, column "softmaxout_1", of the ONNX transform.
45
- // Note that Column "softmaxout_1" would be stored in ImageDataPont.Scores because the added attributed [ColumnName("softmaxout_1")]
46
- // tells that ImageDataPont.Scores is equivalent to column "softmaxout_1".
47
- var transformedDataPoints = mlContext . Data . CreateEnumerable < ImageDataPoint > ( onnx , false ) . ToList ( ) ;
53
+ // Convert IDataView back to IEnumerable<ImageDataPoint> so that user
54
+ // can inspect the output, column "softmaxout_1", of the ONNX transform.
55
+ // Note that Column "softmaxout_1" would be stored in ImageDataPont
56
+ //.Scores because the added attributed [ColumnName("softmaxout_1")]
57
+ // tells that ImageDataPont.Scores is equivalent to column
58
+ // "softmaxout_1".
59
+ var transformedDataPoints = mlContext . Data . CreateEnumerable <
60
+ ImageDataPoint > ( onnx , false ) . ToList ( ) ;
48
61
49
- // The scores are probabilities of all possible classes, so they should all be positive.
62
+ // The scores are probabilities of all possible classes, so they should
63
+ // all be positive.
50
64
foreach ( var dataPoint in transformedDataPoints )
51
65
{
52
66
var firstClassProb = dataPoint . Scores . First ( ) ;
53
67
var lastClassProb = dataPoint . Scores . Last ( ) ;
54
- Console . WriteLine ( $ "The probability of being the first class is { firstClassProb * 100 } %.") ;
55
- Console . WriteLine ( $ "The probability of being the last class is { lastClassProb * 100 } %.") ;
68
+ Console . WriteLine ( "The probability of being the first class is " +
69
+ ( firstClassProb * 100 ) + "%." ) ;
70
+
71
+ Console . WriteLine ( $ "The probability of being the last class is " +
72
+ ( lastClassProb * 100 ) + "%." ) ;
56
73
}
57
74
58
75
// Expected output:
@@ -62,7 +79,8 @@ public static void Example()
62
79
// The probability of being the last class is 0.394428%.
63
80
}
64
81
65
- // This class is used in Example() to describe data points which will be consumed by ML.NET pipeline.
82
+ // This class is used in Example() to describe data points which will be
83
+ // consumed by ML.NET pipeline.
66
84
private class ImageDataPoint
67
85
{
68
86
// Height of Image.
@@ -75,9 +93,9 @@ private class ImageDataPoint
75
93
[ ImageType ( height , width ) ]
76
94
public Bitmap Image { get ; set ; }
77
95
78
- // Expected output of ONNX model. It contains probabilities of all classes.
79
- // Note that the ColumnName below should match the output name in the used
80
- // ONNX model file.
96
+ // Expected output of ONNX model. It contains probabilities of all
97
+ // classes. Note that the ColumnName below should match the output name
98
+ // in the used ONNX model file.
81
99
[ ColumnName ( "softmaxout_1" ) ]
82
100
public float [ ] Scores { get ; set ; }
83
101
0 commit comments