|
6 | 6 | "source": [ |
7 | 7 | "# Importing models from ONNX to Caffe2\n", |
8 | 8 | "\n", |
9 | | - "Coming soon!" |
| 9 | + "In this tutorial we are going to show you how to import ONNX models to Caffe2. You can either\n", |
| 10 | + "\n", |
| 11 | + "- Directly run an ONNX model with Caffe2 in Python\n", |
| 12 | + "\n", |
| 13 | + "or\n", |
| 14 | + "\n", |
| 15 | + "- Convert an ONNX model file to a Caffe2 model file, and then later run the converted Caffe2 model in any environment that Caffe2 supports, e.g. on mobile phones." |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "markdown", |
| 20 | + "metadata": {}, |
| 21 | + "source": [ |
| 22 | + "### Installation\n", |
| 23 | + "\n", |
| 24 | + "The only thing you need for importing ONNX models to Caffe2 is the python package `onnx-caffe2`:\n", |
| 25 | + "\n", |
| 26 | + "```shell\n", |
| 27 | + "$ pip install onnx-caffe2\n", |
| 28 | + "```" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "metadata": {}, |
| 34 | + "source": [ |
| 35 | + "### Run an ONNX model with Caffe2" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 0, |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "import onnx\n", |
| 45 | + "import onnx_caffe2.backend\n", |
| 46 | + "\n", |
| 47 | + "# Prepare the inputs, here we use numpy to generate some random inputs for demo purpose\n", |
| 48 | + "import numpy as np\n", |
| 49 | + "img = np.random.randn(1, 3, 224, 224).astype(np.float32)\n", |
| 50 | + "\n", |
| 51 | + "# Load the ONNX model\n", |
| 52 | + "model = onnx.load('assets/squeezenet.onnx')\n", |
| 53 | + "# Run the ONNX model with Caffe2\n", |
| 54 | + "outputs = onnx_caffe2.backend.run_model(model, [img])" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "markdown", |
| 59 | + "metadata": {}, |
| 60 | + "source": [ |
| 61 | + "### Convert ONNX model file to Caffe2 model file\n", |
| 62 | + "`onnx-caffe2` has bundled a shell command `convert-onnx-to-caffe2` for converting ONNX model file to Caffe2 model file. \n", |
| 63 | + "\n", |
| 64 | + "\n", |
| 65 | + "```shell\n", |
| 66 | + "\n", |
| 67 | + "$ convert-onnx-to-caffe2 assets/squeezenet.onnx --output predict_net.pb --init-net-output init_net.pb\n", |
| 68 | + "\n", |
| 69 | + "```\n", |
| 70 | + "\n", |
| 71 | + "Note in ONNX model file, parameters and network structure are all stored in one model file, while in Caffe2, they are normally stored in separated `init_net.pb` (parameters) and `predict_net.pb` (network structure) files." |
10 | 72 | ] |
11 | 73 | } |
12 | 74 | ], |
13 | 75 | "metadata": { |
14 | 76 | "kernelspec": { |
15 | | - "display_name": "Python 3", |
| 77 | + "display_name": "Python 2", |
16 | 78 | "language": "python", |
17 | | - "name": "python3" |
| 79 | + "name": "python2" |
18 | 80 | }, |
19 | 81 | "language_info": { |
20 | 82 | "codemirror_mode": { |
21 | 83 | "name": "ipython", |
22 | | - "version": 3 |
| 84 | + "version": 2 |
23 | 85 | }, |
24 | 86 | "file_extension": ".py", |
25 | 87 | "mimetype": "text/x-python", |
26 | 88 | "name": "python", |
27 | 89 | "nbconvert_exporter": "python", |
28 | | - "pygments_lexer": "ipython3", |
29 | | - "version": "3.6.3" |
| 90 | + "pygments_lexer": "ipython2", |
| 91 | + "version": "2.7.12" |
30 | 92 | } |
31 | 93 | }, |
32 | 94 | "nbformat": 4, |
|
0 commit comments