注意:要求Ubuntu系統預先安裝opencv和cuda8.0
下載caffe,一般可從GitHub獲得,解壓縮。
一、編譯
1、cd /home/(服務器的名字)/caffe(caffe目錄下)
2、cp Makefile.config.example Makefile.config
打開並修改Makefile.config
若使用GPU加速,則 use CUDNN:=1 去除注釋;
若使用OpenCV3版本,則OPENCV_VERSION := 3 去除注釋;
根據顯卡計算能力刪除(一般只報警告)
Python包含目錄要正確:
3、cd /home/**(您服務器的名字)/caffe
4、mkdir build
5、cd build
6、cmake ..(cmake和..中間有一個空格)
7、make all -j16("‐j16"是使用 CPU 的多核進行編譯,可以極大地加速編譯的速度)
8、make install
9、make runtest(這一步不是必須的)
10、make pycaffe(編譯pycaffe)
二、編譯過程中可能出現的錯誤
錯誤1:/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h:沒有那個文件或目錄compilation terminated.
Makefile:575: recipe for target '.build_release/src/caffe/layer_factory.o' failed
make:***[./build_release/src/caffe/layer_factory.o] Error 1
解決1:終端輸入
$make clean
$export CPLUS_INCLUDE_PATH=/usr/include/python2.7
$make -j16
錯誤2:src/caffe/layers/hdf5_output_layer.cpp:3:18: fatal error: hdf5.h: 沒有那個文件或目錄compilation terminated.
Makefile:575: recipe for target '.build_release/src/caffe/layers/hdf5_output_layer.o' failed
make: *** [.build_release/src/caffe/layers/hdf5_output_layer.o] Error 1
make: *** 正在等待未完成的任務....
解決2:Makefile.config文件第85行左右,添加/usr/include/hdf5/serial/到INCLUDE_DIRS,如下第一行改為第二行:
Makefile.config文件第173行左右,把hdf5_hl和hdf5修改為hdf5_serial_hl和hdf5_serial,如下第三行改為第四行:
1.INCLUDE_DIRS :=$(PYTHON_INCLUDE)/usr/local/incllude
2.INCLUDE_DIRS :=$(PYTHON_INCLUDE)/usr/local/incllude /usr/include/hdf5/serial/
3.LIBRARIES+= glog flags protobuf boost_system boost_filesystem m hdf5_hl hdf5
4.LIBRARIES+= glog flags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
錯誤3:/usr/bin/ld:cannot find -lhdf5_hl
/usr/bin/ld:cannot find -lhdf5
collect2:error:ld returned 1 exit status
Makefile:572: recipe for target '.build_release/lib/libcaffe.so.1.0.0' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0] Error 1
解決3:在Makefile.config中添加:
INCLUDE_DIRS :=$(PYTHON_INCLUDE)/usr/local/incllude /usr/include/hdf5/serial/
LIBRARY := $(PYTHON_LIB)/usr/local/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
錯誤4:在代碼中進行import caffe后報錯ImportError:No module named _caffe
解決4:終端輸入
$make clean
$make -j16
$make pycaffe(關鍵)
錯誤5:caffe/util/cudnn.hpp:8:34: fatal error: caffe/proto/caffe.pb.h:沒有那個文件或目錄(找不到caffe.pb.h)
解決5:caffe編譯過程沒有問題的話,就是因為缺失文件,而缺失的文件在caffe安裝根目錄下的include文件夾中
終端輸入:
$protoc src/caffe/proto/caffe.proto --cpp_out=.
$mkdir include/caffe/proto
$mv src/caffe/proto/caffe.pb.h include/caffe/proto
錯誤6:訓練或者測試時,遇到./build/tools/caffe:not found錯誤
解決6:終端執行命令需在caffe的目錄下。
錯誤7:cudnn版本問題(提示錯誤是與cudnn相關)
解決7:先終端輸入make clean,把caffe里面的所有與cudnn相關的.h和cpp替換成第一次配置caffe時候所用的cudnn版本(caffe/src/caffe/util下的cudnn.cpp和
caffe/include/caffe/layers下與cudnn相關的cpp和cu文件)