Ubuntu16.04下安装caffe,可GPU加速
关于caffe的安装,这里首先要安装Ubuntu16.04,博客上有很多教程。推介三点:1.别在虚拟机上装,推荐双系统安装。2.采用刻盘安装,关于刻盘安装方法,网上都有教程。由于每个人的电脑版本型号不一样,在安装的过程中同一问题会出现不一样的操作,这方面大家有问题的可以在下面评论,lz尽量帮忙解答。3.在安装过程中,要分盘,我介意最少200G的空间,因为大部分是用来搞深度学习的,数据采集需要很大的
关于caffe的安装,这里首先要安装Ubuntu16.04,博客上有很多教程。推介三点:1.别在虚拟机上装,推荐双系统安装。2.采用刻盘安装,关于刻盘安装方法,网上都有教程。由于每个人的电脑版本型号不一样,在安装的过程中同一问题会出现不一样的操作,这方面大家有问题的可以在下面评论,lz尽量帮忙解答。3.在安装过程中,要分盘,我介意最少200G的空间,因为大部分是用来搞深度学习的,数据采集需要很大的空间。
好了,言归正传,开始安装。
Cuda的安装:这里参照keras中文文档里Cuda的安装
打开终端输入:
#系统升级(目的:建议换源,我这里用的阿里的源,相对速度快一点)
sudo apt update
sudo apt upgrade
#安装python基础开发包
sudo apt install -y python-dev python-pip python-nose gcc g++ git gfortran vim
#安装运算加速库,打开终端输入
sudo apt install -y libopenblas-dev liblapack-dev libatlas-base-dev
打开到下载了cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb的目录,注意这里下载的是deb格式,比run格式操作要简单方便很多,也不需要自己下载安装nvidia的驱动。在目录下打开终端输入
sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb
sudo apt update
sudo apt -y install cuda
将CUDA路径添加至环境变量 在终端输入:
sudo gedit /etc/profile
在profile文件中添加:
export CUDA_HOME=/usr/local/cuda-8.0
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}}
之后
source /etc/profile
测试 在终端输入:
nvcc -V
会得到相应的nvcc编译器相应的信息,那么CUDA配置成功了。(记得重启系统)
cudnn的安装:(该步装不装差别不是很大,嫌麻烦可以不用安装
采用5.1版本,即是cudnn-8.0-win-x64-v5.1-prod.zip。 下载解压出来是名为cuda的文件夹,里面有bin、include、lib,将三个文件夹复制到安装CUDA的地方覆盖对应文件夹,在终端中输入:
sudo cp include/cudnn.h /usr/local/cuda/include/
sudo cp lib64/* /usr/local/cuda/lib64/
cd /usr/local/cuda/lib64
sudo ln -sf libcudnn.so.5.1.10 libcudnn.so.5
sudo ln -sf libcudnn.so.5 libcudnn.so
sudo ldconfig -v
Opencv2.4.13的安装:
安装相关依赖项:
>>>sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libdc1394-22-dev libatlas-base-dev gfortran
可能会出现部分依赖无法安装依赖的情况,可能是ubuntu版本过高(本文为ubuntu16.04)
安装cmake-gui:
sudo apt-get install cmake-qt-gui
打开到下载了opencv-2.4.13.zip的目录,建议安装2.4.13在目录下打开终端输入:
unzip opencv-2.4.13.zip
cd opencv-2.4.13/
mkdir opencv-debug
cd opencv-debug/
cmake-gui
在如下的界面下选择源码目录位置,debug目录,再configure ,generate。没有报错即可。
Figure1 Cmake界面
编译安装:
在debug目录下:
make -j8
要是中途出现了一些问题是与cuda有关的,打开opencv下面那个cmakelist文件把with_cuda设置为OFF,之后再cmake,再编译。之后:
sudo make install
环境变量配置:
安装成功后还需要设置opencv的环境变量,打开文件
sudo gedit /etc/profile
在最后一行输入如下内容:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
执行下列命令, 使环境变量立即生效
source /etc/profile
设置lib库路径,打开文件
sudo gedit /etc/ld.so.conf.d/opencv.conf
opencv的库一般安装在’/usr/local/lib’文件夹下,在文件内添加
/usr/local/lib
执行下列命令使之立刻生效
sudo ldconfig
经过上面的流程,这样就可以在eclipse里或者qtcreator里用opencv了。 不过要配置号相应的路径和lib文件。
验证是否安装成功
对opencv的sample进行编译并运行,依次执行如下代码
cd opencv-2.4.13/samples/c
./build_all.sh
./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5 lena.jpg
Caffe的安装:
首先安装各种依赖包,命令行执行入下命令:
sudo apt-get update
sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install -y libatlas-base-dev
sudo apt-get install –y --no-install-recommends libboost-all-dev
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install -y python-pip
sudo apt-get install -y python-dev
sudo apt-get install -y python-numpy python-scipy
将终端cd到想要安装caffe的位置,从github上clone caffe,执行如下指令:
git clone https://github.com/BVLC/caffe.git
从github上git caffe
cd caffe
打开到刚刚git下来的caffe
sudo cp Makefile.config.example Makefile.config
将Makefile.config.example的内容复制到Makefile.config
sudo gedit Makefile.config
打开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 := 2
# 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.
# For CUDA >= 9.0, comment the *_20 and *_21 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)/anaconda3
# 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)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1
# Uucomment 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 ?= @
修改的重点我都已经标记出来,其余的要改的路径啥的根据自己的安装修改即可,这个网上教程也挺全的。配置完成Makefile.config文件以后,保存并执行以下命令:
make all -j8
make runtest -j8
make pycaffe -j8
make matcaffe -j8
如果make runtest –j8 指令能够顺利通过,表明caffe已基本配置完成,接下来只需检查python和matlab接口是否能用。
最终查看python接口是否编译成功:进入python环境,进行import操作:
# python
>>> import caffe
如果没有提示错误,则编译成功。
更多推荐
所有评论(0)