Ubuntu14.04(64bit)+caffe+cuda8.0+cudnnV5安装配置(十步走)
电脑配置:ubuntu14.04 64bit (网上有安装双系统的教程,不建议在虚拟机上安装除非只使用cpu版caffe)内存8G显卡GT 755Mcpu i5软件版本: caffe官网当天git下载, nvidia官网下载cuda8.0, cudnnV5Caffe安装步骤如下:一、安装开发所需依赖包sudo apt-get install build-essent
电脑配置:
ubuntu14.04 64bit (网上有安装双系统的教程,不建议在虚拟机上安装除非只使用cpu版caffe)
内存8G
显卡GT 755M
cpu i5
软件版本: caffe官网当天git下载, nvidia官网下载cuda8.0, cudnnV5
Caffe安装步骤如下:
一、安装开发所需依赖包
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
二、安装Cuda8.0
1. 离线.deb安装:官网下载地址: https://developer.nvidia.com/cuda-downloads
安装之前请先进行md5校验,确保下载的安装包完整
2. 切换到下载的deb所在目录,执行下边的命令
sudo dpkg -i cuda-repo-ubuntu1404-8-0-local_8.0-18_amd64.deb
sudo apt-get update
sudo apt-get install cuda
3. 设置cuda的环境变量
(1)使用如下命令:
sudo vim /etc/profile
在/etc/profile中添加CUDA环境变量末尾添加如下内容:
PATH=/usr/local/cuda/bin:$PATH
export PATH
(2)保存后, 执行下列命令, 使环境变量立即生效
source /etc/profile
(3)同时需要添加lib库路径: 使用
sudo vim/etc/ld.so.conf.d/cuda.conf
在/etc/ld.so.conf.d/加入cuda.conf文件, 内容如下
/usr/local/cuda/lib64
(4)保存后,执行下列命令使之立刻生效
sudo ldconfig
(5)然后重启电脑
sudo reboot
三、安装CudnnV5(最新的为6.0版本)
1. 官网上注册申请下载cudnn-8.0-linux-x64-v5.0-ga.tgz(地址https://developer.nvidia.com/cudnn)
2. 到下载目录下执行如下命令:
$ tar-zxvf cudnn-8.0-linux-x64-v5.0-ga.tgz
$ cd cuda/include
$ sudo cp *.h /usr/local/cuda/include/
$ cd ../lib64
$ sudo cp lib* /usr/local/cuda/lib64/
$ cd /usr/local/cuda/lib64/
$ sudo chmod +r libcudnn.so.5.0.5
$ sudo ln -sf libcudnn.so.5.0.5 libcudnn.so.5
$ sudo ln -sf libcudnn.so.5 libcudnn.so
$ sudo ldconfig
四、安装Cuda Sample(验证)
1. 进入/usr/local/cuda/samples,执行下列命令来编译samples:
$ cd /usr/local/cuda/samples
$ sudo make all -j4(依自己电脑核数而定)
2. 全部编译完成后,进入/usr/local/cuda/samples/bin/x86_64/linux/release, 运行deviceQuery
$ cd /usr/local/cuda/samples/bin/x86_64/linux/release
$ ./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 8.0 / 8.0
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 = 8.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GTX 670
Result = PASS
NOTE:上边的显卡信息是从别的地方拷过来的,我的GT755M显卡不是这些信息,如果没有这些信息,那肯定是安装不成功,找原因吧!
五、安装Inter MKL或openblas或Atlas(矩阵加速库)
装MKL需要上Intel官网用edu邮箱申请,在这我装的Atlas,安装命令:
$ sudo apt-get install libatlas-base-dev
六、安装OpenCV
1. 下载并编译OpenCV(官网原版OpenCV:http://opencv.org/), 或者使用修改版的安装包(百度云盘链接: http://pan.baidu.com/s/1qX1uFHa 密码:wysa)(下面的安装方式使用该包完成,安装包修改了dependencies.sh文件并增加了OpenCV 3.0.0的安装文件)
2. 切换到文件保存的文件夹,然后安装依赖项:(进入 “Install-OpenCV-master” 文件夹下)
$ sudo sh Ubuntu/dependencies.sh
3. 切换目录Ubuntu/3.0/安装OpenCV 3.0.0:
$ cd Ubuntu/3.0/
$ sudo sh opencv3_0_0.sh
保证网络畅通,因为软件需要联网这里时间较长,请耐心等待。
七、安装Pycaffe环境
1. 按caffe官网的推荐使用Anaconda,去Anaconda官网下 安装包(下载python2.7 的Anaconda2-4.0.0-Linux-x86_64.sh)
2. 切换到文件所在目录,执行
bash Anaconda2-4.0.0-Linux-x86_64.sh
NOTE:后边的文件名按自己下的版本号更改,整个安装过程请选择默认,安装路径为 /home/username/anaconda(username为自己系统的用户名)
3. 添加Anaconda Library Path:使用
$ sudo vim /etc/ld.so.conf
在/etc/ld.so.conf最后加入以下路径 (NOTE:下边的username要替换为自己的用户名):
/home/username/anaconda/lib
4. 使用
vim ~/.bashrc
在~/.bashrc最后添加下边路径:
export LD_LIBRARY_PATH="/home/username/anaconda/lib:$LD_LIBRARY_PATH"
5. 退出运行如下命令使配置文件生效:
$ sudo ldconfig
八、安装python依赖库
1. 去caffe的github下载caffe源码包(https://github.com/BVLC/caffe )
2. 进入caffe-master下的python目录,执行如下命令
sudo apt-get install python-pip #如果没有安装pip则需要执行该命令
for req in $(cat requirements.txt); do pip install $req; done
九、编译Caffe
1. 进入caffe-master目录,复制一份Makefile.config.examples:
cp Makefile.config.example Makefile.config
2. 修改配置文件,如果前边和该安装教程一致,那么配置文件应该是这个样子的:
## Refer tohttp://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving ourbuild system are welcome!
# cuDNN acceleration switch (uncomment to buildwith cuDNN).
USE_CUDNN := 1
# CPU-only switch (uncomment to build without GPUsupport).
# CPU_ONLY := 1
# uncomment to disable IO dependencies andcorresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0
# uncomment to allow MDB_NOLOCK when reading LMDBfiles (only if necessary)
# Youshould not set this flag if you will be reading LMDBs with any
# possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
# To customize your choice of compiler, uncommentand set the following.
# N.B. the default for Linux is g++ and thedefault 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 installedvia
# "sudo apt-get installnvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr
# CUDA architecture setting: going with all ofthem.
# For CUDA < 6.0, comment the *_50 lines forcompatibility.
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 #这里根据自己安装的加速库而定
# NOTE: this is required only if you will compilethe python interface.
# We need to be able to find Python.h andnumpy/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 inroot.
ANACONDA_HOME := /home/username/anaconda #记得改username
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
# 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 /usr/lib/x86_64-linux-gnu/hdf5/serial/include
LIBRARY_DIRS := $(PYTHON_LIB)/usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial
# If Homebrew is installed at a non standardlocation (for example your home directory) and you use it for generaldependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew –prefix)/lib
# Uncomment to use `pkg-config` to specify OpenCVlibrary paths.
# (Usually not necessary -- OpenCV libraries arenormally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1
# N.B. both build and distribute dirs are clearedon `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
# Uncomment for debugging. Does not work on OSXdue to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1
# The ID of the GPU that 'make runtest' will useto run unit tests.
TEST_GPUID := 0
# enable pretty build (comment to see fullcommands)
Q ?= @
3. 保存退出,然后编译:(如报错可按报错内容安装相应依赖包或修改环境变量)
$ make all -j4
$ make test
$ make runtest
4. 编译python wrapper
$ make pycaffe
$ vim ~/.bashrc 在末尾添加:
export PYTHONPATH=”/home/username/caffe-master/python:$PYTHONPATH”
$ sudo ldconfig #使之生效
十、使用Mnist数据集测试
Caffe默认情况会安装在$CAFFE_ROOT,就是解压到那个目录,例如:$home/username/caffe-master,所以下面的工作,默认已经切换到了该工作目录。下面的工作主要是,用于测试Caffe是否工作正常,不做详细评估。具体设置请参考官网:http://caffe.berkeleyvision.org/gathered/examples/mnist.html
1. 数据预处理
$ sh data/mnist/get_mnist.sh
2. 重建lmdb文件。Caffe支持多种数据格式输入网络,包括Image(.jpg,.png等),leveldb,lmdb,HDF5等,根据自己需要选择不同输入吧。
$ sh examples/mnist/create_mnist.sh
生成mnist-train-lmdb和mnist-train-lmdb文件夹,这里包含了lmdb格式的数据集
3. 训练mnist,出现如下图所示则caffe安装工作成功。
Reference:
1. http://blog.csdn.net/ubunfans/article/details/47724341
2. http://www.cnblogs.com/karac/p/5456134.html
更多推荐
所有评论(0)