前言
历经几十次的caffe安装经历,终于对它的安装过程有了更深的理解,碰到一些bug也知道如何避过。我先重启一下,如果还能正常进入ubuntu那咱们就把配置过程详细叙述一下(因为之前也配置成功做,重启后连ubunu都进不去了,愿上天保佑我!)
好的,我重启了一下,成功回来了,看来这一次是真真正正的配置好了。
配置要点
总结来看,你想成功配置好,
1)我觉得最最重要的,应该对环境变量
有一定理解。
2)要知道自己的opencv安装在哪儿,因为caffe里面有些函数需要调用opencv库。
3)你应该知道每一行终端命令执行的是干嘛用的,如make all。
我的配置:
系统:ubuntu18.04 LTS(其他版本也可)
OpenCV版本:2.4.13(opencv4我没试,恐怕与caffe不兼容)
Caffe来源:https://github.com/BVLC/caffe
到官方这里下载
配置准备
1.参考caffe官网安装指导,下载好相应的依赖项。
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
这里值得一提的是,如果你的系统版本是跟我一样ubuntu18的,那么在apt install里面人家集成了caffe的包,可以自己下载安装,这样好处是非常方便,你安装完之后,想测试的话,终端输入python3
进入python编程环境,然后就可以import caffe
了,但是,目前以我的水平,只能利用它做caffe的python相关编程,C++下,caffe包老是导入不进去,我姑且就认为,这种方式,只能适合Python环境下用caffe. 如果想用从c++版本的caffe,或者想清清楚楚看看caffe的代码结构,还是老老实实从源码安装吧。
2. 源码编译安装Opencv
为什么要下载源码呢,毕竟前面已经sudo apt install libopencv-dev
了呀( ⊙ o ⊙ )!因为这个自动下载的包不好,首先,你即使侥幸配置好了,会发现,有些功能如显示图片和视频功能用不了,因为上面的包是阉割版的!所以乖乖源码编译安装Opencv。版本2和3都可以,但是4可能用在caffe上面不行,因为其Makefile.config里面Opencv选项只有2和3。
从opencv官方下载网站,下载到自己电脑里面,然后参考Opencv 官方安装指导。这里啰嗦一句,官方安装指导才是最好的,别瞎找一篇博客就干,多数搞得一团糟。
默认的opencv安装位置是,/usr/local
,按照默认挺好的,这样后面Makefile.config就简单一些,cafe能容易找到你这个安装的位置。
我就比较折腾了,opencv2的安装位置是,/usr/opencv2_13/
这是我的安装(不要自己瞎折腾):
cd ~/my path to/opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/opencv2_13 ..
make
sudo make install
这里的make install 就会把各种库文件安装到你的预安装位置上。安装完亲自去那个目录里面看看,原本空空的,现在有了一些东西了。
Makefile.config编写
先cp Makefile.config.example Makefile.config
产生个Makefile.config文件,根据自己的需要进行修改,这里面写的如何直接决定你caffe能够成功安装,非常重要。
我的需求是,暂时不需要python接口,所以可以先把关于Python的都注释掉。后面需要再重新编译再说。
这里得说一下,caffe官网的安装说明有许多的bug,我基本上都遇到过,最终生成了我独一无二的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 #因为不适用GPU,所以不配
# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1 #配置cpu版本的caffe必须要打开
# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0 #这意思是=0就是不用啊,不用那里面一些函数咋整,可能有些操作不用opencv也能实现吧。但opencv挺方便的
# USE_LEVELDB := 0 #
# USE_LMDB := 0 #这个你以后需要将图片转成这种LMDB格式,还是要用的
# 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 #如果你的是opencv3版本,就得把这个打开
# 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 #这个不注释掉也没事,因为前面已经是cpu_only了
# 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训练测试
#PYTHON_INCLUDE := /usr/include/python3.6 \
# /usr/lib/python3/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
#强烈建议不要打开,anaconda和系统默认环境有许多库有冲突,打开的话,选择环境变量会很头疼,我就试过。
#ANACONDA_HOME := $(HOME)/anaconda3
#PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
# $(ANACONDA_HOME)/include/python3.7m \
# $(ANACONDA_HOME)/lib/python3.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/hdf5/serial
#这两个地方非常重要,你能不能成功避开各种坑,成功配置caffe,95%要看这里
INCLUDE_DIRS := /usr/opencv2_13/include /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := /usr/opencv2_13/lib /usr/local/lib /usr/lib /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 ?= @
其中找Include和libs的两行代码非常重要,我是这样写的:
INCLUDE_DIRS := /usr/opencv2_13/include /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := /usr/opencv2_13/lib /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial
因为我的opencv2是安装在/usr/opencv2_13目录里面的,一定要放在最前面,让编译器最先寻找这个路径才可以找到,我试过,放在后面就失败了。因为编译器比较笨,多个opencv版本容易犯冲突。首先系统在sudo apt install libopencv-dev时安装在了默认/usr/local目录了,毕竟这个阉割版本的嘛,一看有某些东西没有,就给我报错了,所以把我的安装目录放在最前面,好堵住编译器的嘴。另外那个hdf5的目录也要给包括进去,这个不冲突,所以放在后面没事问题。
具体实践去安装吧
常见错误:
CXX/LD -o .build_release/tools/extract_features.bin
CXX/LD -o .build_release/tools/convert_imageset.bin
CXX/LD -o .build_release/tools/upgrade_net_proto_binary.bin
CXX/LD -o .build_release/tools/compute_image_mean.bin
.build_release/lib/libcaffe.so: undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
.build_release/lib/libcaffe.so: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imencode(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
.build_release/lib/libcaffe.so: undefined reference to `vtable for cv::_InputArray'
collect2: error: ld returned 1 exit status
Makefile:635: recipe for target '.build_release/tools/upgrade_net_proto_binary.bin' failed
make: *** [.build_release/tools/upgrade_net_proto_binary.bin] Error 1
make: *** Waiting for unfinished jobs.