linux+anaconda++cuda+cudnn+opencv+caffe-gpu安装的流程详解

                             anaconda下安装caffe的流程

这个是17年刚上研一配的caffe环境,不得不说,对于刚接触深度学习的同学来说,caffe的配置让你会感到沮丧,因为问题实在太多。但是caffe还是非常值得我们学习的,尤其对于入门的同学。caffe的源码简洁而有条理,深入学习也是很有必要的。即使我们现在都将框架建立在tensorflow,keras,pytorch上,也值得我们学习。

下面将配置分享给大家,如果有什么问题,欢迎探讨。

1,ubuntu16.04的安装

安装ubuntu16.04,这个网上有很多教材,多参考,注意两点就行:

1,用UEFI或者默认启动,需要修改设置(开机一直按esc进入),建议用UEFI。

2,建议手动分盘,很简单,了解每个区都是干什么的,那么你就可以分配好。我的256的固态。

     根目录:/,挂载在主分区,用ex4文件,大小20左右,用处就是一些系统的一些缓存和日志,太大没有用。

     boost,用于启动系统,大小514M。逻辑分区,ex4.

     usr,用户安装的软件,系统,各种更新,都在这里面,建议也尽量大。256固态,60G左右。逻辑分区,ex4.

     home,用户区域,你的下载,存储的文件都在这个区,尽量大,一般把剩下的都给home。逻辑分区,ex4.

注意,最后的安装引导要选,boost文件。

当然我们可以不手动分区,可以只有/,boost,那么生成的系统里,会自动分配好,home,usr的,只不过他们的大小和根目录/里的其他文件一样,共享根目录空间。所以这会有系统风险!你在安装linux时,要看一看linux教材,你要懂一些linux系统,不要局限与只是一些基本的命令。

当然你也可以安装其他linux版本。但是ubnutu支持caffe需要的依赖等好一些。你可以看看很多文件都是支持ubuntu下载。

 

2,nvida驱动安装。

     建议安装比最新版本低一个的版本,我的是384.最新的比较稳定。驱动的作用这里不再多说。具体安装过程网上有很多教程,大家都是对的。

    最后用,nvida-smi查看。

需要说明一点就是cuda的版本问题。cuda8.0目前支持比较好。如果按照官网介绍的安装cuda9.0,这回在安装opencv等时出现问题,原因在于,cuda太新,opencv发布的版本不受9.0的某些支持。

3,cuda8.0的安装

cuda是英伟达的关于gpu的一个加速器,详细介绍看官网。

去官网下载文件。首先在官网上下载cuda8安装文件https://developer.nvidia.com/cuda-downloads 
(经测试最新的9版本及387驱动在caffe编译runtest时会出现gpu错误,官网说支持caffe2,因为要使用caffe所以这里安装了低版本的驱动375.66和cuda8)

$sudo sh NVIDIA-Linux-x86_64-xxx.run

accept,注意第一个问题是否安装驱动要选no,yes, 
安装位置按默认,yes,yes,sample安装位置输入自定义的~/Develop。

配置系统环境变量:

添加环境变量:

$export PATH=/usr/local/cuda-8.0/bin:$PATH

$ export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH

上面的指令可直接在终端输入,即可添加环境变量(注意千万不要听信网上这样添加的环境变量:

再声明一下环境变量,并将其写入到 ~/.bashrc 的尾部:

export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}

export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

我试过一次,真的会搞死你,因为后面会出现其他系统本身的指令都不能用的问题,反正你可以试试,

然后你就去百度一大堆问题,发现有些庸医跟你说修改 profile 文件,这回是真的死了...你一旦修改了

profile 文件,那你在重启的时候会在登陆界面循环登陆,卡死你,反正我后面没招了,只能重装系统~)

3. 最后再来测试一下 CUDA,运行:在 NVIDIA_CUDA-8.0_Samples 下执行该指令:(其实只要环境变量

设置好,在哪里执行下面的指令都可以)

nvidia-smi

 

4,cudnn的安装

这个安装很简单。注意两点,1,cudnn是用来加速的,最新版本为最好,没有说和cuda匹配的问题,用最新的v7就行。2,关于要建立软链接的问题,你可以安装其他教程建立软链接,也可以用特殊方式安装不需要建立软链接。

这个问题不是很大。

$ sudo cp cuda/include/cudnn.h /usr/local/cuda/include/

(根据你解压后 cuda 的路径的不同而不同,注意

中间有空格)

$ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/

(根据你解压后 cuda 的路径的不同而不同,注

意中间有空格)

cd /usr/local/lib

$ sudo chmod +r libcudnn.so.5.1.3

