深度学习之MAC(cpu-only)下利用anaconda安装caffe

转载自http://blog.youkuaiyun.com/ddreaming/article/details/52739893

如何在Mac下安装caffe
在安装caffe之前,应该了解些计算机的基本知识,以及Linux的基本知识,这是因为Linux和Mac的操作系统非常像。比如什么是Linux,它是怎么来的?Linux和GNU啥关系?gcc又是啥。。Linux的目录配置是咋回事,也就是下图中每个文件都存啥知道不?vim是啥,咋用?shell脚本文件是啥?bash和他啥关系?什么是系统的环境变量?怎么设置环境变量?在mac下怎么通过Homebrew下载软件?如果这些你都很清楚的话,那么直接看第二部分的caffe安装步骤就好。如果你跟我一样,对这些概念稀里糊涂,不太明白,那么建议你在安装caffe之前,用一到两天时间补一补这些基本知识,否则你在安装时候只能是一头雾水。。。关于Linux的基本知识,可以参考鸟哥的linux私房菜,写的非常通俗易懂。我也总结了些对于安装caffe直接相关的概念,大家可以参考第一部分的关于linux的介绍。
一、安装caffe步骤

我的电脑配置如下:
MacBook Pro -OS X EI Captian系统-8G内存-Intel Iris Graphics 6100 1536 MB显卡。
由于电脑配置的不是英伟达显卡,所以不能使用CUDA加速了,只能安装个CPU模式玩一玩。下面是安装的想象步骤。

1.安装caffe需要的依赖包。
opencv2.4:
Anaconda Python 2.7:
snappy:
leveled:
flags:
blog:
ship:
lmdv:
protobuf:
boost:
哈哈,看着很多吧,但是不要怕,下载这些东东,在Mac终端里,只需要几个brew指令而已。所以,如果你电脑里没有Homebrew,那么你要先把Homebrew下载下来。然后我们下面要做的,就是将这些依赖包都下载下来,还需要配置些环境变量。
1.我们先下载Anaconda Python 2.7。下载下来的是个Anaconda2-4.2.0-MacOSX-x86_64.sh文件,我们将这个文件复制到/usr/local/Cellar下面(之所以这么做,是因为使用brew指令下载的东东都会默认存到则个路径,我为了方便,就将所有caffe需要的包都放在这里,完全个人喜好~)。也就是下图中带红点的文件。下面我进行Anaconda的安装。在命令行中输入下面指令
  1. bash Anaconda2-4.2.0-MacOSX-x86_64.sh   
bash Anaconda2-4.2.0-MacOSX-x86_64.sh 
执行完成后会生成下图中绿色标注的anaconda2文件。



之后我们设置下环境变量,就是将安装的ananconda2的bin文件的路径添加到PATH中,再将anaconda2的路径添加到DYLD_FALLBACK_LIBARY_PATH中去,指向下面两个命令后第一步就完成了(ps:可以使用set指令查看是否添加成功)。
  1. <pre name=“code” class=“html”>export PATH=/usr/local/ananconda2/bin:PATH&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>export&nbsp;<span class="attribute">DYLD_FALLBACK_LIBRARY_PATH</span><span>=/usr/local/Cellar/anaconda2:/usr/local/lib:/usr/lib&nbsp;&nbsp;</span></span></li></ol></div><pre code_snippet_id="1914013" snippet_file_name="blog_20161005_2_5155753" name="code" class="html" style="display: none;">&lt;pre name="code" class="html"&gt;export PATH=/usr/local/ananconda2/bin:PATH export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/Cellar/anaconda2:/usr/local/lib:/usr/lib
     2.下面进入批量安装的阶段。
安装snappy,leveled,flags,blog,ship,lmdv,opencv..
  1. brew install –fresh -vd snappy leveldb gflags glog szip lmdb homebrew/science/opencv
  
brew install --fresh -vd snappy leveldb gflags glog szip lmdb homebrew/science/opencv

安装protobuf以及boost
  1. brew install –build-from-source –with-python –fresh -vd protobuf  
  2. 
brew install –build-from-source –fresh -vd boost boost-python
  
brew install --build-from-source --with-python --fresh -vd protobuf

