1 安裝NVIDIA driver、CUDA Toolkit和cuDNN
安裝過程請(qǐng)參考另外一篇文章:ubuntu16.04安裝CUDA8.0+cuDNN5.1。
2 安裝opencv
參考o(jì)pencv官方的安裝文檔:Install OpenCV-Python in Ubuntu。安裝opencv有兩種方式:
- Installing OpenCV-Python from Pre-built Binaries
- Building OpenCV from source
這兩種安裝方式的區(qū)別是:ubuntu的系統(tǒng)標(biāo)準(zhǔn)庫(kù)中已經(jīng)包含特定版本的預(yù)編譯好的opencv安裝包,雖然版本不是最新的,目前是opencv2.4.9,但是因?yàn)閏affe只用到了opencv中的基本IO和圖像處理函數(shù),所以opencv2.4.9足夠了;從源代碼安裝opencv的優(yōu)點(diǎn)是可以安裝任何版本的opencv,如果你想使用最新版本的opencv,只能從源碼安裝。兩種安裝方式對(duì)于caffe的配置都OK。
2.1 使用apt命令安裝opencv
ubuntu的系統(tǒng)標(biāo)準(zhǔn)庫(kù)中已經(jīng)包含了某個(gè)版本的預(yù)編譯好的opencv安裝包,可直接使用apt命令安裝:
sudo apt install python-opencv
安裝完后在終端打開python,使用以下命令查看使用apt命令安裝的opencv版本:
ys@ysubuntu:~$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
2.4.9.1
>>>
可以看到,使用apt安裝的opencv版本比較老,是opencv2.4.9。
2.2 從源代碼編譯Opencv3.3
這部分內(nèi)容參考自:OpenCV 3.3 Installation Guide on Ubuntu 16.04。
也可以參考o(jì)pencv官方的安裝文檔:Install OpenCV-Python in Ubuntu。
還有一篇也是官方的參考文檔:Installation in Linux。
先依次使用以下命令安裝必要的依賴項(xiàng)(其中某些應(yīng)該是不需要的,但都先裝上吧):
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 libatlas-base-dev libboost-all-dev libgflags-dev libgoogle-glog-dev liblmdb-dev
?
sudo apt-get install -y python-pip
sudo apt-get install -y libopencv-dev
?
sudo apt-get install --assume-yes pkg-config unzip ffmpeg qtbase5-dev python-dev python3-dev python-numpy python3-numpy
?
sudo apt-get install --assume-yes libopencv-dev libgtk-3-dev libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev
?
sudo apt-get install --assume-yes libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
?
sudo apt-get install --assume-yes libv4l-dev libtbb-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev
?
sudo apt-get install --assume-yes libvorbis-dev libxvidcore-dev v4l-utils vtk6
sudo apt-get install --assume-yes liblapacke-dev libopenblas-dev libgdal-dev checkinstall
如果安裝過程中某些安裝包因?yàn)镠ASH值不匹配無法獲取,就加上--fix-missing命令安裝。
這里使用的是系統(tǒng)自帶的python2.7,不另外安裝anconda2了。
從這里下載python-3.3,0.zip源代碼文件。
下載下來后,使用:
unzip opencv-3.3.0.zip
將其解壓,為了簡(jiǎn)單,將得到的opencv-3.3.0文件夾重命名為opencv文件夾。
然后從命令行進(jìn)入opencv文件,執(zhí)行以下命令:
mkdir build
cd build/
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j8
這是一個(gè)漫長(zhǎng)的編譯過程,大概需要約半小時(shí),如果一切順利,最后會(huì)輸出下圖的結(jié)果:

繼續(xù)執(zhí)行以下命令:
sudo make install
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
sudo apt-get update
sudo make install完成時(shí),如果沒出錯(cuò)應(yīng)該如下圖的輸出:

