1. 程式人生 > >ubuntu14.04 安裝caffe

ubuntu14.04 安裝caffe

安裝matlab2015b

選擇產品
MATLAB 8.6
Computer Vision System Toolbox 7.0
Image Acquisition Toolbox 4.10
Image Processing Toolbox 9.3
MATLAB Coder 3.0
MATLAB Compiler 6.1
MATLAB Compiler SDK 6.1
MATLAB Report Generator 4.2
Neural Network Toolbox 8.4
Statistics and Machine Learning Toolbox 10.1

安裝caffe

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 libgflags-dev libgoogle-glog-dev liblmdb-dev
from http://caffe.berkeleyvision.org/install_apt.html

如果遇到下面的錯誤

Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:

重新編輯源/etc/apt/sources.list,我遇到好幾次都是這個被改了,無法自動解決依賴關係
初步推測,可能是映象站沒有保留全部的依賴關係,導致了這個問題,因為我在家裡14.04.5上,用163的源就有各種依賴問題,換成清華的就一步搞定了

安裝OpenBLAS

git clone https://github.com/xianyi/OpenBLAS.git
cd OpenBLAS
make
make install

The default directory is /opt/OpenBLAS

cp Makefile.config.example Makefile.config
vim Makefile.config # 編輯 BLAS 和 python 路徑
BLAS := open
BLAS_INCLUDE := /opt/OpenBLAS/include
BLAS_LIB := /opt/OpenBLAS/lib

安裝python所需

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

安裝 CUDA 和 cuDNN

配置編譯caffe

配置openblas 和 matlab的路徑之後,配置
編譯pycaffe前,需要修改 PYTHON_INCLUDE

PYTHON_INCLUDE := /usr/include/python2.7 \
        /usr/lib/python2.7/dist-packages/numpy/core/include \ # 這個路徑我沒有找到,刪不刪除隨意
        /usr/local/lib/python2.7/dist-packages/numpy/core/include # 如果不加這個路徑,會發生下面的錯誤。這個路徑用locate arrayobject.h 可以看到

不加會遇到下面的錯誤

touch python/caffe/proto/__init__.py
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
PROTOC (python) src/caffe/proto/caffe.proto
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory
 #include <numpy/arrayobject.h>
                               ^
compilation terminated.
make: *** [python/caffe/_caffe.so] Error 1

make runtest如果遇到下面的錯誤

.build_release/test/test_all.testbin 0 --gtest_shuffle --gtest_filter="-*GPU*"
.build_release/test/test_all.testbin: error while loading shared libraries: libopenblas.so.0: cannot open shared object file: No such file or directory
make: *** [runtest] Error 127
# 1 在/usr/lib 下建立到/opt/OpenBLAS/lib/libopenblas.so.0 的符號連結
ln -s /opt/OpenBLAS/lib/libopenblas.so.0 /usr/lib/
# 2 export LD\_LIBRARY_PATH with OpenBLAS library path

如果顯示 綠色的 PASSED,說明完成了。

Training LeNet on MNIST with Caffe

cd $CAFFE_ROOT
./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh
./examples/mnist/train_lenet.sh

1080ti 花了大概10幾秒,比CPU_ONLY 快了非常多

make matcaffe 提示

Warning: You are using gcc version '4.8.4'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
Warning: You are using gcc version '4.8.4-2ubuntu1~14.04.3)'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
MEX completed successfully.

想辦法安裝4.7的gcc,並替換4.8的符號連結

apt install -y gcc-4.7 g++-4.7
cd /usr/bin
mkdir gcc-link-bak
mv gcc g++ gcc-link-bak/
ln -s gcc-4.7 gcc
ln -s g++-4.7 g++

執行demo報錯

Error using textscan
'BufSize' is no longer required and has been removed.

Error in read_cell (line 11)
fileLines = textscan(fid,'%s','delimiter',linesep,'BufSize',100000);

Error in matcaffe_batch_feat (line 28)
    list_im = read_cell(filename);

Error in demo (line 30)
[feat_test , list_im] = matcaffe_batch_feat(test_file_list, use_gpu, feat_len, model_def_file,
model_file);

cvprw15這篇文章使用的是matlab2012,而15的介面已經變了
嘗試刪除 ‘BufSize’,100000 這兩個引數後demo,發現找不到libopenblas.so.0,說明前面只能用符號連結這個庫到/usr/lib了