手把手教你在Ubuntu 16.04下配置GPU版Caffe(2)

 

之前的博客中比较详尽地讲解了和GPU相关的一些环境的配置,下面讲解caffe的python环境及其依赖项的安装。

step3

step3:安装Caffe的python环境

(1)Caffe的官方网站推荐使用Anaconda,在Anaconda的官方网站(Downloads)下载安装文件(我下的是Anaconda2-4.1.1-Linux-x86_64.sh,其中4.1.1是Anaconda版本号),切换到该安装文件所在的目录,执行:

bash Anaconda2-4.1.1-Linux-x86_64.sh

整个安装过程选择默认即可。

(2)添加Anaconda Library Path

打开~/.bashrc文件:

sudo gedit ~/.bashrc 

将以下内容写入到~/.bashrc尾部:

export LD_LIBRARY_PATH="/home/username/anaconda2/lib:$LD_LIBRARY_PATH"

注:username为用户名。

保存后执行:

source ~/.bashrc

使配置生效。

(3)查看Anaconda是否安装完毕

终端输入:

python --version

若显示:

Python 2.7.12 :: Anaconda custom (64-bit)

则安装完毕。

附:Anaconda 的 gcc 版本问题

Anaconda的python对应的gcc版本是gcc4.4.7,ubuntu自带的Python对应的gcc版本是gcc5或gcc6(通常和系统当前的gcc版本一致)。那么,需改变anaconda的Python对应的gcc版本,使其和系统当前的gcc版本一致。命令如下:

conda install libgcc

step4

step4:安装Caffe的依赖项

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler protobuf-c-compiler
sudo apt-get install libboost-all-dev build-essential libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
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 

step5

step5:Caffe编译及其python接口配置

(1)安装Caffe的python依赖库

访问并打开Caffe的github网站:BVLC/caffe,下载并解压Caffe源码包到Home目录下,进入caffe-master下的python目录,执行:

pip install -r requirements.txt

这样做的目的是安装requirements.txt中所罗列出的python包,也可以执行以下命令来完成安装:

for req in $(cat requirements.txt); do pip install $req; done

(2)编译Caffe及其python接口

进入caffe-master目录,复制一份Makefile.config.examples并重命名为Makefile.config,命令如下:

cp Makefile.config.examples Makefile.config

打开Makefile.config,修改其中的一些配置并保存,修改后的文件如下:

## Refer to Caffe | Installation
# 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 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 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_52,code=sm_52 \
		-gencode arch=compute_60,code=sm_60 \
		-gencode arch=compute_61,code=sm_61 \
		-gencode arch=compute_61,code=compute_61

# 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

# 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/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial

# 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

# NCCL acceleration switch (uncomment to build with NCCL)
# NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1

# 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 Removing -DNDEBUG from COMMON_FLAGS in Makefile breaks OS  X build · Issue #171 · BVLC/caffe
# 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 ?= @

现对修改的地方做一下讲解:

1.若要使用cuDNN加速,则USE_CUDNN := 1这一行取消注释;

2.若要使用Anaconda的python环境,则与python相关的一些路径需要换成Anaconda的,对应的配置如下:

# 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

# 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

3.若后续使用Caffe时需要用到python类型的层,则WITH_PYTHON_LAYER := 1这一行取消注释;

4.由于Ubuntu16.04的文件包含位置发生了变化,尤其是需要用到的hdf5的位置,因此需要在Makefile.config中添加这一路径,否则在编译Caffe的时候会提醒找不到hdf5.h或者hdf5_hl.h,错误提示为:

./include/caffe/util/hdf5.hpp:6:18:fatal error:hdf5.h:No such file or directory

对应的配置如下:

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial

修改完成后,在caffe-master目录下执行编译命令:

make all -j8

若出现错误提示:

fatal error:caffe/proto/caffe.pb.h:No such file or directory

则有可能是编译太快出现的错误,执行:

make clean

后再进行编译。

编译成功后,继续执行:

make test -j8
make runtest -j8
make pycaffe   # 编译Caffe的python接口
make distribute

此时,Caffe及其python接口配置完成。

(3)Caffe的python接口测试

执行python进入python环境,输入以下命令:

import sys
sys.path.append('/home/username/caffe-master/python')
import caffe

注:username为用户名。

若不报错,则Caffe的python接口配置完成。

猜你喜欢

转载自blog.csdn.net/CVAIDL/article/details/88856351
今日推荐