Caffe原生版本下载地址https://github.com/BVLC/caffe 。后面开发者根据自己需要添加的功能,会在原生caffe文件夹里添加自己的layer,变成衍生版的caffe。一台电脑里可以同时安装多个版本的caffe,如原生caffe, caffe-enet, caffe-segnet等,只需要放在不同的文件夹,分别进行编译即可。卸载只需要删除相应的文件夹即可。具体安装步骤:
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 libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get update
sudo apt-get upgrade
若使用python接口,还需要安装caffe/python/requirements.txt文件里需要的依赖项。conda list查看已经安装的包后发现缺少的包并安装:
conda install leveldb
conda install protobuf
Shortcut: 链接中下载已经修改好的makefile.config文件 https://download.csdn.net/download/cxiazaiyu/10635167 ,放在caffe路径下即可。
普通方法:按照下面的操作一一修改。
sudo cp Makefile.config.example Makefile.config
sudo gedit Makefile.config
根据自己的情况,将如下项前的#去除:
# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1
若使用anaconda,则下面的部分也注释掉:
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.ANACONDA_HOME := $(HOME)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.6m \
$(ANACONDA_HOME)/lib/python3.6/site-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# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.6m
并在文件末尾加上:
LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib
注意:用anaconda的话不要再把下面的部分取消注释啦!!!
# Uncomment to use Python 3 (default is Python 2)
#PYTHON_INCLUDE := /usr/include/python3.5m \
# /usr/lib/python3.5/dist-packages/numpy/core/include \
# 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
参考:Dark_Miro的博客:caffe编译安装( Ubuntu16.04.3+cuda8.0+opencv3.3.0+anaconda3)和 xunan003的博客: caffe利用anaconda配置python接口(cpu版可视化工具)
sudo gedit Makefile
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
改成:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
改成:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5 opencv_core opencv_imgproc opencv_imgcodecs opencv_highgui
sudo gedit ~/.bashrc
在文件末尾加入(文件路径根据自己的情况确定):
export PYTHONPATH=/home/yly/ENet/caffe-enet/python:$PYTHONPATH
更新配置:
sudo ldconfig
若曾经编译失败过,想重新编译,需要先执行:
sudo make clean
然后执行:
make pycaffe -j$(nproc)
make all -j$(nproc) -Wno-deprecated-gpu-targets
make test -j$(nproc) -Wno-deprecated-gpu-targets
make runtest -j$(nproc) -Wno-deprecated-gpu-targets
其中-j$(nproc)表示使用最大可利用的多线程执行,一般6核cpu可以直接写-j12。这里若在make pycaffe前加入sudo 可能报错。
若显示PASSED则安装成功。
注意:本文是使用makefile方式进行编译的,也可以使用cmake编译,二者选一种即可。
参考: Ryan的博客:环境配置5-Ubuntu下安装Caffe和YOLO 和 yhao的博客: Ubuntu16.04 Caffe 安装步骤记录(超详尽)。
很多时候遇到开源代码是基于旧版本的cudnn开发的(cudnn2,cudnn5.1等),而自己配置的环境是新版本的cudnn (cudnn6)。不可能总去根据代码修改cudnn的配置环境,这种情况下可以升级caffe源码里的cudnn文件,即通过github下载新版本的caffe,用里面的cudnn相关文件替换旧版本caffe源码里cudnn文件。具体方法:
sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so /usr/lib/x86_64-linux-gnu/libboost_python3.so
具体libboost_python-py35.so中py后面的版本要根据自己文件下的版本修改。
locate pyconfig.h发现在/usr/include/python2.7
将此位置加入到Makefile.config文件中的PYTHON_INCLUDE搜索路径中:
ANACONDA_HOME := $(HOME)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
/usr/include/python2.7
在Makefile.config加入
LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib
若使用Cuda8.0+CuDNN5.1的版本, 在make runtest 时会出现如下问题:
CuDNNDeconvolutionLayerTest/3.TestSimpleCuDNNDeconvolution
F0802 14:55:42.177832 22003 cudnn.hpp:128] Check failed: status == CUDNN_STATUS_SUCCESS (3 vs. 0) CUDNN_STATUS_BAD_PARAM
*** Check failure stack trace: ***
@ 0x7f66be7be5cd google::LogMessage::Fail()
@ 0x7f66be7c0433 google::LogMessage::SendToLog()
@ 0x7f66be7be15b google::LogMessage::Flush()
@ 0x7f66be7c0e1e google::LogMessageFatal::~LogMessageFatal()
@ 0x7f66b85fdd18 caffe::CuDNNDeconvolutionLayer<>::Reshape()
@ 0x483cf0 caffe::Layer<>::SetUp()
@ 0x6b4d3d caffe::CuDNNDeconvolutionLayerTest_TestSimpleCuDNNDeconvolution_Test<>::TestBody()
@ 0x952603 testing::internal::HandleExceptionsInMethodIfSupported<>()
@ 0x94bc1a testing::Test::Run()
@ 0x94bd68 testing::TestInfo::Run()
@ 0x94be45 testing::TestCase::Run()
@ 0x94d11f testing::internal::UnitTestImpl::RunAllTests()
@ 0x94d443 testing::UnitTest::Run()
@ 0x47096d main
@ 0x7f66b783b830 __libc_start_main
@ 0x4788c9 _start
@ (nil) (unknown)
Makefile:532: recipe for target 'runtest' failed
同样的问题也出现在: https://github.com/CMU-Perceptual-Computing-Lab/openpose/issues/10