Mac系统中caffe环境配置
环境
- 系统mac os
- caffe
- python2.7,numpy(anaconda3环境)
- opencv3
- 运行环境cpu
安装依赖文件
命令:
1.brew install -vd snappy leveldb gflags glog szip lmdb
2.brew install data-science-studio
3.brew install opencv hdf5
4.brew install protobuf boost boost-python(先看注意事项)
注意:
- brew install data-science-studio命令,之前是用brew tap homebrew/science,会报错
- opencv安装成功后,配置环境变量,在.bash_profile文件中加入以下代码,可以通过命令
pkg-config --modversion opencv
查看opencv版本。
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/Cellar/opencv/2.4.12/lib/pkgconfig
export PKG_CONFIG_PATH
export LD_LIBRARY_PATH=/usr/local/Cellar/opencv/2.4.12/bin:SLD_LIBRARY_PATH
export PATH=${PATH}:/usr/local/Cellar/opencv/2.4.12/lib
- protobuf 安装成功后,在编译时可能会报错
make: *** [.build_release/src/caffe/proto/caffe.pb.o] Error 1
可能是版本太高,因此需要对源码进行编译
(1)下载源码 git clone https://github.com/protocolbuffers/protobuf/releases/tag/v2.6.1
(2)安装依赖包brew install automake libtool
(3)安装
./autogen.sh
./configure
make
make install
(4) 执行 protoc --version
如果输出2.6.1,则说明安装成功
- 对于boost-python,如果是python2则安装boost-python,如果是python3则安装boost-python3,一般安装的是最新版本的,如需安装指定版本的,需自己对boost编译。
安装
- 下载caffe源码
git clone https://github.com/BVLC/caffe.git - 修改Makefile.config文件,将文件Makefile.config.example进行复制(备份)并改名为Makefile.config
1.使用cpu:# CPU_ONLY := 0 修改成: CPU_ONLY := 1
2.应用 opencv 版本 # OPENCV_VERSION := 3 修改为: OPENCV_VERSION := 3
3.使用 python 接口# WITH_PYTHON_LAYER := 1 修改为 WITH_PYTHON_LAYER:=1
4 #USE_OPENCV := 0 修改成 USE_OPENCV :=1
5 #USE_LEVELDB := 0 修改为 USE_LEVELDB:=1
6 #USE_LMDB := 0 修改位 USE_LMDB :=1
7 #USE_HDF5 := 0 修改为 USE_HDF5 := 1
8.将 #CUSTOM_CXX := g++ 改为 CUSTOM_CXX := clang++ -std=c++11
9.将 BLAS := atlas 改为 BLAS := open
10.去掉
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib 的注释
11.加入python2的路径
PYTHON_INCLUDE := /Users/duzhongqiang/opt/anaconda3/envs/py27/include/python2.7 \
/Users/duzhongqiang/opt/anaconda3/envs/py27/lib/python2.7/site-packages/numpy/core/include
PYTHON_LIB := /Users/duzhongqiang/opt/anaconda3/envs/py27/lib
贴出我的Makefile.config文件,供大家参考
## 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 := 1
USE_LEVELDB := 1
USE_LMDB := 1
# 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 := /Users/duzhongqiang/opt/anaconda3/envs/py27/include/python2.7 \
/Users/duzhongqiang/opt/anaconda3/envs/py27/lib/python2.7/site-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
# ANACONDA_HOME := $(HOME)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
# $(ANACONDA_HOME)/include/python2.7 \
# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python39 python3.9
# PYTHON_INCLUDE := /Users/duzhongqiang/opt/anaconda3/envs/py39caffe/include/python3.9 \
/Users/duzhongqiang/opt/anaconda3/envs/py39caffe/lib/python3.9/site-packages/numpy/core/include
# We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /Users/duzhongqiang/opt/anaconda3/envs/py27/lib
# PYTHON_LIB := $(ANACONDA_HOME)/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/opencv@3/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opt/opencv@3/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 ?= @
- 修改Makefile文件
PYTHON_LIBRARIES ?= boost_python python2.7
改为
PYTHON_LIBRARIES ?= boost_python27 python2.7
其中,boost_python27和python2.7取决于安装的文件版本及命名。
4. 设置环境变量
修改~/.bash_profile,加上:
export PYTHONPATH=/自己的caffe代码路径/caffe/python:$PYTHONPATH
source ~/.bash_profile
注:环境变量设置,参考 mac系统环境变量的添加
5. 编译
make all
make test -j8
make runtest
make pycaffe
make pytest
补充:在make前,先进入对应的python环境,或者退出anaconda环境。
6. 进入python环境,执行import caffe没有报错,则说明安装成功。
结论
至此,mac系统中的caffe环境安装成功,以上是根据我的电脑配置情况安装的,如果遇到其他问题,欢迎留言,大家一起解决。对于python3版本,我暂时没有安装成功,后续我将继续尝试。