brew install --build-from-source --fresh -vd boost boost-python

执行完这些指令,要保证这些文件都出现在/usr/local/Cellar中。如果缺失,那么就用brew指令单独下载。
3.下载caffe源码
  1. git clone https://github.com/BVLC/caffe.git  
git clone https://github.com/BVLC/caffe.git
跟前面一样,我把下载的caffe文件夹复制到了/usr/local/Cellar路径下面。
4.下面我们要写一个Makefile.config文件,这个文件用来生成makefile文件的。在caffe包中给了一个例子,叫做Makefile.config.example,我们就是要根据自己的电脑的情况,重新写一个Makefile.config,所谓重新写,也就是在Makefile.config.example的基础上,删减些注释、改改路径而已。所以就直接复制Makefile.config.example,然后粘贴生成Makefile.config文件,用下面指令可以实现。
  1. cd /usr/local/Cellar/caffe  
  2. cp Makefile.config.example Makefile.config  
cd /usr/local/Cellar/caffe
cp Makefile.config.example Makefile.config
5.编译安装caffe要用到我们前几步下载的依赖包,那么每个电脑的情况不一样,这些依赖包的路径也就不一样。所以我们要让caffe找到咱们自己下载的那些依赖包。那么我们怎么告诉caffe呢?这就是靠Makefile.config文件啦,系统会根据这个文件生成Makefile文件,然后当我们执行make指令时,系统就会安装Makefile里面的步骤执行编译安装动作。所以,我们要进入Makefile.config文件夹,按照下面修改:

  1. <span style=“font-size:18px;”>  
  2. MacBook-Pro:caffe wei&nbsp;vim&nbsp;Makefile.config&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>#&nbsp;NOTE:&nbsp;this&nbsp;is&nbsp;required&nbsp;only&nbsp;if&nbsp;you&nbsp;will&nbsp;compile&nbsp;the&nbsp;python&nbsp;interface.&nbsp;&nbsp;</span></li><li class="alt"><span>#&nbsp;We&nbsp;need&nbsp;to&nbsp;be&nbsp;able&nbsp;to&nbsp;find&nbsp;Python.h&nbsp;and&nbsp;numpy/arrayobject.h.&nbsp;&nbsp;</span></li><li class=""><span>PYTHON_INCLUDE&nbsp;<span class="attribute">:</span><span>=&nbsp;/usr/include/python2.7&nbsp;\&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/usr/lib/python2.7/dist-packages/numpy/core/include&nbsp;&nbsp;</span></li><li class=""><span>#&nbsp;Anaconda&nbsp;Python&nbsp;distribution&nbsp;is&nbsp;quite&nbsp;popular.&nbsp;Include&nbsp;path:&nbsp;&nbsp;</span></li><li class="alt"><span>#&nbsp;Verify&nbsp;anaconda&nbsp;location,&nbsp;sometimes&nbsp;it's&nbsp;in&nbsp;root.&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">span</span><span>&nbsp;</span><span class="attribute">style</span><span>=</span><span class="attribute-value">"color:#ff0000;"</span><span class="tag">&gt;</span><span>ANACONDA_HOME&nbsp;</span><span class="attribute">:</span><span>=&nbsp;/usr/local/Cellar/anaconda2&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;PYTHON_INCLUDE&nbsp;<span class="attribute">:</span><span>=&nbsp;(ANACONDA_HOME)/include \  
  3.                   (ANACONDA_HOME)/include/python2.7&nbsp;\&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \  
  4. </span>  
  5. # Uncomment to use Python 3 (default is Python 2)  
  6. # PYTHON_LIBRARIES :boost_python3 python3.5m  
  7. # PYTHON_INCLUDE := /usr/include/python3.5m \  
  8. #                 /usr/lib/python3.5/dist-packages/numpy/core/include  
  9.   
  10. # We need to be able to find libpythonX.X.so or .dylib.  
  11. <span style=“color:#ff0000;”>#PYTHON_LIB := /usr/lib  
  12.   PYTHON_LIB :(ANACONDA_HOME)/lib&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="tag">&lt;/</span><span class="tag-name">span</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>#&nbsp;Homebrew&nbsp;installs&nbsp;numpy&nbsp;in&nbsp;a&nbsp;non&nbsp;standard&nbsp;path&nbsp;(keg&nbsp;only)&nbsp;&nbsp;</span></li><li class="alt"><span>#&nbsp;PYTHON_INCLUDE&nbsp;+=&nbsp;(dir (shell&nbsp;python&nbsp;-c&nbsp;'import&nbsp;numpy.core;&nbsp;print(numpy.core.__file__)'))/include&nbsp;&nbsp;</span></li><li class=""><span>#&nbsp;PYTHON_LIB&nbsp;+=&nbsp;(shell brew –prefix numpy)/lib  
  13.   
  14. # Uncomment to support layers written in Python (will link against Python libs)  
  15. # WITH_PYTHON_LAYER :1  
  16.   
  17. # Whatever else you find you need goes here.  
  18. <span style=“color:#ff0000;”>INCLUDE_DIRS :(PYTHON_INCLUDE)&nbsp;/usr/local/include&nbsp;&nbsp;</span></span></li><li class="alt"><span>LIBRARY_DIRS&nbsp;<span class="attribute">:</span><span>=&nbsp;(PYTHON_LIB) /usr/local/lib /usr/lib</span>  
  19.   
  20. # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies  
  21. # INCLUDE_DIRS += (shell&nbsp;brew&nbsp;--prefix)/include&nbsp;&nbsp;</span></li><li class="alt"><span>#&nbsp;LIBRARY_DIRS&nbsp;+=&nbsp;(shell brew –prefix)/lib  
  22.   
  23. # Uncomment to use pkg-config to specify OpenCV library paths.  
  24. # (Usually not necessary – OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)  
  25. # USE_PKG_CONFIG :1  
  26.   
  27. # N.B. both build and distribute dirs are cleared on make&nbsp;clean  
  28. BUILD_DIR :build  
  29. DISTRIBUTE_DIR :distribute  
  30.   
  31. # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 &nbsp;
  32. # DEBUG :1  
  33.   
  34. # The ID of the GPU that ‘make runtest’ will use to run unit tests.  
  35. TEST_GPUID :0  
  36.   
  37. # enable pretty build (comment to see full commands)  
  38. Q ?= @  
  39. </span>  
