1. 程式人生 > >深度學習之MAC(cpu-only)下利用anaconda安裝caffe

深度學習之MAC(cpu-only)下利用anaconda安裝caffe

如何在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的安裝。在命令列中輸入下面指令
[html] view plain copy print?
  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指令檢視是否新增成功) [html] view plain copy print?
  1. <prename=“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.. [html] view plain copy print?
  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 [html] view plain copy print?
  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原始碼 [html] view plain copy print?
  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檔案,用下面指令可以實現。 [html] view plain copy print?
  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資料夾,按照下面修改: [html] view plain copy print?
  1. <spanstyle=“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. # We need to be able to find libpythonX.X.so or .dylib.  
  10. <spanstyle=“color:#ff0000;”>#PYTHON_LIB := /usr/lib  
  11.   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  
  12. # Uncomment to support layers written in Python (will link against Python libs)  
  13. # WITH_PYTHON_LAYER :1
  14. # Whatever else you find you need goes here.  
  15. <spanstyle=“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>
  16. # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies  
  17. # 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  
  18. # Uncomment to use pkg-config to specify OpenCV library paths.  
  19. # (Usually not necessary – OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)  
  20. # USE_PKG_CONFIG :1
  21. # N.B. both build and distribute dirs are cleared on make&nbsp;clean  
  22. BUILD_DIR :build
  23. DISTRIBUTE_DIR :distribute
  24. # DEBUG :1
  25. # The ID of the GPU that ‘make runtest’ will use to run unit tests.  
  26. TEST_GPUID :0
  27. # enable pretty build (comment to see full commands)  
  28. Q ?= @  
  29. </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啦,讓電腦自動編譯安裝吧 [html] view plain copy print?
  1. make all  
make all
這步應該沒啥問題。 7.下一步我們要安裝caffe的python介面,也就是要編譯下pycaffe。執行下面指令: [html] view plain copy print?
  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。執行下面指令 [html] view plain copy print?
  1. export PYTHONPATH=/usr/local/Cellar/caffe/python  
export PYTHONPATH=/usr/local/Cellar/caffe/python
9.進入python看一看~ [html] view plain copy print?
  1. cd /usr/local/Cellar/caffe/python  
cd /usr/local/Cellar/caffe/python
[html] view plain copy print?
  1. python  
python
應該顯示如下資訊: [html] view plain copy print?
  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例子。執行這個例子,我們要要下載資料集啊,執行下面指令 [html] view plain copy print?
  1. cd /usr/local/Cellar/caffe  
cd /usr/local/Cellar/caffe
[html] view plain copy print?
  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下載 [html] view plain copy print?
  1. <spanstyle=“color:#ff0000;”>brew install wget</span>
<span style="color:#ff0000;">brew install wget</span>
下載完成後,如果成功了,會在/usr/local/Cellar/caffe/examples/mnist下生成兩個資料夾,下圖用紅點標註了,這是資料生成的lmdb資料集。


ps:當喔執行

[html] view plain copy print?
  1. ./examples/mnist/create_mnist.sh  
./examples/mnist/create_mnist.sh
指令時,出了點問題,提示我找不到hdf5開頭的一個動態連結庫。安裝相關說明,hdf5是包含在anaconda裡面的,而且我在anaconda的lib中也發現了hdf5的動態連結庫,如下圖:

但是不知道為什麼找不到,所以我又用brew下載了一個hdf5,指令如下: [html] view plain copy print?
  1. brew install hdf5  
brew install hdf5
然後將anaconda資料夾lib中hdf5開頭的連結庫都貼上到下面hdf5資料夾中的lib裡面。

這樣做之後,就沒問題了。希望小夥伴不要遇到這個問題。 11.起飛前最後準備。修改成CPU模式。通過vim進入./examples/mnist/lenet_solver.prototxt。 然後將solver_mode改成CPU模式[html] view plain copy print?
  1. <spanstyle=“font-size:18px;”>solver_mode: CPU</span>
<span style="font-size:18px;">solver_mode: CPU</span>

然後,可以飛起來了 [html] view plain copy print?
  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.csdn.net/lkj345/article/details/51260811