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,請先安裝:python

sudo apt-get install git

下載caffe源碼:linux

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

或:git

mv Makefile.config.example Makefile.config

打開配置文件:github

vim Makefile.config

修改配置文件ubuntu

# CPU_ONLY:=1

改成:vim

CPU_ONLY:=1
   INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
改成:bash

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/

注意:最後面的斜槓/不能去掉,不然編譯會報錯
進入目錄,並打開終端app

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/python2.7

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已經成功安裝!

相關文章
相關標籤/搜索