1.安装相关的依赖
按照官网的流程进行安装,过程如下:
安装通用的依赖:
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
注意其中安装了libopencv-dev,我比较关心opencv的版本,所以在安装之前查看这个包的信息为2.4.8,感觉可以就安装了。
sudo apt-cache show libopencv-dev
Package: libopencv-dev
Priority: optional
Section: universe/libdevel
Installed-Size: 704
Maintainer: Kubuntu Developers <kubuntu-devel@lists.ubuntu.com>
Original-Maintainer: Debian Science Team <debian-science-maintainers@lists.alioth.debian.org>
Architecture: amd64
Source: opencv
Version: 2.4.8+dfsg1-2ubuntu1
......
Description-en: development files for opencv
This is a metapackage providing development package necessary for
development of OpenCV (Open Computer Vision).
.
The Open Computer Vision Library is a collection of algorithms and sample
code for various computer vision problems. The library is compatible with
IPL (Intel's Image Processing Library) and, if available, can use IPP
(Intel's Integrated Performance Primitives) for better performance.
.
OpenCV provides low level portable data types and operators, and a set
of high level functionalities for video acquisition, image processing and
analysis, structural analysis, motion analysis and object tracking, object
recognition, camera calibration and 3D reconstruction.
Description-md5: f9dc67381f1013c39fe59842c79cbddf
Homepage: http://opencv.org/
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
Supported: 9m
安装cuda
参见我的另一篇博客 Ubuntu安装记录:软件安装 中的安装方法,很简单。去CUDA GPUs 确定你的显卡是英伟达独立显卡并且支持cuda,否则就只用CPU吧。
安装BLAS
BLAS,英文全称Basic Linear Algebra Subprograms,即基础线性代数子程序库,里面拥有大量已经编写好的关于线性代数运算的程序。caffe官网提供了关于blas库的三种选择,ATLAS,MKL,OpenBLAS。详情参见博客 Ubuntu14.04 BLAS安装 。这里,为了简单,安装ATLAS。
sudo apt-get install libatlas-base-dev
安装Python
sudo apt-get install python-dev
安装14.04所额外需要的依赖
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
2.make编译
首先下载caffe-master并解压。编译教程参见官网。如下:
cd到caffe根目录,修改配置文件
cd Desktop/caffe-master/
cp Makefile.config.example Makefile.config
这里我们一直采用apt-get安装,并且默认使用GPU加速,所以不用怎么修改Makefile.config,只需要将WITH_PYTHON_LAYER := 1前的注释去掉,方便使用Python调用即可。使用命令行开始编译:
make all -j4
这一步时间较长,-j4表示采用多核方式,我的机器是X4的,所以用的4这个参数。完成后结果如下:
CXX/LD -o .build_release/tools/compute_image_mean.bin
CXX/LD -o .build_release/tools/convert_imageset.bin
CXX/LD -o .build_release/examples/cpp_classification/classification.bin
CXX/LD -o .build_release/examples/siamese/convert_mnist_siamese_data.bin
CXX/LD -o .build_release/examples/mnist/convert_mnist_data.bin
CXX/LD -o .build_release/examples/cifar10/convert_cifar_data.bin
接着输入命令:
make test -j4
结果如下:
LD .build_release/src/caffe/test/test_split_layer.o
LD .build_release/src/caffe/test/test_image_data_layer.o
LD .build_release/src/caffe/test/test_softmax_layer.o
LD .build_release/src/caffe/test/test_data_transformer.o
LD .build_release/cuda/src/caffe/test/test_im2col_kernel.o
CXX/LD -o .build_release/test/test_all.testbin src/caffe/test/test_caffe_main.cpp
继续等待,完成后输入如下命令:
make runtest -j4
这里就是一大波绿色的RUN OK了,
[ RUN ] SplitLayerTest/0.Test
[ OK ] SplitLayerTest/0.Test (0 ms)
[ RUN ] SplitLayerTest/0.TestSetup
[ OK ] SplitLayerTest/0.TestSetup (0 ms)
[ RUN ] SplitLayerTest/0.TestGradient
[ OK ] SplitLayerTest/0.TestGradient (4 ms)
[----------] 3 tests from SplitLayerTest/0 (4 ms total)
[----------] Global test environment tear-down
[==========] 2021 tests from 267 test cases ran. (506614 ms total)
[ PASSED ] 2021 tests.
至此make编译caffe成功了。
3.Cmake编译
同样先给出官网教程,记录如下:
在caffe根目录下运行:
mkdir cbuild
cd cbuild
cmake ..
之后输出一个配置文件,大概选项如下:
-- ******************* Caffe Configuration Summary *******************
-- General:
-- Version : 1.0.0-rc3
-- Git : unknown
-- System : Linux
-- C++ compiler : /usr/bin/c++
-- Release CXX flags : -O3 -DNDEBUG -fPIC -Wall -Wno-sign-compare -Wno-uninitialized
-- Debug CXX flags : -g -fPIC -Wall -Wno-sign-compare -Wno-uninitialized
-- Build type : Release
--
-- BUILD_SHARED_LIBS : ON
-- BUILD_python : ON
-- BUILD_matlab : OFF
-- BUILD_docs : ON
-- CPU_ONLY : OFF
-- USE_OPENCV : ON
-- USE_LEVELDB : ON
-- USE_LMDB : ON
-- ALLOW_LMDB_NOLOCK : OFF
--
-- Dependencies:
-- BLAS : Yes (Atlas)
-- Boost : Yes (ver. 1.54)
-- glog : Yes
-- gflags : Yes
-- protobuf : Yes (ver. 2.5.0)
-- lmdb : Yes (ver. 0.9.10)
-- LevelDB : Yes (ver. 1.15)
-- Snappy : Yes (ver. 1.1.0)
-- OpenCV : Yes (ver. 2.4.8)
-- CUDA : Yes (ver. 8.0)
--
-- NVIDIA CUDA:
-- Target GPU(s) : Auto
-- GPU arch(s) : sm_30
-- cuDNN : Not found
--
-- Python:
-- Interpreter : /usr/bin/python2.7 (ver. 2.7.6)
-- Libraries : /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.6)
-- NumPy : /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2)
--
-- Documentaion:
-- Doxygen : No
-- config_file :
--
-- Install:
-- Install path : /home/gph/Desktop/caffe-master/cbuild/install
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/gph/Desktop/caffe-master/cbuild
之后输入
make -j 4
这里-j 4因为我的机器是4核的。之后等到编译,时间较长。完成后输入:
make install
结果如下:
Linking CXX executable mnist/convert_mnist_data
[100%] Built target convert_mnist_data
Linking CXX executable siamese/convert_mnist_siamese_data
[100%] Built target convert_mnist_siamese_data
Linking CXX executable cpp_classification/classification
[100%] Built target classification
Linking CXX shared library ../lib/_caffe.so
Creating symlink /home/gph/Desktop/caffe-master/python/caffe/_caffe.so -> /home/gph/Desktop/caffe-master/cbuild/lib/_caffe.so
[100%] Built target pycaffe
至此,Cmake编译完成。需要说明的是make和cmake编译互不影响,之所以需要cmake编译是因为楼主用Cmakelist.txt管理工程,需要添加caffe的头文件和相应的库,使用cmake编译非常方便。具体参看我的另一篇博客Caffe + ROS + OpenCV + Qt creator 。
想要跑一跑例程的请参看我的其他博客:
Caffe学习:从头到尾跑一遍模型的训练和测试
caffe学习:通过研读classification.cpp了解如何使用caffe模型 。
遇到的问题记录
1)cmake ..之后出现错误
CMake Error at CMakeLists.txt:83 (add_dependencies): The dependency
target “pycaffe” of target “pytest” does not exist.目前还未解决,但是仍然可以继续,只是好像要输入两次命令才行。这也是我重装的原因之一,重装就没有这个错误。
2)库错误
在利用cmake编译caffe时,出现如下错误:
Linking CXX shared library ../../lib/libcaffe-d.so /usr/bin/ld:
/usr/local/lib/libcblas.a(cblas_sgemv.o): relocation R_X86_64_32
against `.rodata.str1.1’ can not be used when making a shared object;
recompile with -fPIC /usr/local/lib/libcblas.a: error adding symbols:
Bad value collect2: error: ld returned 1 exit status make[2]: *
[lib/libcaffe-d.so.1.0.0-rc3] Error 1 make[1]: *
[src/caffe/CMakeFiles/caffe.dir/all] Error 2 make: * [all] Error 2解决方法: 编辑cbuild文件夹下的CMakeCache.txt,将
//Path to a library. Atlas_CBLAS_LIBRARY:FILEPATH=<path to
libcblas.a>改为
//Path to a library.
Atlas_CBLAS_LIBRARY:FILEPATH=/usr/lib/libcblas.so //<path to
libcblas.so in your machine>这就应该是机器上利用不同方式多次装过这个库,文件较为混乱,找不到正确的库造成的。所以大家安装的时候能使用apt-get
install的尽量这样安装,除非你有明确的目的要源码安装,否则确实要做很多工作,很“锻炼”人。