虚拟环境python3中安装caffe过程

前提:下载caffe,装好cuda,opencv

 

一、安装依赖:

sudo ap install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler

sudo apt install --no-install-recommends libboost-all-dev

 

在 {caffe root}/python 下启动虚拟环境,在虚拟环境下安装一些需要的库。运行

for req in $(cat requirements.txt); do pip install $req; done

如果遇到权限问题,则到虚拟环境中的bin目录下安装,如:

cd /home/hd/.pyenv/versions/env361/bin

sudo pip install -r ~/caffe/python/requirements.txt

 

二、修改Makefile.config文件

先运行cp Makefile.config.example Makefile.config以备份

1、如果用CPU则取消CPU_ONLY := 1的注释,如果使用GPU则取消USE_CUDNN := 1的注释。

2、如果使用opencv3,则取消OPENCV_VERSION := 3的注释

3、选择BLAS,比如BLAS := open(或者使用atlas,CPU的则使用mkl较好)

4、选择python路径,修改PYTHON_INCLUDE(根据自己的虚拟环境位置修改):

PYTHON_INCLUDE := /home/hd/.pyenv/versions/3.6.1/include/python3.6m \

/home/hd/.pyenv/versions/3.6.1/lib/python3.6/site-packages/numpy/core/include

5、取消PYTHON_LIBRARIES的注释,并修改为:PYTHON_LIBRARIES := boost_python-py35 python3.6m。重点:找到虚拟环境中的libpython3.6m.so文件,复制到/usr/lib/x86_64-linux-gnu下;确认/usr/lib/x86_64-linux-gnu下的libboost_python-py35.so的文件存在,如果不存在py35后缀,而存在别的py3*文件,如libboost_python-py34.so,则修改Makefile.config中为boost_python-py34。

6、修改PYTHON_LIB := /home/hd/.pyenv/versions/3.6.1/lib

7、取消WITH_PYTHON_LAYER := 1的注释

8、在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

9、开始编译:

make all

make test

make runtest

后两步可省去,只运行make all

make pycaffe

语句后面加上-j8(CPU线程数)可以提高编译速度,如make all -j8

10、编译结束后,添加环境变量

sudo gedit ~/.bashrc

在末尾添加export PYTHONPATH=/home/hd/caffe/python:$PYTHONPATH

source ~/.bashrc

三、测试

启动虚拟环境,运行python

输入import caffe,如果没任何提示,则编译通过。

可能遇到的问题:

Traceback (most recent call last):

  File "", line 1, in

  File "/home/.../Downloads/caffe-master/python/caffe/__init__.py", line 1, in

    from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer

  File "/home/.../Downloads/caffe-master/python/caffe/pycaffe.py", line 15, in

    import caffe.io

...

File "/home/.../Downloads/yes/lib/python3.5/site-packages/skimage/io/_plugins/matplotlib_plugin.py", line 3, in

    import matplotlib.pyplot as plt

...

  File "/home/.../Downloads/yes/lib/python3.5/site-packages/dateutil/rrule.py", line 55

    raise ValueError, "Can't create weekday with n == 0"

原因是matplotlib版本过低,运行pip install matplotlib --upgrade 再import caffe就可以通过。

四、番外:

在编译RMPE项目中的caffe时,遇到

libcaffe.so:undefined reference to ‘cv::VideoCapture::VideoCapture()’

这样的问题,仔细看看发现是opencv里的video模块出了问题

解决方法:找到Makefile,195行

LIBRARIES += opencv_core opencv_highgui opencv_imgproc

改为

LIBRARIES += opencv_core opencv_highgui opencv_imgproc opencv_videoio

估计是作者放出来的代码不完善导致的

 

你可能感兴趣的:(python,caffe,python,caffe)