Ubuntu14.04+caffe+cuda8.0+cudnn v5.1配置

1. 环境准备

在Ubuntu下用apt-get大法搞定所有依赖包。opencv和python建议通过别的方法自行安装

sudo apt-get install git
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install python-dev  
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

2. 安装cuda8.0。从CUDA官网下载目前最新版的cuda8.0的.run文件。

安装之前先按下ctrl+alt+F1进入命令行界面,关闭图形界面

sudo stop lightdm

进入到下载的cuda8.0安装包所在文件夹下,直接在命令行运行.run文件开始安装

进入并accept完声明后注意第一个选项,询问你是否需要安装NVIDIA驱动程序,如果电脑已经安装过适合自己显卡的显卡驱动,此处一定得选择为no,后面的基本可以默认选择就好,根据自己需要。如果之前没有安装过NVIDIA驱动则第一个需要选择YES进行驱动的安装然后再进行cuda的安装。

打开图形界面,ctrl+alt+F7回到图形界面:

sudo start lightdm

查看驱动是否安装成功:

nvidia-smi

3. 安装配置cudnn5.1。下载最新的cudnn-8.0-linux-x64-v5.1.tgz文件。将解压出的cuda文件夹下的lib64文件夹里的静态库和动态库文件一并拷到cuda8.0的安装目录下的lib64中去,默认是/usr/local/cuda/lib64/。同时将include文件夹下的头文件cudnn.h拷贝到cuda8.0的include中去,默认为/usr/local/cuda/include/

tar -zxvf cudnn-8.0-linux-x64-v5.1.tgz
cd cuda
sudo cp lib64/* /usr/local/cuda/lib64/
sudo cp include/cudnn.h /usr/local/cuda/include/ 
建立软连接
cd /usr/local/cuda/lib64/
sudo chmod +r libcudnn.so.5.1.5
sudo ln -sf libcudnn.so.5.1.5 libcudnn.so.5
sudo ln -sf libcudnn.so.5 libcudnn.so
sudo ldconfig

4. 设置环境变量

在/etc/profile中添加CUDA环境变量
sudo gedit /etc/profile
在文件尾部添加以下内容:
PATH=/usr/local/cuda/bin:$PATH
exportPATH
保存后,执行下列命令,使环境变量立即生效
source /etc/profile
同时需要添加lib库路径: 在 /etc/ld.so.conf.d/加入文件 cuda.conf,
sudo gedit /etc/ld.so.conf.d/cuda.conf
添加以下内容:

/usr/local/cuda/lib64

保存后,执行下列命令使之立刻生效
sudo ldconfig

此时可以通过命令行查看cuda是否安装成功:

nvcc -V

5. 通过git获取github上的官方caffe源码。

git clone https://github.com/bvlc/caffe.git

6. 配置caffe的config文件。

cd caffe/
cp Makefile.config.example Makefile.config  #以example为备份
sudo gedit Makefile.config

根据自己需要进行配置,将cudnn的注释解开,并确保opencv(2或者3),python(或anaconda),cuda等路径的正确

7. 编译caffe

make all && make pycaffe

8. 相对于通过python命令import caffe到python库中,建议是在需要使用caffe的项目中再将库引进去,因为caffe版本可能会有多个,方法如下:

在python文件头部加上:

import sys

sys.path.append('/home/yourname/caffe/python')

import caffe

这样caffe即可正常使用了

9. 往python库里导入caffe时可能错误的解决情况

(0) ubuntu中很可能碰到的问题,系统的protobuf和pyhton的protobuf版本不一致,可能在C++调用python接口时候遇到问题,此时最好的方法是将python的protobuf版本降回2.6

pip install protobuf==2.6.0

(1)import caffe时出错:can not find module skimage.io

sudo apt-get install python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags Cython ipython
sudo apt-get update
(2)python caffe libcaffe.so.1.0.0-rc3: cannot open shared object file
cd /etc/ld.so.conf.d
sudo gedit ./pycaffe.conf

在里边添加以下内容:

/home/yourname/caffe/build/lib

别忘了再load一下:

sudo ldconfig

然后将caffe/python文件夹下的整个caffe文件夹拷贝到/usr/lib/python2.7/dist-packages下

cd caffe/
sudo cp -r ./python/caffe /usr/lib/python2.7/dist-packages

然后再import caffe就没问题了

(3)python caffe报错:No module named google.protobuf.internal

conda install protobuf

import google.protobuf

最后贴一份opencv3+anaconda3的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

# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0

# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
#	You should 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,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)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		 $(ANACONDA_HOME)/include/python3.5m \
		 $(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include \

# Uncomment to use Python 3 (default is Python 2)
 PYTHON_LIBRARIES := boost_python-py34 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
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

# N.B. both build and distribute dirs are cleared on `make clean`
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 ?= @

相关文章

文章浏览阅读2.3k次,点赞4次,收藏22次。最近安装了CARLA预...
文章浏览阅读6.3k次,点赞5次,收藏15次。在清华镜像中下载U...
文章浏览阅读5k次。linux环境, python3.7.问题描述: 安装...
文章浏览阅读4.2k次,点赞4次,收藏17次。要安装这个 standa...
文章浏览阅读894次,点赞51次,收藏31次。在安卓使用vscode主...