1.安裝caffe依賴包,apt-get大法搞定: 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 python-dev sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
2.下載caffe源碼
倉庫:git clone https://github.com/BVLC/caffe.git my_caffe
3.修改Makefile.config配置文件
cd my_caffe/
ps:配置爲CPU版本,贊不支持GPU
cp Makefile.config.example Makefile.config
修改:
--- a/Makefile.config
+++ b/Makefile.config
@@ -5,7 +5,7 @@
# USE_CUDNN := 1
# CPU-only switch (uncomment to build without GPU support).
-# CPU_ONLY := 1
+CPU_ONLY := 1
# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
@@ -92,8 +92,8 @@ PYTHON_LIB := /usr/lib
# WITH_PYTHON_LAYER := 1
# Whatever else you find you need goes here.
-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
編譯caffe:
make all
make test
make runtest
測試全部通過,說明caffe編譯成功(默認支持C++接口)。
3.配置支持python接口:
安裝python相關依賴庫
cd my_caffe
cat python/requirements.txt |xargs -l sudo pip install
可能會報scikit-image錯誤
make pycaffe
執行make pycaffe命令的時候,如果報錯:fatalerror: numpy/arrayobject.h沒有那個文件或目錄
解決方法:
sudo apt-get install python-numpy 重新 make pycaffe
配置python路徑; 在.bashrc末尾添加: export PYTHONPATH=/home/leon/work/ai/my_caffe/python:$PYTHONPATH
import caffe沒報錯,說明路徑配置成功;
5、訓練MNIST集
(1)獲取MNIST數據
./data/mnist/get_mnist.sh
(2)將其轉換成Lmdb數據庫格式
./examples/mnist/create_mnist.sh
(3)訓練網絡
./examples/mnist/train_lenet.sh
訓練完成
測試:
./build/tools/caffe.bin test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -iterations 100
參數解釋:
test:只做測試(前向傳播計算),不更新參數(後向傳播計算)
-model examples/mnist/lenet_train_test.prototxt:指定模型描述文件
-weights examples/mnist/lenet_iter_10000.caffemodel :指定模型預先訓練好的權值文件
-iterations 100:測試迭代次數。測試數目爲(iterations*batch_size),batch_size在model prototxt中設定。
PS:本流程爲caffe默認配置,支持了C++和Python 2.7接口,接下來想配置支持python3.6。