上一章描述的是原版的caffe环境搭建,这一章介绍caffe ssd环境搭建,和上一章稍有不同。
环境:
系统:Ubuntu16.04
显卡:NVIDIA GTX2070
搭建步骤:
1、下载caffe ssd
SSD采用的是在caffe文件夹中内嵌例程的方式,作者改动了原版caffe,所以你需要把原来的caffe文件夹移除,git命令会新建一个带有SSD程序的caffe文件夹,当然,这个新的caffe要重新编译一次。
git clone https://github.com/weiliu89/caffe.git
cd caffe
git checkout ssd
安装caffe 的一些依赖项
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler libatlas-base-dev libboost-all-dev libgflags-dev libgoogle-glog-dev liblmdb-dev
随便说一下, Caffe 框架训练属于自己的 SSD 模型了, SSD 的官方实现的 github 地址为:https://github.com/weiliu89/caffe/tree/ssd
2、修改 Makefile.config
(1`)首先将Makefile.config.example的内容复制到Makefile.config
cd caffe
cp Makefile.config.example Makefile.config
(2) 修改 Makefile.config里面的内容
sudo gedit Makefile.config
修改如下:
1.若使用cudnn,取消“# USE_CUDNN := 1” 前的注释即:USE_CUDNN := 1
2.若使用opencv3.x,取消“# OPENCV_VERSION := 3” 前的注释,即:OPENCV_VERSION := 3
3.取消“# WITH_PYTHON_LAYER := 1” 前的注释。即 WITH_PYTHON_LAYER := 1
4.加入hdf5的目录:
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
修改为:
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
5.修改CUDA_ARCH
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_52,code=sm_52 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61 #根据自己GPU型号增加
下面是CUDA7以后 CUDA_ARCH和GPU的对应关系:
Supported on CUDA 7 and later
- Fermi (CUDA 3.2 until CUDA 8) (deprecated from CUDA 9):
- SM20 or SM_20, compute_30 – Older cards such as GeForce 400, 500, 600, GT-630
- Kepler (CUDA 5 and later):
- SM30 or SM_30, compute_30 – Kepler architecture (generic – Tesla K40/K80, GeForce 700, GT-730)
Adds support for unified memory programming - SM35 or SM_35, compute_35 – More specific Tesla K40
Adds support for dynamic parallelism. Shows no real benefit over SM30 in my experience. - SM37 or SM_37, compute_37 – More specific Tesla K80
Adds a few more registers. Shows no real benefit over SM30 in my experience
- SM30 or SM_30, compute_30 – Kepler architecture (generic – Tesla K40/K80, GeForce 700, GT-730)
- Maxwell (CUDA 6 and later):
- SM50 or SM_50, compute_50 – Tesla/Quadro M series
- SM52 or SM_52, compute_52 – Quadro M6000 , GeForce 900, GTX-970, GTX-980, GTX Titan X
- SM53 or SM_53, compute_53 – Tegra (Jetson) TX1 / Tegra X1
- Pascal (CUDA 8 and later)
- SM60 or SM_60, compute_60 – Quadro GP100, Tesla P100, DGX-1 (Generic Pascal)
- SM61 or SM_61, compute_61 – GTX 1080, GTX 1070, GTX 1060, GTX 1050, GTX 1030, Titan Xp, Tesla P40, Tesla P4, Discrete GPU on the NVIDIA Drive PX2
- SM62 or SM_62, compute_62 – Integrated GPU on the NVIDIA Drive PX2, Tegra (Jetson) TX2
- Volta (CUDA 9 and later)
- SM70 or SM_70, compute_70 – DGX-1 with Volta, Tesla V100, GTX 1180 (GV104), Titan V, Quadro GV100
- SM72 or SM_72, compute_72 – Jetson AGX Xavier
- Turing (CUDA 10 and later)
- SM75 or SM_75, compute_75 – GTX Turing – GTX 1660 Ti, RTX 2060, RTX 2070, RTX 2080, Titan RTX, Quadro RTX 4000, Quadro RTX 5000, Quadro RTX 6000, Quadro RTX 8000
参考: http://arnon.dk/matching-sm-architectures-arch-and-gencode-for-various-nvidia-cards/
3、Makefile修改
sudo gedit Makefile
修改如下:
第一处:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
修改为:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
第二处:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
修改为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
第三处:
LIBRARIES += boost_thread stdc++后加boost_regex
修改为:
LIBRARIES += boost_thread stdc++ boost_regex
4、 编译caffe
make all -j16
make py -j16
make test -j16
make runtest -j16
make runtest -j16之后的图:
完成。
遇到的问题:
问题1:
在执行make all -j16时出现:
LD -o .build_release/lib/libcaffe.so.1.0.0-rc3
/usr/bin/ld: cannot find -lopenblas
collect2: error: ld returned 1 exit status
Makefile:569: recipe for target '.build_release/lib/libcaffe.so.1.0.0-rc3' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc3] Error 1
原因:缺少openblas
解决:
安装openblas就可以了:
sudo apt-get install libopenblas-dev
问题2:
在执行make runtest -j16时出现:
*** Error in `.build_release/tools/caffe': free(): invalid pointer: 0x0000000000e282c0 ***
Makefile:529: recipe for target 'runtest' failed
make: *** [runtest] Aborted (core dumped)
原因:很可能是缺少libtcmalloc-minimal4库,
解决:
首先安装
sudo apt-get install libtcmalloc-minimal4
然后打开~/.bashrc文件
sudo gedit ~/.bashrc
在文件末尾添加如下代码:
export LD_PRELOAD="/usr/lib/libtcmalloc_minimal.so.4"
最后重新载入环境变量即可
source ~/.bashrc
最后重新编译。