转载来至
http://blog.csdn.net/shuzfan/article/details/50037273
由于caffe依赖性太多,配置极其复杂,所以将接下来的学习转向mxnet.因此本文主要记录我的学习历程,如果描述有什么问题,欢迎大家的指正。
mxnet的优点很明显,简洁灵活效率高 ,多机多卡支持好。
mxnet的github下载链接:https://github.com/dmlc/mxnet/
mxnet的开发文档链接:http://mxnet.readthedocs.org/en/latest/build.html
1- linux下安装mxnet和python接口配置
mxnet可以安装到Linux、OSX和windows下。由于windows下配置相对较为复杂,故这里选择安装在linux下。 具体我的linux版本为64位的ubuntu14.04LTS,之前使用ubuntu15时发现不是很稳定。还有,这里我是在笔记本上安装的,因此没有安装CUDA、CUDNN之类的GPU管理程序。
mxnet支持在线安装,在终端中单步依次执行以下代码:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">sudo apt<span class="hljs-attribute" style="box-sizing: border-box;">-get</span> update sudo apt<span class="hljs-attribute" style="box-sizing: border-box;">-get</span> install <span class="hljs-attribute" style="box-sizing: border-box;">-y</span> build<span class="hljs-attribute" style="box-sizing: border-box;">-essential</span> git libatlas<span class="hljs-attribute" style="box-sizing: border-box;">-base</span><span class="hljs-attribute" style="box-sizing: border-box;">-dev</span> libopencv<span class="hljs-attribute" style="box-sizing: border-box;">-dev</span> git clone <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">--</span>recursive https:<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//github.com/dmlc/mxnet</span> cd mxnet; make <span class="hljs-attribute" style="box-sizing: border-box;">-j4</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
Python接口的配置:
mxnet目前主要支持python、R和Julia这3种接口。我选择了相对熟悉的python。
mxnet要求2.7及以上版本的python和numpy库,一般linux都自带python编译器,因此我们只需要再检查一下是否缺少numpy库。可以直接输入下面的numpy安装代码:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">sudo apt<span class="hljs-attribute" style="box-sizing: border-box;">-get</span> install python<span class="hljs-attribute" style="box-sizing: border-box;">-numpy</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
上面处理完成之后可以尝试一下能否运行官方提供的一个案例:在mnist数据集上训练一个多层感知器MLP。
<code class="language-matlab hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">python mxnet/example/image-classification/<span class="hljs-transposed_variable" style="box-sizing: border-box;">train_mnist.</span>py</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
如果之前配置过GPU(如何配置可以参考官方文档),可以尝试用GPU测试一下网络:
<code class="language-matlab hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">python mxnet/example/image-classification/<span class="hljs-transposed_variable" style="box-sizing: border-box;">train_mnist.</span>py --network lenet --gpus <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
注意:上述过程完成后,直接在python中执行以下代码(如 import mxnet as mx)可能会提示“no module named ”,这可能是因为未添加路径导致的。可以通过单步依次执行下面代码解决:
<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// ~表示我的mxnet安装在home下面</span> export PYTHONPATH=~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/mxnet/</span>python cp -r ~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/mxnet/</span>python/mxnet . cp ~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/mxnet/</span>lib/libmxnet.so mxnet/</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
2- 如何将训练图片转换格式并送入网络
就好像在caffe中训练模型之前要现将训练图片转化为leveldb或者LMDB格式数据一样,mxnet在训练之前也要先将图片转化为REC格式的数据。
这里直接记录转换过程,后续会详细讲解REC数据格式的编码以及最终如何将REC格式数据再加载到网络当中。
linux下转换使用的工具是mxnet/bin下名为“im2rec”的可执行文件,该文件的源码可以查看mxnet/tools/im2rec.cc
首先,来说明以下im2rec这个转换工具有哪些输入参数:
首先是必须输入的3个参数,依次是
<code class="hljs http has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">image_lst</span>: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> //保存图片和标签信息的文本的名字</span> <span class="cs" style="box-sizing: border-box;">image_root_dir:<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//图片的存放路径</span> output_rec:<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//转换后的.rec格式文件的存放路径</span> </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
可选的输入参数有:
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> label_width:<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//标签个数,默认为1,即每个图片只有一个标签</span> resize: <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//默认为-1,即不调整图片尺寸。否则将图片的较短边的长度调整为resize大小,另一边按比例调整</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%%</span>例如图片尺寸为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*200</span>,resize=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>,那么调整之后图片尺寸为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*100</span>. quality: <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//JPEG编码质量,默认为80。输入范围为1-100.(mxnet转换数据时是编码存储的)</span> center_crop: <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//默认为0,if(center_crop),则将图片以中心裁剪为正方形。注意:只有if(resize>0)成立时,该参数才有效。</span> nsplit:<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//默认为1,用于将部分图片进行转换,按照位置将image_lst中的图片分成nsplit个part</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%%</span>注意,这里只是大概平分,例如<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>个训练图片,nsplit=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,实际分块为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,而不是预想中的<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4.</span>(关于这一点我也还没搞明白) part: <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//默认为0,用于将部分图片进行转换。和nsplit结合使用,可以只转换nsplit中的第part个分块</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>
这里举一个实际应用的例子
(1)首先是保存图片和标签信息的文本,这里我的文件是train.txt.
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">编号 标签 图片名 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> FY2D1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bmp</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> FY2E1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bmp</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> FY2D2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bmp</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> FY2E2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bmp</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
(2)为了方便运行和调整参数,这里写了一个类似于windows批处理命令一样的可执行文件,来调用im2rec。这里我的可执行文件名为convert.sh,训练图片存储于data文件夹,转换后的结果保存到当前文件夹下,命名为train.rec
<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-shebang" style="color: rgb(0, 102, 102); box-sizing: border-box;">#!/bin/sh</span> /home/zfan/mxnet/bin/im2rec train.txt data/ train.rec quality=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">90</span> resize=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
(3)打开终端,cd到convert.sh所在文件夹,输入 ./convert.sh 回车。运行提示如下:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">44</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span>] tools/im2rec<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.cc</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">54</span>: New Image Size: Short Edge <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span> [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">44</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span>] tools/im2rec<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.cc</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">76</span>: Write to output: train<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.rec</span> [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">44</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span>] tools/im2rec<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.cc</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">78</span>: Output: train<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.rec</span> [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">44</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span>] tools/im2rec<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.cc</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span>: JPEG encoding quality: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">90</span> [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">44</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span>] tools/im2rec<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.cc</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">160</span>: Total: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> images processed, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.166033</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sec</span> elapsed </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
迭代器-将转换后的数据送入网络:
总体来说,开始一项训练,需要3个部分:训练参数、网络模型和训练数据。在上一小节,我已经详细介绍了如何将原始图片数据转化为mxnet可以接受的REC格式数据。但是,实际训练的时候我们还需要一个工具来将REC数据送入我们的训练网络中。这个工具就是“迭代器”-iterator
mxnet为我们提供了快速构建迭代器的函数ImageRecordIter,该函数位于python/mxnet/io.py中,其返回一个迭代器对象。或者可以在python命令窗中键入以下命令来获取io函数的位置以及说明:
<code class="language-python hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> mxnet <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">as</span> mx help(mx.io) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#或者输入help(mx.io.ImageRecordIter)来直接查看迭代器的参数说明</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
接下来说明ImageRecordIter的几个常用参数:(更多的参数可以通过上面的help查看)
<code class="language-matlab hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> path_imgrec:rec图像数据的存储路径;string型数据;默认为<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'./data/imgrec.rec'</span> data_shape:迭代器产生的每一个实例的形状,即图片数据的形状;元组型数据;必选参数 mean_img:图像均值文件的存放路径;string型数据;默认为空 batch_size:batch的大小;int型数据;必选参数 shuffle:是否打乱数据;布尔型数据;默认为False; mirror:是否对图片执行镜像操作;布尔型数据;默认为False; rand_mirror:是否随机地对图片执行镜像操作;布尔型数据;默认为False; rand_crop:是否随机地对图片执行剪切操作;布尔型数据;默认为False; label_width:图片的标签个数;int型数据;默认为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> mean_r、 mean_g、 mean_b:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>个颜色通道的均值;float型数据;默认为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> num_parts:将训练数据分为几份;int型数据;默认为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> part_index:第几部分数据将会被读取;int型数据;默认为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul>
接下来以实例来说明怎样构建迭代器,该例子位于example/image-classification/train_imagenet.py中
<code class="language-python hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#分别为训练数据和验证数据各构建一个迭代器</span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">def</span> <span class="hljs-title" style="box-sizing: border-box;">get_iterator</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(args, kv)</span>:</span> train = mx.io.ImageRecordIter( path_imgrec = args.data_dir + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"train.rec"</span>, mean_img = args.data_dir + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mean.bin"</span>, data_shape = data_shape, batch_size = args.batch_size, rand_crop = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">True</span>, rand_mirror = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">True</span>, num_parts = kv.num_workers, part_index = kv.rank) val = mx.io.ImageRecordIter( path_imgrec = args.data_dir + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"val.rec"</span>, mean_img = args.data_dir + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mean.bin"</span>, rand_crop = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">False</span>, rand_mirror = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">False</span>, data_shape = data_shape, batch_size = args.batch_size, num_parts = kv.num_workers, part_index = kv.rank) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> (train, val)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul>
3- 构建一个网络模型
一个网络模型是一层一层连接器来的,因此想要构建一个网络模型,首先我们得知道mxnet中都有哪些可以用的层。(以下按照使用频率来排序)
下面介绍一些比较常用的层,更多的层大家可以在python命令窗执行以下代码来查询
<code class="hljs haskell has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-import" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> mxnet <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">as</span> mx</span> <span class="hljs-title" style="box-sizing: border-box;">help</span>(mx.symbol)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
(1)Activation:激活函数
<code class="language-matlab hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> data: 输入数据,symbol型变量 act_type: <span class="hljs-cell" style="box-sizing: border-box;">{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'relu'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'sigmoid'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tanh'</span>}</span>, 可选的<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>种激活函数</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
(2)Convolution:卷积层
<code class="language-matlab hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">data:输入数据,symbol型变量 weight:权重矩阵,symbol型变量 bias:偏置,symbol型变量 kernal:卷积核的尺寸(y,x),二维tuple型变量 stride:卷积步长(y,x),二维tuple型变量,默认为(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>) pad:卷积的填补量(y,x),二维tuple型变量,默认为(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) num_filter :卷积滤波器(通道)个数,int型变量 no_bias :是否取消偏置,布尔型变量,默认为False num_group:groups划分的个数,int型变量,默认为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.</span>该选项不被CUDNN所支持, you can use SliceChannel to num_group,apply convolution and concat instead to achieve the same <span class="hljs-transposed_variable" style="box-sizing: border-box;">need.</span> workspace:卷积的Tmp工作空间的大小MB。long型变量,默认为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">512</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
(3)Deconvolution:反卷积层
<code class="hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">参数同卷积层一致</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
(4)Dropout:
<code class="language-matlab hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">data:输入数据,symbol型变量 p : 训练时对输入数据进行dropped out的比例,float型变量,默认为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.5</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
(5)Flatten:将N维数据变为1维
<code class="language-matlab hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">data:输入数据,symbol型变量 name : string, <span class="hljs-transposed_variable" style="box-sizing: border-box;">optional.</span> Name of the resulting <span class="hljs-transposed_variable" style="box-sizing: border-box;">symbol.</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
(6)FullyConnected:全连接层
<code class="language-matlab hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">data:输入数据,symbol型变量 weight:权重矩阵,symbol型变量 bias:偏置,symbol型变量 no_bias :是否取消偏置,布尔型变量,默认为False num_hidden:输出隐藏节点的个数,int型变量</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
(7)LRN:
<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">data:输入数据,symbol型变量 alpha : float, optional, default=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.0001</span>.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">value</span> <span class="hljs-operator" style="box-sizing: border-box;">of</span> <span class="hljs-operator" style="box-sizing: border-box;">the</span> alpha variance scaling parameter <span class="hljs-operator" style="box-sizing: border-box;">in</span> <span class="hljs-operator" style="box-sizing: border-box;">the</span> normalization formula beta : float, optional, default=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.75</span>.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">value</span> <span class="hljs-operator" style="box-sizing: border-box;">of</span> <span class="hljs-operator" style="box-sizing: border-box;">the</span> beta power parameter <span class="hljs-operator" style="box-sizing: border-box;">in</span> <span class="hljs-operator" style="box-sizing: border-box;">the</span> normalization formula knorm : float, optional, default=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">value</span> <span class="hljs-operator" style="box-sizing: border-box;">of</span> <span class="hljs-operator" style="box-sizing: border-box;">the</span> k parameter <span class="hljs-operator" style="box-sizing: border-box;">in</span> normalization formula nsize : int (non-negative), required.normalization window width <span class="hljs-operator" style="box-sizing: border-box;">in</span> elements.</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
(8)Pooling
<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">data:输入数据,symbol型变量 kernal:pooling核的尺寸(y,x),二维tuple型变量 stride:pooling步长(y,x),二维tuple型变量,默认为(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>) pad:pooling的填补量(y,x),二维tuple型变量,默认为(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) pool_<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">type</span> : pooling的类型,可选的<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>种{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'avg'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'max'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'sum'</span>}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
(9)SoftmaxOutput:使用logloss进行BP
<code class="language-python hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">data:输入数据,symbol型变量 grad_scale : 为梯度乘以一个比例系数,float型变量,默认为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> multi_output : 布尔型变量,默认为<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">False</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># If set to true, for a (n,k,x_1,..,x_n) dimensionalinput tensor, softmax will generate n*x_1*...*x_n output, eachhas k classes</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
(10)Variable:定义一个symbol型变量
<code class="language-matlab hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">name : 变量的名字,str型变量 attr : dict of string -> <span class="hljs-transposed_variable" style="box-sizing: border-box;">string.</span>Additional attributes to set on the <span class="hljs-transposed_variable" style="box-sizing: border-box;">variable.</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
mxnet/example/image-classification文件夹下以symbol开头的几个.py文件都是定义好的网络模型。我们可以任意打开一个来具体了解一下如何构建一个网络。
有一点需要注意的是:mxnet中通常要在卷积层和全连接层之间价加一个Flatten层用于衔接
4- 开始训练
首先,来总结一下我们现在都有了什么:保存网络模型的.py文件、REC格式的训练数据、将数据送入网络的迭代器。
在此基础上,我们缺少的就只剩下网络训练的一些控制参数:如学习率、训练日志、GPU选择等等。
在初期,我们往往可以先快速的拿来用,而不用在意其细节。那么,最简单的办法就是直接修改套用mxnet中的示例。示例主要集中在/example/image-classification和/tests/python当中。下面就简单的介绍如何快速开始训练:
(1)新建一个文件夹,将/example/image-classification文件夹下find_mxnet.py、symbol_alexnet.py、train_imagenet.py和train_model.py这4个文件copy进来;
(2)保持find_mxnet.py文件不变,在symbol_alexnet.py中按照里面Alexnet网络的构建方法来建立自己的网络。最后最好再把文件名改为自己的网络名,如mynet.py;
(3)将train_imagenet.py最好也修改一下名字,如改为train_xx.py.将第40行代码中的’symbol’删除,否则你的网络文件就必须以‘symbol’开始命名,如symbol_mynet.py.
<code class="hljs dos has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-winutils" style="color: rgb(0, 0, 136); box-sizing: border-box;">net</span> = importlib.import_module('symbol_' + args.network).get_symbol(args.num_classes) 改为: <span class="hljs-winutils" style="color: rgb(0, 0, 136); box-sizing: border-box;">net</span> = importlib.import_module(args.network).get_symbol(args.num_classes)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
剩下的就是要在parser.add_argument中修改默认值了,否则你在调用mynet.py就必须准确的指定每一个参数。
(4)写一个脚本文件,运行脚本文件就可以开始训练了。如
<code class="language-python hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">python train_xx.py --data-dir=xx --model-prefix==xx --gpus=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 如果想要在每次epoch结束时保存一个模型,那么model-prefix就不能为空</span></code>