天天看點

Ubuntu16.04下安裝Caffe(CPU版)第一步:安裝Caffe依賴第二步:安裝Caffe第三步:設定Python Caffe 路徑第四步:遇到的錯誤最後的最後:後續的學習。。。

OS:Ubuntu16.04

使用Caffe Python接口

Envs:CPU(可以沒有NVIDIA 顯示卡)

參考:CAFFE官網安裝指南頁面http://caffe.berkeleyvision.org/installation.html#compilation

https://blog.csdn.net/u010402483/article/details/51506616

https://blog.csdn.net/muzilinxi90/article/details/53673184

https://www.linuxidc.com/Linux/2016-09/135034.htm

https://www.cnblogs.com/------------/p/6070324.html

第一步:安裝Caffe依賴

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
           

Python需要2.7版本,這是作業系統本身已經安裝好的. 輸入

python2.7 --version

會顯示具體的版本号說明安裝了.

但是還需要

sudo apt-get install python-dev

這裡我要說明的是,我在Ubuntu中提前安裝了Anaconda2,并在Anaconda2的環境下又安裝了Anaconda3,但是這些對Caffe的安裝影響不大,因為我安裝的環境實在Ubuntu自帶的Python2.7下進行安裝的。

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
           

然後把Caffe的源代碼下載下傳下來:

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

(當然沒有安裝Git的,根據提示得先安裝一下)

下載下傳完成之後,進入caffe檔案夾, 進入裡面的python檔案夾,然後輸入

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

(pip如果沒有安裝得先安裝一下:

sudo apt install python-pip

)

第二步:安裝Caffe

到caffe檔案夾, 使用模闆寫個

Makefile.config

. 具體就是先複制一下模闆, 再改一些内容,用下面的指令就可以複制

Makefile.config

.

cp Makefile.config.example Makefile.config
           

-因為CPU mode, 是以在

Makefile.config

檔案中找到

CPU_ONLY := 1

,将前面的#要去掉.表示CPU模式。

-找到下面兩個路徑要改成這樣:(添加後面的兩個hdf5的路徑, 否則編譯時報hdf5錯誤)

#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/hdf5/serial
           

之後儲存關閉即可。

以上工作做完以後,依次輸入下面的指令:

make pycaffe
make all
make test
make runtest
           

結果顯示

ALL TESTS PASSED

就安裝好了, 隻需要再加上一個

PYTHONPATH;

另外, 這個make預設是用CPU單核運算,如果想要快一點, 比如我想使用四核, 在所有的指令後面加上-j4标簽.如

make pycaffe -j4

如果上面4行某一行報錯之後想要重試,建議先

make clean

再重新開始.

第三步:設定Python Caffe 路徑

簡單快捷的方式:

export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
           

其中,

“/path/to/caffe/python”

為克隆得到的caffe中python路徑,根據克隆的路徑不同,可以自行修改。

我自己克隆caffe路徑為:

/home/peng/文檔/caffe/python
是以上述指令在我的環境中應該為:
export PYTHONPATH=/home/peng/文檔/caffe/python:$PYTHONPATH
           

當時上述指令執行後,如果視窗關閉後,那麼下次還需再次執行該指令,才能夠生效。

一次性的路徑更改方法,永久有效:

A. 把環境變量路徑放到

~/.bashrc

檔案中

sudo echo export PYTHONPATH="~/caffe/python" >> ~/.bashrc
           

B. 使環境變量生效

source ~/.bashrc
           

測試是否安裝成功

重新打開一個控制台,輸入

python2.7

或者

python

>>> import caffe
>>> 
           

如果出現上述情況,說明安裝成功!

第四步:遇到的錯誤

基本上所有錯誤都是因為dependencies缺乏或者路徑不對,是以根據具體的錯誤資訊對症下藥.例如:

A . -編譯時顯示hdf5錯誤, 按照上面所說, 增加路徑之後就解決了.

-import caffe時顯示scikit-image錯誤, 那就安裝一下scikit-image就好了.

pip install scikit-image
           

B. 設定到這裡開始編譯,make pycaffe,結果報錯,錯誤和numpy相關,重新打開Makefile.config目錄,又查找了一下numpy的安裝目錄,發現對應不上,需要重新設定,需要把原本如下的内容:

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \
		/usr/lib/python2.7/dist-packages/numpy/core/include
           

更改為:

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \
		/usr/local/lib/python2.7/dist-packages/numpy/core/include
           

C. 配置完成後,

>>> import caffe

出現下面的錯誤提示:

Ubuntu16.04下安裝Caffe(CPU版)第一步:安裝Caffe依賴第二步:安裝Caffe第三步:設定Python Caffe 路徑第四步:遇到的錯誤最後的最後:後續的學習。。。

解決方案:

使用febootstrap時碰到這個錯誤

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe6 in position 19: ordinal not in range(128)

經過搜尋,發現應該是因為python2.x的預設編碼是ascii,而代碼中可能由utf-8的字元導緻,解決方法是設定utf-8。

找到出錯的檔案,

/home/peng/anaconda2/lib/python2.7/site-packages/matplotlib/backends/__init__.py

在import後增加下面幾行

import sys
if sys.getdefaultencoding() != 'utf-8':
    reload(sys)
    sys.setdefaultencoding('utf-8')
           

參考:https://blog.csdn.net/jewelsu/article/details/78683024?utm_source=copy

http://shirley-ren.iteye.com/blog/1018750

最後的最後:後續的學習。。。

Ubuntu 16.04下Matlab2014a+Anaconda2+OpenCV3.1+Caffe安裝 http://www.linuxidc.com/Linux/2016-07/132860.htm

Ubuntu 16.04系統下CUDA7.5配置Caffe教程 http://www.linuxidc.com/Linux/2016-07/132859.htm

Caffe在Ubuntu 14.04 64bit 下的安裝 http://www.linuxidc.com/Linux/2015-07/120449.htm

深度學習架構Caffe在Ubuntu下編譯安裝 http://www.linuxidc.com/Linux/2016-07/133225.htm

Caffe + Ubuntu 14.04 64bit + CUDA 6.5 配置說明 http://www.linuxidc.com/Linux/2015-04/116444.htm

Ubuntu 16.04上安裝Caffe http://www.linuxidc.com/Linux/2016-08/134585.htm

Caffe配置簡明教程 ( Ubuntu 14.04 / CUDA 7.5 / cuDNN 5.1 / OpenCV 3.1 ) http://www.linuxidc.com/Linux/2016-09/135016.htm

繼續閱讀