在ubuntu(Ubuntu16.04、ubuntu18.04)系統下成功安裝caffe框架詳細筆記
目錄
安裝SSH人臉檢測算法源碼中的caffe框架
ssh人臉檢測算法:https://blog.csdn.net/wsq_zqfl/article/details/92810501
Caffe編譯安裝指導
說明:本文假定用戶已經正確安裝了cuda和cudnn,如果未正確安裝,可參考如下面的鏈接
-
Caffe安裝參考文檔:
https://blog.csdn.net/haoji007/article/details/52081273
https://blog.csdn.net/yhaolpz/article/details/71375762
-
確認編譯器版本
首先確定系統所使用的gcc/g++/c++的版本,並且儘量和
“cat /proc/driver/nvidia/version”輸出中的gcc版本保持一致。
查看系統安裝了那些編譯器版本:
- 查看系統gcc/g++/c++/cpp所在的目錄
- 查看編譯器版本
- 查看gcc版本
上圖中的gcc軟鏈接指向的是gcc-5,說明版本正確
- 查看g++版本
- 查看c++版本
- 查看cpp版本
- gcc安裝
如果所需要的編譯器版本不存在,則可以安裝相應的編譯器,安裝方法如下:
參考文檔:https://www.cnblogs.com/L-Arikes/p/3734382.html
sudo apt-get gcc-5
sudo apt-get g++-5
-
安裝anaconda2
下載地址:https://www.anaconda.com/distribution/#download-section
官網安裝參考地址:https://docs.anaconda.com/anaconda/install/linux/
下載成功後,在終端執行(2.7版本):
# bash Anaconda2-2.4.1-Linux-x86_64.sh
或者3.5 版本:
# bash Anaconda3-2.4.1-Linux-x86_64.sh
在安裝的過程中,會問你安裝路徑,直接回車默認就可以了。有個地方問你是否將anaconda安裝路徑加入到環境變量(.bashrc)中,這個一定要輸入yes
安裝成功後,會有當前用戶根目錄下生成一個anaconda2的文件夾,裏面就是安裝好的內容。
輸入conda list 就可以查詢,你現在安裝了哪些庫,常用的numpy, scipy名列其中。如果你還有什麼包沒有安裝上,可以運行
conda install *** 來進行安裝,
如果某個包版本不是最新的,運行 conda update *** 就可以了
-
安裝opencv和protobuf
安裝OPENCV:
下載地址:https://opencv.org/releases/
安裝指導:https://docs.opencv.org/3.4.6/d7/d9f/tutorial_linux_install.html
protobuf安裝
下載地址:https://github.com/protocolbuffers/protobuf/releases
安裝指導:https://github.com/protocolbuffers/protobuf/blob/master/src/README.md
-
編譯caffe
1、下載ssh代碼
cd ~/work
git clone --recursive https://github.com/mahyarnajibi/SSH.git
2、修改源碼
首先在你要安裝的路徑下 clone :
git clone https://github.com/BVLC/caffe.git
替換 cudnn.hpp、cudnn.cpp
cd ~/work
git clone https://github.com/BVLC/caffe.git
cd ~/work/SSH/caffe-ssh
mv include/caffe/util/cudnn.hpp include/caffe/util/cudnn.hpp.bk
cp ~/work/caffe/include/caffe/util/cudnn.hpp include/caffe/util/cudnn.hpp
mv src/caffe/util/cudnn.cpp src/caffe/util/cudnn.cpp.bk
cp ~/work/caffe/src/caffe/util/cudnn.cpp src/caffe/util/cudnn.cpp
# rm -rf ~/work/caffe #下載caffe的目的 其實就是要獲取上面的兩個文件,替換完成後 caffe就可以刪掉了
- 修改Makefile.config
cp Makefile.config.example Makefile.config
vim Makefile.config
- 第23行
# 修改前: # OPENCV_VERSION := 3 # 修改後: OPENCV_VERSION := 3
第38行
# 修改前:
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
-gencode arch=compute_30,code=sm_30 \
# 修改後:
CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \
第52行
# 修改前:
BLAS := atlas
# 修改後:
BLAS := open
# 注:ATLAS、MKL、OpenBlas 都是矩陣運算庫(對矩陣運算過程進行了優化)
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
第68行:請根據具體環境修改
# 修改前: # 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. # ANACONDA_HOME := $(HOME)/anaconda # 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 # 修改後: # 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. ANACONDA_HOME := $(HOME)/anaconda3/envs/caffe_python-2-7/ 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
- 第96行
# 修改前: INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib # 修改後: INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/ LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
cp Makefile Makefile.bk
vim Makefile
第180行
#修改前:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
#修改後:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
第417行
# 修改前:
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
# 修改後:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
OK ,可以開始編譯了,在 caffe 目錄下執行 :
cd ~/work/SSH/caffe-ssh
make clean
make -j8
make runtest -j8
make pycaffe -j8