macOS High Sierra安装Caffe框架




英特尔杯人工智能挑战赛需要用到Caffe深度学习框架。Caffe框架在macOS上需要手动编译,然而官方的安装教程年久失修,并且编译过程中会遇到由不同版本或环境而引发的问题,我历时三天,踩了无数的坑之后,终于成功安装了Caffe框架。在此记录下安装的过程和遇到的错误,希望对你有所帮助。

系统环境及安装配置

  • Macbook Air 13′ macOS High Sierra
  • Homebrew
  • Xcode
  • Miniconda Python2(Caffe建议使用Anaconda Python,另外我在尝试使用Python3编译Caffe时遇到了未知的错误,因此建议使用Python2.7)
  • openBLAS(Intel的MKL库会提供更高性能且更稳定的计算,在校学生可以通过这里申请:Intel® Math Kernel Library (Intel® MKL) | Intel® Software
  • CPU ONLY模式(MacBook Air没有NVIDIA GPU,因此使用CPU ONLY模式,不需要安装CUDA及cuDNN)

安装依赖

首先安装Miniconda安装包,并确保Miniconda的路径已经被加入PATH,编辑~/.zshrc(或bash_profile),添加

export PATH="/Users/frank/miniconda2/bin:$PATH"

使用Brew安装依赖

Brew tap homebrew/science
brew install --fresh -vd snappy leveldb gflags glog szip lmdb opencv hdf5 openblas
brew install --build-from-source --with-python --fresh -vd protobuf
brew install --build-from-source --fresh -vd boost boost-python

Miniconda只附带了很少的包,使用pip安装其余的依赖。如果你使用Anaconda,则可以跳过这一步。

pip install -r python/requirement.txt

编译caffe

git clone https://github.com/BVLC/caffe.git
cd caffe

编辑编译配置文件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模式
CPU_ONLY := 1

# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0

# 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++

# 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.
# 由于我们使用CPU模式,所以涉及到CUDA的地方不用管
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
# 我们安装的是openBLAS,将下面字段改为open
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# 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的路径。在macOS中往往会有多个Python环境,确保你所填写的路径都属于同一个Python环境,避免混淆。
 ANACONDA_HOME := /Users/frank/miniconda2/
 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_python3 python3.6m
# 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

# 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 
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
# 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 ?= @

开始编译

make clean
make all -j8(数字表示线程数量,可以根据你的硬件配置增加或减少)
make test -j8
make runtest
make pycaffe
make pytest

设置环境变量

编译完成的文件位于/path/to/caffe/python目录下,需要将这个路径加入PYTHONPATH
编辑~/.zshrc(或bash_profile),添加

export PYTHONPATH=/Users/username/git/caffe/python:$PYTHONPATH

Troubleshooting

TypeError: new() got an unexpected keyword argument ‘file’

TypeError: __new__() got an unexpected keyword argument ‘file'

可能的原因是brew安装的protobuf版本高于3.5.0(我这里是3.5.1),而pip或conda安装的protobuf版本低于3.5.0。然而错误信息中的file字段是在3.5.0引入的,因此引发错误。

解决方案为使用pip或conda安装新版本的protobuf
首先使用pip show protobuf查看当前安装的版本,如果低于3.5.0,则pip install protobuf==3.5.1

[.build_release/lib/libcaffe.so.1.0.0] Error 1

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

解决方案为在makefile文件中找到LIBRARIES在后面添加opencv_imgcodecs

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5 opencv_imgcodecs

References

https://github.com/BVLC/caffe/issues/6143
http://blog.csdn.net/wxy_2017/article/details/78609843
http://xxuan.me/2016-11-12-install-caffe-under-macos.html
https://github.com/BVLC/caffe/issues/5357
https://github.com/BVLC/caffe/issues/6054




Posted

in

by

Comments

发表回复/Leave a Reply

您的电子邮箱地址不会被公开。/Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.