Caffe从源码编译安装及问题集锦

Caffe从源码编译安装

测试ubuntu18.04平台下的caffe源码编译安装

安装环境:

  • OS: ubuntu18.04 64bit

  • 显卡:Nvidia GTX 1650 / RTX 2070 super

  • CUDA 10.1

  • cuDNN: 7.6.5

  • OpenCV: 3.4.1

  • Nvidia driver: 435.21

  • gcc, g++ >=5.0

  • cmake >= 2.8 | make

一、安装依赖

sudo apt-get install libprotobuf-dev protobuf-compiler libsnappy-dev libhdf5-serial-dev

sudo apt-get install --no-install -recommends libboost-all-dev

sudo apt-get install libatlas-base-dev

sudo apt-get install libflags-dev libgoogle-glog-dev liblmdb-dev

注意:如果机器没有安装opencv还需要安装相应的opencv版本:

sudo apt-get install libopencv-dev

二、下载源码

git clone https://github.com/BVLC/caffe.git

cd caffe

mkdir build

三、编译

  • 使用cmake 编译

    打开CMakeLists.txt,根据变量名设置配置,保存

    cd build

    cmake …

    make

    make install

四、安装python

  • 将 build/install/python文件夹下的caffe文件夹拷贝至相应的python解析器中的site-packages文件夹下,如:/usr/local/lib/python2.7/dist-packages

    打开python
    import caffe 进行测试

问题集锦

  1. 编译支持python;设置caffe编译时支持python2,boost库对python2的支持不好,会出现编译错误。

    在安装之前,对python安装numpy库,否则将自动不安装python caffe

    pip install numpy

    /usr/lib/gcc/x86_64-linux-gnu/7/…/…/…/x86_64-linux-gnu/libboost_python.so:对‘PyClass_Type’未定义的引用

  2. 出现与opencv相关的错误:

    /usr/local/lib/libopencv_imgcodecs.so.3.4.1:对‘TIFFReadDirectory@LIBTIFF_4.0’未定义的引用

    重新安装opencv,使其支持libtiff

  3. 问题描述:在使用gcc编译包含有cuda头文件的cpp文件的时候出现cuda_runtime_api.h中未定义某类变量的错误,如下所示:

     /usr/local/cuda/include/cuda_runtime_api.h:9580:60: error: ‘cudaGraphExec_t’ was not declared in this scope
     extern __host__ cudaError_t CUDARTAPI cudaGraphExecDestroy(cudaGraphExec_t graphExec);
                                                                 ^~~~~~~~~~~~~~~
     /usr/local/cuda/include/cuda_runtime_api.h:9580:60: note: suggested alternative: ‘cudaGraphExecUpdate’
    extern __host__ cudaError_t CUDARTAPI cudaGraphExecDestroy(cudaGraphExec_t graphExec);
                                                                 ^~~~~~~~~~~~~~~
                                                                 cudaGraphExecUpdate
    /usr/local/cuda/include/cuda_runtime_api.h:9600:56: error: ‘cudaGraph_t’ was not declared in this scope
     extern __host__ cudaError_t CUDARTAPI cudaGraphDestroy(cudaGraph_t graph);
     ^~~~~~~~~~~
    /usr/local/cuda/include/cuda_runtime_api.h:9600:56: note: suggested alternative: ‘cudaError_t’
     extern __host__ cudaError_t CUDARTAPI cudaGraphDestroy(cudaGraph_t graph);
                                                             ^~~~~~~~~~~
                                                             cudaError_t
     src/caffe/CMakeFiles/caffe.dir/build.make:503: recipe for target 'src/caffe/CMakeFiles/caffe.dir/blob.cpp.o' failed
     make[2]: *** [src/caffe/CMakeFiles/caffe.dir/blob.cpp.o] Error 1
    CMakeFiles/Makefile2:426: recipe for target 'src/caffe/CMakeFiles/caffe.dir/all' failed
    make[1]: *** [src/caffe/CMakeFiles/caffe.dir/all] Error 2
    Makefile:129: recipe for target 'all' failed
    make: *** [all] Error 2
    

    显示cuda库下的头文件driver_types.h中定义的类型没有被cuda_runtime_api.h包含进入。

    尝试的措施:

    • 重装cuda:将cuda10.2 降为cuda10.1 —> 无效
    • 重装显卡驱动:将驱动版本440 降为 435 —> 无效
    • 修改编译器路径为 软链接 include 和 lib64所指向的文件夹 —> 无效
    • 将 #include “driver_types.h” 修改为 #include —> 无效

    测试时,nvcc能够正常编译.cu文件,而使用gcc/g++编译.cpp文件则不行;通过搜索发现路径/usr/include下包含有过去卸载cuda所残留的cuda头文件,同时/usr/include位于gcc编译器的搜索路径中,将/usr/include下所有cuda头文件删除,问题解决。

    分析其原因/usr/include在gcc中的搜索优先级高于其他路径,因此当cuda_runtime_api.h/usr/local/cuda/include文件夹下,而driver_types.h/usr/include存在,则会优先去匹配/usr/include下的driver_types.h文件,从而造成两个文件冲突。

你可能感兴趣的:(调试笔记)