ubutu16.04 python3.6 opencv3.2 caffe(CPU) 填坑指南

最近由于需要,安装一下caffe,由于手边暂时没有gpu,所以在自己笔记本先安装一下cpu版本的caffe。由于caffe是属于比较老的框架,然后对python 2.7支持比较好,或者python3.5以下也是可以的,但是由于我们的python是3.6,以及tensorflow == 1.6,因此我们尝试在python 3.6的情况下,安装caffe CPU版本。

系统版本:

  • ubutu 16.04
  • anoconda 4.3.30
  • python 3.6
  • opencv 3.2.0
  • caffe 1.0.0

1. 首先利用anaconda创建虚拟环境

为了和tensorflowpytorchtheano等环境区分,我们首先创建一个caffe环境,使用anaconda创建。conda创建、查看、删除虚拟环境

1.首先查看当前已有环境:

conda-env list

2.创建caffe虚拟环境

conda create -n CAFFE  python=3.6

3.进入环境

source activate CAFFE

小提示,退出环境命令为source deactivate

2. 安装opencv3以及其他各种依赖项

2.1 安装opencv3

为了安装caffe,我们首先先将opencv3安装好

参考我的博客:
ubutu16.04卸载opencv2安装opencv3

2.2 安装其他各种依赖项

1.切换到root权限,su, 依次安装:

sudo apt-get install libprotobuf-dev 
sudo apt-get install libleveldb-dev 
sudo apt-get install libsnappy-dev 
sudo apt-get install libopencv-dev 
sudo apt-get install libhdf5-serial-dev 
sudo apt-get install protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev

CPU Only的情况下,跳过了CUDA相关的安装;
接下来是BLAS:

sudo apt-get install libatlas-base-dev

使用默认Python来建立pycaffe接口,需要安装:

sudo apt-get install python-dev

一些兼容性依赖库:

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

3 安装caffe

参考:

  1. ubuntu16.04 python3.6 caffe(CPU) 配置记录
  2. caffe安装:基于anaconda3—python3.6, linux, 仅CPU
  3. Ubuntu16.04安装Caffe(CPU Only)
  4. 利用C++ Boost库将C++项目封装为Python模块

3.1 下载caffe相关包

没有安装git的话需要先装一下git,同样也是需要root权限的。

sudo apt-get install git

下载Caffe源码:

git clone https://github.com/BVLC/caffe.git

如果需要Caffe的Python接口,切换到caffe下的python目录下,输入以下命令下载python依赖库(先安装pip):

sudo apt-get install python-pip
for req in $(cat requirements.txt); do pip install $req; done

小技巧,从这里可以看出这里的requirements.txt需要安装的东西还是比较多的,由于下载速度可能会比较慢,所以有些包可能需要下载,然后离线安装,安装包的格式就是.whl.zip格式等。

ubutu16.04 python3.6 opencv3.2 caffe(CPU) 填坑指南_第1张图片

3.2 开始编译caffe

前面的各种包并不是太难,caffe难装的原因就是编译可能会出现各种各样的错误,这里我们也是一步一步来。

3.2.1 到Caffe文件夹中
cd caffe

拷贝一份Makefile.config.example并重命名成Makefile.config,修改该配置文件:

cp Makefile.config.example Makefile.config
3.2.2 修改Makefile.config

这一步非常麻烦,但是也十分重要。。。

这是我的Makefile.config,里面做了一些修改。主要参考的是ubuntu16.04 python3.6 caffe(CPU) 配置记录,同时也在这个基础上,做了一些修改,这是依照后面的错误来的。

  1. 添加HOME := /home/xuchao,原来没有写。
  2. 添加代码
LIBRARIES += glog gflags protobuf leveldb snappy lmdb boost_system hdf5_hl hdf5 m  opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

我的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
# This code is taken from https://github.com/sh1r0/caffe-android-lib
# USE_HDF5 := 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/local/lib/python2.7/dist-packages/numpy/core/include

# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.

# HOME path !!!!

HOME := /home/xuchao

