gcc6.3 + Caffe+matlab2018a+python+cuda 编译安装, protobuf reference解决办法
1.重点
这里主要是罗列一些问题
-
matlab只支持某个版本的gcc,matlab与gcc版本关系如下:
Matlab 2014a gcc/g++ 4.7.x,Matlab 2017a gcc/g++ 4.9.x
Matlab 2018a gcc/g++ 6.3.x, 其他版本的需要网上搜一下 -
考虑gcc兼容性问题,如果是自带源里的gcc,以下库是自动兼容的;如果是第三方源里的gcc,则有可能出现不兼容问题,为了保持gcc版本不变,不会再安装依赖的时候被动更新,使用 apt-mark hold pkgName / apt-mark unhold pkgName 使库不能被修改,此时,如果安装以下库时报错,则该库需要重新编译。
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
apt-get install libopenblas-dev
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
注:我重新编译了OpenBlas和HDF5
出现protobuf reference的错误:
这时候是由于protobuf与gcc版本不兼容问题,升级gcc版本吧,当然也不要太高,根据我的尝试,protobuf2.6.1可以和gcc5.5 、gcc6.3 、gcc6.5兼容,与gcc 4.*不兼容(此时会出现protobuf reference的错误),gcc8.*等更高则需要caffe支持c++11(具体怎么弄我也不清楚,只是编译的时候提示需要支持c++11标准)
2. 安装步骤
1.安装gcc-6.3
1). sudo apt-get remove --purge gcc-6 g++-6 gcc-6-multilib
sudo apt autoremove
2). Add the gcc-6.3 PPA:
3). sudo add-apt-repository ppa:jonathonf/gcc-6.3
sudo apt-get update
4). Install the specific gcc-6.3 version, `6.3.0-21ubuntu1~16.04.york0` in our case:
VER=6.3.0-21ubuntu1~16.04.york0 && sudo apt-get install gcc-6-base=$VER gcc-6=$VER g++-6=$VER cpp-6=$VER libgcc-6-dev=$VER libstdc++-6-dev=$VER libasan3=$VER
5.) Check the version of the just installed gcc with `gcc-6 -v`. Expected:
gcc version 6.3.0 20170628 (Ubuntu 6.3.0-21ubuntu1~16.04.york0)
参考网址:https://askubuntu.com/questions/1088658/how-to-install-gcc6-3-x-g6-3-x-on-ubuntu-16-04
https://launchpad.net/~jonathonf/+archive/ubuntu/gcc-6.3
2. 安装编译caffe所需要的依赖
1)先不让依赖更新gcc6.3
apt-mark hold gcc-6.3 # 这是保持gcc不变,取消则用apt-mark unhold gcc-6.3
- 安装依赖
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
apt-get install libopenblas-dev
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
注:如果在安装过程中报错,则说明更依赖需要其对应版本的gcc,与gcc6.3不兼容,请用源码编译安装
- 安装完依赖之后,就需要在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.
CUDA_ARCH := -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 := open #使用OpenBlas库,我重新编译了OpenBlas,没有用apt-get install 安装
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /usr/local/OpenBlas/include # 编译后,需要设置路径,以使caffe能找到openblas
BLAS_LIB := /usr/local/OpenBlas/lib
# 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 := /opt/matlab2018a # 编译matlab时,需要设置该路径
# 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.
# python环境 和 python头文件路径,我用的是miniconda3,anaconda参照这个设置
ANACONDA_HOME := $(HOME)/miniconda3/envs/caffe_img
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.5m \
$(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)
# 设置python库路径
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库路径(和上面的路径不一样,虽然都是库路径,但是内容不一样,有区别,设置好就完事了)
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 # 编译python接口pycaffe
# Whatever else you find you need goes here.
# 这里设置caffe依赖库的include头文件路径和lib库文件路径
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /usr/local/ /usr/local/hdf5/include # /usr/local/protobuf/include/
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/local/hdf5/lib# /usr/local/protobuf/lib
# 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 ?= @
- 编译caffe
1. make all -jN # N为你的淳朴有多少个核心,我这的是48个,所以是-j48
2. make pycaffe -j48
3. make matcaffe -j48
4. make test -j48
5. make runtest -j48 # 测试所有接口
6. make mattest -j48 # 测试matlab接口
6. 编译完成,配置下接口,就好了,待后续更新
- 运行mattest出错的修复办法:
caffe/matlab/+caffe/Net.m 或 caffe/matlab/+caffe/Solver.m
修改如下:
function delete (self) %caffe重新建立网络时会自动删除之前建立的网络
if self.isvalid
caffe_('delete_net', self.hNet_self); 或者 caffe_('delete_solver', self.hSolver_self);
end
end
引用:https://github.com/BVLC/caffe/pull/5588