[Mac] Mac 下安装 mxnet 库和基本使用

安装环境:
Mac OS 10.11.1 Capitan
Xcode 7.1
Python 2.7
mxnet 0.5.0
Homebrew

1、安装依赖库:
0)安装 Homebrew*:
后面的安装基本上都是使用 Homebrew 这个工具,如果你没有这个工具的话可以使用如下命令进行安装:

1)安装 OpenCV:
为了方便依赖库的安装,我们这里使用 homebrew 进行库的安装和管理,有了这一工具后,您只需在命令窗口运行如下命令即可:

这样系统便会自动将相关的库安装好。

2)配置 python 环境:
找到 mxnet 源码下的 python 目录,执行以下命令运行其下的 setup.py 脚本:

如果返回如下结果说明配置成功了:
Finished processing dependencies for mxnet==0.5.0

2、编译步骤:
1)首先复制 make/osxmk 目录到根目录下:

2)打开文件 config.mk ,找到如下一行:

修改为:

附件是我的 config.mk 文件,如果遇到问题可以试试这个:
config.mk

3)在 mxnet 根目录下执行以下命令进行编译:

如果编译没有问题,那么就可以开始使用了。
我们这里采用一个官方自带的手写识别的例子来试验刚才编译安装是否成功了。

3、运行例子:手写识别
很多 mxnet 教程都以这 mnist 个例子开始,不过新版 mxnet 中将这个例子并入了 image-classification 目录,同时好像也没有自动下载数据的脚本了。
我们这里结合新版说下如何运行手写识别的例子,重点是通过这个例子简单跑下 mxnet 的训练和预测:

1)下载手写识别数据库:
网址如下:
http://yann.lecun.com/exdb/mnist/

将四个 gz 文件都存入 example/image-classification/mnist 目录下,并逐个解压缩

如果不能访问上面的地址也可以下载我打包的:
http://pan.baidu.com/s/1Bjir0

这个数据库其实就是类似下面的手写数字图片:
huidutu

这个图片每个大小为28x28。

2)运行训练:
训练 mnist:

如果看到类似以下输出则说明训练成功,并且在测试集取得了 0.971154 的准确率:
2015-11-27 16:44:13,059 Node[0] Epoch[9] Train-accuracy=0.991036
2015-11-27 16:44:13,060 Node[0] Epoch[9] Time cost=0.630
2015-11-27 16:44:13,097 Node[0] Epoch[9] Validation-accuracy=0.971154

类似还有如下一些例子,可参考官网说明研究下:
https://github.com/dmlc/mxnet/tree/master/example/image-classification

保存每个阶段结果:

从第 8 阶段开始继续训练:

更改初始阈值:

使用 GPU0 进行训练:

也可以改成使用多个GPU进行训练,例如: ---gpus 0,1,3

相关问题:
1、错误:Error: The /usr/local directory is not writable.
如果在升级 brew update 时出现此问题:
Error: The /usr/local directory is not writable.
Even if this directory was writable when you installed Homebrew, other
software may change permissions on this directory. Some versions of the
"InstantOn" component of Airfoil are known to do this.

You should probably change the ownership and permissions of /usr/local
back to your user account.
sudo chown -R $(whoami):admin /usr/local
请参照提示执行如下命令:

然后再执行:

一般就可以了。

2、错误:fatal error: 'cblas.h' file not found
如果出现如下错误:
In file included from ./mshadow/mshadow/tensor.h:16:
./mshadow/mshadow/./base.h:120:14: fatal error: 'cblas.h' file not found
#include

请打开文件 config.mk 找到如下一行:

修改为:

3、错误:No module named mxnet
如果出现如下错误:
import mxnet as mx
ImportError: No module named mxnet

说明没有配置好 python 环境,请参照 运行例子 中的 步骤 1)配置 python 环境 进行配置。

4、错误:Please compile with CUDA enabled
出现这个错误是因为系统中没有安装 CUDA 驱动库。请参照安装步骤 2)中的内容进行安装。

5、错误:directory not found for option '-L/usr/local/cuda/lib64'
如果在编译时出现如上错误,原因是默认安装的 cuda 库在 /usr/local/cuda/lib 下边。可以考虑使用如下方式增加一个名为 lib64 的软链接:

6、错误:Library not loaded: @rpath/libcudart.7.5.dylib
出现该错误是因为新版系统中的 sip,如果你也遇到这一问题(我也遇到了)可以参考文章 [4] 中的方法进行解决:

Follow these steps to disable SIP:

Restart your Mac.
Before OS X starts up, hold down Command-R and keep it held down until you see an Apple icon and a progress bar. Release. This boots you into Recovery.
From the Utilities menu, select Terminal.
At the prompt type exactly the following and then press Return: csrutil disable
Terminal should display a message that SIP was disabled.
From the  menu, select Restart.

7、错误:wget: command not found
出现这一错误是因为 Mac 系统默认没有安装 wget 组件,可以使用 homebrew 进行安装,命令如下:

8、错误:No module named cpplint
如果你使用如下命令检查代码风格(如果你不是开发者就不用了):

有可能会遇到这样的错误:
python ../../../dmlc-core/scripts/lint.py mxnet "cpp" ./
Traceback (most recent call last):
File "../../../dmlc-core/scripts/lint.py", line 11, in
import cpplint
ImportError: No module named cpplint

出现这一错误的原因是系统中没有安装 cpplint 和 pylint 库,可以执行如下命令进行安装:

其中 pip 工具需要首先安装 python。如果你没有安装请执行如下命令:

参考文献:
[1] http://www.52cs.org/?p=639
[2] https://github.com/dmlc/mxnet/tree/master/example/image-classification
[3] Installing mxnet for R on Yosemite
[4] https://groups.google.com/forum/#!topic/caffe-users/waugt62RQMU

Add a Comment

您的电子邮箱地址不会被公开。 必填项已用*标注