使用anaconda3环境下的python2.7, 机器macos mojave 10.14
1.安装Xcode
首先现在app store中安装Xcode:
不然会有” framework not found vecLib “的问题出现
2.相应包安装
1.首先要安装homebrew包管理工具,在终端运行下面命令:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2.然后利用该管理工具安装caffe依赖包
brew install -vd snappy leveldb gflags glog szip lmdb brew install hdf5 opencv
brew install openblas
3.安装boost
brew install boost@1.59 boost-python@1.59 brew link boost@1.59 --force brew link boost-python@1.59 --force
4. 安装2.6.1版本的protobuf
一开始的是安装3.5.1版本的protobuf,但是后面出现问题,所以还是安装2.6.1版本
下面只是记录一下:
cd ~/Downloads wget https://github.com/protocolbuffers/protobuf/archive/v3.5.1.zip unzip v3.5.1.zip cd protobuf-3.5.1 ./autogen.sh ./configure make make install
运行./autogen.sh过程出错:
+ autoreconf -f -i -Wall,no-obsolete ./autogen.sh: line 50: autoreconf: command not found
解决方案是安装protobuf的依赖项:
(deeplearning2) userdeMacBook-Pro:protobuf-3.5.1 user$ brew install autoconf automake libtool
再重新运行./autogen.sh及之后命令
成功后查看版本:
(deeplearning2) userdeMacBook-Pro:protobuf-3.5.1 user$ protoc --version libprotoc 3.5.1
这里在下面caffe文件夹中make all之前忘记make clean了,可能是这个原因没成功也不一定,但是2.6.0版本的的确成功了,直接用该版本即可
后面发现还是不行,就去下了2.6.1版本的:
/Library/Developer/CommandLineTools/usr/bin/make check-TESTS PASS: protobuf-test PASS: protobuf-lazy-descriptor-test PASS: protobuf-lite-test PASS: google/protobuf/compiler/zip_output_unittest.sh PASS: google/protobuf/io/gzip_stream_unittest.sh ============================================================================ Testsuite summary for Protocol Buffers 2.6.1 ============================================================================ # TOTAL: 5 # PASS: 5 # SKIP: 0 # XFAIL: 0 # FAIL: 0 # XPASS: 0 # ERROR: 0 ============================================================================
版本查看:
(deeplearning2) userdeMacBook-Pro:~ user$ protoc --version libprotoc 2.6.1
3.下载caffe源码
(deeplearning2) userdeMacBook-Pro:~ user$ git clone https://github.com/bvlc/caffe.git Cloning into 'caffe'... remote: Enumerating objects: 65269, done.
下载好后:
(deeplearning2) userdeMacBook-Pro:~ user$ cd caffe (deeplearning2) userdeMacBook-Pro:caffe user$ cp Makefile.config.example Makefile.config
4.更改Makefile.config文件
打开CPU_ONLY选项,保存
# CPU-only switch (uncomment to build without GPU support). CPU_ONLY := 1
即去掉前面的#号,表示caffe编译时仅支持CPU,不支持GPU
取消CPU_ONLY前面的注释
取消USE_OPENCV前面的注释
取消USE_HDF5前面的注释,并且改成1
更改CUSTOM_CXX
注释掉CUDA_DIR那一行
将BLAS改成open
设置blas的路径,取消下面两行的注释
# uncomment to disable IO dependencies and corresponding data layers USE_OPENCV := 0 # This code is taken from https://github.com/sh1r0/caffe-android-lib USE_HDF5 := 1 # Uncomment if you're using OpenCV 3 OPENCV_VERSION := 3
# CUDA directory contains bin/ and lib/ directories that we need.
#CUDA_DIR := /usr/local/cuda
BLAS := open # Homebrew puts openblas in a directory that is not on the standard search path BLAS_INCLUDE := $(shell brew --prefix openblas)/include BLAS_LIB := $(shell brew --prefix openblas)/lib
# Homebrew puts openblas in a directory that is not on the standard search path
BLAS_INCLUDE := $(shell brew --prefix openblas)/include
BLAS_LIB := $(shell brew --prefix openblas)/lib
下面就是设置路径,使用的是anaconda3,这个根据你自己的路径设置
注释掉:
# 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
更改为:
# Anaconda Python distribution is quite popular. Include path: # Verify anaconda location, sometimes it's in root. ANACONDA_HOME := /anaconda3 PYTHON_INCLUDE := $(ANACONDA_HOME)/envs/deeplearning2/include \ $(ANACONDA_HOME)/envs/deeplearning2/include/python2.7 \ $(ANACONDA_HOME)/envs/deeplearning2/lib/python2.7/site-packages/numpy/core/include
下面这个也更改为:
# We need to be able to find libpythonX.X.so or .dylib. # PYTHON_LIB := /usr/lib PYTHON_LIB := $(ANACONDA_HOME)/envs/deeplearning2/lib
然后添加一些include和lib路径:
# Whatever else you find you need goes here. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/opt/boost@1.59/include /usr/local/opt/hdf5/include LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opt/boost-python@1.59/lib /usr/local/opt/hdf5/lib
最终版本为:
## Refer to http://caffe.berkeleyvision.org/installation.html # Contributions simplifying and improving our build system are welcome! # cuDNN acceleration switch (uncomment to build with cuDNN). # USE_CUDNN := 1 # CPU-only switch (uncomment to build without GPU support). CPU_ONLY := 1 # uncomment to disable IO dependencies and corresponding data layers USE_OPENCV := 0 # USE_LEVELDB := 0 # USE_LMDB := 0 # This code is taken from https://github.com/sh1r0/caffe-android-lib USE_HDF5 := 1 # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary) # You should not set this flag if you will be reading LMDBs with any # possibility of simultaneous read and write # ALLOW_LMDB_NOLOCK := 1 # Uncomment if you're using OpenCV 3 # OPENCV_VERSION := 3 # To customize your choice of compiler, uncomment and set the following. # N.B. the default for Linux is g++ and the default for OSX is clang++ # CUSTOM_CXX := g++ CUSTOM_CXX := clang++ -std=c++11 # CUDA directory contains bin/ and lib/ directories that we need. CUDA_DIR := /usr/local/cuda # On Ubuntu 14.04, if cuda tools are installed via # "sudo apt-get install nvidia-cuda-toolkit" then use this instead: # CUDA_DIR := /usr # CUDA architecture setting: going with all of them. # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility. # For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility. # For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility. CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \ -gencode arch=compute_20,code=sm_21 \ -gencode arch=compute_30,code=sm_30 \ -gencode arch=compute_35,code=sm_35 \ -gencode arch=compute_50,code=sm_50 \ -gencode arch=compute_52,code=sm_52 \ -gencode arch=compute_60,code=sm_60 \ -gencode arch=compute_61,code=sm_61 \ -gencode arch=compute_61,code=compute_61 # BLAS choice: # atlas for ATLAS (default) # mkl for MKL # open for OpenBlas BLAS := open # Custom (MKL/ATLAS/OpenBLAS) include and lib directories. # Leave commented to accept the defaults for your choice of BLAS # (which should work)! # BLAS_INCLUDE := /path/to/your/blas # BLAS_LIB := /path/to/your/blas # Homebrew puts openblas in a directory that is not on the standard search path BLAS_INCLUDE := $(shell brew --prefix openblas)/include BLAS_LIB := $(shell brew --prefix openblas)/lib # This is required only if you will compile the matlab interface. # MATLAB directory should contain the mex binary in /bin. # MATLAB_DIR := /usr/local # MATLAB_DIR := /Applications/MATLAB_R2012b.app # 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 # Anaconda Python distribution is quite popular. Include path: # Verify anaconda location, sometimes it's in root. ANACONDA_HOME := /anaconda3 PYTHON_INCLUDE := $(ANACONDA_HOME)/envs/deeplearning2/include \ $(ANACONDA_HOME)/envs/deeplearning2/include/python2.7 \ $(ANACONDA_HOME)/envs/deeplearning2/lib/python2.7/site-packages/numpy/core/include # Uncomment to use Python 3 (default is Python 2) # PYTHON_LIBRARIES := boost_python3 python3.5m # PYTHON_INCLUDE := /usr/include/python3.5m \ # /usr/lib/python3.5/dist-packages/numpy/core/include # We need to be able to find libpythonX.X.so or .dylib. # PYTHON_LIB := /usr/lib PYTHON_LIB := $(ANACONDA_HOME)/envs/deeplearning2/lib # Homebrew installs numpy in a non standard path (keg only) # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include # PYTHON_LIB += $(shell brew --prefix numpy)/lib # Uncomment to support layers written in Python (will link against Python libs) # WITH_PYTHON_LAYER := 1 # Whatever else you find you need goes here. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/opt/boost@1.59/include /usr/local/opt/hdf5/include LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opt/boost-python@1.59/lib /usr/local/opt/hdf5/lib # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies # INCLUDE_DIRS += $(shell brew --prefix)/include # LIBRARY_DIRS += $(shell brew --prefix)/lib # NCCL acceleration switch (uncomment to build with NCCL) # https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0) # USE_NCCL := 1 # Uncomment to use `pkg-config` to specify OpenCV library paths. # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.) # USE_PKG_CONFIG := 1 # N.B. both build and distribute dirs are cleared on `make clean` BUILD_DIR := build DISTRIBUTE_DIR := distribute # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 # DEBUG := 1 # The ID of the GPU that 'make runtest' will use to run unit tests. TEST_GPUID := 0 # enable pretty build (comment to see full commands) Q ?= @View Code
如果你在运行make test命令出下面类似的错误:
In file included from /usr/local/include/leveldb/iterator.h:20: /usr/local/include/leveldb/status.h:27:11: error: expected ';' at end of declaration list Status() noexcept : state_(nullptr) {} ^ ; /usr/local/include/leveldb/status.h:33:16: warning: rvalue references are a C++11 extension [-Wc++11-extensions] Status(Status&& rhs) noexcept : state_(rhs.state_) { rhs.state_ = nullptr; } ^ /usr/local/include/leveldb/status.h:33:23: error: expected ';' at end of declaration list Status(Status&& rhs) noexcept : state_(rhs.state_) { rhs.state_ = nullptr; } ^ ; /usr/local/include/leveldb/status.h:103:16: error: 'Status' is missing exception specification 'throw()' inline Status::Status(const Status& rhs) { ^ throw() /usr/local/include/leveldb/status.h:24:22: note: previous declaration is here class LEVELDB_EXPORT Status { ^ /usr/local/include/leveldb/status.h:115:40: warning: rvalue references are a C++11 extension [-Wc++11-extensions] inline Status& Status::operator=(Status&& rhs) noexcept { ^ /usr/local/include/leveldb/status.h:115:48: error: expected function body after function declarator inline Status& Status::operator=(Status&& rhs) noexcept { ^ In file included from src/caffe/util/db.cpp:2:
之前出这样的内容以为是leveldb版本的问题,但是后面发现设置该为:
# To customize your choice of compiler, uncomment and set the following. # N.B. the default for Linux is g++ and the default for OSX is clang++ # CUSTOM_CXX := g++ CUSTOM_CXX := clang++ -std=c++11
就没有这样的问题了,跟着上面的配置走应该就不会有问题
5.make编译:
make all make test make runtest make pycaffe
下面的问题基本上都使用更改配置的方法解决了,所以如果你跟着我上面的配置走,下面的一些问题你应该不会遇见
下面只是记录一下
⚠️运行make命令时,出了任何错,更改过后都要记得make clean一次,然后再从头make all进行编译
make all问题:
bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, ^ src/caffe/net.cpp:774:7: error: use of undeclared identifier 'H5Fis_hdf5' if (H5Fis_hdf5(trained_filename.c_str())) { ^ 11 warnings and 1 error generated. make: *** [.build_release/src/caffe/net.o] Error 1
解决,在配置中添加boost路径:
# Whatever else you find you need goes here. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/opt/boost@1.59/include /usr/local/opt/hdf5/include LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opt/boost-python@1.59/lib /usr/local/opt/hdf5/lib
make runtest问题:
(deeplearning2) userdeMacBook-Pro:caffe user$ make runtest .build_release/tools/caffe dyld: Library not loaded: /usr/local/opt/gcc/lib/gcc/8/libgfortran.5.dylib Referenced from: /usr/local/opt/openblas/lib/libopenblasp-r0.3.5.dylib Reason: image not found make: *** [runtest] Abort trap: 6
原因:
因为我本地安装的gcc是9版本,只有/usr/local/opt/gcc/lib/gcc/9,没有8的路径
解决办法就是安装了个8的版本:
(deeplearning2) userdeMacBook-Pro:caffe user$ brew install gcc@8 ==> Downloading https://homebrew.bintray.com/bottles/gcc@8-8.3.0.mojave.bottle.tar.gz ==> Downloading from https://akamai.bintray.com/b1/b1e150c72b4c3b7f3493371d71cdb668f691bfee2e998e5b0bf570eed28254d6?__gda__=exp=1565346562~hmac=81794cd252808740593fa500900f13dbd ######################################################################## 100.0% ==> Pouring gcc@8-8.3.0.mojave.bottle.tar.gz