caffe及其依赖库交叉编译

 

记录caffe几个依赖库交叉编译流程

经过公司大神的操作,caffe的cpu版只依赖于3个第三方库,分别是opencv,openblas,probobuf.

opencv移植

此部分流程来自博客,没有找到官方信息.

  • 下载最新版opencv(最新版移植更方便)
  • 打开cmake-gui,设置好代码目录和编译目录
  • 点击configure,选择 specify option for cross-compiling并设置好交叉编译器
  • 第一次配置完成后,在cmake的配置中搜索cmake_install_prefix,修改为要安装的目录(也可以顺便关掉test)
  • 再次configure,然后generate,进入命令行,make -j(虚拟机中使用所有核心有死机风险)
  • 编译过程中遇到以下错误:

    ./../lib/libopencv_core.so: undefined reference to pthread_mutexattr_destroy' ../../lib/libopencv_imgcodecs.so: undefined reference to pthread_create’ ../../lib/libopencv_core.so: undefined reference to pthread_key_create' ../../lib/libopencv_core.so: undefined reference to pthread_getspecific’ ../../lib/libopencv_core.so: undefined reference to dlopen' ../../lib/libopencv_core.so: undefined reference to pthread_mutex_trylock’ ../../lib/libopencv_core.so: undefined reference to pthread_key_delete' ../../lib/libopencv_core.so: undefined reference to dlclose’ ../../lib/libopencv_core.so: undefined reference to dlsym' ../../lib/libopencv_core.so: undefined reference to pthread_mutexattr_settype’ ../../lib/libopencv_imgcodecs.so: undefined reference to pthread_join' ../../lib/libopencv_core.so: undefined reference to pthread_setspecific’ ../../lib/libopencv_core.so: undefined reference to `pthread_mutexattr_init’

    解决方案:在编译目录的CMakeCache.txt某行的CMAKE_EXE_LINKER_FLAGS:STRING后添加-lpthread -lrt -ldl

  • 继续编译,完成后安装即可.

openblas移植

来自官方wiki

  • clone代码,切换到0.2.19版(之后的版本交叉编译需要FORTRAN,我是用的交叉编译链没有FORTRAN)
  • 使用以下命令进行编译
    make TARGET=ARMV7 HOSTCC=gcc CC=/opt/Xilinx/SDK/2017.4/gnu/aarch32/lin/gcc-arm-linux-gnueabi/bin/arm-linux-gnueabihf-gcc NOFORTRAN=1 libs -j
    
  • 删掉libs 继续编译
    make TARGET=ARMV7 HOSTCC=gcc CC=/opt/Xilinx/SDK/2017.4/gnu/aarch32/lin/gcc-arm-linux-gnueabi/bin/arm-linux-gnueabihf-gcc NOFORTRAN=1 -j
    
  • 使用以下命令安装
    make PREFIX=./install install -j
    

    protobuf移植

    来自readme

需要保证交叉编译链在环境变量中.

  • clone代码
  • 先编译宿主机版的protobuf.
    git submodule update --init --recursive
    ./autogen.sh
    ./configure
    make -j
    make check
    sudo make install -j
    sudo ldconfig
    
  • 交叉编译
    ./configure --build=i686-pc-linux  --host=arm-linux-gnueabihf --with-protoc=protoc --prefix=/home/y/project/protobuf/install_ps
    make clean
    make -j
    make install
    

完整caffe移植到arm

由于移除了caffe对部分库的依赖,在和合作伙伴对接新的算法时较复杂,重新移植了完整的caffe到arm.

移植分为两部分

  • caffe的依赖库的移植
  • caffe自身的移植

    caffe的依赖库的移植

    整个过程需要移植的库包括:glog gflags protobuf openblas opencv leveldb lmdb boost snappy hdf5

编译过程可参考:caffe交叉编译

其中大部分库的移植都是指定交叉编译器即可,记录几个特殊的.

hdf5交叉编译

  • 下载代码(我使用了cmake版)
  • 使用cmake-gui进行configure和generate,第一次configure会失败,重新configure即可
  • 进行make,两次,编译失败
  • 拷贝bin/H5detect bin/H5make_libsettings libhdf5.settings到arm平台
  • 在arm平台修改文件执行权限,执行H5detect和H5make_libsettings,把程序输出分别保存到H5Tinit.c和H5lib_settings.c
  • 拷贝H5Tinit.c和H5lib_settings.c到主机的编译目录下,继续编译.

boost交叉编译

  • 下载代码
  • 使用脚本生成编译文件./bootstrap.sh –prefix=/home/path
  • 修改project-config.jam文件,gcc修改为交叉编译器
    10 if ! gcc in [ feature.values <toolset> ]
    11 {
    12     using gcc : arm : arm-hisiv500-linux-gcc ; #注意中间空格
    13 }
    
  • 使用脚本进行编译安装:
    ./bjam install toolset=gcc-arm --layout=tagged --build-type=complete --prefix=/home/path
    

caffe的移植

caffe的移植需要在原版caffe编译的基础上,修改编译器 头文件路径 库文件路径

  • 修改Makefile
    COMMON_FLAGS += -march=armv7-a -mtune=cortex-a9 -mfpu=neon -mfloat-abi=hard
    
  • 修改Makefile.config
    CPU_ONLY := 1
    OPENCV_VERSION := 3 
    CUSTOM_CXX := /opt/Xilinx/SDK/2017.4/gnu/aarch32/lin/gcc-arm-linux-gnueabi/bin/arm-linux-gnueabihf-g++
    BLAS := open
    # Whatever else you find you need goes here.
    INCLUDE_DIRS := $(PYTHON_INCLUDE) 
    INCLUDE_DIRS += /opt/Xilinx/SDK/2017.4/gnu/aarch32/lin/gcc-arm-linux-gnueabi/include 
    INCLUDE_DIRS += /home/y/project/caffe_ps/src/glog-0.3.5/install_ps/include 
    INCLUDE_DIRS += /home/y/project/caffe_ps/src/gflags/install_ps/include 
    INCLUDE_DIRS += /home/y/project/protobuf/install_ps/include 
    INCLUDE_DIRS += /home/y/project/OpenBLAS/install/include 
    INCLUDE_DIRS += /home/y/project/opencv_/install_ps/include 
    INCLUDE_DIRS += /home/y/project/caffe_ps/src/leveldb-1.20/include 
    INCLUDE_DIRS += /home/y/project/caffe_ps/src/liblmdb/install_ps/include 
    INCLUDE_DIRS += /home/y/project/caffe_ps/src/boost_1_68_0/install_ps/include 
    INCLUDE_DIRS += /home/y/project/caffe_ps/src/snappy-1.1.7/install_ps/include 
    INCLUDE_DIRS += /home/y/project/caffe_ps/src/hdf5-1.8.20/install_ps/include 
    # INCLUDE_DIRS += /usr/local/include
    LIBRARY_DIRS := $(PYTHON_LIB) 
    LIBRARY_DIRS += /opt/Xilinx/SDK/2017.4/gnu/aarch32/lin/gcc-arm-linux-gnueabi/lib 
    LIBRARY_DIRS += /home/y/project/caffe_ps/src/glog-0.3.5/install_ps/lib 
    LIBRARY_DIRS += /home/y/project/caffe_ps/src/gflags/install_ps/lib 
    LIBRARY_DIRS += /home/y/project/protobuf/install_ps/lib 
    LIBRARY_DIRS += /home/y/project/OpenBLAS/install/lib 
    LIBRARY_DIRS += /home/y/project/opencv_/install_ps/lib 
    LIBRARY_DIRS += /home/y/project/caffe_ps/src/leveldb-1.20/out-shared 
    LIBRARY_DIRS += /home/y/project/caffe_ps/src/leveldb-1.20/out-static 
    LIBRARY_DIRS += /home/y/project/caffe_ps/src/liblmdb/install_ps/lib 
    LIBRARY_DIRS += /home/y/project/caffe_ps/src/boost_1_68_0/install_ps/lib 
    LIBRARY_DIRS += /home/y/project/caffe_ps/src/snappy-1.1.7/install_ps/lib 
    LIBRARY_DIRS += /home/y/project/caffe_ps/src/hdf5-1.8.20/install_ps/lib 
    # LIBRARY_DIRS += /usr/local/lib 
    # LIBRARY_DIRS += /usr/lib
    #PYTHON_LIB := /usr/lib
    USE_PKG_CONFIG := 1
    

注意事项

  • opencv库的库文件会依赖于其余的库文件,但是目录是../../lib下,经查证,某些版本/平台下的opencv移植会出现这个问题,手动在编译和执行目录的../../lib下放置opencv库可以让程序正常编译运行
  • hdf5的库文件名字不是libname.a和libname.so,需要手动链接或重命名