LUbuntu安装配置CPU版本的Caffe

LUbuntu安装caffe. 这和安装caffe_ssd没有区别.
必需的依赖库有:
CUDA is required for GPU mode.
BLAS via ATLAS, MKL, or OpenBLAS.
Boost >= 1.55.
protobuf, glog, gflags, hdf5.
可选的依赖库:
OpenCV >= 2.4 including 3.0.
IO libraries: lmdb, leveldb (note: leveldb requires snappy).
cuDNN for GPU acceleration (v6).
For Python Caffe: Python 2.7 or Python 3.3+, numpy (>= 1.7), boost-provided boost.python.
For MATLAB Caffe: MATLAB with the mex compiler.

修改caffe_Makefile.config:
1) CPU_ONLY := 1
2) USE_OPENCV := 1
USE_LEVELDB := 1
USE_LMDB := 1
3) OPENCV_VERSION := 2
4) BLAS := open
BLAS_INCLUDE := /opt/OpenBLAS/include
BLAS_LIB := /opt/OpenBLAS/lib
5) PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy

6) INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/i386-linux-gnu /usr/lib/i386-linux-gnu/hdf5/serial

7) 安装依赖的库.
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler # 利用root用户安装protobuf, leveldb, opencv, hdf5-serial.
sudo apt-get install –no-install-recommends libboost-all-dev # 安装boost库.
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev # 安装OPENBLAS, LAPACK, ATLAS库.
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
8) make all -j4

protobuf, glog, gflags这三个库利用sudo安装出现版本不兼容的问题!!
apt-get命令下载的libprotobuf、glog、gflags,其共享库.so文件存于/usr/lib/i386-linux-gnu目录, include路径在/usr/include下. 这三个库在LUbuntu上利用apt-get install安装后, 会出现版本不匹配的问题或没有正确链接, 会出现以下错误:
出现错误:
1> undefined reference to google::protobuf::Message::InitializationErrorString() const'
2> undefined reference to
google::base::CheckOpMessageBuilder::NewString()’
解决办法: 下载源码, 手动安装. 首先利用apt-get remove下载这三个库(安装时候的名称和卸载时候的名称要一致, 不然找不到package!!):
sudo apt-get remove libgflags-dev
sudo apt-get remove libprotobuf-dev
sudo apt-get remove libgoogle-glog-dev

(1) protobuf
git clone –recursive https://github.com/google/protobuf.git
./autogen.sh # If you get the source from github, you need to generate the configure script first.
./configure # 将protobuf安装在/usr下面. 不然需要重新指定环境变量: LD_LIBRARY_PATH和C_INCLUDE_PATH, CPLUS_INCLUDE_PATH.
make
make check
sudo make install
sudo ldconfig # refresh shared library cache.
安装完毕, include路径在/usr/local/include/google下, lib在/usr/local/lib下. sudo ldconfig

glog、gflags库的安装可参照http://caffe.berkeleyvision.org/install_apt.html
(2) glog
git clone –recursive https://github.com/google/glog.git
./autogen.sh && ./configure && make && make install # 为加快编译速度, 可以指定编译线程数: -j4.
安装完毕, include路径在/usr/local/include/glog下, lib在/usr/local/lib下. sudo ldconfig
(3) gflags
git clone –recursive https://github.com/schuhschuh/gflags.git
mkdir build && cd build
export CXXFLAGS=”-fPIC” && cmake .. && make VERBOSE=1
make && make install
安装完毕, include路径在/usr/local/include/gflags下, lib在/usr/local/lib下. sudo ldconfig

make clean
make all -j4 # 这样就不会再出现protobuf, glog, gflags库的错误.

