一般依赖库:
$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
BLAS:
$ sudo apt-get install libatlas-base-dev
Ubuntu16.04安装Opencv3.1:
准备工作:
安装基本编译环境:
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
设置cuda的环境变量,sudo gedit ~/.bashrc,添加以下代码:
export PATH=/usr/local/cuda-8.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH
1.先创建文件夹:
cd ~/opencv-3.1.0 #进入解压的opencv3.1.0文件
mkdir build #创建build文件
cd build
2.配置:
cmake -D CMAKE_BUILD_TYPE=Release-D CMAKE_INSTALL_PREFIX=/usr/local ..
执行cmake 时, 若出现ippicv_linux_20151201.tgz的hash码不对,则需手动下载ippicv_linux_20141027.tgz,然后手动替换掉 /home/alphacocoa/opencv-3.1.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e 目录下的ippicv_linux_20141027.tgz。然后重新cmake即可 ippicv_linux_20141027.tgz链接:https://pan.baidu.com/s/1o9tvClc 密码:urkj
3.编译:
make -j32 #-j8表示cpu多线程并行计算,根据自己电脑配置进行设置
以上只是将opencv编译成功,运行以下指令进行安装:
sudo make install
至此,我的编译没有问题。以下列出可能的错误:
错误1:
gcc-4.9: error trying to exec 'cc1plus': execvp:
没有那个文件或目录
说明gcc与g++版本不兼容,解决办法跟gcc版本太高时一样:安装低版本gcc与g++:
sudo apt-get install gcc-4.9 g++-4.9
cd /usr/bin
先删除和gcc5.0关联的gcc:
sudo rm gcc
sudo rm g++
再建立软连接
sudo ln -s gcc-4.9 gcc
sudo ln -s g++-4.9 g++
错误2:
modules/cudalegacy/src/graphcuts.cpp:120:54: error:
‘NppiGraphcutState’ has not been declared
typedef NppStatus (*init_func_t)(NppiSize oSize,
NppiGraphcutState** ppState, Npp8u* pDeviceMem);
这是因为opecv3.0与cuda8.0不兼容导致的。解决办法: 修改 ~/opencv/modules/cudalegacy/src/graphcuts.cpp文件中第45行的内容:
原内容:#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
替换为:#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) ||(CUDART_VERSION>=8000)
配置caffe:
1.从github上获取caffe:
git clone https://github.com/BVLC/caffe.git
如果没有安装git,先安装git:
sudo apt-get install git
2.将Makefile.config.example的内容复制到Makefile.config
sudo cp Makefile.config.example Makefile.config
3.打开并修改配置文件:
sudo gedit Makefile.config
若使用cudnn,则将#USE_CUDNN:1的注释符#去掉
若使用opencv的版本为3,则将#OPENCV_VERSION:=3的注释符#去掉
若要使用python来编写layer,则将#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
这是因为ubuntu16.04的文件包含位置发生了变化,尤其是需要用到的hdf5的位置,所以需要更改这一路径.
4.修改Makefile文件:
打开Makefile文件,将415行修改如下:
原内容:NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
5.编辑/usr/local/cuda/include/host_config.h,将第115行注释掉:
将:
#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
改为:
//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
6.最后编译
make all -j32
编译过程中可能会出现如下错误:
错误1:
"fatal error: hdf5.h: 没有那个文件或目录"
或者在make all的时候出现如下错误:
Makefile:160:recipe for target ‘all’ failed
make:*** [all] Error 2
解决办法:
修改Makefile.config文件的第95行:
原内容:INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
修改为:INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
修改Makefile文件的第173行:
原内容:LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
修改为:LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
错误2:
./build_release/tools/caffe:error while loading shared libraries:libcudnn.so.5:cannot open shared
object file: No such file or directory
Makefile:514:recipe for target ‘runtest’ failed
make:*** [runtest] Error 127
解决办法:
sudo cp /usr/local/cuda-8.0/lib64/libcudnn.so.5 /usr/local/lib/libcudnn.so.5 && sudo ldconfig
错误3:
"libcudart.so.8.0 cannot open shared object file: No such file or directory"
解决办法为将一些文件复制到/usr/local/lib文件夹下:
#注意自己CUDA的版本号!
sudo cp /usr/local/cuda-8.0/lib64/libcudart.so.8.0 /usr/local/lib/libcudart.so.8.0 && sudo ldconfig
sudo cp /usr/local/cuda-8.0/lib64/libcublas.so.8.0 /usr/local/lib/libcublas.so.8.0 && sudo ldconfig
sudo cp /usr/local/cuda-8.0/lib64/libcurand.so.8.0 /usr/local/lib/libcurand.so.8.0 && sudo ldconfig
7.最后测试sudo make runtest
出现错误:
build_release/tools/caffe: error while loading shared libraries: libopencv.so.3.2: cannot open shared object file: No such file or directory
make: *** [runtest] Error 127
解决办法:
cd caffe
sudo ldconfig
至此caffe配置成功!
参考文献:[1]http://blog.csdn.net/xuzhongxiong/article/details/52717285