{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exporting models from CNTK to ONNX" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this tutorial, we will demonstrate how to export a CNTK model to the ONNX format." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To export to ONNX, simply make sure you have CNTK 2.3.1 or higher installed.
\n", "Follow CNTK installation instructions __[here](https://docs.microsoft.com/en-us/cognitive-toolkit/Setup-CNTK-on-your-machine)__." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## API Usage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To save a CNTK model to the ONNX format, specify the ONNX format in the format parameter of the save function." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "** Using Python API ** " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "import cntk as C\n", "\n", "x = C.input_variable()\n", "z = create_model(x) #your create model function\n", "z.save(, format=C.ModelFormat.ONNX)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "** Exporting in C# **" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```csharp\n", "var x = CNTKLib.InputVariable();\n", "Function z = CreateModel(x); //your create model function\n", "z.Save(, ModelFormat.ONNX);\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Trying it out with ResNet-20" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's go through an example of exporting a pretrained CNTK model to ONNX." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 1: Prepare a CNTK model to export" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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", "Download link: https://www.cntk.ai/Models/CNTK_Pretrained/ResNet20_CIFAR10_CNTK.model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 2: Load the model into CNTK" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import cntk as C" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "model_path = \"ResNet20_CIFAR10_CNTK.model\"\n", "z = C.Function.load(model_path, device=C.device.cpu())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 3: Export the model to ONNX" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, export the CNTK model by saving it out to the ONNX format." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "z.save(\"model.onnx\", format=C.ModelFormat.ONNX)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }