Ubuntu14.04搭建Caffe(僅CPU)詳解教程
1.安裝依賴
1 sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler2sudo apt-get install --no-install-recommends libboost-all-dev3sudo apt-get2.下載Caffe使用Git直接下載Caffe非常簡單,或者去https://github.com/BVLC/caffe下載。由於我習慣去github上找程式碼,所以就直接去下載的原始碼。
使用git下載caffe到家目錄,下載完成後,會在家目錄下的下載裡找到caffe資料夾,切換到caffe資料夾中。
$ cd ~
$ git clone git://github.com/BVLC/caffe.git$ cd caffe3.編譯Caffe(1)建立Makefile.comfig檔案
cp Makefile.config.example Makefile.config
(2)修改配置檔案Makefile.config
- style="font-size:14px;"># CPU-only switch (uncomment to build without GPU support).
- CPU_ONLY := 1 #一定需要開啟。
- # Uncomment if you're using OpenCV 3
- # OPENCV_VERSION := 3 #用的是2.4版本不需要開啟。
- # To customize your choice of compiler, uncomment and set the following.
- # N.B. the default for Linux is g++ and the default for OSX is clang++
- # CUSTOM_CXX := g++ #已經更新到至少4.8,選擇預設的就好。
- # CUDA directory contains bin/ and lib/ directories that we need. #這是GPU用到的,註釋掉就可以。
- #CUDA_DIR := /usr/local/cuda
- # On Ubuntu 14.04, if cuda tools are installed via
- # "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
- # CUDA_DIR := /usr
- # CUDA architecture setting: going with all of them.
- # For CUDA < 6.0, comment the *_50 lines for compatibility. #這些部分全部註釋,和我們cpu家族沒有關係。
- #CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
- ## -gencode arch=compute_20,code=sm_21 \
- # -gencode arch=compute_30,code=sm_30 \
- # -gencode arch=compute_35,code=sm_35 \
- # -gencode arch=compute_50,code=sm_50 \
- # -gencode arch=compute_50,code=compute_50
- # BLAS choice:
- # atlas for ATLAS (default)
- # mkl for MKL
- # open for OpenBlas
- BLAS := atlas #如果安裝mkl並且設定好路徑的可以試一下mkl,不過一般會出錯,找不到lmkl檔案,建議採用atlas,簡單易行。
- # Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
- # Leave commented to accept the defaults for your choice of BLAS
- # (which should work)!
- # BLAS_INCLUDE := /path/to/your/blas
- # BLAS_LIB := /path/to/your/blas
- # Homebrew puts openblas in a directory that is not on the standard search path
- # BLAS_INCLUDE := $(shell brew --prefix openblas)/include
- # BLAS_LIB := $(shell brew --prefix openblas)/lib
- # This is required only if you will compile the matlab interface. #python很流行,matlab暫時沒安裝,也就用不到了。
- # MATLAB directory should contain the mex binary in /bin.
- # MATLAB_DIR := /usr/local
- # MATLAB_DIR := /Applications/MATLAB_R2012b.app
- # 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 \ #因為下安裝的時候採用的時2.7版本的python,所以這裡開啟2.7版本的,但是要確保python安裝在系統預設路徑下。
- /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.
- # ANACONDA_HOME := $(HOME)/anaconda2
- # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
- $(ANACONDA_HOME)/include/python2.7 \
- $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
- # 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.
- PYTHON_LIB := /usr/lib
- # PYTHON_LIB := $(ANACONDA_HOME)/lib
- # 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.
- INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial #增加hdf5路徑
- LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial
- # 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
- 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 #挑錯模式,喜歡debug的朋友不妨試試。
(3)編譯 Caffe
- make pycaffe -jX #為了提高編譯速度,這裡的x設成自己的cpu核數
- make all -jX
- make test -jX
- make runtest -jX #注意,這裡也可直接執行測試,make runtest
export LC_ALL="C"
如果上面4行某一行報錯之後想要重試,建議先make clean再重新開始。4.編譯Python介面Caffe擁有python\C++\shell介面,在Caffe使用python特別方便,在例項中都有介面的說明。
- 確保pip已經安裝
sudo apt-get install python-pip
- 執行安裝依賴
在caffe根目錄的python資料夾下,有一個requirements.txt的清單檔案,上面列出了需要的依賴庫,按照這個清單安裝就可以了。
在安裝scipy庫的時候,需要fortran編譯器(gfortran),如果沒有這個編譯器就會報錯,因此,我們可以先安裝一下。
首先回到caffe的根目錄,然後執行安裝程式碼:
cd ~/caffesudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-nextsudo apt-get update sudo apt-get install gfortrancd ./pythonfor req in $(cat requirements.txt); do pip install $req; done安裝完成以後,再次回到caffe根目錄我們可以執行:
sudo pip install -r python/requirements.txt
就會看到,安裝成功的,都會顯示Requirement already satisfied, 沒有安裝成功的,會繼續安裝。
- 執行python結構
如果沒有報錯,說明caffe安裝全部完成
注意:如果import caffe提示找不到caffe,需要將 PYTHONPATH加入.bashrc中:
- vim ~/.bashrc
- 在檔案末端新增下面兩句
- #set PYTHONPATH 設定caffe下的路徑。否則caffe也找不到。
- export PYTHONPATH="/home/tom/caffe/python:$PYTHONPATH" 標紅的地方換成自己的caffe路徑
source ~/.bashrc
5.在Mnist執行Lenet
- 獲取資料來源
- 因為是CPU執行,所以修改在examples檔案下的Mnist下的lenet_solver.prototxt中的solver_mode:CPU
solver_mode: CPU
- 訓練模型
./examples/mnist/train_lenet.sh
6.最後,我的文章是基於各位前輩大神們的文章,雖然按他們的過程走我都報錯了,但是最終還是幫助我安裝成功。為了表示對別人成果的尊重,這裡留下大神們的參考連結:
http://www.linuxidc.com/Linux/2016-09/135034.htm
http://blog.csdn.net/u010402483/article/details/51506616
http://www.cnblogs.com/denny402/p/5679037.html
http://blog.csdn.net/u012029332/article/details/51098248
http://blog.csdn.net/mandagod/article/details/52337434