基于Docker的Caffe编译(CUDA11的makefile版)

基于Docker的Caffe编译(CUDA11的makefile版)

  • 用于R2CNN模型

cuda10版(CMake版)

  • https://blog.csdn.net/a429367172/article/details/103471789

nvidia-cuda镜像

下载与cuda版本对应的nvidia-cuda镜像https://hub.docker.com/r/nvidia/cuda	
#查看cuda版本
chenchangke@gpu003:~/xxxxxxxx$ nvidia-smi 
Sun Apr 25 18:18:23 2021       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.102.04   Driver Version: 450.102.04   CUDA Version: 11.0     |
|-------------------------------+----------------------+----------------------+
以上cuda版本为11.0,选择对应版本的devel镜像:
docker pull nvidia/cuda:11.0.3-devel-ubuntu20.04
# 但是这个版本里面没有cudnn,需要下载一个对应版本的cudnn,后面编译会用到
https://developer.nvidia.com/rdp/cudnn-archive

容器的使用

镜像的运行使用如下脚本(run-caffe-r2cnn-docker.sh):

#!/bin/bash
export MY_CONTAINER="caffe-r2cnn"
num=`sudo docker ps -a|grep "$MY_CONTAINER"|wc -l`
echo $num
echo $MY_CONTAINER
if [ 0 -eq $num ]; then
    sudo docker run \
        --gpus all \
        --ipc=host \
        -it --privileged --name $MY_CONTAINER \
        DISPLAY=$DISPLAY \
		-v $PWD/R2CNN/:/home/R2CNN \
        -w /home/R2CNN \
        0ec3cd91fe0b \
        /bin/bash
else
    sudo docker start $MY_CONTAINER
    sudo docker exec -w /home/R2CNN -ti $MY_CONTAINER /bin/bash
fi
  • 将run-caffe-r2cnn-docker.sh文件和R2CNN放在同一路径下,使用./run-caffe-r2cnn-docker.sh运行容器,或者自己打命令也行

caffe的编译

1.安装python
容器中没有python2.7,需要自己下载,参考https://blog.csdn.net/weixin_39981624/article/details/110964940
apt update

apt install python2
root@f698dbd13e4b:/home/R2CNN# python2 -V
	Python 2.7.18
#设置替代版本
update-alternatives --install /usr/bin/python python /usr/bin/python2 1
#因为容器中只有这一个python,到此就可以了。如果还要用python3的话,再单独配置

apt-get install python-dev #安装python-dev,否则后面pip安装会报错
pip也要自己下载,参考https://zhuanlan.zhihu.com/p/137114974
apt install curl

curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py

python2 get-pip.py

pip2 --version
2.安装python依赖
  • 我用的是pip,使用pip install package命令安装如下包
easydict
shapely
Polygon2 #注意这里是Polygon2
matplotlib
scikit-image
thrift
cython
numpy==1.11.0 #pip install -U numpy==1.11.0
opencv-python==3.4.3.18 #pip install opencv-python==3.4.3.18
pip install pyyaml # r2cnn用到
3.下载faster rcnn版的caffe源码并编译
  • 参考:https://github.com/YuwenXiong/py-R-FCN
#在R2CNN文件下下载microsoft的caffe(用于faster rcnn)
apt-get install git
# 这里需要注意的是得回到(@commit 1a2be8e)这个commit
git clone https://github.com/microsoft/caffe.git
cmake的安装

参考:https://www.cnblogs.com/freeweb/p/5788729.html

apt-get install wget
cd /home
wget https://github.com/Kitware/CMake/releases/download/v3.20.1/cmake-3.20.1-linux-x86_64.tar.gz

#解压,cmake命令位于cmake-3.20.1-linux-x86_64/bin下
tar -zxvf cmake-3.20.x-linux-x86_64.tar.gz
#链接到usr/bin下
cd /usr/bin
ln -s /home/cmake-3.20.1-linux-x86_64/bin/cmake cmake
cmake --version
caffe编译

参考:http://caffe.berkeleyvision.org/installation.html

1.依赖安装 boost, glog, hdf5, protobuf, gflags
apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
apt-get install python-protobuf
apt-get install libboost-all-dev    #安装boost库
apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
apt-get install git build-essential cmake-gui #注意这里没有cmake
apt-get -y --force-yes install libgoogle-glog-dev
#protobuf, gflags, hdf5的安装百度即可
# gflags:https://www.cnblogs.com/freeweb/p/10696872.html
# hdf5:https://www.it610.com/article/1279360672467009536.htm
# protobuf:https://blog.csdn.net/u012414189/article/details/84074036
	## protobuf的include需要加入Makefile.config
