caffe gpu环境的配置就是一部赤裸裸的血泪史,每次都能遇到很多问题~
环境依赖
ubuntu 16.04
cuda 10.0
python 3.5
opencv 3.4.10
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install git cmake build-essential
git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config
修改Makefile.config
1.
# USE_CUDNN := 1
# USE_OPENCV := 0
# OPENCV_VERSION := 3
# WITH_PYTHON_LAYER := 1
# 改为:
USE_CUDNN := 1
USE_OPENCV := 1
OPENCV_VERSION := 3
WITH_PYTHON_LAYER := 1
2.
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
3.
# 推荐使用python3.5的版本,一般Ubuntu系统自带的就行,python2.7太老,python3.6或者3.7容易出问题
# 把python2.7代码加注释,python3.5前的注释去掉
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3.5/dist-packages/numpy/core/include
4.
# 如果cuda版本大于等于9.0,需要注释掉前两行,如下
# -gencode arch=compute_20,code=sm_20 \
# -gencode arch=compute_20,code=sm_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 \
-gencode arch=compute_61,code=compute_61
修改Makefile
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
改为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m
改为:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
开始编译
make all -j8
这里遇到一个报错:
cannot find -lboost_python3
解决方案如下(https://github.com/BVLC/caffe/issues/4843)
cd /usr/lib/x86_64-linux-gnu
sudo ln -s libboost_python-py35.so libboost_python3.so
报错解决之后make clean一下,重新编译
sudo make all -j8
sudo make test -j8
sudo make runtest -j8
sudo make all的结果
sudo make test的结果
sudo make runtest的结果
如果你的运行结果和上面的一致,那么恭喜你,让无数人蛋疼的caffe-gpu环境配置的深坑成功让你淌过去了,接下来测试一下。
mnist数据集测试
# 下载mnist数据集
cd ~/caffe/data/mnist
sudo sh ./get_mnist.sh
# 在该目录下将有相应图片和标签文件mnist数据格式转换
cd ~/caffe
sudo sh ./examples/mnist/create_mnist.sh
# 将在mnist文件夹(上个步骤的路径)生成LMDB格式数据集
# 训练mnistcd ~/caffe
sudo sh ./examples/mnist/train_lenet.sh
测试结果如下,到此caffe成功安装完毕
配置python接口
# 配置环境变量
sudo echo export PYTHONPATH="{caffe所在路径}/python" >> ~/.bashrc
# 使之生效
source ~/.bashrc
测试报错,缺少包
在这里卡了一下,系统目前默认python解释器是python3.5没问题,但pip安装的时候不知道为啥老是自动装到python3.6的环境中了
解决方案:
python -m pip install numpy scikit-image
如果默认python版本不是3.5,这里就需要指定具体的路径了,比如
/usr/bin/python3.5 -m pip install scikit-image
参考:
http://www.share4y.cn/2020/06/install-the-pip-package-to-the-specified-python-environment/
最终测试
完美!!!
参考来源:
https://blog.csdn.net/qq_33431368/article/details/84717053
https://blog.csdn.net/qq525003138/article/details/103910929
https://blog.csdn.net/g11d111/article/details/78141202
https://blog.csdn.net/weixin_38396731/article/details/78239117
https://blog.csdn.net/chenlufei_i/article/details/80068953
下一篇,
如何用caffe-ssd进行训练,
敬请期待...
往期推荐
Ubuntu 配置多个版本cuda(10.0、10.1)
用树莓派4b构建深度学习应用(九)Yolo篇
用树莓派4b构建深度学习应用(口罩篇)
用AI从零开始创建一个宫崎骏的世界