|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import paddle.fluid as fluid\n", |
| 10 | + "import os" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "## 定义ResNet模型" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": 2, |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "def conv_bn_layer(input,\n", |
| 27 | + " ch_out,\n", |
| 28 | + " filter_size,\n", |
| 29 | + " stride,\n", |
| 30 | + " padding,\n", |
| 31 | + " act='relu',\n", |
| 32 | + " bias_attr=False):\n", |
| 33 | + " tmp = fluid.layers.conv2d(\n", |
| 34 | + " input=input,\n", |
| 35 | + " filter_size=filter_size,\n", |
| 36 | + " num_filters=ch_out,\n", |
| 37 | + " stride=stride,\n", |
| 38 | + " padding=padding,\n", |
| 39 | + " act=None,\n", |
| 40 | + " bias_attr=bias_attr)\n", |
| 41 | + " return fluid.layers.batch_norm(input=tmp, act=act)\n", |
| 42 | + "\n", |
| 43 | + "def shortcut(input, ch_in, ch_out, stride):\n", |
| 44 | + " if ch_in != ch_out:\n", |
| 45 | + " return conv_bn_layer(input, ch_out, 1, stride, 0, None)\n", |
| 46 | + " else:\n", |
| 47 | + " return input\n", |
| 48 | + "\n", |
| 49 | + "def basicblock(input, ch_in, ch_out, stride):\n", |
| 50 | + " tmp = conv_bn_layer(input, ch_out, 3, stride, 1)\n", |
| 51 | + " tmp = conv_bn_layer(tmp, ch_out, 3, 1, 1, act=None, bias_attr=True)\n", |
| 52 | + " short = shortcut(input, ch_in, ch_out, stride)\n", |
| 53 | + " return fluid.layers.elementwise_add(x=tmp, y=short, act='relu')\n", |
| 54 | + "\n", |
| 55 | + "def layer_warp(block_func, input, ch_in, ch_out, count, stride):\n", |
| 56 | + " tmp = block_func(input, ch_in, ch_out, stride)\n", |
| 57 | + " for i in range(1, count):\n", |
| 58 | + " tmp = block_func(tmp, ch_out, ch_out, 1)\n", |
| 59 | + " return tmp\n", |
| 60 | + "\n", |
| 61 | + "def resnet_cifar10(ipt, depth=32):\n", |
| 62 | + " # depth should be one of 20, 32, 44, 56, 110, 1202\n", |
| 63 | + " assert (depth - 2) % 6 == 0\n", |
| 64 | + " n = (depth - 2) // 6\n", |
| 65 | + " nStages = {16, 64, 128}\n", |
| 66 | + " conv1 = conv_bn_layer(ipt, ch_out=16, filter_size=3, stride=1, padding=1)\n", |
| 67 | + " res1 = layer_warp(basicblock, conv1, 16, 16, n, 1)\n", |
| 68 | + " #res2 = layer_warp(basicblock, res1, 16, 32, n, 2)\n", |
| 69 | + " #res3 = layer_warp(basicblock, res2, 32, 64, n, 2)\n", |
| 70 | + " pool = fluid.layers.pool2d(\n", |
| 71 | + " input=res1, pool_size=8, pool_type='avg', pool_stride=1)\n", |
| 72 | + " predict = fluid.layers.fc(input=pool, size=10, act='softmax')\n", |
| 73 | + " return predict\n", |
| 74 | + "\n", |
| 75 | + "data_shape = [None, 3, 32, 32]\n", |
| 76 | + "images = fluid.data(name='pixel', shape=data_shape, dtype='float32')\n", |
| 77 | + "predict = resnet_cifar10(images, 32)\n", |
| 78 | + "exe = fluid.Executor(fluid.CPUPlace())\n", |
| 79 | + "_ =exe.run(fluid.default_startup_program())" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "markdown", |
| 84 | + "metadata": {}, |
| 85 | + "source": [ |
| 86 | + "# 转换模型(数被保存为多个文件(not combined))" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 3, |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [ |
| 94 | + { |
| 95 | + "data": { |
| 96 | + "text/plain": [ |
| 97 | + "['save_infer_model/scale_0.tmp_0']" |
| 98 | + ] |
| 99 | + }, |
| 100 | + "execution_count": 3, |
| 101 | + "metadata": {}, |
| 102 | + "output_type": "execute_result" |
| 103 | + } |
| 104 | + ], |
| 105 | + "source": [ |
| 106 | + "model_dir = './resnet_not_combined/'\n", |
| 107 | + "fluid.io.save_inference_model(model_dir, [\"pixel\"], [predict], exe)" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": 4, |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [ |
| 115 | + { |
| 116 | + "name": "stdout", |
| 117 | + "output_type": "stream", |
| 118 | + "text": [ |
| 119 | + "__model__ batch_norm_2.w_2 batch_norm_6.w_2 conv2d_10.w_0\n", |
| 120 | + "batch_norm_0.b_0 batch_norm_3.b_0 batch_norm_7.b_0 conv2d_2.b_0\n", |
| 121 | + "batch_norm_0.w_0 batch_norm_3.w_0 batch_norm_7.w_0 conv2d_2.w_0\n", |
| 122 | + "batch_norm_0.w_1 batch_norm_3.w_1 batch_norm_7.w_1 conv2d_3.w_0\n", |
| 123 | + "batch_norm_0.w_2 batch_norm_3.w_2 batch_norm_7.w_2 conv2d_4.b_0\n", |
| 124 | + "batch_norm_1.b_0 batch_norm_4.b_0 batch_norm_8.b_0 conv2d_4.w_0\n", |
| 125 | + "batch_norm_1.w_0 batch_norm_4.w_0 batch_norm_8.w_0 conv2d_5.w_0\n", |
| 126 | + "batch_norm_1.w_1 batch_norm_4.w_1 batch_norm_8.w_1 conv2d_6.b_0\n", |
| 127 | + "batch_norm_1.w_2 batch_norm_4.w_2 batch_norm_8.w_2 conv2d_6.w_0\n", |
| 128 | + "batch_norm_10.b_0 batch_norm_5.b_0 batch_norm_9.b_0 conv2d_7.w_0\n", |
| 129 | + "batch_norm_10.w_0 batch_norm_5.w_0 batch_norm_9.w_0 conv2d_8.b_0\n", |
| 130 | + "batch_norm_10.w_1 batch_norm_5.w_1 batch_norm_9.w_1 conv2d_8.w_0\n", |
| 131 | + "batch_norm_10.w_2 batch_norm_5.w_2 batch_norm_9.w_2 conv2d_9.w_0\n", |
| 132 | + "batch_norm_2.b_0 batch_norm_6.b_0 conv2d_0.w_0 fc_0.b_0\n", |
| 133 | + "batch_norm_2.w_0 batch_norm_6.w_0 conv2d_1.w_0 fc_0.w_0\n", |
| 134 | + "batch_norm_2.w_1 batch_norm_6.w_1 conv2d_10.b_0\n" |
| 135 | + ] |
| 136 | + } |
| 137 | + ], |
| 138 | + "source": [ |
| 139 | + "!ls ./resnet_not_combined/" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": null, |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [ |
| 148 | + "# 执行shell命令,jupyter可能因为shell环境问题找到paddle2onnx命令,出现问题请到命令行执行\n", |
| 149 | + "!paddle2onnx --model_dir ./resnet_not_combined/ --save_file onnx-model/model.onnx --opset_version 10" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "markdown", |
| 154 | + "metadata": {}, |
| 155 | + "source": [ |
| 156 | + "# 转换模型(参数被保存在一个单独的二进制文件中(combined))" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": 5, |
| 162 | + "metadata": {}, |
| 163 | + "outputs": [ |
| 164 | + { |
| 165 | + "data": { |
| 166 | + "text/plain": [ |
| 167 | + "['save_infer_model/scale_0.tmp_1']" |
| 168 | + ] |
| 169 | + }, |
| 170 | + "execution_count": 5, |
| 171 | + "metadata": {}, |
| 172 | + "output_type": "execute_result" |
| 173 | + } |
| 174 | + ], |
| 175 | + "source": [ |
| 176 | + "model_dir = './resnet_combined/'\n", |
| 177 | + "fluid.io.save_inference_model(model_dir, [\"pixel\"], [predict], exe, model_filename='__model__', params_filename='__params__')" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "code", |
| 182 | + "execution_count": 6, |
| 183 | + "metadata": {}, |
| 184 | + "outputs": [ |
| 185 | + { |
| 186 | + "name": "stdout", |
| 187 | + "output_type": "stream", |
| 188 | + "text": [ |
| 189 | + "__model__ __params__\n" |
| 190 | + ] |
| 191 | + } |
| 192 | + ], |
| 193 | + "source": [ |
| 194 | + "!ls ./resnet_combined/" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "code", |
| 199 | + "execution_count": null, |
| 200 | + "metadata": {}, |
| 201 | + "outputs": [], |
| 202 | + "source": [ |
| 203 | + "# 执行shell命令,jupyter可能因为shell环境问题找到paddle2onnx命令,出现问题请到命令行执行\n", |
| 204 | + "!paddle2onnx --model_dir ./resnet_combined/ --model_filename '__model__' --params_filename '__params__' --save_file onnx-model/model.onnx --opset_version 10" |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "code", |
| 209 | + "execution_count": null, |
| 210 | + "metadata": {}, |
| 211 | + "outputs": [], |
| 212 | + "source": [] |
| 213 | + } |
| 214 | + ], |
| 215 | + "metadata": { |
| 216 | + "kernelspec": { |
| 217 | + "display_name": "Python [conda env:x2paddle]", |
| 218 | + "language": "python", |
| 219 | + "name": "conda-env-x2paddle-py" |
| 220 | + }, |
| 221 | + "language_info": { |
| 222 | + "codemirror_mode": { |
| 223 | + "name": "ipython", |
| 224 | + "version": 3 |
| 225 | + }, |
| 226 | + "file_extension": ".py", |
| 227 | + "mimetype": "text/x-python", |
| 228 | + "name": "python", |
| 229 | + "nbconvert_exporter": "python", |
| 230 | + "pygments_lexer": "ipython3", |
| 231 | + "version": "3.6.0" |
| 232 | + } |
| 233 | + }, |
| 234 | + "nbformat": 4, |
| 235 | + "nbformat_minor": 4 |
| 236 | +} |
0 commit comments