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

Skip to content

Commit 965d125

Browse files
committed
Add onnx->caffe2 tutorial
1 parent 603042d commit 965d125

1 file changed

Lines changed: 68 additions & 6 deletions

File tree

tutorials/OnnxCaffe2Import.ipynb

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,89 @@
66
"source": [
77
"# Importing models from ONNX to Caffe2\n",
88
"\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."
1072
]
1173
}
1274
],
1375
"metadata": {
1476
"kernelspec": {
15-
"display_name": "Python 3",
77+
"display_name": "Python 2",
1678
"language": "python",
17-
"name": "python3"
79+
"name": "python2"
1880
},
1981
"language_info": {
2082
"codemirror_mode": {
2183
"name": "ipython",
22-
"version": 3
84+
"version": 2
2385
},
2486
"file_extension": ".py",
2587
"mimetype": "text/x-python",
2688
"name": "python",
2789
"nbconvert_exporter": "python",
28-
"pygments_lexer": "ipython3",
29-
"version": "3.6.3"
90+
"pygments_lexer": "ipython2",
91+
"version": "2.7.12"
3092
}
3193
},
3294
"nbformat": 4,

0 commit comments

Comments
 (0)