<span style=”font-size:18px;”> 
MacBook-Pro:caffe wei$ vim Makefile.config

# NOTE: this is required only if you will compile the python interface. # We need to be able to find Python.h and numpy/arrayobject.h. PYTHON_INCLUDE := /usr/include/python2.7 \ /usr/lib/python2.7/dist-packages/numpy/core/include # Anaconda Python distribution is quite popular. Include path: # Verify anaconda location, sometimes it's in root. <span style="color:#ff0000;">ANACONDA_HOME := /usr/local/Cellar/anaconda2 PYTHON_INCLUDE := (ANACONDAHOME)/include  (ANACONDA_HOME)/include/python2.7 \ $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \ </span> # Uncomment to use Python 3 (default is Python 2) # PYTHON_LIBRARIES := boost_python3 python3.5m # PYTHON_INCLUDE := /usr/include/python3.5m \ # /usr/lib/python3.5/dist-packages/numpy/core/include # We need to be able to find libpythonX.X.so or .dylib. <span style="color:#ff0000;">#PYTHON_LIB := /usr/lib PYTHON_LIB := $(ANACONDA_HOME)/lib </span> # Homebrew installs numpy in a non standard path (keg only) # PYTHON_INCLUDE += (dir (shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include # PYTHON_LIB += $(shell brew --prefix numpy)/lib # Uncomment to support layers written in Python (will link against Python libs) # WITH_PYTHON_LAYER := 1 # Whatever else you find you need goes here. <span style="color:#ff0000;">INCLUDE_DIRS := (PYTHONINCLUDE)/usr/local/includeLIBRARYDIRS:= (PYTHON_LIB) /usr/local/lib /usr/lib</span> # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies # INCLUDE_DIRS += $(shell brew --prefix)/include # LIBRARY_DIRS += $(shell brew --prefix)/lib # Uncomment to use `pkg-config` to specify OpenCV library paths. # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.) # USE_PKG_CONFIG := 1 # N.B. both build and distribute dirs are cleared on `make clean` BUILD_DIR := build DISTRIBUTE_DIR := distribute # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 # DEBUG := 1 # The ID of the GPU that 'make runtest' will use to run unit tests. TEST_GPUID := 0 # enable pretty build (comment to see full commands) Q ?= @ </span> 要特别注意上面红色标注的路径,这是些路径就只想了我们自己下载的那些工具包,直接影响成败啊,其实主要是指向了anaconda
更改完成后,保存退出(先按esc,在输入冒号:,在输入wq  ‘esc’+’:’+’wq’)

