先将caffe的包git下来
git clone https://github.com/BVLC/caffe.git
然后cd到caffe目录下
首先确认是否安装了python2.7版本
然后
sudo apt-get install python-dev
cd到caffe目录下,复制Makefile.config.example文件为Makefile.config
sudo cp Makefile.config.example Makefile.config
将CPU_ONLY:=1前面的注释去掉
更改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
然后将PYTHON_INCLUDE后面的第二行路径lib前面加上/local
更改之后变为
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/local/lib/python2.7/distpackages/numpy/core/include
安装所需要的依赖
基本依赖
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
BLAS安装
sudo apt-get install libatlas-base-dev
其他依赖安装
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
都安装成功之后进行编译
make all -j16
make test -j16
make runtest -j16
接下来编译python接口
首先安装pip
sudo apt-get install python-pip
python目录下的requirements.txt文件中列出了所有需要安装的依赖库,可以照着这个清单安装
安装scipy需要fortran编译器gfortran
sudo apt-get install gfortran
cd ./python
for req in $(cat requirements.txt); do pip install $req; done
安装完成之后cd到caffe目录下进行依赖库安装
sudo pip install -r python/requirements.txt
然后会安装清单中所有的依赖库,但中间可能会因为某个依赖库版本不一致而造成报错,可以将清单中该项屏蔽掉,然后手动安装该项。
然后进行编译
make pycaffe -j16
编译成功后可以使用python2 import caffe测试caffe是否可以调用
这时可能会报错
ModuleNotFoundError:No Module named 'caffe'
解决方法:
vim ~/.bashrc
在最后一行加上对应的/caffe/python路径
我的路径如下
export PYTHONPATH=/home/zzzz/20190429/caffe_offical/caffe/python:$PYTHONPATH
之后source ~/.bashrc
使环境变量生效即可
再次进行import即可
若这时出现ImportError: No module named skimage.io的错误
sudo apt-get install python-skimage
进行安装即可
import caffe时出现
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/caffe/python/caffe/__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/opt/caffe/python/caffe/pycaffe.py", line 13, in <module>
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
ImportError: No module named _caffe
也是需要将caffe路径添加到环境变量
方法一(一次性):
在终端输入:
PYTHONPATH=/workspace/caffe/python
注意这种方法是一次性的,关闭终端再打开后就会失效
方法二(永久):
vim ~/.bashrc
在打开的文件最后加上
export PYTHONPATH=/workspace/caffe/python:$PYTHONPATH
保存关闭之后运行下面运行使环境变量生效
source ~/.bashrc
然后再次import caffe即可