2.编译
make all
make install
make runtest
  • 装了下opencv(源码编译):https://www.jb51.net/article/193925.htm

  • Makefile文件:

    • cudnn不用(Makefile里面,我也不知道为啥有的教程选择钩上,如果钩上就就编译不通过,似乎是cudnn那边写的有点问题)

    • CUDA_ARCH(这个是算力,需要查询自己显卡的算力)

    • INCLUDE_DIRS/LIBRARY_DIRS加上hdf5地址(需要装)

    • ## Refer to http://caffe.berkeleyvision.org/installation.html
      # Contributions simplifying and improving our build system are welcome!
      
      # cuDNN acceleration switch (uncomment to build with cuDNN).
      #USE_CUDNN := 1
      
      # CPU-only switch (uncomment to build without GPU support).
      # CPU_ONLY := 1
      
      # uncomment to disable IO dependencies and corresponding data layers
      # USE_OPENCV := 0
      # USE_LEVELDB := 0
      # USE_LMDB := 0
      
      # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
      #	You should not set this flag if you will be reading LMDBs with any
      #	possibility of simultaneous read and write
      # ALLOW_LMDB_NOLOCK := 1
      
      # Uncomment if you're using OpenCV 3
      # OPENCV_VERSION := 3
      
      # To customize your choice of compiler, uncomment and set the following.
      # N.B. the default for Linux is g++ and the default for OSX is clang++
      # CUSTOM_CXX := g++
      
      # CUDA directory contains bin/ and lib/ directories that we need.
      CUDA_DIR := /usr/local/cuda
      # On Ubuntu 14.04, if cuda tools are installed via
      # "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
      # CUDA_DIR := /usr
      
      # CUDA architecture setting: going with all of them.
      # For CUDA < 6.0, comment the *_50 lines for compatibility.
      # CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
      #		-gencode arch=compute_20,code=sm_21 \
      #		-gencode arch=compute_30,code=sm_30 \
      #		-gencode arch=compute_35,code=sm_35 \
      #		-gencode arch=compute_50,code=sm_50 \
      #		-gencode arch=compute_50,code=compute_50
      CUDA_ARCH := -gencode=arch=compute_75,code=sm_75 
      # BLAS choice:
      # atlas for ATLAS (default)
      # mkl for MKL
      # open for OpenBlas
      BLAS := atlas
      # Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
      # Leave commented to accept the defaults for your choice of BLAS
      # (which should work)!
      # BLAS_INCLUDE := /path/to/your/blas
      # BLAS_LIB := /path/to/your/blas
      
      # Homebrew puts openblas in a directory that is not on the standard search path
      # BLAS_INCLUDE := $(shell brew --prefix openblas)/include
      # BLAS_LIB := $(shell brew --prefix openblas)/lib
      
      # This is required only if you will compile the matlab interface.
      # MATLAB directory should contain the mex binary in /bin.
      # MATLAB_DIR := /usr/local
      # MATLAB_DIR := /Applications/MATLAB_R2012b.app
      
      # NOTE: this is required only if you will compile the python interface.
      # We need to be able to find Python.h and numpy/arrayobject.h.
      PYTHON_INCLUDE := /usr/include/python2.7 \
      		/usr/lib/python2.7/dist-packages/numpy/core/include
      # Anaconda Python distribution is quite popular. Include path:
      # Verify anaconda location, sometimes it's in root.
      # ANACONDA_HOME := $(HOME)/anaconda
      # PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
      		# $(ANACONDA_HOME)/include/python2.7 \
      		# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
      
      # Uncomment to use Python 3 (default is Python 2)
      # PYTHON_LIBRARIES := boost_python3 python3.5m
      # PYTHON_INCLUDE := /usr/include/python3.5m \
      #                 /usr/lib/python3.5/dist-packages/numpy/core/include
      
      # We need to be able to find libpythonX.X.so or .dylib.
      PYTHON_LIB := /usr/lib
      # PYTHON_LIB := $(ANACONDA_HOME)/lib
      
      # Homebrew installs numpy in a non standard path (keg only)
      # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
      # PYTHON_LIB += $(shell brew --prefix numpy)/lib
      
      # Uncomment to support layers written in Python (will link against Python libs)
      WITH_PYTHON_LAYER := 1
      
      # Whatever else you find you need goes here.
      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
      
      # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
      # INCLUDE_DIRS += $(shell brew --prefix)/include
      # LIBRARY_DIRS += $(shell brew --prefix)/lib
      
      # Uncomment to use `pkg-config` to specify OpenCV library paths.
      # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
      # USE_PKG_CONFIG := 1
      
      # N.B. both build and distribute dirs are cleared on `make clean`
      BUILD_DIR := build
      DISTRIBUTE_DIR := distribute
      
      # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
      # DEBUG := 1
      
      # The ID of the GPU that 'make runtest' will use to run unit tests.
      TEST_GPUID := 0
      
      # enable pretty build (comment to see full commands)
      Q ?= @
      
      

make的时候的一些错误

# make时候的一些错误
	
# fatal error: leveldb/db.h: No such file or directory
apt-get install libleveldb-dev

# fatal error: lmdb.h: No such file or directory
apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

# fatal error: opencv2/core/core.hpp: No such file or directory
apt-get install libopencv-core-dev

# nvcc fatal   : A single input file is required for a non-link phase when an outputfile is specified
CUDA_ARCH 这么改,这个得看显卡算力,我的是TITian RTX
http://arnon.dk/matching-sm-architectures-arch-and-gencode-for-various-nvidia-cards/
CUDA_ARCH := -gencode=arch=compute_75,code=sm_75 

# build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
https://blog.csdn.net/bhniunan/article/details/104124347
Makefile文件中:LIBRARIES += opencv_core opencv_highgui opencv_imgproc
改为:LIBRARIES += opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

# /usr/bin/ld: cannot find -lboost_python
# 第一个链接是安装(注意装的时候不选库就只装最基本的玩意,这是不够的,一定要选库:--with-libraries),第二个是链接
https://blog.csdn.net/csfreebird/article/details/9627153
https://blog.csdn.net/u012505617/article/details/88556621
wget https://sourceforge.net/projects/boost/files/boost/1.69.0/boost_1_69_0.tar.gz
按照上面的教程装py2的boost,然后链接一下即可(参考py3的链接命名方式即可(3去掉))

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

椰子奶糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值