Caffe編譯和影象訓練測試
開始本文閱讀之前,推薦閱讀“CAFFE原始碼解釋與依賴項”:http://write.blog.csdn.net/postedit/76684030
CAFFE程式是用C++寫的,Caffe支援Matlab和PYTHON介面
1.首先是下載原始碼
https://github.com/BVLC/caffe(官網原始碼)
https://github.com/weiliu89/caffe/tree/ssd(支援SSD)
使用CAFFE前建議做以下事情:
瞭解一下CAFFE 的檔案結構
瞭解一下深度學習(神經網路)和影象原理:
瞭解一下C++編譯原理(CMake, Make,Make Install)
PYTHON有靈活的程式設計方式,尤其在科學計算上有明顯優勢,因此建議再c++編譯之後,使用python呼叫。
(CAFFE中很多依賴需要下載安裝,必需安裝pip)網上有教程,
兩個步驟/命令:sudo apt-get install python-pip python-dev
sudo apt-get upgrade
Ubuntu強大的第三方依賴下載功能,因此建議使用Ubuntu進行編譯。
推薦環境:Ubuntu16.04 Python2.7.12
2.然後是CAFFE依賴專案:
cmake python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb libhdf5-serial-dev libboost-all-dev libopenblas-dev liblapack-dev libatlas-base-dev libgflags-dev libgoogle-glog-dev scikit-image python-networkx python-nose python-pandas python-gflags Cython ipython jupyter-notebook
CAFFE2的依賴專案基本包括了CAFFE的依賴項,也可以參考,CAFFE2依賴項:http://blog.csdn.net/yan_joy/article/details/70241319
上面所有外掛使用apt-get或者pip進行安裝
eg sudo apt-get install python-protobuf
或者sudo pip install python-protobuf
上面安裝都是自動下載並安裝的
安裝完使用sudo apt-get install&&update python-protobuf
或者udo pip install python-protobuf --upgrade
進行依賴升級,如果不能安裝成功,如下面安裝:
其他未能下載的使用第三種方法:原始碼安裝
分為deb,whl,bin或者tar.gz/zip等壓縮檔案。
deb,雙擊安裝,
whl:檔案路徑,開啟終端,使用pip安裝 sudo pip install xxxxx.whl
bin:檔案路徑開啟終端,再輸入 sudo chmod u+x a.bin 按回車,提示輸入密碼,輸入登入密碼,回車;此行給檔案執行許可權,然後 sudo ./a.bin 就可以開始安裝了
tar.gz/zip等壓縮檔案,要看檔案是什麼型別,如果是原始檔,按照官網幫助安裝;如果存在編譯檔案cmake,CmakeLists.txt,Makefile.config使用編譯安裝:cmake,make,make install。
編譯解除安裝方法:部分未安裝成功的,使用make uninstall解除安裝,rm -r build刪除編譯檔案(並將/usr/lib和/usr/bin的軟連結刪除乾淨,使用find / -name xxxx.so刪除同名檔案)。
3.編譯原始碼
再Caffe根目錄下,新建build資料夾
並且修改CMakeLists.txt和Makefile.Config這兩個檔案,根據需求修改Python路徑以及對依賴項的支援情況
可以參考以下配置:
CMakeList.txt
# ---[ Options
caffe_option( USE_CUDNN "Build Caffe with cuDNN library support" OFF)
caffe_option(BUILD_SHARED_LIBS "Build shared libraries" ON)
caffe_option(BUILD_python "Build Python wrapper" ON)
set(python_version "2" CACHE STRING "Specify which Python version to use")
caffe_option(BUILD_matlab "Build Matlab wrapper" OFF)
caffe_option(BUILD_docs "Build documentation" ON)
caffe_option( BUILD_python_layer "Build the Caffe Python layer" ON)
caffe_option( USE_OPENCV "Build with OpenCV support" ON)
caffe_option(USE_LEVELDB "Build with levelDB" OFF)
caffe_option( USE_LMDB "Build with lmdb" ON)
caffe_option(ALLOW_LMDB_NOLOCK "Allow MDB_NOLOCK when reading LMDB files (only if necessary)" OFF)
Makefile.config
BLAS := atlas
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /usr/include/atlas
BLAS_LIB := /usr/lib/atlas-base/atlas
# 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
# 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 /usr/lib/x86_64-linux-gnu/hdf5/serial /home/wiener/Downloads/caffe-ssd/python
# The ID of the GPU that 'make runtest' will use to run unit tests.
#TEST_GPUID := 0
注意:GPU 模式需要將CPU-ONLY相關的選項都OFF掉,反之需要將GPU專案註釋掉。
修改後編譯中應該沒有錯誤,否則檢查3或者依賴項是否正確安裝。
正確的編譯步驟為:
首先進入build資料夾(CAFFE根目錄下面):開啟終端,執行cmake ..(中間是空格,最後是兩個點點)
然後進行Caffe根目錄,開啟終端,依次執行:
sudo make all
sudo make pycaffe
sudo make test
sudo make runtest
其中前兩個為必需項,用來生成待呼叫caffe和_caffe模組,如果最後的python指令碼執行過程中報沒有Caffe或者_Caffe模組,就是兩個的編譯問題。錯誤:no module name _caffe
上面命令可以加核心數加速編譯,如:sudo make all -j8
編譯成功後,就可以開始測試或者訓練了。
4。訓練和測試模型
1)測試 一般是用python demo.py(再example下)測試影象識別效果,部分需要用jupyter notebook下載,參照網上案例(例如可以參考SSD檢測單張影象:http://blog.csdn.net/jesse_mx/article/details/52965281),需要制定影象路徑和模型路徑。
模型是.caffemodel字尾,可以再網上案例下載。
測試影象一般放在example的image資料夾中,或者再網上下載任意影象
2)訓練
將資料放在制定資料夾,或者使用軟連結再資料夾建立快捷方式。
首先將檔案轉化為LMDB(LEVELDB執行效率慢就不推薦了),根據教程,載入creat_xxx.sh(再資料夾中開啟終端執行 .create_xxxx.sh或者bash .create_xxxx.sh)
然後,使用tran_xxxx.sh進行訓練(再example裡找)
最後生成.caffemodel檔案。
測試訓練其他步驟:http://suanfazu.com/t/caffe/281