|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Exporting MXNet models to ONNX\n", |
| 8 | + "\n", |
| 9 | + "In this tutorial, we will show how you can save MXNet models to ONNX format.\n", |
| 10 | + "ONNX exporter it a part of [MXNet repository](https://github.com/apache/incubator-mxnet/tree/master/python/mxnet/contrib/onnx/mx2onnx).\n", |
| 11 | + "\n", |
| 12 | + "Current MXNet-ONNX import and export operator support and coverage can be found [here](Operator support and coverage - https://cwiki.apache.org/confluence/display/MXNET/ONNX):\n" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "markdown", |
| 17 | + "metadata": {}, |
| 18 | + "source": [ |
| 19 | + "## Step 1: Installations:\n", |
| 20 | + "#### Install **ONNX 1.2.1 version.** Follow instructions on [ONNX repo](https://github.com/onnx/onnx).\n", |
| 21 | + "\n", |
| 22 | + "MXNet ONNX importer and exporter follows version 7 of ONNX operator set.\n", |
| 23 | + "\n", |
| 24 | + "#### Make sure to install latest MXNet either from source or pip.\n", |
| 25 | + "\n", |
| 26 | + "```bash\n", |
| 27 | + "pip install mxnet --pre\n", |
| 28 | + "```\n", |
| 29 | + "\n", |
| 30 | + "* Note: ONNX exporter will be released as a part of MXNet v1.3. " |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "## Step 2: Prepare MXNet model to convert to ONNX:\n", |
| 38 | + "\n", |
| 39 | + "Let's try out pretrained resnet model from [MXNet model zoo](http://data.mxnet.io/models/)." |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 1, |
| 45 | + "metadata": { |
| 46 | + "collapsed": true |
| 47 | + }, |
| 48 | + "outputs": [], |
| 49 | + "source": [ |
| 50 | + "import mxnet as mx\n", |
| 51 | + "import numpy as np\n", |
| 52 | + "from mxnet.contrib import onnx as onnx_mxnet" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": 2, |
| 58 | + "metadata": {}, |
| 59 | + "outputs": [ |
| 60 | + { |
| 61 | + "data": { |
| 62 | + "text/plain": [ |
| 63 | + "['resnet-18-0000.params', 'resnet-18-symbol.json', 'synset.txt']" |
| 64 | + ] |
| 65 | + }, |
| 66 | + "execution_count": 2, |
| 67 | + "metadata": {}, |
| 68 | + "output_type": "execute_result" |
| 69 | + } |
| 70 | + ], |
| 71 | + "source": [ |
| 72 | + "# Download pretrained resnet model - json and params from mxnet model zoo.\n", |
| 73 | + "path='http://data.mxnet.io/models/imagenet/'\n", |
| 74 | + "[mx.test_utils.download(path+'resnet/18-layers/resnet-18-0000.params'),\n", |
| 75 | + " mx.test_utils.download(path+'resnet/18-layers/resnet-18-symbol.json'),\n", |
| 76 | + " mx.test_utils.download(path+'synset.txt')]" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "## Step 3: Use MXNet to ONNX exporter:\n", |
| 84 | + "\n", |
| 85 | + "MXNet's ONNX \"export_model\" API accepts following inputs: \n" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 3, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [ |
| 93 | + { |
| 94 | + "name": "stdout", |
| 95 | + "output_type": "stream", |
| 96 | + "text": [ |
| 97 | + "Help on function export_model in module mxnet.contrib.onnx.mx2onnx.export_model:\n", |
| 98 | + "\n", |
| 99 | + "export_model(sym, params, input_shape, input_type=<type 'numpy.float32'>, onnx_file_path=u'model.onnx', verbose=False)\n", |
| 100 | + " Exports the MXNet model file, passed as a parameter, into ONNX model.\n", |
| 101 | + " Accepts both symbol,parameter objects as well as json and params filepaths as input.\n", |
| 102 | + " Operator support and coverage - https://cwiki.apache.org/confluence/display/MXNET/ONNX\n", |
| 103 | + " \n", |
| 104 | + " Parameters\n", |
| 105 | + " ----------\n", |
| 106 | + " sym : str or symbol object\n", |
| 107 | + " Path to the json file or Symbol object\n", |
| 108 | + " params : str or symbol object\n", |
| 109 | + " Path to the params file or params dictionary. (Including both arg_params and aux_params)\n", |
| 110 | + " input_shape : List of tuple\n", |
| 111 | + " Input shape of the model e.g [(1,3,224,224)]\n", |
| 112 | + " input_type : data type\n", |
| 113 | + " Input data type e.g. np.float32\n", |
| 114 | + " onnx_file_path : str\n", |
| 115 | + " Path where to save the generated onnx file\n", |
| 116 | + " verbose : Boolean\n", |
| 117 | + " If true will print logs of the model conversion\n", |
| 118 | + " \n", |
| 119 | + " Returns\n", |
| 120 | + " -------\n", |
| 121 | + " onnx_file_path : str\n", |
| 122 | + " Onnx file path\n", |
| 123 | + "\n" |
| 124 | + ] |
| 125 | + } |
| 126 | + ], |
| 127 | + "source": [ |
| 128 | + "help(onnx_mxnet.export_model)" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "markdown", |
| 133 | + "metadata": {}, |
| 134 | + "source": [ |
| 135 | + "From the API description, you can see that `export_model` API accepts 2 kinds of inputs:\n", |
| 136 | + "\n", |
| 137 | + "### MXNet sym, params objects:\n", |
| 138 | + "\n", |
| 139 | + " This is useful if you are training a model. At the end of training, you just need to invoke the `export_model` function and provide sym and params objects as inputs with other attributes to save the model in ONNX format.\n", |
| 140 | + " \n", |
| 141 | + "### MXNet's exported json and params files:\n", |
| 142 | + "\n", |
| 143 | + " This is useful if you have pretrained models and you want to convert them to ONNX format.\n", |
| 144 | + "\n", |
| 145 | + "In this tutorial, we will show second usecase: " |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": 4, |
| 151 | + "metadata": { |
| 152 | + "collapsed": true |
| 153 | + }, |
| 154 | + "outputs": [], |
| 155 | + "source": [ |
| 156 | + "# Downloaded input symbol and params files\n", |
| 157 | + "sym = 'resnet-18-symbol.json'\n", |
| 158 | + "params = 'resnet-18-0000.params'\n", |
| 159 | + "# Standard Imagenet input - 3 channels, 224*224\n", |
| 160 | + "input_shape = (1,3,224,224)\n", |
| 161 | + "# Path of the output file\n", |
| 162 | + "onnx_file = 'mxnet_exported_resnet50.onnx'" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "code", |
| 167 | + "execution_count": 5, |
| 168 | + "metadata": { |
| 169 | + "collapsed": true |
| 170 | + }, |
| 171 | + "outputs": [], |
| 172 | + "source": [ |
| 173 | + "# Invoke export model API. It returns path of the converted onnx model\n", |
| 174 | + "converted_model_path = onnx_mxnet.export_model(sym, params, [input_shape], np.float32, onnx_file)" |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "code", |
| 179 | + "execution_count": 6, |
| 180 | + "metadata": {}, |
| 181 | + "outputs": [ |
| 182 | + { |
| 183 | + "name": "stdout", |
| 184 | + "output_type": "stream", |
| 185 | + "text": [ |
| 186 | + "mxnet_exported_resnet50.onnx\n" |
| 187 | + ] |
| 188 | + } |
| 189 | + ], |
| 190 | + "source": [ |
| 191 | + "print(converted_model_path)" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "markdown", |
| 196 | + "metadata": {}, |
| 197 | + "source": [ |
| 198 | + "## Step 4: Check validity\n", |
| 199 | + "\n", |
| 200 | + "You can check validity of the converted ONNX model by using ONNX checker tool as follows:" |
| 201 | + ] |
| 202 | + }, |
| 203 | + { |
| 204 | + "cell_type": "code", |
| 205 | + "execution_count": 7, |
| 206 | + "metadata": {}, |
| 207 | + "outputs": [], |
| 208 | + "source": [ |
| 209 | + "from onnx import checker\n", |
| 210 | + "import onnx\n", |
| 211 | + "# Load onnx model\n", |
| 212 | + "model_proto = onnx.load(converted_model_path)\n", |
| 213 | + "\n", |
| 214 | + "# Check if converted ONNX protobuf is valid\n", |
| 215 | + "checker.check_graph(model_proto.graph)" |
| 216 | + ] |
| 217 | + }, |
| 218 | + { |
| 219 | + "cell_type": "markdown", |
| 220 | + "metadata": {}, |
| 221 | + "source": [ |
| 222 | + "This method confirms input model protobuf is valid." |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "markdown", |
| 227 | + "metadata": {}, |
| 228 | + "source": [ |
| 229 | + "## What's next\n", |
| 230 | + "\n", |
| 231 | + "Take a look at [other tutorials, including importing of ONNX models to other frameworks](https://github.com/onnx/tutorials/tree/master/tutorials)" |
| 232 | + ] |
| 233 | + }, |
| 234 | + { |
| 235 | + "cell_type": "code", |
| 236 | + "execution_count": null, |
| 237 | + "metadata": { |
| 238 | + "collapsed": true |
| 239 | + }, |
| 240 | + "outputs": [], |
| 241 | + "source": [] |
| 242 | + } |
| 243 | + ], |
| 244 | + "metadata": { |
| 245 | + "kernelspec": { |
| 246 | + "display_name": "onnx_mxnet", |
| 247 | + "language": "python", |
| 248 | + "name": "onnx_mxnet" |
| 249 | + }, |
| 250 | + "language_info": { |
| 251 | + "codemirror_mode": { |
| 252 | + "name": "ipython", |
| 253 | + "version": 2 |
| 254 | + }, |
| 255 | + "file_extension": ".py", |
| 256 | + "mimetype": "text/x-python", |
| 257 | + "name": "python", |
| 258 | + "nbconvert_exporter": "python", |
| 259 | + "pygments_lexer": "ipython2", |
| 260 | + "version": "2.7.14" |
| 261 | + } |
| 262 | + }, |
| 263 | + "nbformat": 4, |
| 264 | + "nbformat_minor": 2 |
| 265 | +} |
0 commit comments