Caffe(1)--环境配置教程(Ubuntu16.04+opencv3.1+Anaconda3+CUDA9.0+cuDNN7.0.5)

系统:ubuntu16.04 cuda:9.0(已安装),如未安装,可参考:https://blog.csdn.net/lwplwf/article/details/79881699 cudnn:7.0.5(已安装),如未安装,可参考:https://blog.csdn.net/lwplwf/article/details/79881699 anaconda:python3.6(已安装),也可直接用系统自带的python2.7或3.5 opencv:3.1.0(已安装),如未安装,可参考:https://download.csdn.net/download/lwplwf/10342878


0.安装依赖

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 libatlas-base-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

1.安装BLAS(基本线性代数子库) Caffe支持3种,MKL(有限制)、AtLas(效率相对低)、OpenBlas(无限制,效率高)

sudo apt-get install openblas-dev

2.下载caffe

git clone https://github.com/BVLC/caffe.git

3.进入caffe目录,进入里面的python文件夹,执行

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

4.进入caffe目录,将 Makefile.config.example 文件复制一份并更名为 Makefile.config

sudo cp Makefile.config.example Makefile.config

5.修改 Makefile.config 文件 (1)应用 cudnn

#USE_CUDNN := 1 修改为: USE_CUDNN := 1

(2)应用 opencv 版本

#OPENCV_VERSION := 3 修改为: OPENCV_VERSION := 3

(3)使用 python 接口

#WITH_PYTHON_LAYER := 1 修改为:WITH_PYTHON_LAYER := 1

(4)选择BLAS

# open for OpenBlas 
BLAS := open

(5)修改 python 路径,具体见文件

ANACONDA_HOME := $(HOME)/bu5/bu5app/anaconda3
PYTHON_LIBRARIES := boost_python3 python3.6m
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
        $(ANACONDA_HOME)/include/python3.6m \
        $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
PYTHON_LIB := $(ANACONDA_HOME)/lib

(6)修改下面路径,否则编译时报hdf5错误

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

6.添加caffe的python路径到系统环境变量

sudo vim ~/.bashrc

在最后添加(修改为自己的路径):

export PYTHONPATH=/home/bu5/bu5projects/caffe-master/python:$PYTHONPATH

保存,退出

source ~/.bashrc

7.编译

make all -j8

8.安装pycaffe

make pycaffe -j8

完成后: 进python 执行import caffe 若不报错则表示 caffe 的 python 接口已正确编译

终于完成了。。。

9.测试

make runtest -j8

10.MNIST数据集测试 (1)进入caffe根目录,下载MNIST数据集并解压缩

./data/mnist/get_mnist.sh

(2)将其转换成lmdb数据格式

./examples/mnist/create_mnist.sh

(3)训练网络

./examples/mnist/train_lenet.sh

编译遇见的错误见下面错误汇总 报错解决办法:

错误1://home/idc/anaconda3/lib/libpng16.so.16: undefined reference toinflateValidate@ZLIB_1.2.9’解决办法:https://blog.csdn.net/zhangruijerry/article/details/79724720 下载安装zlib1.2.9,没有1.2.9,安装了1.2.11,我装的是zlib1g_1.2.11.dfsg-0ubuntu2_amd64.deb,安装后make clean`,重新编译。

sudo dpkg -i zlib1g_1.2.11.dfsg-0ubuntu2_amd64.deb

错误2:fatal error: pyconfig.h: No such file or directory #include "pyconfig.h" 解决办法:执行 locate pyconfig.h 找到相关路径

export CPLUS_INCLUDE_PATH=/home/bu5/bu5app/anaconda3/include/python3.6m
make clean

再次编译

错误3:cannot find -lboost_python3 查看/usr/lib/x86_64-linux-gnu下只有libboost_python-py35.so,没有libboost_python3.so 解决办法1(无效):手动建立连接,在/usr/lib/x86_64-linux-gnusudo ln -s libboost_python-py35.so libboost_python3.so 解决办法2(有效):修改Makefile.configPYTHON_LIBRARIES := boost_python-py35 python3.6m

错误4:cannot find -lpython3.6m 查看/usr/lib/x86_64-linux-gnu下没有3.6的,只有2.7和3.5的,参考错误3中改Makefile.config内为3.5m,无效。 解决办法: 执行locate libpython3.6m 找到相关路径 发现libpython3.6m.so/home/anaconda3/lib/下,复制到/usr/lib/x86_64-linux-gnu下,完成。

sudo cp /home/anaconda3/lib/libpython3.6m.so /usr/lib/x86_64-linux-gnu/
make clean
make all -j8

错误5:make pycaffe时No such file or directory Python.h 解决办法1(无效): 执行locate Python.h 找到相关路径

export CPLUS_INCLUDE_PATH=/home/anaconda3/include/python3.6m:$CPLUS_INCLUDE_PATH

解决办法2(有效): 修改Makefile.config,添加绝对路径

PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
        $(ANACONDA_HOME)/include/python3.6m \
        $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include \
        /home/anaconda3/include/python3.6m

错误6:在python中import caffe的时候报错

>>> import caffe
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/k/caffe/python/caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
  File "/home/k/caffe/python/caffe/pycaffe.py", line 15, in <module>
    import caffe.io
  File "/home/k/caffe/python/caffe/io.py", line 2, in <module>
    import skimage.io
  File "/home/k/.local/lib/python3.5/site-packages/skimage/io/__init__.py", line 15, in <module>
    reset_plugins()
  File "/home/k/.local/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 95, in reset_plugins
    _load_preferred_plugins()
  File "/home/k/.local/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 75, in _load_preferred_plugins
    _set_plugin(p_type, preferred_plugins['all'])
  File "/home/k/.local/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 87, in _set_plugin
    use_plugin(plugin, kind=plugin_type)
  File "/home/k/.local/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 258, in use_plugin
    _load(name)
  File "/home/k/.local/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 302, in _load
    fromlist=[modname])
  File "/home/k/.local/lib/python3.5/site-packages/skimage/io/_plugins/matplotlib_plugin.py", line 4, in <module>
    import matplotlib.pyplot as plt
  File "/home/k/.local/lib/python3.5/site-packages/matplotlib/pyplot.py", line 39, in <module>
    from matplotlib.figure import Figure, figaspect
  File "/home/k/.local/lib/python3.5/site-packages/matplotlib/figure.py", line 40, in <module>
    from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
  File "/home/k/.local/lib/python3.5/site-packages/matplotlib/axes/__init__.py", line 4, in <module>
    from ._subplots import *
  File "/home/k/.local/lib/python3.5/site-packages/matplotlib/axes/_subplots.py", line 10, in <module>
    from matplotlib.axes._axes import Axes
  File "/home/k/.local/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 24, in <module>
    import matplotlib.dates as _  # <-registers a date unit converter
  File "/home/k/.local/lib/python3.5/site-packages/matplotlib/dates.py", line 152, in <module>
    from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY,
  File "/usr/local/lib/python3.5/dist-packages/dateutil/rrule.py", line 55
    raise ValueError, "Can't create weekday with n == 0"
                    ^
SyntaxError: invalid syntax

解决办法:

pip install python-dateutil --upgrade

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券