執(zhí)行完上面步驟之后,重啟電腦。
opencv3.3編譯完成。
編譯時(shí)可能遇到的問題:
問題1:
在高版本的cuda上無法編譯低版本的opencv,比如我安裝的cuda9.0,此時(shí)安裝opencv3.3.0就會(huì)失敗,但是安裝較新的opencv3.4.4就沒問題。
問題2:
在安裝高版本的opencv時(shí),可能遇到opencv編譯安裝成功,但是在python2/python3中仍然無法導(dǎo)入的情況,此時(shí)只需執(zhí)行:
sudo cp path/to/build/lib/cv2.so /usr/local/lib/python2.7/dist-packages/
sudo cp path/to/build/lib/python3/cv2.cpython-35m-x86_64-linux-gnu.so /usr/local/lib/python3.5/dist-packages/
就是把build文件夾下面的兩個(gè)文件拷貝到相應(yīng)的python模塊文件夾下面。
2.3 使用pip命令安裝opencv
一種十分方便的安裝opencv的方式,可指定安裝特定版本
pip install opencv-python==3.3.0
如果沒有該版本,錯(cuò)誤信息中會(huì)有可用版本號(hào)提示:
Collecting opencv-python==3.3.0
Could not find a version that satisfies the requirement opencv-python==3.3.0 (from versions: 3.1.0.0, 3.1.0.1, 3.1.0.2, 3.1.0.3, 3.1.0.4, 3.1.0.5, 3.2.0.6, 3.2.0.7, 3.2.0.8, 3.3.0.9, 3.3.0.10, 3.3.1.11, 3.4.0.12, 3.4.0.14, 3.4.1.15, 3.4.2.16, 3.4.2.17, 3.4.3.18, 3.4.4.19, 3.4.5.20, 4.0.0.21, 4.0.1.23, 4.0.1.24, 4.1.0.25)
No matching distribution found for opencv-python==3.3.0
這時(shí)就可選擇其中想要安裝的版本了。
3 安裝必要的依賴項(xiàng)
官網(wǎng)的caffe安裝教程中,編譯caffe前需要安裝以下依賴項(xiàng):
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 libopenblas-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
4 編譯caffe
首先從Github上面下載caffe源代碼:
git clone https://github.com/BVLC/caffe.git
首先修改Makefile.config文件,在命令行進(jìn)入caffe主目錄,執(zhí)行
cp Makefile.config.example Makefile.config
使用atom打開Makefile.config配置文件:
atom Makefile.config
Makefile.config文件的修改參考這篇文章:編譯caffe-Makefile.config解析。
比如,我使用的GTX1080ti、CUDA8.0.61、Cudnn v5.1、系統(tǒng)自帶的python2.7、opencv2.4.9。
修改過后的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 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)/anaconda
# 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
?
# 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 ?= @
接著修改Makefile文件,將大約415行處的:
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
替換為:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
最后修改/usr/local/cuda/include/host_config.h 文件,使用atom打開:
sudo atom /usr/local/cuda/include/host_config.h
將以下這句:
#error-- unsupported GNU version! gcc versions later than 5 are not supported!
改為:
//#error-- unsupported GNU version! gcc versions later than 5 are not supported!
也就是把它注釋掉。這一步如果不注釋掉的話,就要像很多網(wǎng)上教程那樣,手動(dòng)將gcc版本降級(jí)。
準(zhǔn)備工作完畢,依次執(zhí)行以下命令編譯caffe:
make all -j8
make test -j8
make runtest -j8
加上-j8能大大提高編譯過程,這是利用了多核處理器的并行同步執(zhí)行的優(yōu)點(diǎn),這里的數(shù)字取決于你的電腦CPU的線程數(shù)量,一般最多和CPU的線程數(shù)量相等。
5 編譯pycaffe
以上編譯過程只是完成了caffe的命令行接口,下面安裝caffe的python接口。
首先cd到path/to/your/caffe/python/目錄,就是你clone的caffe下的python目錄,然后執(zhí)行:
for req in $(cat requirements.txt); do sudo -H pip install $req; done
這一句是安裝幾個(gè)pycaffe依賴的python模塊。這些模塊被安裝到了/usr/local/lib/python2.7/dist-packages目錄下。
然后cd回caffe主目錄,執(zhí)行:
make pycaffe -j8 #編譯pycaffe接口
最后為了能在python中import caffe,需要在~/.bashrc中添加以下路徑:
export PYTHONPATH=/home/yan/caffe/python:$PYTHONPATH
因?yàn)槲野裞affe下載到了home目錄,具體的路徑根據(jù)你下載的caffe位置而定。
這樣就能順利import caffe了:
yan@yanubuntu:~$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>>
6 MNIST數(shù)據(jù)集測(cè)試
在caffe主目錄下打開終端,執(zhí)行腳本:
bash data/mnist/get_mnist.sh
這個(gè)腳本的功能是從網(wǎng)上下載MNIST數(shù)據(jù)集二進(jìn)制文件,執(zhí)行完后,在data/mnist/下面得到如下4個(gè)文件:

得到數(shù)據(jù)集之后,接著需要將二進(jìn)制的數(shù)據(jù)集格式轉(zhuǎn)換成caffe需要的LMDB格式。
執(zhí)行以下命令:
bash example/mnist/create_mnist.sh
在examples/mnist/文件夾下得到兩個(gè)LMDB格式的數(shù)據(jù)集文件:

接著執(zhí)行:
bash example/mnist/train_lenet.sh
如果一切順利,系統(tǒng)就開始訓(xùn)練模型了,最終會(huì)得到大約0.99的準(zhǔn)確率。
MNIST數(shù)據(jù)集測(cè)試完成。