|
22 | 22 | "cell_type": "code", |
23 | 23 | "execution_count": 1, |
24 | 24 | "metadata": {}, |
25 | | - "outputs": [ |
26 | | - { |
27 | | - "name": "stderr", |
28 | | - "output_type": "stream", |
29 | | - "text": [ |
30 | | - "WARNING:root:This caffe2 python run does not have GPU support. Will run in CPU only mode.\n", |
31 | | - "WARNING:root:Debug message: No module named caffe2_pybind11_state_gpu\n" |
32 | | - ] |
33 | | - } |
34 | | - ], |
| 25 | + "outputs": [], |
35 | 26 | "source": [ |
36 | 27 | "from __future__ import absolute_import\n", |
37 | 28 | "from __future__ import division\n", |
38 | 29 | "from __future__ import print_function\n", |
39 | 30 | "from __future__ import unicode_literals\n", |
40 | 31 | "\n", |
41 | 32 | "import io\n", |
| 33 | + "import time\n", |
42 | 34 | "import numpy as np\n", |
43 | 35 | "import torch\n", |
44 | 36 | "import onnx\n", |
|
49 | 41 | "from caffe2.python import core\n", |
50 | 42 | "from torch.autograd import Variable\n", |
51 | 43 | "from caffe2.python.onnx.backend import Caffe2Backend\n", |
52 | | - "from caffe2.python.onnx.helper import c2_native_run_net, save_caffe2_net, load_caffe2_net, \\\n", |
53 | | - " benchmark_caffe2_model, benchmark_pytorch_model\n", |
| 44 | + "from caffe2.python.onnx.helper import c2_native_run_net, save_caffe2_net, load_caffe2_net,benchmark_pytorch_model\n", |
54 | 45 | "\n", |
55 | 46 | "\n", |
56 | 47 | "class MNIST(nn.Module):\n", |
|
97 | 88 | "name": "stdout", |
98 | 89 | "output_type": "stream", |
99 | 90 | "text": [ |
100 | | - "graph(%0 : Float(3, 1, 28, 28)\n", |
101 | | - " %1 : Float(10, 1, 5, 5)\n", |
102 | | - " %2 : Float(10)\n", |
103 | | - " %3 : Float(20, 10, 5, 5)\n", |
104 | | - " %4 : Float(20)\n", |
105 | | - " %5 : Float(50, 320)\n", |
106 | | - " %6 : Float(50)\n", |
107 | | - " %7 : Float(10, 50)\n", |
108 | | - " %8 : Float(10)) {\n", |
109 | | - " %9 : UNKNOWN_TYPE = Conv[kernel_shape=[5, 5], strides=[1, 1], pads=[0, 0, 0, 0], dilations=[1, 1], group=1](%0, %1)\n", |
110 | | - " %10 : Float(3, 10, 24, 24) = Add[broadcast=1, axis=1](%9, %2)\n", |
111 | | - " %11 : Float(3, 10, 12, 12) = MaxPool[kernel_shape=[2, 2], pads=[0, 0], strides=[2, 2]](%10)\n", |
112 | | - " %12 : Float(3, 10, 12, 12) = Relu(%11)\n", |
113 | | - " %13 : UNKNOWN_TYPE = Conv[kernel_shape=[5, 5], strides=[1, 1], pads=[0, 0, 0, 0], dilations=[1, 1], group=1](%12, %3)\n", |
114 | | - " %14 : Float(3, 20, 8, 8) = Add[broadcast=1, axis=1](%13, %4)\n", |
115 | | - " %15 : Float(3, 20, 4, 4) = MaxPool[kernel_shape=[2, 2], pads=[0, 0], strides=[2, 2]](%14)\n", |
116 | | - " %16 : Float(3, 20, 4, 4) = Relu(%15)\n", |
117 | | - " %17 : Float(3, 320) = Reshape[shape=[-1, 320]](%16)\n", |
118 | | - " %18 : Float(320!, 50!) = Transpose[perm=[1, 0]](%5)\n", |
119 | | - " %20 : Float(3, 50) = Gemm[alpha=1, beta=1, broadcast=1](%17, %18, %6)\n", |
120 | | - " %21 : Float(3, 50) = Relu(%20)\n", |
121 | | - " %22 : Float(3, 50), %23 : UNKNOWN_TYPE = Dropout[is_test=1, ratio=0.5](%21)\n", |
122 | | - " %24 : Float(50!, 10!) = Transpose[perm=[1, 0]](%7)\n", |
123 | | - " %26 : Float(3, 10) = Gemm[alpha=1, beta=1, broadcast=1](%22, %24, %8)\n", |
124 | | - " %27 : Float(3, 10) = Softmax[axis=1](%26)\n", |
125 | | - " %28 : Float(3, 10) = Log(%27)\n", |
126 | | - " return (%28);\n", |
127 | | - "}\n", |
| 91 | + "graph(%input.1 : Float(3, 1, 28, 28),\n", |
| 92 | + " %conv1.weight : Float(10, 1, 5, 5),\n", |
| 93 | + " %conv1.bias : Float(10),\n", |
| 94 | + " %conv2.weight : Float(20, 10, 5, 5),\n", |
| 95 | + " %conv2.bias : Float(20),\n", |
| 96 | + " %fc1.weight : Float(50, 320),\n", |
| 97 | + " %fc1.bias : Float(50),\n", |
| 98 | + " %fc2.weight : Float(10, 50),\n", |
| 99 | + " %fc2.bias : Float(10)):\n", |
| 100 | + " %9 : Float(3, 10, 24, 24) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[5, 5], pads=[0, 0, 0, 0], strides=[1, 1]](%input.1, %conv1.weight, %conv1.bias), scope: MNIST/Conv2d[conv1] # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/modules/conv.py:340:0\n", |
| 101 | + " %10 : Float(3, 10, 12, 12) = onnx::MaxPool[kernel_shape=[2, 2], pads=[0, 0, 0, 0], strides=[2, 2]](%9), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:487:0\n", |
| 102 | + " %11 : Float(3, 10, 12, 12) = onnx::Relu(%10), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:913:0\n", |
| 103 | + " %12 : Float(3, 20, 8, 8) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[5, 5], pads=[0, 0, 0, 0], strides=[1, 1]](%11, %conv2.weight, %conv2.bias), scope: MNIST/Dropout2d[conv2_drop] # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:844:0\n", |
| 104 | + " %13 : Float(3, 20, 4, 4) = onnx::MaxPool[kernel_shape=[2, 2], pads=[0, 0, 0, 0], strides=[2, 2]](%12), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:487:0\n", |
| 105 | + " %14 : Float(3, 20, 4, 4) = onnx::Relu(%13), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:913:0\n", |
| 106 | + " %15 : Tensor = onnx::Constant[value= -1 320 [ Variable[CPULongType]{2} ]](), scope: MNIST\n", |
| 107 | + " %16 : Float(3, 320) = onnx::Reshape(%14, %15), scope: MNIST # <ipython-input-1-c1a2123bd117>:34:0\n", |
| 108 | + " %17 : Float(3, 50) = onnx::Gemm[alpha=1, beta=1, transB=1](%16, %fc1.weight, %fc1.bias), scope: MNIST/Linear[fc1] # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:1369:0\n", |
| 109 | + " %18 : Float(3, 50) = onnx::Relu(%17), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:806:0\n", |
| 110 | + " %19 : Float(3, 10) = onnx::Gemm[alpha=1, beta=1, transB=1](%18, %fc2.weight, %fc2.bias), scope: MNIST/Linear[fc2] # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:1369:0\n", |
| 111 | + " %20 : Float(3, 10) = onnx::LogSoftmax[axis=1](%19), scope: MNIST # /home/marouenez/anaconda3/envs/masterthesis/lib/python3.7/site-packages/torch/nn/functional.py:1316:0\n", |
| 112 | + " return (%20)\n", |
128 | 113 | "\n", |
129 | 114 | "Check the ONNX model.\n" |
130 | 115 | ] |
|
164 | 149 | "source": [ |
165 | 150 | "# Convert the ONNX model to a Caffe2 model.\n", |
166 | 151 | "print(\"Convert the model to a Caffe2 model.\")\n", |
167 | | - "init_net, predict_net = Caffe2Backend.onnx_graph_to_caffe2_net(onnx_model.graph, device=\"CPU\")" |
| 152 | + "init_net, predict_net = Caffe2Backend.onnx_graph_to_caffe2_net(onnx_model, device=\"CPU\")" |
168 | 153 | ] |
169 | 154 | }, |
170 | 155 | { |
|
177 | 162 | { |
178 | 163 | "cell_type": "code", |
179 | 164 | "execution_count": 4, |
180 | | - "metadata": { |
181 | | - "collapsed": true |
182 | | - }, |
| 165 | + "metadata": {}, |
183 | 166 | "outputs": [], |
184 | 167 | "source": [ |
185 | 168 | "# Prepare the inputs for Caffe2.\n", |
|
196 | 179 | { |
197 | 180 | "cell_type": "code", |
198 | 181 | "execution_count": 5, |
199 | | - "metadata": { |
200 | | - "collapsed": true |
201 | | - }, |
| 182 | + "metadata": {}, |
202 | 183 | "outputs": [], |
203 | 184 | "source": [ |
204 | 185 | "# Save the converted Caffe2 model in the protobuf files. (Optional)\n", |
|
263 | 244 | "print(\"The exported model achieves {}-decimal precision.\".format(expected_decimal))" |
264 | 245 | ] |
265 | 246 | }, |
| 247 | + { |
| 248 | + "cell_type": "code", |
| 249 | + "execution_count": 8, |
| 250 | + "metadata": {}, |
| 251 | + "outputs": [], |
| 252 | + "source": [ |
| 253 | + "def benchmark_caffe2_model(init_net, predict_net,inputs, warmup_iters=3, main_iters=10):\n", |
| 254 | + " '''\n", |
| 255 | + " Run the model several times, and measure the execution time.\n", |
| 256 | + " Print the execution time per iteration (millisecond) and the number of iterations per second.\n", |
| 257 | + " '''\n", |
| 258 | + " for _i in range(warmup_iters):\n", |
| 259 | + " ws, caffe2_results = c2_native_run_net(init_net, predict_net, inputs) \n", |
| 260 | + "\n", |
| 261 | + " total_time = 0.0\n", |
| 262 | + " for _i in range(main_iters):\n", |
| 263 | + " ts = time.time()\n", |
| 264 | + " ws, caffe2_results = c2_native_run_net(init_net, predict_net, inputs)\n", |
| 265 | + " te = time.time()\n", |
| 266 | + " total_time += te - ts\n", |
| 267 | + " \n", |
| 268 | + " return total_time / main_iters * 1000" |
| 269 | + ] |
| 270 | + }, |
266 | 271 | { |
267 | 272 | "cell_type": "markdown", |
268 | 273 | "metadata": {}, |
|
275 | 280 | }, |
276 | 281 | { |
277 | 282 | "cell_type": "code", |
278 | | - "execution_count": 8, |
| 283 | + "execution_count": 9, |
279 | 284 | "metadata": {}, |
280 | 285 | "outputs": [ |
281 | 286 | { |
282 | 287 | "name": "stdout", |
283 | 288 | "output_type": "stream", |
284 | 289 | "text": [ |
285 | | - "PyTorch model's execution time is 0.580716133118 milliseconds/ iteration, 1722.011742 iterations per second.\n", |
286 | | - "Caffe2 model's execution time is 0.399529695511 milliseconds / iteration, 2502.94286316 iterations per second\n" |
| 290 | + "PyTorch model's execution time is 0.6218433380126953 milliseconds/ iteration, 1608.1220765278736 iterations per second.\n", |
| 291 | + "Caffe2 model's execution time is 3.189969062805176 milliseconds / iteration, 313.48266403581545 iterations per second\n" |
287 | 292 | ] |
288 | 293 | } |
289 | 294 | ], |
290 | 295 | "source": [ |
291 | 296 | "pytorch_time = benchmark_pytorch_model(pytorch_model, inputs)\n", |
292 | | - "caffe2_time = benchmark_caffe2_model(init_net, predict_net)\n", |
| 297 | + "caffe2_time = benchmark_caffe2_model(init_net, predict_net, caffe2_inputs)\n", |
293 | 298 | "\n", |
294 | 299 | "print(\"PyTorch model's execution time is {} milliseconds/ iteration, {} iterations per second.\".format(\n", |
295 | 300 | " pytorch_time, 1000 / pytorch_time))\n", |
296 | 301 | "print(\"Caffe2 model's execution time is {} milliseconds / iteration, {} iterations per second\".format(\n", |
297 | 302 | " caffe2_time, 1000 / caffe2_time))" |
298 | 303 | ] |
| 304 | + }, |
| 305 | + { |
| 306 | + "cell_type": "code", |
| 307 | + "execution_count": null, |
| 308 | + "metadata": {}, |
| 309 | + "outputs": [], |
| 310 | + "source": [] |
299 | 311 | } |
300 | 312 | ], |
301 | 313 | "metadata": { |
302 | 314 | "kernelspec": { |
303 | | - "display_name": "Python 2", |
| 315 | + "display_name": "Python 3", |
304 | 316 | "language": "python", |
305 | | - "name": "python2" |
| 317 | + "name": "python3" |
306 | 318 | }, |
307 | 319 | "language_info": { |
308 | 320 | "codemirror_mode": { |
309 | 321 | "name": "ipython", |
310 | | - "version": 2 |
| 322 | + "version": 3 |
311 | 323 | }, |
312 | 324 | "file_extension": ".py", |
313 | 325 | "mimetype": "text/x-python", |
314 | 326 | "name": "python", |
315 | 327 | "nbconvert_exporter": "python", |
316 | | - "pygments_lexer": "ipython2", |
317 | | - "version": "2.7.13" |
| 328 | + "pygments_lexer": "ipython3", |
| 329 | + "version": "3.7.4" |
318 | 330 | } |
319 | 331 | }, |
320 | 332 | "nbformat": 4, |
|
0 commit comments