Ubuntu编译安装caffe

22
五月
2021

背景

记录今年年初在ubuntu18.04上编译caffe的过程,非常的繁琐,仅供参考

clone源代码

root@rtlab-computer:/home/rtlab/szc# git clone https://github.com/BVLC/caffe.git

然后进入caffe目录

修改配置文件

Makefile.config

先复制一份Makefile.config.example,重命名为Makefile.config,再修改以下内容:

a、去掉USE_CUDNN := 1、OPENCV_VERSION := 3和WITH_PYTHON_LAYER := 1的注释

b、修改CUDA_DIR

CUDA_DIR := /usr/local/cuda-10.0

c、去掉CUDA_ARCH中的20与21选项:

CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \
        -gencode arch=compute_35,code=sm_35 \
        -gencode arch=compute_50,code=sm_50 \
        -gencode arch=compute_52,code=sm_52 \
        -gencode arch=compute_60,code=sm_60 \
        -gencode arch=compute_61,code=sm_61

d、使用Python3:

PYTHON_LIBRARIES := boost_python3 python3.6m
PYTHON_INCLUDE := /usr/include/python3.6m \
                 /usr/local/lib/python3.6/dist-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /home/rtlab/anaconda3/lib/

e、指定hdfs的头文件路径:

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial

Makefile

a、给LIBRARIES添加hdf5_serial_hl和hdf5_serial

# LIBRARIES += glog gflags protobuf boost_system boost_filesystem m
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

b、给NVCCFLAGS添加-D_FORCE_INLINES参数

# NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

CMakeLists.txt

修改python_version为3(或者自己的版本):

set(python_version "3.6" CACHE STRING "Specify which Python version to use")

cmake/Dependencies.cmake

修改OPENCV版本号:

if(USE_OPENCV)
  find_package(OpenCV 3.4.1 QUIET COMPONENTS core highgui imgproc imgcodecs videoio)
  if(NOT OpenCV_FOUND) # if not OpenCV 3.x, then imgcodecs are not found
    find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc)
  endif()
  include_directories(SYSTEM ${OpenCV_INCLUDE_DIRS})
  list(APPEND Caffe_LINKER_LIBS ${OpenCV_LIBS})
  message(STATUS "OpenCV found (${OpenCV_CONFIG_PATH})")
  add_definitions(-DUSE_OPENCV)
endif()

cmake/Cuda.cmake

指定cuda版本号:
find_package(CUDA 10.0 REQUIRED QUIET)

host_config.h

此步骤可选,如果 /usr/local/cuda-10.0/include/host_config.h存在以下内容,则注释掉

#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!

LD_LIBRARY_PATH

确保LD_LIBRARY_PATH没有多个版本的cuda:

root@rtlab-computer:/home/rtlab# echo $LD_LIBRARY_PATH
/usr/local/cuda-10.0/lib64:/home/rtlab/szc/openvx/pgm/:/home/szc/openvx/hog/build/lib/:/usr/local/lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/7/:/home/rtlab/szc/openvx/feather-trace/:/usr/local/cuda-10.0/lib64/:/usr/local/ffmpeg/lib/:/home/rtlab/szc/opencv-3.4.1/build/lib:/home/rtlab/szc/tensorflow/lib/

ibboost

如果报错libboost相关内容,可以尝试用python2.7编译libboost,参见文章动态知识图谱库DynGEM的学习之库的安装与调试中关于Boost.Python的编译过程

编译

root@rtlab-computer:/home/rtlab/szc/caffe# make all -j8

编译结果(不报错就行):

测试

编译运行测试用例:

root@rtlab-computer:/home/rtlab/szc/caffe# make runtest -j8

结果如下(最后CTRL-C了):

在Python中引用

若要在python中引入caffe,还需要修改caffe目录下的CMakeCache.txt文件,把所有的python2.7改成python3.6(或python3.6m)

//Path to a program.
PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3.6


//Path to a file.
PYTHON_INCLUDE_DIR:PATH=/usr/include/python3.6


//Path to a library.
PYTHON_LIBRARY:FILEPATH=/usr/lib/x86_64-linux-gnu/libpython3.6m.so

然后重新编译所有的东西,并把caffe根目录下的python目录添加到PYTHONPATH中去:

