You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -125,6 +125,7 @@ The official ML.NET samples are divided in multiple categories depending on the
125
125
<tr>
126
126
<tdalign="middle"><imgsrc="images/database.png"alt="Database chart"><br><imgsrc="images/app-type-getting-started-term-cursor.png"alt="Getting started icon"><br><b>Loading data with LoadFromEnumerable<br><ahref="samples/csharp/getting-started/DatabaseIntegration">C#</a><b></td>
Copy file name to clipboardExpand all lines: samples/csharp/getting-started/DeepLearning_ObjectDetection_Onnx/README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ The dataset contains images which are located in the [assets](./ObjectDetectionC
17
17
## Pre-trained model
18
18
There are multiple models which are pre-trained for identifying multiple objects in the images. here we are using the pretrained model, **Tiny Yolo2** in **ONNX** format. This model is a real-time neural network for object detection that detects 20 different classes. It is made up of 9 convolutional layers and 6 max-pooling layers and is a smaller version of the more complex full [YOLOv2](https://pjreddie.com/darknet/yolov2/) network.
19
19
20
-
The Open Neural Network eXchange i.e [ONNX](http://onnx.ai/) is an open format to represent deep learning models. With ONNX, developers can move models between state-of-the-art tools and choose the combination that is best for them. ONNX is developed and supported by a community of partners.
20
+
The Open Neural Network Exchange i.e [ONNX](http://onnx.ai/) is an open format to represent deep learning models. With ONNX, developers can move models between state-of-the-art tools and choose the combination that is best for them. ONNX is developed and supported by a community of partners.
21
21
22
22
The model is downloaded from the [ONNX Model Zoo](https://github.com/onnx/models) which is a is a collection of pre-trained, state-of-the-art models in the ONNX format.
In this sample, you'll see how to use ML.NET to train a regression model and then convert that model to the ONNX format.
8
+
9
+
## Problem
10
+
11
+
The Open Neural Network Exchange i.e [ONNX](http://onnx.ai/) is an open format to represent deep learning models. With ONNX, developers can move models between state-of-the-art tools and choose the combination that is best for them. ONNX is developed and supported by a community of partners.
12
+
13
+
There may be times when you want to train a model with ML.NET and then convert to ONNX, for instance if you want to consume your model with WinML to take advantage of GPU inferencing in Windows applications.
14
+
15
+
Not all ML.NET models can be converted to ONNX; it is dependent on the trainers and transforms in the training pipeline. For a list of supported trainers see the tables in the ML.NET [Algorithms Doc](https://docs.microsoft.com/dotnet/machine-learning/how-to-choose-an-ml-net-algorithm) and for a list of supported transforms check out the [Data transforms Doc](https://docs.microsoft.com/dotnet/machine-learning/resources/transforms).
16
+
17
+
## Dataset
18
+
19
+
This sample uses the [NYC Taxi Fare dataset](https://github.com/dotnet/machinelearning-samples/blob/main/datasets/README.md#nyc-taxi-fare) for training.
20
+
21
+
## Solution
22
+
23
+
The console application project `ONNXExport` can be used to train an ML.NET model that predicts the price of taxi fare based on several features such as distance travelled and number of passengers, to export that model to ONNX, and then to consume the ONNX model and make predictions with it.
24
+
25
+
### NuGet Packages
26
+
27
+
To export an ML.NET model to ONNX, you must install the following NuGet packages in your project:
28
+
29
+
- Microsoft.ML.OnnxConverter
30
+
31
+
You must also install:
32
+
33
+
- Microsoft.ML for training the ML.NET model
34
+
- Microsoft.ML.ONNXRuntime and Microsoft.ML.OnnxTransformer for scoring the ONNX model
35
+
36
+
### Transforms and trainers
37
+
38
+
This pipeline contains the following transforms and trainers which are all ONNX exportable:
39
+
40
+
- OneHotEncoding transform
41
+
- Concatenate transform
42
+
- Light GBM trainer
43
+
44
+
### Code
45
+
46
+
After training an ML.NET model, you can use the following code to convert to ONNX:
47
+
48
+
```csharp
49
+
using (varstream=File.Create("taxi-fare-model.onnx"))
You need a transformer and input data to convert an ML.NET model to an ONNX model. By default, the ONNX conversion will generate the ONNX file with the latest OpSet version
54
+
55
+
After converting to ONNX, you can then consume the ONNX model with the following code:
You should get the same results when comparing the ML.NET model and ONNX model on the same sample input. If you run the project, you should get similar to the following output in the console:
66
+
67
+
```console
68
+
Predicted Scores with ML.NET model
69
+
Score 19.60645
70
+
Score 18.673796
71
+
Score 5.9175444
72
+
Score 4.8969507
7FD8
73
+
Score 19.108932
74
+
Predicted Scores with ONNX model
75
+
Score 19.60645
76
+
Score 18.673796
77
+
Score 5.9175444
78
+
Score 4.8969507
79
+
Score 19.108932
80
+
```
81
+
82
+
## Performance
83
+
84
+
The default ONNX to ML.NET conversion is not optimal and produces extra graph outputs that are not needed for ML.NET usage. ONNX Runtime does reverse depth first search which results in a lot of conversion operations of native memory to managed memory from ONNX Runtime to ML.NET and execution of more than the necessary kernels.
85
+
86
+
If you specify just the necessary graph outputs, it will only execute a subset of the graph. Thus, by eliminating all graph outputs except Score, you can improve inference performance.
0 commit comments