caffe: make all -j4
出现错误:
1> .build_release/lib/libcaffe.so: undefined reference to cv::imread(std::string const&, int)'
opencv的问题.
不使用ffmpeg库, 即-D WITH_FFMPEG=OFF. 安装成功!! 可以正常使用!! 利用gcc或g++编译时, 在LUbuntu上需为: g++ *.cpp
pkg-config opencv –cflags –libs opencv
不然就会报错: undefined reference to
cv::…` 由于没有使用ffmpeg, 因此现在的opencv库不能读取视频和摄像头!!! sudo ldconfig

caffe: make all -j4
出现错误:
undefined reference to `leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)
是链接的问题, 源码安装试试.
git clone –recursive https://github.com/google/leveldb.git
将Makefile中的$(CC)改为gcc, 然后make -j4. 此时leveldb/下多出out-shared和out-static目录, 其中out-shared/下有:
db db_bench helpers libleveldb.so libleveldb.so.1 libleveldb.so.1.20 port table util
将out-shared/libleveldb.so*拷贝到/usr/local/lib , 将include/*拷贝到/usr/local/include, 即拷贝leveldb文件夹. sudo ldconfig

caffe: make all -j4 # caffe编译成功!!!
make test -j4 # 成功通过
make runtest -j4 # 进行单元测试, 但是一些程序无法通过单元测试. 出现错误:
[———-] 12 tests from NesterovSolverTest/1, where TypeParam = caffe::CPUDevice
[ RUN ] NesterovSolverTest/1.TestNesterovLeastSquaresUpdate
Aborted at 1500259849 (unix time) try “date -d @1500259849” if you are using GNU date *
PC: @ 0xb74e5516 boost::filesystem::path::operator/=()
SIGSEGV (@0x5) received by PID 11788 (TID 0xb3bf2500) from PID 5; stack trace: *
@ 0xbfea44cc (unknown)
@ 0xffffffff (unknown)
Segmentation fault (core dumped)
Makefile:532: recipe for target ‘runtest’ failed

依据http://blog.csdn.net/solomon1558/article/details/52015754的解释, 可能是boost库出现了问题, 删除原先安装的libboost-all-dev, 源码安装boost库.
sudo apt autoremove libboost-all-dev # 卸载boost库. remove和autoremove是有区别的.
http://www.boost.org/上下载boost库. boost_1_64_0.tar.bz2
tra -xvf boost_1_64_0.tar.bz2
boost中, 用到了别的函数库, 所以为了使用boost中相应的功能, 需要先安装系统中可能缺失的库.
apt-get install mpi-default-dev  #安装mpi库
apt-get install libicu-dev     #支持正则表达式的UNICODE字符集
apt-get install python-dev     #需要python的话
apt-get install libbz2-dev     #如果编译出现错误:bzlib.h: No such file or directory
boost里有个bootstrap.sh的脚本文件, 运行这个脚本:
./bootstrap.sh # 运行boostrap.sh时, 不带–prefix参数默认路径是/usr/local/include和/usr/local/lib, 分别存放头文件和各种库. 执行完成后, 会生成bjam和b2这两个是一样的.
./b2 install # 可以正确安装.
sudo ldconfig # 源码编译, 运行时遇到链接问题, 将不会找到库. 需要最后执行: sudo ldconfig.

caffe: make runtest -j4 # 进行单元测试. 最终出现如下标志:
[———-] Global test environment tear-down
[==========] 1106 tests from 150 test cases ran. (62835 ms total)
[ PASSED ] 1106 tests.
代表caffe配置成功.

MNIST数据集测试
下载MNIST数据库并解压缩:
./data/mnist/get_mnist.sh
将数据集转换成Lmdb数据库格式:
./examples/mnist/create_mnist.sh
训练网络:
先修改lenet_solver.prototxt中的: solver_mode: CPU
./examples/mnist/train_lenet.sh
可以正常训练, 测试. 自动并行.

利用sudo apt-get install *后如果遇到, undefined reference to的问题, 很可能是某个库和机器不兼容, 解决的办法可以是手动源码安装. 在从源代码编译库之后, 将在运行时遇到链接问题, 将不会找到库. 需要最后执行: sudo ldconfig.

CPU caffe安装完成!!!

你可能感兴趣的:(caffe)