Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7abff36

Browse files
mx-iaoprasanthpul
authored andcommitted
updated CntkOnnxExport to include pretrained model example (#7)
1 parent c2dfe22 commit 7abff36

1 file changed

Lines changed: 96 additions & 5 deletions

File tree

tutorials/CntkOnnxExport.ipynb

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
"cell_type": "markdown",
2626
"metadata": {},
2727
"source": [
28-
"To export to ONNX, simply make sure you have CNTK 2.3 or higher installed. <br>\n",
28+
"To export to ONNX, simply make sure you have CNTK 2.3.1 or higher installed. <br>\n",
2929
"Follow CNTK installation instructions __[here](https://docs.microsoft.com/en-us/cognitive-toolkit/Setup-CNTK-on-your-machine)__."
3030
]
3131
},
3232
{
3333
"cell_type": "markdown",
3434
"metadata": {},
3535
"source": [
36-
"## Exporting in Python"
36+
"## API Usage"
3737
]
3838
},
3939
{
@@ -43,6 +43,13 @@
4343
"To save a CNTK model to the ONNX format, specify the ONNX format in the format parameter of the save function."
4444
]
4545
},
46+
{
47+
"cell_type": "markdown",
48+
"metadata": {},
49+
"source": [
50+
"** Using Python API ** "
51+
]
52+
},
4653
{
4754
"cell_type": "markdown",
4855
"metadata": {},
@@ -51,7 +58,7 @@
5158
"import cntk as C\n",
5259
"\n",
5360
"x = C.input_variable(<input shape>)\n",
54-
"z = create_model(x)\n",
61+
"z = create_model(x) #your create model function\n",
5562
"z.save(<path of where to save your ONNX model>, format=C.ModelFormat.ONNX)\n",
5663
"```"
5764
]
@@ -60,7 +67,7 @@
6067
"cell_type": "markdown",
6168
"metadata": {},
6269
"source": [
63-
"## Exporting in C# #"
70+
"** Exporting in C# **"
6471
]
6572
},
6673
{
@@ -69,10 +76,94 @@
6976
"source": [
7077
"```csharp\n",
7178
"var x = CNTKLib.InputVariable(<specify input variable parameters>);\n",
72-
"Function z = CreateModel(x);\n",
79+
"Function z = CreateModel(x); //your create model function\n",
7380
"z.Save(<path of where to save your ONNX model>, ModelFormat.ONNX);\n",
7481
"```\n"
7582
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"metadata": {},
87+
"source": [
88+
"## Trying it out with ResNet-20"
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"Let's go through an example of exporting a pretrained CNTK model to ONNX."
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"metadata": {},
101+
"source": [
102+
"### Step 1: Prepare a CNTK model to export"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"metadata": {},
108+
"source": [
109+
"For this tutorial, we will be using a pretrained ResNet-20 model (trained on the CIFAR-10 dataset) from the collection of pretrained CNTK models found [here](https://github.com/Microsoft/CNTK/blob/master/PretrainedModels/Image.md). Download the model to your working directory. (Note that not all of the models found here are exportable to the ONNX format yet.) \n",
110+
"Download link: https://www.cntk.ai/Models/CNTK_Pretrained/ResNet20_CIFAR10_CNTK.model"
111+
]
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"### Step 2: Load the model into CNTK"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": 10,
123+
"metadata": {
124+
"collapsed": true
125+
},
126+
"outputs": [],
127+
"source": [
128+
"import cntk as C"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": 11,
134+
"metadata": {
135+
"collapsed": false
136+
},
137+
"outputs": [],
138+
"source": [
139+
"model_path = \"ResNet20_CIFAR10_CNTK.model\"\n",
140+
"z = C.Function.load(model_path, device=C.device.cpu())"
141+
]
142+
},
143+
{
144+
"cell_type": "markdown",
145+
"metadata": {},
146+
"source": [
147+
"### Step 3: Export the model to ONNX"
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"metadata": {},
153+
"source": [
154+
"Next, export the CNTK model by saving it out to the ONNX format."
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": 12,
160+
"metadata": {
161+
"collapsed": true
162+
},
163+
"outputs": [],
164+
"source": [
165+
"z.save(\"model.onnx\", format=C.ModelFormat.ONNX)"
166+
]
76167
}
77168
],
78169
"metadata": {

0 commit comments

Comments
 (0)