配置环境homebrew + Anaconda Python 3.7 version
安装步骤
安装依赖项
brew install -vd snappy leveldb gflags glog szip lmdb
brew install hdf5 opencv wget
brew install openblas
安装boost-python@1.59 --without-python@2 --with-python 可能会出错,boost-python3是boost1.68版本,boost@1.69暂时不支持python3
brew install boost-python3
安装protobuf3.5.1
cd ~/Downloads
wget https://github.com/protocolbuffers/protobuf/archive/v3.5.1.zip
unzip protobuf-3.5.1.zip
cd protobuf-3.5.1
./autogen.sh
./configure
make
make install
配置caffe
cd ~
git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config
修改Makefile.config文件:
# 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_HDF5 := 1
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
BLAS := open
BLAS_INCLUDE := $(shell brew --prefix openblas)/include
BLAS_LIB := $(shell brew --prefix openblas)/lib
ANACONDA_HOME := $(HOME)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.7m \
$(ANACONDA_HOME)/lib/python3.7/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python37 python3.7m
# 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)/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
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# 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 ?= @
修改makefile文件:
PYTHON_LIBRARIES ?= boost_python37 python3.7m
在bash_profile和bashrc中添加python路径:
vi ~/.bash_profile
export PATH="~/anaconda3/bin:$PATH"
source ~/.bash_profile
vi ~/.bashrc
export PATH="~/anaconda3/bin:$PATH"
source ~/.bashrc
编译
cd ~/caffe
make all
make test
make runtest1
make pycaffe
make distribute
在~/caffe/python/caffe目录下生_caffe.so就好了
错误解决
make runtest 如果出现错误dyld: Library not loaded: @rpath/libhdf5_hl.100.dylib,将添加libhdf5_hl.100.dylib所在路径添加到rpath:
install_name_tool -add_rpath '/Users/lxy/anaconda3/lib' /Users/lxy/caffe/build/tools/caffe
继续make runtest,报错dyld: Library not loaded:@rpath/libhdf5_hl.100.dylib,再次添加rpath:
install_name_tool -add_rpath '/Users/lxy/anaconda3/lib' /Users/lxy/caffe/build/test/test_all.testbin
↩︎