Ubuntu 14.04 配置caffe环境
Last editted on 2016.7.23.
原文地址:
http://blog.csdn.net/ubunfans/article/details/47724341
略有更改,更改的部分参考了http://www.cnblogs.com/platero/p/3993877.html
-----------------------------------------------------------------------------------
Caffe 安装配置步骤:
1, 安装开发所需的依赖包
- 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校验,确保下载的安装包完整。
这里给出一个我下载好的链接:http://pan.baidu.com/s/1kVi2uFX 密码:af78
- sudo dpkg -i cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb #sudo dpkg -i cuda-repo-<distro>_<version>_<architecture>.deb
- sudo apt-get update
- sudo apt-get install cuda
- 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/
- cd /usr/local/cuda/lib64/
- sudo rm -rf libcudnn.so libcudnn.so.6.5
chmod u=rwx,g=rx,o=rx libcudnn.so.6.5.48
- sudo ln -s libcudnn.so.6.5.48 libcudnn.so.6.5
- sudo ln -s libcudnn.so.6.5 libcudnn.so
4,设置环境变量
- PATH=/usr/local/cuda/bin:$PATH
- export PATH
- source /etc/profile
- /usr/local/cuda/lib64
- sudo ldconfig
- sudo make all -j4
- ./deviceQuery
- ./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
- sudo apt-get install libatlas-base-dev
这个尽量不要手动安装, Github上有人已经写好了完整的安装脚本:https://github.com/jayrambhia/Install-OpenCV
去掉去掉bash脚本中 rm -rf OpenCV,否则卸载时候比较麻烦~github上大神给的bash脚本在应用在caffe上cmake时候会有一些问题。
这里给出一个我自己修改过的bash脚本链接:http://pan.baidu.com/s/1chyMZ0 密码:oohx
下载该脚本,进入Ubuntu/2.4 目录, 给所有shell脚本加上可执行权限
chmod +x *.sh
然后安装 (用2.4.9吧,2.4.10貌似有人说有问题 )
sudo ./opencv2_4_9.sh
脚本会自动安装依赖项,下载安装包,编译并安装OpenCV。整个过程大概半小时左右。
注意,中途可能会报错
opencv-2.4.9/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(51): error: a storage class is not allowed in an explicit specialization
解决方法在此:http://code.opencv.org/issues/3814 下载 NCVPixelOperations.hpp 替换掉opencv2.4.9内的文件, 重新build。
链接:http://pan.baidu.com/s/1c2DOFDY 密码:rohl 来源:http://download.csdn.net/download/solomon1558/9581114,感谢博主Solomon1588
这个是opencv的一个bug,在后续版本里被修复了。
The compilation problem can be solved by removing all the 'static' keywords in the template functions in that file.
可能错误:nvcc fatal : Unsupported gpu architecture 'compute_11'
解决方法:cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler ..
error:fatal error: opencv2/cudalegacy/NCV.hpp: No such file or directory
8,安装Caffe所需要的Python环境
切换到文件所在目录,执行
- bash Anaconda-2.3.0-Linux-x86_64.sh
- /root/anaconda/lib
- export LD_LIBRARY_PATH="/root/anaconda/lib:$LD_LIBRARY_PATH"
9,安装python依赖库
进入caffe-master下的python目录
- for req in $(cat requirements.txt); do pip install $req; done
10,安装MATLAB
Caffe提供了MATLAB接口, 有需要用MATLAB的同学可以额外安装MATLAB。 安装教程请自行搜索。
http://blog.csdn.net/lanbing510/article/details/41698285
安装完成后添加图标 http://www.linuxidc.com/Linux/2011-01/31632.htm
sudo vi /usr/share/applications/Matlab.desktop
输入以下内容
[Desktop Entry]
Type=Application
Name=Matlab
GenericName=Matlab 2010b
Comment=Matlab:The Language of Technical Computing
Exec=sh /usr/local/MATLAB/R2010b/bin/matlab -desktop
Icon=/usr/local/MATLAB/Matlab.png
Terminal=false
Categories=Development;Matlab;
(I use the R2013b patched package. First you should uncompress the .iso file. Then use sudo cp to copy the patch file)
- cp Makefile.config.example Makefile.config
- ## Refer to http://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 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 ?= @
cudnnConvolutionBwdFilterAlgo_t *bwd_filter_algo_;
- make all -j4
- make test
- make runtest
12.1. 编译Matlab wrapper
执行如下命令
make matcaffe
然后就可以跑官方的matlab demo啦。
12.2. 编译Python wrapper
make pycaffe
到这里就基本结束了,可以跑个MNIST尝试一下,可参考http://www.cnblogs.com/denny402/p/5075490.html, run MNIST. my GPU is GTX 760. finished in less than 1 minute with cudnn acceleration. (my ubuntu is new. i have not installed Chinese language support. it does not have Chinese input method. orz)
(HAVE FUN!)