Caffe 及相关软件的安装(三)

Caffe 及相关软件的安装分为如下几个大块:
1.安装CUDA
2.安装其他依赖库如BLAS、BOOST、OPENCV、cuDNN 等
3.安装MATLAB
4.安装及编译caffe
5.小结

下面就逐个展开介绍。


四.安装及编译caffe

1.首先到官网下载caffe:
http://caffe.berkeleyvision.org/

点击 View On Github后,即可进入GitHub 上的下载界面。

2.根据配置修改编译文件
将下载后的包解压后,进入caffe 的根目录,会看到“Makefile.config.example”
文件,将其复制为“Makefile.config”文件后,根据自己的配置修改其中的内容,
具体可查看官网的安装教程。
$ sudo cp Makefile.config.example Makefile.config
官网安装教程:http://caffe.berkeleyvision.org/install_apt.html
3.编译caffe
直接输入如下命令:
$ make all -j56(“56”是由自己电脑中cpu 的core 数决定的)
4.编译pycaffe 及matcaffe
为了让caffe 能调用与python 及matlab 的接口,我们需要首先编译之。

4.1 编译matcaffe
首先需要将Makefile.config 中的MATLAB_DIR 改为自己机器matlab 的路径,如我
们的机器为“/opt/matlab”,然后:
$ make matcaffe
4.2 利用anaconda 来配置python 环境
先到
https://www.continuum.io/downloads
下载anaconda, 现在的版本有python2.7 版本和python3.5 版本,下载好对应版
本、对应系统的anaconda,它实际上是一个sh 脚本文件,大约288M 左右。我
下载的是linux 版的python 2.7 版本。

下载成功后,在终端执行(2.7 版本):
$ sudo bash Anaconda2-4.4.1-Linux-x86_64.sh
在安装的过程中,会问你安装路径,可按默认路径也可自己选择,这里我是自己
选择了路径,“/opt/anaconda2”。有个地方问你是否将anaconda 安装路径加入
到环境变量( .bashrc) 中, 这个一定要输入yes 。( 这里其实是将路径
“/opt/anaconda2/bin”添加到了环境变量PATH 中)
另外, 我们还需要自己将Anaconda 库文件的路径添加到环境变量
LD_LIBRARY_PATH 中,不然之后在“make runtest”时可能会报错“error while loading
shared libraries: libhdf5_hl.so.10: cannot open shared object file...”:
$ sudo gedit ~/.bashrc
文件打开后,在最后加入:
export LD_LIBARY_PATH=/opt/anaconda2/lib:$LD_LIBRARY_PATH
保存退出,并运行$ sudo ldconfig

安装成功后,会在“/opt”目录下下生成一个anaconda2 的文件夹,里面就是安
装好的内容。
输入conda list 就可以查询,你现在安装了哪些库,常用的numpy, scipy 名列其
中。然后打开caffe 根目录下的python 文件夹中的“requirement.txt”文件,查
看你是否还有什么包没有安装上,注:leveldb 不属于python 系列。
可以运行conda install *** 来进行安装,
如果某个包版本不是最新的,运行conda update *** 就可以了。

4.3 编译pycaffe
至于编译python,需要先将将caffe 根目录下的python 文件夹加入到用户环境
变量。打开配置文件bashrc:
$ sudo gedit ~/.bashrc
在最后面加入:
export PYTHONPATH=/home/smart/下载/caffe-master/python:$PYTHONPATH
注意“/home/smart/下载/caffe-master/python”是我的路径,这个地方每个人都
不同,需要修改。
保存退出,更新配置文件:
$ sudo ldconfig
然后由于这里我们用了Anaconda 来作为python 的库,因此需要在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
# 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-8.0
# 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 := /opt/matlab
# 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 := /opt/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
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 ?= @
修改完配置文件后,最后进行编译:
$ make pycaffe

4. 4 查看python 接口是否编译成功
进入python 环境,进行import 操作:
$ python
>>> import caffe
如果没有出现错误,则编译成功。
但,我在这一步碰到过这么两个错误。
错误1:ImportError: No module named google
谷歌后找到了解决方案:
$ pip install -U scikit-image
如果提示不识别pip 命令,在Ubuntu14.04(64bit)下,使用下面命令安装pip
包管理软件,也可以使用新立得软件包搜索“scikit-image”安装。
$ wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
$ sudo python get-pip.py

错误2:Import error: No module named google.protobuf.internal
解决:这是因为我在编译时是将Anaconda 添加到python 的包含路径中,而
在“conda list”里并没有“protobuf”这一项,因此只需要运行$ conda install
protobuf 后就可以了。
4.5 测试编译是否成功
通过以下两行命令,可测试之前的编译是否成功:
另外可在编译完成后通过如下两个命令检验编译是否完全成功:

$ make test -j56
$ make runtest -j56
其中,我最初在make runtest 的时候是用的指令$ sudo make runtest,结果就报
错:
.build_release/tools/caffe: error while loading shared libraries: libcudart.so.8.0:
cannot open shared object file: No such file or directory
而换为$ make runtest 后就没报错了,刚开始特别纳闷,为什么加了超级用户权
限后反而还会有错误呢,谷歌后才知道错误里的“libcudart.so.8.0”是位于
“/usr/local/cuda-8.0/lib64”文件夹下的,而这个路径我们之前已将其添加进了
“LD_LIBRARY_PATH”环境变量中,但通过sudo 来执行该命令可能会导致其无法
使用“LD_LIBRARY_PATH”环境变量,这只是我目前个人的猜测,如果有清楚具
体原因的人,也请麻烦告知我一下,谢了!

五.小结
执行完上面的四大步后,我们就完成了caffe 及相关软件的安装了,下一步
你就可以从官网上的例程或网上其他的一些教程开始学习caffe、运行一些酷炫
的demo、用自己的数据训练网络、自己搭网络、快速测试自己的idea 等等等等
了。环境配置说简单也简单,说难也难,因为网上有很多教程可参考,但指不定
什么时候你就会碰到教程中未提及的问题,碰到这种情况,就当做是又一次新的
学习机会(ren pin bu hao)吧...总而言之,有问题,谷哥就对了(度娘仅作参考,
你懂的...)。

参考及感谢链接:
1.caffe 学习系列,内容包含caffe 的环境搭建及使用教程,满满的干货。
http://www.cnblogs.com/denny402/p/5067265.html

2.我看的第一个安装CUDA 的教程,之后每次搭环境时这个教程都是我的重要参
考。
http://blog.csdn.net/tingyue_/article/details/45618413
3.caffe 搭建中常见问题及解决方案,以及ubuntu 使用中的问题(持续更新)。
遇到什么新问题,先到这个网页来“ctrl+f”查找一下,大部分都是很有帮助的。
http://www.cnblogs.com/empty16/p/4828476.html
4.这是与我最相似的一个教程,都是用的GTX 1080,也曾给予了我一些帮助
https://github.com/BVLC/caffe/wiki/GeForce-GTX-1080,---CUDA-8.0,---Ubuntu-16.04,
---Caffe

其他还有很多关于解决某个小问题的链接都让我受益匪浅,就不一一列举了,感
谢这么多热心又乐于分享的网友们!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值