caffe Ununtu 16.04.4开发环境配置(CPU版)

caffe是一种常用的深度学习框架,主要应用在视频、图像处理方面的应用上。由于没钱买gpu,只能在虚拟机环境下配置Ubuntu CPU版caffe开发环境,大家勿笑。

1.首先,先安装依赖库:

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 libgflags-dev libgoogle-glog-dev liblmdb-dev

2.下载编码caffe源码

如果没有安装git,请先安装:

    sudo apt-get install git

下载caffe源码:

    git clone https://github.com/BVLC/caffe.git
    cd caffe
    cp Makefile.config.example Makefile.config

或:

    mv Makefile.config.example Makefile.config

打开配置文件:

    vim Makefile.config

修改配置文件

    # CPU_ONLY:=1

改为:

    CPU_ONLY:=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/hdf5/serial/

注意:最后面的斜杠/不能去掉,否则编译会报错
进入目录,并打开终端

    cd /usr/lib/x86_64-linux-gnu/

ubuntu 16.04.4自带python3.5,但需要找出该so库的版本号:

    find .|xargs grep -rl "libboost_python-py35.so."
    ./libboost_mpi_python-py35.so.1.58.0
    ./libboost_python-py35.so.1.58.0
    ./libboost_mpi_python-py35.so.1.58.0
    ./libboost_python-py35.so.1.58.0
    ./libboost_python-py35.so
    ./libboost_mpi_python-py35.so

该so库的版本号为1.58.0,软链到/usr/local/lib/

    sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so.1.58.0 /usr/local/lib/libboost_python3.so

至此,caffe CPU版配置完毕,下面开始编译caffe

    sudo make all -j4
    sudo make test
    sudo make runtest -j4
    

注:如何编译报错,去掉后面的-j4再试试

4.安装python库

进入python目录

    cd python

查看是ubuntu否有安装pip3,如果没安装会出现如下提示:

    pip3 -v
    The program 'pip3' is currently not installed. You can install it by typing:sudo apt install python3-pip

安装pip3:

    sudo apt install python3-pip
    

修改用户配置文件:

vim ~/.bashrc

在最后一行加入:

export PYTHONPATH=/caffepath/python:$PYTHONPATH

加载配置文件:

    sudo ldconfig

安装numpy库:

    sudo apt-get install python-numpy

编译caffe,在caffe目录运行:

    sudo make pycaffe -j16
    

安装成功后,进入python2(python2对caffe的支持比较好)进行测试:

    python
    import caffe
    

报错:

    ImportError: No module named skimage.io

于是考安装skimage.io:

    sudo pip install scikit-image
     

结果发现运行pip报错,真是倒霉的时候喝水都会呛着。错误信息如下:

    Traceback (most recent call last):
    File "/usr/bin/pip", line 9, in <module>
    from pip import main
    ImportError: cannot import name 'main'
     

问了一下度娘,还好有解,先修改pip3:

    sudo vi /usr/bin/pip3

原文: from pip import main
修改后: from pip._internal import main
然后修改pip:

    sudo vi /usr/bin/pip

原文:

    from pip import main  
    if __name__ == '__main__':  
         sys.exit(main()) 
         

修改为:

    from pip import __main__
    if __name__ == '__main__':  
        sys.exit(__main__._main())
     

然后输入pip -V验证:

    pip -V
    pip 18.0 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
    

继续安装scikit-image:

    sudo pip install scikit-image
    

安装成功后,继续输入:

    import caffe
    No module named google.protobuf.internal
    

google.protobuf缺失,安装protobuf

   sudo pip install protobuf
   

安装完毕后输入

    python
    import caffe

如果无错误提示,则说明caffe已经成功安装!

    原文作者:懒熊工作室
    原文地址: https://segmentfault.com/a/1190000015854242
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