前期准备:
最好是python虚拟环境
- 【anaconda的创建虚拟环境】
创建
conda create -n caffeEnv(虚拟环境名字) python=3.6
激活环境
source activate caffeEnv
关闭
deactivate
- 【python virtualenv创建虚拟环境】
创建
pip install virtualenv
sudo apt-get virtualenv
virtualenv caffeEnv(虚拟环境名字) -p /usr/bin/python3(版本)
激活
cd caffeEnv && source ./bin/activate
关闭
deactivate
环境条件
深度学习加速模块和opencv
- cuda8.0+cudnn5.1+opencv3.4.0
- cuda9.1+cudnn7.0+opencv3.4.0
(我试过8.0+5.1和9.1+7.0都可以)
安装教程另外两片博客记录了
caffe依赖库
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install 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
开始安装caffe
- 首先在你要安装的路径下 clone :
git clone https://github.com/BVLC/caffe.git
- 进入 caffe ,将 Makefile.config.example 文件复制一份并命名为 Makefile.config
cd caffe
cp Makefile.config.example Makefile.config
复制一份的原因是编译 caffe 时需要的是 Makefile.config 文件,Makefile.config.example 只是caffe 给出的配置文件例子
- 修改 Makefile.config 文件
gedit Makefile.config
-
开启cudnn
将 #USE_CUDNN := 1 修改成: USE_CUDNN := 1
-
应用 opencv 版本
将 #OPENCV_VERSION := 3 修改为: OPENCV_VERSION := 3
-
使用 python 接口
将 #WITH_PYTHON_LAYER := 1 修改为 WITH_PYTHON_LAYER := 1
-
修改库路径
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
-
修改cuda路径(可选),一般不用改,由于我服务器上的cuda的版本太多,路径不一样
CUDA_DIR := /usr/local/cuda 修改为: CUDA_DIR := /usr/local/nvidia/cuda/8.0
OK ,可以开始编译了,在 caffe 目录下执行 :
make all -j8
(-j8表示自己的cpu核数,如果不知道就直接make all)
如果出错,则检查前面步骤,或者利用搜索引擎解决问题
- 编译成功后可运行测试(出现以下图片类似即成功):
make runtest -j8
编译caffe的python接口
1、 编译
make pycaffe -j8
2、配置到环境变量
gedit ~/.bahsrc
把以下内容加到最下方
export PYTHONPATH=/(caffe所在目录)/caffe/python:$PYTHONPATH
若有多个环境需要添加则像如下添加方法,环境之后需要加“ : ”
export PYTHONPATH=/(caffe所在目录)/caffe/python:/home/xxx/python/:$PYTHONPATH
让环境变量生效
source ~/.bahsrc
have a try!看看能不能用
>>> import caffe
bingo!It's OK
临时配置法(记录一下给自己看)
import sys
sys.path.append("/(caff所在目录)/caffe/python")
sys.path.append("/(caffe所在目录)/caffe/python/caffe")
最后贴一些可能会出现的安装问题:
可能出现的问题
(1)
问题:
Unsupported gpu architecture 'compute_20'
解决方案:
https://askubuntu.com/questions/960238/nvcc-fatal-unsupported-gpu-architecture-compute-20
即去掉Makefile.config 中两行:
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_50,code=sm_50
-gencode arch=compute_52,code=sm_52
-gencode arch=compute_60,code=sm_60
-gencode arch=compute_62,code=sm_62
-gencode arch=compute_61,code=compute_61
(2)
问题:
awk: symbol lookup error: /home/lzm/.conda/envs/lzm2/lib/libreadline.so.6: undefined symbol: PC
解决方案:
https://github.com/conda-forge/rpy2-feedstock/issues/1
https://github.com/bioconda/bioconda-recipes/issues/5350
即 run
conda install -c conda-forge readline = 6.2
(3)
问题:
./include/caffe/util/hdf5.hpp:6:18: fatal error: hdf5.h: no such file or directory
解决方案:
https://github.com/BVLC/caffe/issues/2690
https://github.com/NVIDIA/DIGITS/issues/156
即Makefile.config 拿两行改掉:
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/hdf5/serial/
(4)(这个应该很少人出现)
问题:
./include/caffe/util/nccl.hpp:5:18: fatal error: nccl.h: No such file or directory
解决方案:
新建文件为env
将服务器已经安装的nccl路径配置到env:
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/home/lzm/data/caffe/caffe1.0_nccl/nccl/install/include
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/home/lzm/data/caffe/caffe1.0_nccl/nccl/install/include
export LIBRARY_PATH=$LIBRARY_PATH:/home/lzm/data/caffe/caffe1.0_nccl/nccl/install/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/lzm/data/caffe/caffe1.0_nccl/nccl/install/lib
每次要用的时候都激活环境:
source ./env
(5)
问题:
.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode
解决方案:https://github.com/BVLC/caffe/issues/4621
把Makefile.config 中 OPENCV_VERSION = 3的注释去掉即可
(6)
问题:
/caffe/bin/../lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by caffe-ssh/python/caffe/_caffe.so)
解决方案:https://github.com/BVLC/caffe/issues/4953
conda install libgcc