ANACONDA_HOME := $(HOME)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		  $(ANACONDA_HOME)/include/python3.6m \
		  $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)

PYTHON_LIBRARIES := boost_python3 python3.6m

# PYTHON_INCLUDE := /usr/include/python3.6m \
# /usr/lib/python3.6/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
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

LIBRARIES += glog gflags protobuf leveldb snappy lmdb boost_system hdf5_hl hdf5 m  opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

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

LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib#
3.2.3 安装libboost(基于python3.6)的库

1.首先输入命令:

sudo ln -s /home/xuchao/anaconda3/bin/python3  /usr/lib/python3.6m(这里是建立一个软链接,把我本地目录下的python3.6加到usr/lib/中去。

2.下载boost_1_67_0

wget -O boost_1_67_0.tar.gz http://sourceforge.net/projects/boost/files/boost/1.67.0/boost_1_67_0.tar.gz/download  

tar xzvf boost_1_67_0.tar.gz      

3.安装附加依赖库

sudo apt-get update  

sudo apt-get install build-essential g++ python-dev autotools-dev libicu-dev build-essential libbz2-dev libboost-all-dev 

4.编译boost_1_67_0

cd boost_1_67_0/

./bootstrap.sh --with-libraries=python --with-toolset=gcc  

./b2 --with-python include="/home/xuchao/anaconda3/include/python3.6m/"  

sudo ./b2 install

/home/xuchao/可能要换成你自己的目录名字。

编译安装成功后,/usr/local/lib下会有libboost_python36.solibboost_python36.a,有些应用link时需要的是libboost_python3.so或者libboost_python3.a,我们建个软链:

cd /usr/local/lib  

sudo ln -s libboost_python-py36.so libboost_python3.so  

sudo ln -s libboost_python-py36.a libboost_python3.a  

这时,我们要把/usr/local/lib 中,相关文件,建立相对于名称的软链接到/usr/lib/x86_64-linux-gnu中。

sudo cp /usr/local/lib/libboost_python36.a  /usr/lib/x86_64-linux-gnu/libboost_python_python36.a

sudo cp /usr/local/lib/libboost_python36.so.1.67.0  /usr/lib/x86_64-linux-gnu/libboost_python3.so
3.2.4 编译caffe

依次输入以下命令:

sudo make all -j4
sudo make test -j4
sudo make runtest -j4
sudo make pycaffe -j4

ubutu16.04 python3.6 opencv3.2 caffe(CPU) 填坑指南_第2张图片
ubutu16.04 python3.6 opencv3.2 caffe(CPU) 填坑指南_第3张图片
make默认单核运算,如果想加快速度,我这里是4核,可以在每条命令后面加上-j4,如果有报错,建议最好make clean重新开始。
如果所有测试都通过,则说明安装好了。

4 测试caffe

测试Caffe的Python接口,切换到caffe/python文件目录下,记录下来当前路径,输入以下命令:

export PYTHONPATH=/home/xuchao/caffe/python:$PYTHONPATH

/home/xuchao换成你自己的。

进入python环境,输入:

import caffe

果没有报错,证明安装成功。
ubutu16.04 python3.6 opencv3.2 caffe(CPU) 填坑指南_第4张图片
上面的方法,一旦关闭终端或者打开新终端则失效,如果放到配置文件中,可以永久有效果,命令操作如下:

#A.把环境变量路径放到 ~/.bashrc文件中
sudo echo export PYTHONPATH="~/caffe/python" >> ~/.bashrc
#B.使环境变量生效
source ~/.bashrc

ubutu16.04 python3.6 opencv3.2 caffe(CPU) 填坑指南_第5张图片
其他参考:

  1. BVLC/caffe
  2. Error: ‘make all’ ‘make test’ #2348
  3. Trouble building caffe on Ubuntu 18.04 #6720

花了一天的时间,终于把caffe安装好了。后面安装gpu版本的caffe再说了。

你可能感兴趣的:(caffe,caffe,python3.6,opencv3,conda,make)