|
25 | 25 | "cell_type": "markdown", |
26 | 26 | "metadata": {}, |
27 | 27 | "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", |
29 | 29 | "Follow CNTK installation instructions __[here](https://docs.microsoft.com/en-us/cognitive-toolkit/Setup-CNTK-on-your-machine)__." |
30 | 30 | ] |
31 | 31 | }, |
32 | 32 | { |
33 | 33 | "cell_type": "markdown", |
34 | 34 | "metadata": {}, |
35 | 35 | "source": [ |
36 | | - "## Exporting in Python" |
| 36 | + "## API Usage" |
37 | 37 | ] |
38 | 38 | }, |
39 | 39 | { |
|
43 | 43 | "To save a CNTK model to the ONNX format, specify the ONNX format in the format parameter of the save function." |
44 | 44 | ] |
45 | 45 | }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "metadata": {}, |
| 49 | + "source": [ |
| 50 | + "** Using Python API ** " |
| 51 | + ] |
| 52 | + }, |
46 | 53 | { |
47 | 54 | "cell_type": "markdown", |
48 | 55 | "metadata": {}, |
|
51 | 58 | "import cntk as C\n", |
52 | 59 | "\n", |
53 | 60 | "x = C.input_variable(<input shape>)\n", |
54 | | - "z = create_model(x)\n", |
| 61 | + "z = create_model(x) #your create model function\n", |
55 | 62 | "z.save(<path of where to save your ONNX model>, format=C.ModelFormat.ONNX)\n", |
56 | 63 | "```" |
57 | 64 | ] |
|
60 | 67 | "cell_type": "markdown", |
61 | 68 | "metadata": {}, |
62 | 69 | "source": [ |
63 | | - "## Exporting in C# #" |
| 70 | + "** Exporting in C# **" |
64 | 71 | ] |
65 | 72 | }, |
66 | 73 | { |
|
69 | 76 | "source": [ |
70 | 77 | "```csharp\n", |
71 | 78 | "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", |
73 | 80 | "z.Save(<path of where to save your ONNX model>, ModelFormat.ONNX);\n", |
74 | 81 | "```\n" |
75 | 82 | ] |
| 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 | + ] |
76 | 167 | } |
77 | 168 | ], |
78 | 169 | "metadata": { |
|
0 commit comments