#: 安装流程参考自《深度学习 21天实战Caffe》——赵永科 著
(1)安装homebrew包管理工具,作用相当于yum或apt-get:
$ ruby -e "(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
(2)等待安装成功,然后利用该工具安装caffe依赖包:
$ brew install -vd snappy leveldb gflags glog szip lmdb
$ brew tap homebrew/science
$ brew install hdf5 opencv
$ brew install protobuf boost wget
如果python是3.x版本,需要安装boost-python3:
$ brew install boost-python3
(3)下载Caffe源码:
$ git clone https://github.com/bvlc/caffe.git
$ cd caffe/
$ mv Makefile.config.example Makefile.config
(4)修改Makefile.config,打开CPU_ONLY选项,修改BLAS选项,保存。
#: 仅CPU模式开关,打开该选项(去掉“#”)表示Caffe编译时仅支持CPU,不支持GPU
CPU_ONLY := 1
BLAS := atlas
如果python版本是3.x,需要修改PYTHON_LIBRARIES,因为我的电脑是python3.6,所以这里是boost_python36 python3.6
:
PYTHON_LIBRARIES := boost_python36 python3.6
(5)执行make进行编译:
#: -j 选项表示使用多线程编译,利用所有可用的CPU,加快编译速度
#: 也可指定数字,如-j8表示开启8个线程编译
$ make -j
等待编译成功。
遇到的问题:
1、
Error: Your Xcode (8.3.3) is too outdated.
Please update to Xcode 9.1 (or delete it).
Xcode can be updated from the App Store.
Error: Kernel.exit
/usr/local/Homebrew/Library/Homebrew/cmd/install.rb:305:in `exit'
/usr/local/Homebrew/Library/Homebrew/cmd/install.rb:305:in `check_development_tools'
/usr/local/Homebrew/Library/Homebrew/cmd/install.rb:320:in `perform_preinstall_checks'
/usr/local/Homebrew/Library/Homebrew/cmd/install.rb:223:in `install'
/usr/local/Homebrew/Library/Homebrew/brew.rb:100:in `<main>'
XCode版本过低,需要升级。只需要从App Store中升级XCode到最新版本就好。
2、
Undefined symbols for architecture x86_64:
"cv::imread(cv::String const&, int)", referenced from:
caffe::WindowDataLayer<float>::load_batch(caffe::Batch<float>*) in window_data_layer.o
caffe::WindowDataLayer<double>::load_batch(caffe::Batch<double>*) in window_data_layer.o
caffe::ReadImageToCVMat(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int, int, bool) in io.o
"cv::imdecode(cv::_InputArray const&, int)", referenced from:
caffe::DecodeDatumToCVMatNative(caffe::Datum const&) in io.o
caffe::DecodeDatumToCVMat(caffe::Datum const&, bool) in io.o
"cv::imencode(cv::String const&, cv::_InputArray const&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, std::__1::vector<int, std::__1::allocator<int> > const&)", referenced from:
caffe::ReadImageToDatum(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int, int, int, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, caffe::Datum*) in io.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [.build_release/lib/libcaffe.so.1.0.0] Error 1
编译时没有添加相关依赖库。需要添加opencv_highgui opencv_imgproc opencv_core opencv_imgcodecs opencv_videoio
这几个依赖库到Makefile
的LIBRARIES
字段后面,make clean
后重新编译即可。
3、
make成功之后,调用caffe:import caffe
报错:segmentation fault :11
是因为make时候使用的Python的lib和include地址不正确。
注意Makefile.config中此处代码:
# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python2.7/dist-packages/numpy/core/include
这个配置需要修改为你自己的可以找到Python.h
和numpy/arrayobject.h
两个文件的路径。
然后注意Makefile.config中此处代码:
# We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /usr/lib
这个配置需要修改为你自己的可以找到libpythonX.X.so
或者libpythonX.X.dylib
的路径,此处注意,这个路径要和上面PYTHON_INCLUDE
的路径保持版本统一,要么都是自己装的目录,要么都是系统自带的。
比如:我的系统自带的libpython.dylib
在系统自带路径/usr/lib
和后来自己通过brew安装的路径/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/
下都有,但是上面PYTHON_INCLUDE
我配置的是
PYTHON_INCLUDE := /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7 \
/usr/local/Cellar/numpy/1.13.3/lib/python2.7/site-packages/numpy/core/include
所以PYTHON_LIB
我要配置为/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/
而不是/usr/lib
。
修改完之后,make clean
,然后重新make -j
。编译完成,再import caffe
发现就不报segmentation fault :11
错了。