深度學習之MAC(cpu-only)下利用anaconda安裝caffe
阿新 • • 發佈:2019-01-25
如何在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?
之後我們設定下環境變數,就是將安裝的ananconda2的bin檔案的路徑新增到PATH中,再將anaconda2的路徑新增到DYLD_FALLBACK_LIBARY_PATH中去,指向下面兩個命令後第一步就完成了(ps:可以使用set指令檢視是否新增成功)。 [html] view plain copy print?
3.下載caffe原始碼 [html] view plain copy print?(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?
但是不知道為什麼找不到,所以我又用brew下載了一個hdf5,指令如下: [html] view plain copy print?
這樣做之後,就沒問題了。希望小夥伴不要遇到這個問題。 11.起飛前最後準備。修改成CPU模式。通過vim進入./examples/mnist/lenet_solver.prototxt。 然後將solver_mode改成CPU模式[html] view plain copy print?
然後,可以飛起來了 [html] view plain copy print?
參考資源: 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
boost: 哈哈,看著很多吧,但是不要怕,下載這些東東,在Mac終端裡,只需要幾個brew指令而已。所以,如果你電腦裡沒有Homebrew,那麼你要先把Homebrew下載下來。然後我們下面要做的,就是將這些依賴包都下載下來,還需要配置些環境變數。
1.我們先下載Anaconda Python 2.7。下載下來的是個Anaconda2-4.2.0-MacOSX-x86_64.sh檔案,我們將這個檔案複製到/usr/local/Cellar下面(之所以這麼做,是因為使用brew指令下載的東東都會預設存到則個路徑,我為了方便,就將所有caffe需要的包都放在這裡,完全個人喜好~)。也就是下圖中帶紅點的檔案。下面我進行Anaconda的安裝。在命令列中輸入下面指令
- 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?
- <prename=“code”class=“html”>export PATH=/usr/local/ananconda2/bin:PATH </span></span></li><li class=""><span>export <span class="attribute">DYLD_FALLBACK_LIBRARY_PATH</span><span>=/usr/local/Cellar/anaconda2:/usr/local/lib:/usr/lib </span></span></li></ol></div><pre code_snippet_id="1914013" snippet_file_name="blog_20161005_2_5155753" name="code" class="html" style="display: none;"><pre name="code" class="html">export PATH=/usr/local/ananconda2/bin:PATH export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/Cellar/anaconda2:/usr/local/lib:/usr/lib 2.下面進入批量安裝的階段。
- 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?- brew install –build-from-source –with-python –fresh -vd protobuf
- 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?
- 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?- cd /usr/local/Cellar/caffe
- 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?- <spanstyle=“font-size:18px;”>
- MacBook-Pro:caffe wei vim Makefile.config </span></li><li class="alt"><span> </span></li><li class=""><span># NOTE: this is required only if you will compile the python interface. </span></li><li class="alt"><span># We need to be able to find Python.h and numpy/arrayobject.h. </span></li><li class=""><span>PYTHON_INCLUDE <span class="attribute">:</span><span>= /usr/include/python2.7 \&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;nbsp; </span></span></li><li class="alt"><span> /usr/lib/python2.7/dist-packages/numpy/core/include </span></li><li class=""><span># Anaconda Python distribution is quite popular. Include path: </span></li><li class="alt"><span># Verify anaconda location, sometimes it's in root. </span></li><li class=""><span> <span class="tag"><</span><span class="tag-name">span</span><span> </span><span class="attribute">style</span><span>=</span><span class="attribute-value">"color:#ff0000;"</span><span class="tag">></span><span>ANACONDA_HOME </span><span class="attribute">:</span><span>= /usr/local/Cellar/anaconda2 </span></span></li><li class="alt"><span> PYTHON_INCLUDE <span class="attribute">:</span><span>= (ANACONDA_HOME)/include \
- (ANACONDA_HOME)/include/python2.7 \&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;nbsp; </span></li><li class="alt"><span> (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.
- <spanstyle=“color:#ff0000;”>#PYTHON_LIB := /usr/lib
- PYTHON_LIB := (ANACONDA_HOME)/lib </span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">span</span><span class="tag">></span><span> </span></span></li><li class=""><span># Homebrew installs numpy in a non standard path (keg only) </span></li><li class="alt"><span># PYTHON_INCLUDE += (dir (shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include </span></li><li class=""><span># 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.
- <spanstyle=“color:#ff0000;”>INCLUDE_DIRS := (PYTHON_INCLUDE) /usr/local/include </span></span></li><li class="alt"><span>LIBRARY_DIRS <span class="attribute">:</span><span>= (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 </span></li><li class="alt"><span># 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
- # 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>
<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 := 更改完成後,儲存退出(先按esc,在輸入冒號:,在輸入wq ‘esc’+’:’+’wq’) 6.然後就是make啦,讓電腦自動編譯安裝吧 [html] view plain copy print?
- make all
make all
這步應該沒啥問題。
7.下一步我們要安裝caffe的python介面,也就是要編譯下pycaffe。執行下面指令:
[html] view plain copy print?- for req in (cat python/requirements.txt); do pip install req; done
- make pycaffe
- 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?- export PYTHONPATH=/usr/local/Cellar/caffe/python
export PYTHONPATH=/usr/local/Cellar/caffe/python
9.進入python看一看~
[html] view plain copy print?- cd /usr/local/Cellar/caffe/python
cd /usr/local/Cellar/caffe/python
[html] view plain copy print?- python
python
應該顯示如下資訊:
[html] view plain copy print?- 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
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?- cd /usr/local/Cellar/caffe
cd /usr/local/Cellar/caffe
[html] view plain copy print?- ./data/mnist/get_mnist.sh
- ./examples/mnist/create_mnist.sh
./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh
ps,執行上面兩條指令,需要mac電腦裡有wget
,如果沒有就會報錯,說找不到這個指令,我們可以brew下載
[html] view plain copy print?- <spanstyle=“color:#ff0000;”>brew install wget</span>
<span style="color:#ff0000;">brew install wget</span>
下載完成後,如果成功了,會在/usr/local/Cellar/caffe/examples/mnist下生成兩個資料夾,下圖用紅點標註了,這是資料生成的lmdb資料集。
ps:當喔執行
- ./examples/mnist/create_mnist.sh
./examples/mnist/create_mnist.sh
指令時,出了點問題,提示我找不到hdf5開頭的一個動態連結庫。安裝相關說明,hdf5是包含在anaconda裡面的,而且我在anaconda的lib中也發現了hdf5的動態連結庫,如下圖:
但是不知道為什麼找不到,所以我又用brew下載了一個hdf5,指令如下: [html] view plain copy print?
- 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?
- <spanstyle=“font-size:18px;”>solver_mode: CPU</span>
<span style="font-size:18px;">solver_mode: CPU</span>
然後,可以飛起來了 [html] view plain copy print?
- cd /usr/local/Cellar/caffe/
- ./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