$ sudo ln -sf libcudnn.so.5.1.3 libcudnn.so.5

$ sudo ln -sf libcudnn.so.5 libcudnn.so

$ sudo ldconfig

(注意,每一次修改后都要运行使改变生效)

 首先安装一系列的依赖项文件:

sudo apt-get install build-essential

sudo apt-get install --no-install-recommends libboost-all-dev

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev

libboost-all-dev libhdf5-serial-dev

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev protobufcompiler

sudo apt-get install libatlas-base-dev

sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev

libavformat-dev libswscale-dev

sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev

libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

sudo apt-get install vim

 

 注意,这是在没有安装anaconda的情况下对ubuntu16.04系统带的python的库安装,因为这些caffe需要依赖,所以,在caffe的python-lib设置里只能设置一个,或者用系统自带的,或者用anaconda的,这两个都对,但是切忌,不能不选或者都选,因为系统 自带的python2.7和anaconda里的上述库有可能会冲突。安装软件的最好途径就是你把需要的软件都非常明了,知道那一块都是做什么的,为什么要配置,那么,你就会少去很多坑。别人教程只是适合小部分情况,不要全部参考。 

5,opencv2.4.13.15

去 opencv 官网下载 opencv 2.4.13.15,(不要用opencv3,还是cuda和它的版本问题),然后使用 unzip 命令解压 opencv 文件

$ cd ~/opencv xx(根据个人刚刚下载的 opencv 文件名字而修改)

$ mkdir build

$ cd build

~/opencv/build# cmake -D CMAKE_BUILD_TYPE=Release -D

CMAKE_INSTALL_PREFIX=/usr/local .. (注意后面的省略号~都是需要添加进去的,都是在同一条语句下的)

~/opencv/build# make -j4 #根据个人电脑选择 最好是选择电脑的核数

(怎么查询个人电脑的核数?$ cat /proc/cpuinfo| grep "cpu cores"| uniq)

~/opencv/build# sudo make install

不报错,完成。

6,anaconda2的安装

官网下载,直接sh命令运行。注意安装python2.7.14或者python3就行。没有什么技巧。

anaconda安装后禁了环境变量。选了no。用export PATH="~/anaconda2/bin:$PATH"代替。

当然你也可以设置为yes。

下面的才是重点,当你按要求配置好之前的依赖后,按照下面的修改,可以用anaconda很好的解决caffe的依赖问题。而且不会报错。

anaconda好处在于集成了很多python的库,这个非常的好,不仅解决了python包的依赖问题,而且非常便于用pycaffe接口可视化训练模型。关于pycaffe接口的设置。

7,caffe的安装

在github上clone。

1,~/caffe# cp Makefile.config.example Makefile.config

2,修改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
# 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 \
        /usr/include/python2.7#修改
LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda2/lib#修改
# 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

# 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 ?= @

 

3,修改~/. Bashrc

                                                                   ~/. bashrc

# added by Anaconda2 installer

#export PATH="/home/iebsn/anaconda2/bin:$PATH"

#export PYTHONPATH=/home/iebsn/soft3/caffe/python:$PYTHONPATH

export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

export PATH=/usr/local/cuda/bin:$PATH

export PATH

export PATH="~/anaconda2/bin:$PATH"

 

经过上面的设置,可以避免出现各种错误。直接passed。尤其hdf5版本不匹配问题。

$:make all -j4

$make test

$make runtest

到此,caffe安装成功。你可以使用简单的mnisit测试一下。因为我们把图片格式转为了lmdb,所以你在命令行看到的就是数字化的结果。看不到可视化的结果。如果想更加直观的使用,那么我们需要配置caffe的两个接口,供python调用或者matlab使用。一般pycaffe就够了。

pycaffe接口的配置

使用pip更新一些库(不是必须)。然后pip –version查看是来此哪里的pip,安装anaconda后一般pip显示为anaconda里的。这个和conda一样,都是包的管理工具,可以更新,安装,删除,等操作。之后配置pycaffe接口。

confige as this in ~/.bashrc: export PYTHONPATH=/home/iebsn/soft3/caffe/python:$PYTOHNPATH

更新之后的~/. Bashrc

# added by Anaconda2 installer
export PATH="/home/iebsn/anaconda2/bin:$PATH"
export PYTHONPATH=/home/iebsn/soft3/caffe/python:$PYTHONPATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
export PATH=/usr/local/cuda/bin:$PATH
export PATH
export PATH="~/anaconda2/bin:$PATH"
make pycaffe

(在caffe主目录下)

在任何目录下进入python,import caffe,不报错就可以了。当然你也可以进入python3,ipython,ipython notbook使用。或者集成在spyder里。

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值