caffe Ununtu 16.04.4開發環境配置(CPU版)

懶熊工作室發表於2019-02-16
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已經成功安裝!

相關文章