当前位置: > CentOS > CentOS教程 >

在centos7的无外网环境下编译安装caffe

时间:2017-05-01 23:46来源:csdn.net 作者:csdn.net

由于机器涉密,无法连接外网,导致yum命令不可用,配置安装caffe的过程变得繁琐复杂,所有的依赖都需要手动编译。先列出caffe需要的依赖:

  • Boost
  • glog
  • protobuf
  • lmdb
  • LevelDB
  • Snappy
  • gflags
  • BLAS
  • opencv
  • HDF5
  • cuda

我的cmake版本:2.8.12.2 
所有的安装包都上传到/usr/local 下解压编译


1.Boost: 
博主的Boost下载地址

# tar jxvf  boost_1_55_0.tar.bz2  
# cd boost_1_55_0
# ./bootstrap.sh  
# ./b2  
# sudo ./b2 install  

2.glog:  博主的glog下载地址

# tar zxvf glog-0.3.3.tar.gz 
# cd glog-0.3.3 
# ./configure 
# make && make install 

3.protobuf:  博主的protobuf下载地址

# tar -zxf protobuf-2.5.0.tar.gz
# cd protobuf-2.5.0
# ./configure --prefix=/usr/local/protobuf/
# make && make install
//最后将下面的的语句加入到环境变量中
# export PATH=/usr/local/protobuf/bin:$PATH

4.lmdb  博主的lmdb的下载地址

# unzip lmdb-mdb.master.zip
# cd lmdb-mdb.master/libraries/liblmdb 
# make && make install

5.LevelDB  博主的levelDB下载地址

# unzip leveldb-master.zip
# cd leveldb-master
# make
# sudo cp -r include/leveldb /usr/local/include
# cd out-shared 
# cp lib* /usr/local/lib

6.Snappy  博主的Snappy下载地址

# tar -zvxf snappy-1.1.2.tar.gz
# cd snappy-1.1.2
# ./configure 
# make
# make install

7.gflags:  博主的gflags下载地址

# unzip gflags-master.zip 
# cd gflags-master  
# mkdir build && cd build  
# export CXXFLAGS="-fPIC"
# cmake .. && make VERBOSE=1  
# make && make install  

我在执行 export CXXFLAGS=”-fPIC” 后cmake出现问题(也许大家不会出现该问题):

The C++ compiler "/usr/bin/c++" is not able to compile a simple test program

始终无法解决,决定不执行export CXXFLAGS=”-fPIC”  但编译安装gflags成功后,编译caffe时出现问题:

Linking CXX shared library ../../lib/libcaffe-d.so
/usr/bin/ld: /usr/local/lib/libgflags.a(gflags.cc.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libgflags.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make[2]: *** [lib/libcaffe-d.so.1.0.0-rc3] Error 1
make[1]: *** [src/caffe/CMakeFiles/caffe.dir/all] Error 2
make: *** [all] Error 2

说明gflags编译时没有生成共享库,重新编译gflags

解决方法:

# unzip gflags-master.zip 
# cd gflags-master  
# mkdir build && cd build  
# cmake .. -DBUILD_SHARED_LIB=ON
# make VERBOSE=1  
# make && make install  

8.BLAS:

BLAS是用来进行矩阵运算的,在这里我试了两种blas。最终我使用openblas完成了编译安装。

首先,我尝试安装atlas 博主的atlas下载地址

用rpm命令安装该包  rpm -ivh Atlas-2.2.1-2.l7.centos.src.rpm

安装成功后atlas目录被放在了/usr/lib64/atlas  然而,编译caffe时cmake出错:

CMake Error at /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:108 (message):
  Could NOT find Atlas (missing: Atlas_CLAPACK_INCLUDE_DIR
  Atlas_CBLAS_LIBRARY Atlas_BLAS_LIBRARY)

解决方法:  在cmake .. 后加 -DAtlas=/usr/lib64/atlas

但编译时出现问题:

../lib/libcaffe-d.so.1.0.0-rc3: undefined reference to `cblas_sgemv' 
../lib/libcaffe-d.so.1.0.0-rc3: undefined reference to `cblas_dgemm' 
../lib/libcaffe-d.so.1.0.0-rc3: undefined reference to `cblas_sscal' 
../lib/libcaffe-d.so.1.0.0-rc3: undefined refere
...

查阅资料后,个人认为有两种出错的可能  一是atlas安装不完全,缺少依赖,因为github上的atlas rpm包都是在centos6.x和5.x安装的版本,我在网上找到的版本也许还需要安装其他的atlas rpm包。二是除了安装atlas以外,要再安装一个叫做cblas的库。问题出现的具体原因由于时间关系没能继续研究,于是放弃了atlas,选择了OpenBLAS。

博主的OpenBLAS下载地址

# tar -xzvf OpenBLAS.tar.gz 
# make 
# make --PREFIX='usr/local/' install 
# cp lib* /usr/local/lib

OpenBLAS安装完成


9.Opencv  opencv安装教程都比较多,博主这里主要列出安装时我出现的问题

  • 我使用的opencv版本是3.2.0,cmake时会从网上download一个名为 ippicv_linux_20151201.tgz 的包,但是机器无法连接外网,故该包下载失败,我用自己机器在互联网上找到了该包(该包下载地址),将其放在/usr/local/opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e 下,重新cmake,成功。

  • 编译时出现问题1

make[2]: *** [modules/gpu/CMakeFiles/opencv_gpu.dir/src/graphcuts.o] Error 1 
make[1]: *** [modules/gpu/CMakeFiles/opencv_gpu.dir/all] Error 2 
make: *** [all] Error 2
解决方法:Cmake时 加-D BUILD_opencv_gpu=OFF(做测试用,并不需要gpu训练数据)
  • 编译时出现问题2
modules/core/CMakeFiles/cuda_compile.dir/__/dynamicuda/src/cuda/./cuda_compile_generated_matrix_operations.cu.o
arm-linux-gnueabihf-gcc: error: unrecognized command line option ‘-m64’
CMake Error at cuda_compile_generated_matrix_operations.cu.o.cmake:206 (message):
Error generating
/home/yy/opencv-2.4.9/build/modules/core/CMakeFiles/cuda_compile.dir/__/dynamicuda/src/cuda/./cuda_compile_generated_matrix_operations.cu.o
make[2]: *** [modules/core/CMakeFiles/cuda_compile.dir/__/dynamicuda/src/cuda/./cuda_compile_generated_matrix_operations.cu.o] 错误 1
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] 错误 2
make: *** [all] 错误 2
解决方法:Cmake时 加-D CMAKE_BUILD_TYPE=bulid -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler ..”

10.HDF5 
博主的HDF5下载地址

# gunzip < hdf5-X.Y.Z.tar.gz | tar xf -   #解压缩
# cd hdf5-X.Y.Z
# ./configure --prefix=/usr/local/hdf5  #安装路径
# make
# make check                # run test suite.
# make install
# make check-install        # verify installation.

11.CUDA  由于使用的机器上安装了CUDA,博主并未进行这一步


编译caffe:  在Makefile.config文件中更改:

BLAS:=open
BLAS_INCLUDE := /usr/local/include
BLAS_LIB := /usr/local/lib
# cmake .. -DBUILD_SHARED_LIB=ON -DBLAS=open -DOpenCV_DIR=/usr/local/opencv-3.2.0/build

# make && make install

常出现的问题:cmake时找不到依赖库  解决方法:在环境变量里添加export语句,例如,  在/etc/profile里添加  export PATH=/usr/local/protobuf/bin:$PATH;  或在cmake语句后明确指出依赖位置,例如,  -DOpenCV_DIR=/usr/local/opencv-3.2.0/build


参考资料:

http://blog.csdn.net/chang_ge/article/details/52650982  http://yongyuan.name/blog/compiling-cpp-code-using-caffe.html  http://blog.csdn.net/s2392735818/article/details/49796017  http://stackoverflow.com/questions/15161278/compile-opencv-without-gpu  http://opencv-users.1802565.n2.nabble.com/OpenCV-with-CUDA-4-1-Support-on-64bit-Ubuntu-11-10-td7304408.html  http://blog.csdn.net/tjusxh/article/details/41944403  http://blog.csdn.net/hairetz/article/details/18049257  http://blog.csdn.net/luo6620378xu/article/details/8521223  http://www.linuxdiyf.com/linux/26164.html

文:http://blog.csdn.net/ChrisYoung95/article/details/70574844




(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容