@@ -15,44 +15,61 @@ public static void Example()
1515 // Download the squeeznet image model from ONNX model zoo, version 1.2
1616 // https://github.com/onnx/models/tree/master/squeezenet or use
1717 // 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".
2020 var modelPath = @"squeezenet\00000001\model.onnx" ;
2121
2222 // Create ML pipeline to score the data using OnnxScoringEstimator
2323 var mlContext = new MLContext ( ) ;
2424
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.
2627 var dataPoints = new ImageDataPoint [ ]
2728 {
2829 new ImageDataPoint ( Color . Red ) ,
2930 new ImageDataPoint ( Color . Green )
3031 } ;
3132
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.
3335 var dataView = mlContext . Data . LoadFromEnumerable ( dataPoints ) ;
3436
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+
4150 var model = pipeline . Fit ( dataView ) ;
4251 var onnx = model . Transform ( dataView ) ;
4352
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 ( ) ;
4861
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.
5064 foreach ( var dataPoint in transformedDataPoints )
5165 {
5266 var firstClassProb = dataPoint . Scores . First ( ) ;
5367 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 ) + "%." ) ;
5673 }
5774
5875 // Expected output:
@@ -62,7 +79,8 @@ public static void Example()
6279 // The probability of being the last class is 0.394428%.
6380 }
6481
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.
6684 private class ImageDataPoint
6785 {
6886 // Height of Image.
@@ -75,9 +93,9 @@ private class ImageDataPoint
7593 [ ImageType ( height , width ) ]
7694 public Bitmap Image { get ; set ; }
7795
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.
8199 [ ColumnName ( "softmaxout_1" ) ]
82100 public float [ ] Scores { get ; set ; }
83101
0 commit comments