从这里复制来,做了一些修改
Caffe 安装配置步骤:
我的系统是64位Ubuntu14.04,cuda7.5
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校验,确保下载的安装包完整
切换到下载的deb所在目录,执行下边的命令(官网也有安装命令)
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,官网申请不到,网上自己找的,就不给地址了。
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
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
保存后, 执行下列命令, 使环境变量立即生效
source /etc/profile
同时需要添加lib库路径: 在 /etc/ld.so.conf.d/加入文件 cuda.conf, 内容如下
/usr/local/cuda/lib64
保存后,执行下列命令使之立刻生效
sudo ldconfig
5,安装CUDA SAMPLE
进入/usr/local/cuda/samples, 执行下列命令来build samples
sudo make all
整个过程大概10分钟左右, 全部编译完成后, 进入 samples/bin/x86_64/linux/release, 运行deviceQuery
./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
NOTE:上边的显卡信息是从别的地方拷过来的,我的GTX650显卡不是这些信息,如果没有这些信息,那肯定是安装不成功,找原因吧!
6,安装Intel MKL 或Atlas
我没有MKL,装的Atlas
安装命令:
sudo apt-get install libatlas-base-dev
7,安装OpenCV
我安装的是2.4.10
1)下载安装脚本
2)进入目录 Install-OpenCV/Ubuntu/2.4
3)执行脚本
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 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 ?= @
保存退出
编译
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
答案在这里
(1) edit CMakeCache.txt
(2) Change:
CMAKE_CXX_FLAGS:STRING=-fPIC
(3) re-compile and install.
编译成功!!
make testmake runtest
11,编译Python wrapper
make pycaffe
到这里就基本结束了,跑个自带的例子测试一下吧!