export PYTHONPATH=$PYTHONPATH:/home/rtlab/szc/caffe-master/python

如果要在pycharm中添加编译好的caffe,则必须使用pycharm专业版,然后在File->Setings->Project: projectName->Python Intepreter->Show All中,先添加自己编译Caffe时用的python(这里我的是python3.6),再点击下方目录结构样的图标:

然后在弹出来的面板里点击左下角的+来添加路径:

选择/home/rtlab/szc/caffe-master/python目录即可:

问题解决

找不到cudnn的版本

解决方法:

下载解压7.5版本的cudnn(链接:https://pan.baidu.com/s/1BLAAClZjqyqYxJSCqpg2lw提取码:3mt8),然后把include目录下的文件复制到/usr/include下,把lib64目录下的文件复制到/usr/lib目录下

root@rtlab-computer:/home/rtlab/szc# tar -zxvf cudnn-10.0-linux-x64-v7.5.0.56.tar.gz
root@rtlab-computer:/home/rtlab/szc# cd cuda
root@rtlab-computer:/home/rtlab/szc/cuda# cp include/cudnn.h /usr/include/
root@rtlab-computer:/home/rtlab/szc/cuda# cp lib64/* /usr/lib/

liblapack.so未定义gotoblas和libblas.so.3未定义gotoblas

解决方法:

根据报错信息,查看加载的liblapack.so和libblas.so.3路径(我的是/usr/lib/x86_64-linux-gnu/下的这两个文件),然后查看它们的指向关系:

root@rtlab-computer:/home/rtlab/szc/cuda# ll /usr/lib/x86_64-linux-gnu/liblapa*
....
lrwxrwxrwx 1 root root      47 Sep 22  2018 /usr/lib/x86_64-linux-gnu/liblapack.so -> /etc/alternatives/liblapack.so-x86_64-linux-gnu
....
root@rtlab-computer:/home/rtlab/szc/cuda# ll /usr/lib/x86_64-linux-gnu/libblas.*
....
lrwxrwxrwx 1 root root 47 Sep 21  2018 /usr/lib/x86_64-linux-gnu/libblas.so.3 -> /etc/alternatives/libblas.so.3-x86_64-linux-gnu

然后分别修改这两个指向内容,让它们都指向atlas目录下的库文件:

root@rtlab-computer:/home/rtlab/szc/cuda# update-alternatives --config liblapack.so.3-x86_64-linux-gnu
There are 3 choices for the alternative liblapack.so.3-x86_64-linux-gnu (providing /usr/lib/x86_64-linux-gnu/liblapack.so.3).


  Selection    Path                                               Priority   Status
------------------------------------------------------------
* 0            /usr/lib/x86_64-linux-gnu/openblas/liblapack.so.3   40        auto mode
  1            /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3      35        manual mode
  2            /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3     10        manual mode
  3            /usr/lib/x86_64-linux-gnu/openblas/liblapack.so.3   40        manual mode


Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3 to provide /usr/lib/x86_64-linux-gnu/liblapack.so.3 (liblapack.so.3-x86_64-linux-gnu) in manual mode

root@rtlab-computer:/home/rtlab/szc/cuda# update-alternatives --config libblas.so.3-x86_64-linux-gnu
There are 2 choices for the alternative libblas.so.3-x86_64-linux-gnu (providing /usr/lib/x86_64-linux-gnu/libblas.so.3).


  Selection    Path                                             Priority   Status
------------------------------------------------------------
* 0            /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3   40        auto mode
  1            /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3      35        manual mode
  2            /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3   40        manual mode


Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3 to provide /usr/lib/x86_64-linux-gnu/libblas.so.3 (libblas.so.3-x86_64-linux-gnu) in manual mode

如此再make all即可

python中import caffe报错raise ValueError, "Can`t create weekday with n == 0"

解决方法:升级matplotlib

root@rtlab-computer:/home/rtlab/szc# pip3 install matplotlib --upgrade

分享结果

最后给出我编译好的caffe压缩包网盘链接:https://pan.baidu.com/s/1_I6zea4GY7dMAKV3axhykA

提取码:cms3

TAG

网友评论

共有访客发表了评论
请登录后再发布评论,和谐社会,请文明发言,谢谢合作! 立即登录 注册会员