歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> caffe+Ubuntu14.04+cuda+cudnn+opencv配置

caffe+Ubuntu14.04+cuda+cudnn+opencv配置

日期:2017/3/3 12:53:31   编辑:Linux技術
從其他博客復制來,做了一些修改
Caffe 安裝配置步驟:
1, 安裝開發所需的依賴包
[plain] view plain copy print?
sudo apt-get install build-essential # basic requirement
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler #required by caffe
2,安裝CUDA 7.0
驗證過程省略,按照官方文檔自己操作吧(遇到問題首先要看官方文檔啊,血淚教訓)
安裝CUDA有兩種方法,
離線.run安裝:從官網下載對應版本的.run安裝包安裝,安裝過程挺復雜,嘗試過幾次沒成功,遂放棄。
在離線.deb安裝:deb安裝分離線和在線,我都嘗試過都安裝成功了,官網下載地址
安裝之前請先進行md5校驗,確保下載的安裝包完整
切換到下載的deb所在目錄,執行下邊的命令
[plain] view plain copy print?
sudo dpkg -i cuda-repo-<distro>_<version>_<architecture>.deb
sudo apt-get update
sudo apt-get install cuda
然後重啟電腦:sudo reboot
NOTE:裝不成功卸了多來幾遍,總會成的
3,安裝cuDNN
下載cudnn-6.5-linux-x64-v2.tgz,官網申請不到,網上自己找的,就不給地址了。
[plain] view plain copy print?
tar -zxvf cudnn-6.5-linux-x64-v2.tgz
cd cudnn-6.5-linux-x64-v2
sudo cp lib* /usr/local/cuda/lib64/
sudo cp cudnn.h /usr/local/cuda/include/
更新軟連接
[plain] view plain copy print?
cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.6.5
sudo ln -s libcudnn.so.6.5.48 libcudnn.so.6.5
sudo ln -s libcudnn.so.6.5 libcudnn.so
上邊的操作為什麼這麼做,不知道,原理是什麼,不知道。等我知道了再來補充
4,設置環境變量
在/etc/profile中添加CUDA環境變量
sudo gedit /etc/profile
[plain] view plain copy print?
PATH=/usr/local/cuda/bin:$PATH
export PATH
保存後, 執行下列命令, 使環境變量立即生效
[plain] view plain copy print?
source /etc/profile
同時需要添加lib庫路徑: 在 /etc/ld.so.conf.d/加入文件 cuda.conf, 內容如下
[plain] view plain copy print?
/usr/local/cuda/lib64
保存後,執行下列命令使之立刻生效
[plain] view plain copy print?
sudo ldconfig
5,安裝CUDA SAMPLE
進入/usr/local/cuda/samples, 執行下列命令來build samples
[plain] view plain copy print?
sudo make all -j4
整個過程大概10分鐘左右, 全部編譯完成後, 進入 samples/bin/x86_64/linux/release, 運行deviceQuery
[plain] view plain copy print?
./deviceQuery
如果出現顯卡信息, 則驅動及顯卡安裝成功:
[plain] view plain copy print?
./deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)
Device 0: "GeForce GTX 670"
CUDA Driver Version / Runtime Version 6.5 / 6.5
CUDA Capability Major/Minor version number: 3.0
Total amount of global memory: 4095 MBytes (4294246400 bytes)
( 7) Multiprocessors, (192) CUDA Cores/MP: 1344 CUDA Cores
GPU Clock rate: 1098 MHz (1.10 GHz)
Memory Clock rate: 3105 Mhz
Memory Bus Width: 256-bit
L2 Cache Size: 524288 bytes
Maximum Texture Dimension Size (x,y,z) 1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
Maximum Layered 1D Texture Size, (num) layers 1D=(16384), 2048 layers
Maximum Layered 2D Texture Size, (num) layers 2D=(16384, 16384), 2048 layers
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 49152 bytes
Total number of registers available per block: 65536
Warp size: 32
Maximum number of threads per multiprocessor: 2048
Maximum number of threads per block: 1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
Maximum memory pitch: 2147483647 bytes
Texture alignment: 512 bytes
Concurrent copy and kernel execution: Yes with 1 copy engine(s)
Run time limit on kernels: Yes
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Alignment requirement for Surfaces: Yes
Device has ECC support: Disabled
Device supports Unified Addressing (UVA): Yes
Device PCI Bus ID / PCI location ID: 1 / 0
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 6.5, CUDA Runtime Version = 6.5, NumDevs = 1, Device0 = GeForce GTX 670
Result = PASS
NOTE:上邊的顯卡信息是從別的地方拷過來的,我的GTX650顯卡不是這些信息,如果沒有這些信息,那肯定是安裝不成功,找原因吧!
6,安裝Intel MKL 或Atlas
我沒有MKL,裝的Atlas
安裝命令:
[plain] view plain copy print?
sudo apt-get install libatlas-base-dev
7,安裝OpenCV
我安裝的是2.4.10
1)下載安裝腳本
2)進入目錄 Install-OpenCV/Ubuntu/2.4
3)執行腳本
[plain] view plain copy print?
sh sudo ./opencv2_4_10.sh
8,安裝Caffe所需要的Python環境
按caffe官網的推薦使用Anaconda
去Anaconda官網下載安裝包
切換到文件所在目錄,執行
bash Anaconda-2.3.0-Linux-x86_64.s<em>h</em>
NOTE:後邊的文件名按自己下的版本號更改,整個安裝過程請選擇默認
8.1,添加Anaconda Library Path
在/etc/ld.so.conf最後加入以下路徑,並沒有出現重啟不能進入界面的問題(NOTE:下邊的username要替換)
[plain] view plain copy print?
/home/username/anaconda/lib #我沒添加
在~/.bashrc最後添加下邊路徑
[plain] view plain copy print?
export LD_LIBRARY_PATH="/home/username/anaconda/lib:$LD_LIBRARY_PATH" #這個好像安裝時自動添加了
9,安裝python依賴庫
去caffe的github下載caffe源碼包
首先安裝pip和python-dev (系統默認有python環境的, 不過我們需要的使python-dev)
sudo apt-get install python-dev python-pip
然後執行如下命令安裝編譯caffe python wrapper 所需要的額外包
for req in $(cat requirements.txt); do sudo pip install $req; done
在執行上述命令時, 會報錯導致不能完全安裝所有需要的包。 可以按照官方建議安裝anaconda包。 在anaconda官網下載.sh文件,執行,最後添加bin目錄到環境變量即可。
10,編譯Caffe
終於來到這裡了
進入caffe-master目錄,復制一份Makefile.config.examples
[plain] view plain copy print?
cp Makefile.config.example Makefile.config
修改其中的一些路徑,如果前邊和我說的一致,都選默認路徑的話,那麼配置文件應該張這個樣子
[plain] view plain copy print?
## Refer tohttp://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!
# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1
# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1
# 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++
# CUDA directory contains bin/ and lib/ directories that we need.
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.
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
# 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.
# 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 \
/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 \
# 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
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
# 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 tohttps://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 ?= @
保存退出
編譯
[plain] view plain copy print?
make all -j4
出錯:make: protoc:命令未找到
make: *** [.build_release/src/caffe/proto/caffe.pb.h] 錯誤 127
解決方法:
編譯以及安裝protoc
那就下載protobuf吧,在github下載,解壓https://github.com/google/protobuf:
運行
$ ./autogen.sh
原來先要安裝curl:額,貌似curl安裝不成功那就跳過吧。因為curl祈禱的作用就是下載文件,但https://googletest.googlecode.com/files/gtest-1.7.0.zip這個東西還是要下,那就直接下載吧,可能需要科學上網,這個方法那就八仙過海各顯省通,我就不贅述。
下載好以後把壓縮包移動過去。
找到對應位置注釋掉curl
再次運行,提示錯誤
./autogen.sh: 40: ./autogen.sh: autoreconf: not found
原因是沒安裝 automake 工具
運行命令安裝:sudo apt-get install autoconf automake libtool
再一次./autogen.sh,成功!然後依次運行以下命令
$ ./configure
$ make
$ make check
$ make install
執行完make check以後出現:
就說明沒什麼問題了,就可以繼續執行make install
執行完以後切換到caffe目錄,先make clean一下,接著make all
還是提示找不到庫,
protoc: error while loading shared libraries: libprotoc.so.8: cannot open shared object file: No such file or directory
3、安裝一系列依賴庫
再執行安裝一些庫:sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev,稍等
再執行make clean,接著make all,提示:
./include/caffe/common.hpp:5:27: fatal error: gflags/gflags.h: 沒有那個文件或目錄。說明protobuf的庫已經差不多了。
4、安裝gflags
這裡繼續安裝gflags:https://github.com/schuhschuh/gflags/archive/master.zip 下載。解壓
cd gflags-master mkdir build && cd build export CXXFLAGS="-fPIC" && cmake .. && make VERBOSE=1 make sudo make install在執行第三句話的時候可能會提示版本較低,直接在這裡
http://www.cmake.org/install/
下載最新版本的cmake就好了(下載.sh的文件直接運行就可以了)。要配置一下環境變量
然後接著執行下面兩條命令,就好了。弄完之後繼續切換到caffe目錄,先make clean一下,接著make all
有一個什麼glog庫找不到,因為第一步我有些庫沒安裝,安裝上就行了後來又安了lmdn(沒全部安裝,有些庫找不到,缺哪個安哪個)(sudo apt-get install libgoogle-glog-dev liblmdb-dev)有些教程還要到哪裡下載,好像直接apt-get就行了
說明gflag安裝成功了
然後到caffe目錄,make clean ,make all(好像成功了??)
出錯!!!
/usr/local/lib/libgflags.a: error adding symbols: 錯誤的值
collect2: error: ld returned 1 exit status
make: *** [.build_release/lib/libcaffe.so] 錯誤 1
https://github.com/BVLC/caffe/issues/2171答案在這裡
(1) edit CMakeCache.txt
(2) Change:
CMAKE_CXX_FLAGS:STRING=-fPIC
(3) re-compile and install.
編譯成功!!
make test
make runtest
11,編譯Python wrapper
[plain] view plain copy print?
make pycaffe
到這裡就基本結束了,跑個自帶的例子測試一下吧!
Copyright © Linux教程網 All Rights Reserved