Ink One

Ubuntu16.04 安装caffe

安装 OpenCV 3.2.0

创建virtualenv

1
virtualenv caffe

激活virtualenv

1
source caffe/bin/activate

从 GitHub 上 clone 最新的 OpenCV 和 OpenCV_contrib

1
2
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

在OpenCV目录下创建build文件夹

1
2
3
cd opencv
mkdir build
cd build

安装OpenCV依赖库

1
2
3
4
5
6
7
sudo apt-get install build-essential
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 libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev #视频处理
sudo apt-get install ffmpeg
pip install numpy

Build OpenCV (with Cuda 8.0)

1
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON -D WITH_CUBLAS=ON -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D CUDA_ARCH_BIN="6.1" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -D BUILD_EXAMPLES=ON ..

Install OpenCV

1
2
make -j8
sudo make install

复制 cv2.so 到virtualenv下

1
sudo cp /usr/local/lib/python2.7/site-packages/cv2.so ~/caffe/lib/python2.7/site-packages/

验证是否安装成功

1
python -c "import cv2; print cv2.__version__"

如果正确安装将输出3.2.0

安装 Caffe

安装Caffe依赖库

1
2
3
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 libgflags-dev libgoogle-glog-dev liblmdb-dev

从 GitHub 上 clone Caffe

1
git clone https://github.com/BVLC/caffe.git ~/caffe_pkg

安装 requirements 到 virtualenv

1
pip install -r ~/caffe_pkg/python/requirements.txt

修改配置文件

1
2
3
cd caffe_pkg #打开到刚刚git下来的caffe
cp Makefile.config.example Makefile.config # make指令只能make Makefile.config
gedit Makefile.config #编辑Makefile.config文件

编辑配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
USE_CUDNN := 1 # Build with CuDNN
OPENCV_VERSION := 3 # 使用 OpenCV3
BLAS := mkl # BLAS库使用Intel的mkl
MATLAB_DIR := /usr/local/MATLAB/R2017a # MATLAB位置
PYTHON_INCLUDE := ~/caffe/include/python2.7 \
~/caffe/lib/python2.7/site-packages/numpy/core/include
WITH_PYTHON_LAYER := 1 # 支持使用Python编写layers
##
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
##
USE_NCCL := 1

编译 Caffe

1
2
3
make all -j8
make test -j8
make runtest

make pycaffe

1
2
3
4
5
6
gedit ~/.bashrc
# add line
# export PYTHONPATH="~/caffe/python:$PYTHONPATH"
source ~/.bashrc
make pycaffe
make distribute

验证是否安装成功

1
2
python
>>> import caffe

若无报错则安装成功。