6.然后就是make啦,让电脑自动编译安装吧
  1. make all  
make all
这步应该没啥问题。
7.下一步我们要安装caffe的python接口,也就是要编译下pycaffe。执行下面指令:
  1. for req in (cat&nbsp;python/requirements.txt);&nbsp;do&nbsp;pip&nbsp;install&nbsp;req; done  
  2. make pycaffe  
  3. make distribute  
for req in $(cat python/requirements.txt); do pip install $req; done
make pycaffe
make distribute
8.设置环境变量PYTHONPATH。执行下面指令
  1. export PYTHONPATH=/usr/local/Cellar/caffe/python  
export PYTHONPATH=/usr/local/Cellar/caffe/python
9.进入python看一看~
  1. cd /usr/local/Cellar/caffe/python  
cd /usr/local/Cellar/caffe/python
  1. python  
python
应该显示如下信息:
  1. Python 2.7.12 |Anaconda 4.2.0 (x86_64)| (default, Jul  2 2016, 17:43:17)   
  2. [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin  
  3. Type “help”, “copyright”, “credits” or “license” for more information.  
  4. Anaconda is brought to you by Continuum Analytics.  
  5. Please check out: http://continuum.io/thanks and https://anaconda.org  
Python 2.7.12 |Anaconda 4.2.0 (x86_64)| (default, Jul  2 2016, 17:43:17) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
如果显示这些,那就说明成功啦。我们就可以跑一个例子看看啦。
10.运行MNIST例子。运行这个例子,我们要要下载数据集啊,执行下面指令
  1. cd /usr/local/Cellar/caffe  
cd /usr/local/Cellar/caffe
  1. ./data/mnist/get_mnist.sh  
  2. ./examples/mnist/create_mnist.sh  
./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh
ps,执行上面两条指令,需要mac电脑里有 wget ,如果没有就会报错,说找不到这个指令,我们可以brew下载
  1. <span style=“color:#ff0000;”>brew install wget</span>  
<span style="color:#ff0000;">brew install wget</span>
下载完成后,如果成功了,会在/usr/local/Cellar/caffe/examples/mnist下生成两个文件夹,下图用红点标注了,这是数据生成的lmdb数据集。


ps:当喔执行

  1. ./examples/mnist/create_mnist.sh  
./examples/mnist/create_mnist.sh
指令时,出了点问题,提示我找不到hdf5开头的一个动态链接库。安装相关说明,hdf5是包含在anaconda里面的,而且我在anaconda的lib中也发现了hdf5的动态链接库,如下图:


但是不知道为什么找不到,所以我又用brew下载了一个hdf5,指令如下:
  1. brew install hdf5  
brew install hdf5
然后将anaconda文件夹lib中hdf5开头的链接库都粘贴到下面hdf5文件夹中的lib里面。




这样做之后,就没问题了。希望小伙伴不要遇到这个问题。

11.起飞前最后准备。修改成CPU模式。通过vim进入./examples/mnist/lenet_solver.prototxt。
然后将solver_mode改成CPU模式
  1. <span style=“font-size:18px;”>solver_mode: CPU</span>  
<span style="font-size:18px;">solver_mode: CPU</span>

然后,可以飞起来了
  1. cd /usr/local/Cellar/caffe/  
  2. ./examples/mnist/train_lenet.sh  
cd /usr/local/Cellar/caffe/
./examples/mnist/train_lenet.sh

12.运行结果如下




参考资源:
http://www.megastormsystems.com/news/how-to-install-caffe-on-mac-os-x-10-11
http://caffe.berkeleyvision.org/install_osx.html
http://blog.youkuaiyun.com/lkj345/article/details/51260811












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值