使用ubuntu16.04安裝編譯caffe框架踩了很多坑,裝了很久,編譯成功的是CPU版本,先將就用。
一、環境
- Linux: ubuntu-16.04-desktop-amd64
- OpenCV3.4.8,安裝教程:OpenCV3.4.8安裝(自己隨便安裝一個版本即可)
- cuda9.0(網上的安裝教程很多,這個不難)
- cudnn(同上)
注:這些環境已經提前安裝好
二、安裝
1、必要的環境
sudo apt-get update #更新軟件列表
sudo apt-get upgrade #更新軟件
sudo apt-get install build-essential #安裝build essentials
2、安裝必要的庫
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev protobuf-compiler gfortran libjpeg62 libfreeimage-dev libatlas-base-dev git python-dev python-pip libgoogle-glog-dev libbz2-dev libxml2-dev libxslt-dev libffi-dev libssl-dev libgflags-dev liblmdb-dev python-yaml
sudo easy_install pillow
3、下載caffe
cd ~
git clone https://github.com/BVLC/caffe.git
4、安裝python相關的依賴庫
cd caffe
cat python/requirements.txt | xargs -L 1 sudo pip install
5、增加符號鏈接
sudo ln -s /usr/include/python2.7/ /usr/local/include/python2.7
sudo ln -s /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/ /usr/local/include/python2.7/numpy
6、修改Makefile.config配置文件
cp Makefile.config.example Makefile.config
用gedit打開Makefile.config(或者直接用vim在終端中打開修改也可以)
gedit Makefile.config
結果如圖:(ctrl + F 直接搜索CPU_ONLY就可以)
把 /usr/lib/python2.7/dist-packages/numpy/core/include
改爲: /usr/local/lib/python2.7/dist-packages/numpy/core/include
如圖所示:
安裝hdf5:
sudo apt-get install libhdf5-dev
添加hdf5庫文件:
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/
如圖所示:
7、編譯caffe
在caffe目錄下面執行:
make test
make pycaffe
make all
理論上應該是就可以編譯成功,但是到這個步驟我遇到幾個問題,重新編譯好幾次,每次的錯誤都不太一樣,下面的解決方法解決了所有的報錯。
- 問題
LD .build_release/src/caffe/test/test_db.o
.build_release/lib/libcaffe.so:對‘cv::imread(cv::String const&, int)’未定義的引用
.build_release/lib/libcaffe.so:對‘cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)’未定義的引用
.build_release/lib/libcaffe.so:對‘cv::imdecode(cv::_InputArray const&, int)’未定義的引用
collect2: error: ld returned 1 exit status
Makefile:628: recipe for target '.build_release/test/test_db.testbin' failed
make: *** [.build_release/test/test_db.testbin] Error 1
這個問題的出現應該是opencv類的問題,因爲我使用的是opencv3,所以出現這種報錯。
解決:
- 在Makefile文件中將
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m
改爲:
LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system boost_filesystem hdf5_hl hdf5 m \
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs opencv_videoio
- 將Makefile.config文件中的
# OPENCV_VERSION := 3
取消註釋,變爲:
OPENCV_VERSION := 3
- 執行:
make clean #清除編譯信息
再次執行:
make test
make all
make pycaffe
就可以編譯成功!
8、使用mnist測試
- 依次執行:
cd ~/caffe
./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh
- 編輯examples/mnist文件夾下的lenet_solver.prototxt文件,將solver_mode模式從GPU改爲CPU。
- 訓練模型
./examples/mnist/train_lenet.sh
訓練結果:
注:每個人在安裝的時候都可能遇到不一樣的問題,需要耐心慢慢解決
參考鏈接:
- https://blog.csdn.net/weixin_37850264/article/details/79065695
- https://blog.csdn.net/w5688414/article/details/76695413?utm_source=blogxgwz1
- http://melonteam.com/posts/quan_juan_ji_shen_jing_wang_luo_fcn_xue_xi_bi_ji/#%E5%9B%9B%E4%BD%BF%E7%94%A8caffe%E8%BF%9B%E8%A1%8Cfcn%E7%9A%84%E8%AE%AD%E7%BB%83
- https://www.cnblogs.com/houjun/p/9982903.html