关于Ubuntu 版本的选择,强烈建议用14.04这个比较稳定的版本,但是千万不要用麒麟版!!!
主要参考以下博客,稍作修改解决opencv安装可能出现的错误。
2015.08.17 Ubuntu 14.04+cuda 7.5+caffe安装配置
配置:Caffe + Ubuntu 14.04 64bit + CUDA7.5+cuDnn v5 + Anaconda2
1.系统更新
首先安装好Ubuntu 14.04 64bit。如果刚刚安装好系统,建议在开启网络连接的情况下等待一段时间,Ubuntu会自动检测到更新,点击确定更新系统软件,然后重启。(注意:不是更新Ubuntu16)
2.安装开发所需的依赖包
1. sudo apt-get install build-essential
2. 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
3.准备好所有的安装文件
Caffe:
输入终端下载:
1. git clone https://github.com/BVLC/caffe.git
这样,/home 下就有Caffe文件夹了
CUDA 7.5:
https://developer.nvidia.com/cuda-toolkit-archive 选择CUDA Toolkit 7.5 (Sept 2015)
一定要选择离线 .deb文件版本
cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb
下载放到Documents 或者桌面等文件夹即可;
cuDnn v5 :
https://developer.nvidia.com/cudnn
这里面先要注册账号才能下载,型号选择
cudnn-7.5-linux-x64-v5.0-ga.tgz
下载放到Documents 或者桌面等文件夹即可;
Anaconda2:
https://www.continuum.io/downloads 选择Linux,下载Python 2.7 version 64bit
Anaconda2-4.2.0-Linux-x86_64.sh
下载放到Documents 或者桌面等文件夹即可;
Opencv自动安装脚本:
https://github.com/bearpaw/Install-OpenCV 右上角下载
Install-OpenCV-master.zip
下载放到Documents 或者桌面等文件夹即可;
上述文件,除了Caffe,其他下载统一到一个文件夹,我这里是Documents。
4.安装CUDA 7.5
Before install CUDA 7.5, you need update gcc 4.8+ to gcc 4.9+
安装之前gcc,g++升级到4.9.
1. sudo add-apt-repository ppa:ubuntu-toolchain-r/test
2. sudo apt-get update
3. sudo apt-get install gcc-4.9
4. sudo apt-get install g++-4.9
6. cd ../../usr/bin
7. ln -s /usr/bin/g++-4.9 /usr/bin/g++ -f
8. ln -s /usr/bin/gcc-4.9 /usr/bin/gcc -f
在安装cuda的时候,Nvidia 显卡驱动会同时装好,所以不需要单独装显卡驱动
开始安装前,按 ctrl+alt+F1 ,进入黑色的终端界面。
输入自己的账号,再输入密码,登录到你的账号。
然后输入以下命令
9. sudo service lightdm stop
然后cd 到文件下载的目录,我这里是Documents
10. sudo dpkg -i cuda-repo-ubuntu1404-7-0-local_7.0-28_amd64.deb
11. sudo apt-get update
12. sudo apt-get install cuda
等待安装结束,重启
13. sudo reboot
5.安装cuDNN
cd 到文件下载的目录,我这里是Documents
1. tar -zxvf cudnn-7.5-linux-x64-v5.0-ga.tgz
2. cd cuda
3. sudo cp lib64/lib* /usr/local/cuda/lib64/
4. sudo cp include/cudnn.h /usr/local/cuda/include/
更新软连接
5. cd /usr/local/cuda/lib64/
6. sudo chmod +r libcudnn.so.5.0.5
7. sudo ln -sf libcudnn.so.5.0.5 libcudnn.so.5
8. sudo ln -sf libcudnn.so.5 libcudnn.so
9. sudo ldconfig
6. 设置环境变量
1. sudo gedit /etc/profile
在打开的文件尾部加上
PATH=/usr/local/cuda/bin:$PATH
export PATH
保存关闭后执行以下命令使之生效
2. source /etc/profile
同时创建以下文件
3. sudo gedit /etc/ld.so.conf.d/cuda.conf
内容是
/usr/local/cuda/lib64
保存关闭后,使之生效
4. sudo ldconfig
7.安装CUDA SAMPLE
cd进入/usr/local/cuda/samples, 执行下列命令来build samples
1. sudo make all -j4
整个过程大概10分钟左右, 全部编译完成后, 进入 samples/bin/x86_64/linux/release, 运行deviceQuery
2. ./deviceQuery
如果出现显卡信息,则驱动及显卡安装成功:
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)
Device 0: "Tesla K40c"
CUDA Driver Version / Runtime Version 8.0 / 7.5
CUDA Capability Major/Minor version number: 3.5
Total amount of global memory: 11439 MBytes (11995054080 bytes)
(15) Multiprocessors, (192) CUDA Cores/MP: 2880 CUDA Cores
GPU Max Clock rate: 745 MHz (0.75 GHz)
Memory Clock rate: 3004 Mhz
Memory Bus Width: 384-bit
L2 Cache Size: 1572864 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 2 copy engine(s)
Run time limit on kernels: No
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Alignment requirement for Surfaces: Yes
Device has ECC support: Enabled
Device supports Unified Addressing (UVA): Yes
Device PCI Domain ID / Bus ID / location ID: 0 / 4 / 0
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 7.5, NumDevs = 2, Device0 = Tesla K40c,
Result = PASS
NOTE:上边的显卡信息是从别的地方拷过来的,我的GTX650显卡不是这些信息,如果没有这些信息,那肯定是安装不成功,找原因吧!
8.安装Atlas
Atlas免费,安装起来简单,安装命令:
1. sudo apt-get install libatlas-base-dev
9.安装OpenCV
在这之前,必须对gcc降级,降到原来的4.8
1. sudo su
2. cd ../../usr/bin
3. ln -s /usr/bin/g++-4.8 /usr/bin/g++ -f
4. ln -s /usr/bin/gcc-4.8 /usr/bin/gcc -f
输入gcc --version检查是不是4.8版
我安装的是2.4.10,比较稳定,不要装 3.0等,麻烦。从前面下载的解压文件,进入目录 Install-OpenCV/Ubuntu/2.4
执行脚本
5. sudo ./opencv2_4_10.sh
10.安装Caffe所需要的Python环境
前面下载了Anaconda2,切换到文件所在目录,执行
1. bash Anaconda2-4.2.0-Linux-x86_64.sh
过程中,问起yes/no,全部输入yes。问起安装路径,则直接回车默认安装
然后,添加Anaconda Library Path
在/etc/ld.so.conf最后加入以下路径,并没有出现重启不能进入界面的问题(NOTE:下边的username要替换)
2. sudo gedit /etc/ld.so.conf
在文件最后一行输入:
/home/username/anaconda2/lib 然后保存关闭
3. sudo gedit ~/.bashrc
在文件最后一行输入:export LD_LIBRARY_PATH="/home/username/anaconda2/lib:$LD_LIBRARY_PATH"
然后关闭终端,再打开终端
11.安装python依赖库
cd进入caffe下的python目录
执行如下命令
1. for req in $(cat requirements.txt); do pip install $req; done
12.编译Caffe
进入caffe目录,复制一份Makefile.config.examples
1. cp Makefile.config.example Makefile.config
双击打开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)/anaconda2
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 ?= @
保存退出
编译(下面命令一个一个执行,过程中50%概率会出错)
2. make all -j4
3. make test
4. make runtest
5. make pycaffe
过程很长,如果都没报错,那就安装好了
记得关闭系统自动更新,否则可能会有想不到的错误