mac 安装caffe

http://caffe.berkeleyvision.org/install_osx.html
https://www.jianshu.com/p/cc16e2977e27
https://www.jianshu.com/p/cc16e2977e27

由于iMac显卡是A卡,所以用不了CUDA加速,这里就装CPU-ONLY版的caffe

caffe编译安装可以采用make或者cmake方式
make方式就是在caffe_root下,修改makefile.config文件以后,直接make all -j8
cmake方式就是mkdir build && cd build && cmake .. && make all -j8
默认使用的是caffe_root下的Cmakelist.txt 构造相应的makefile文件,进行编译。

准备:

1、安装Homebrew
参见我的另一片文章,mac安装brew
2、安装cmake(开始尝试cmake方式,但是没成功,改用make方式,所以cmake可装可不装)参见我的另一篇文章,安装cmake
3、python环境,这里使用anaconda的python环境
使用anacond-python-2.7.13,设置成默认的python环境。
安装boost和caffe都需要。
4、安装caffe依赖的环境
brew版本是2.1.11。
这是官方提供的:

brew install -vd snappy leveldb gflags glog szip lmdb
# need the homebrew science source for OpenCV and hdf5
brew tap homebrew/science 
# 该命令执行不成功,更换成了brew tap brewsci/science ,参考brew tap homebrew/science error
brew install hdf5 opencv

rf: brew tap homebrew/science error

这里我之前编译安装opencv,这里没有brew install opencv。

特别注意的是protobug和boost问题。
#protobuf安装如下,会直接安装最新版本,这里是3.9.1。直接使用如下安装方式就可以。
brew uninstall --force protobuf; brew install -vd protobuf

#boost安装可以采用这种方式,需要指定安装python接口,但是新版的brew好像不支持 --with-python,也不支持--fresh。
brew uninstall boost boost-python; brew install -vd boost boost-python

如果需要安装caffe的python接口,protobuf和boost使用如下的方式安装
-build-from-source --with-python,从源码安装,指定增加python接口
brew install --build-from-source --with-python -vd protobuf
brew install --build-from-source -vd boost boost-python。教程是这么说的,但是我并没有成功。不推荐

总之就是,protobuf安装采用如上方式,boost不采用如上方式,从源码编译安装
参加我的另一篇文章
mac boost 安装

4、blas=atlas
mac默认是有blas的,但是编译中存在问题

ld: cannot link directly with /System/Library/Frameworks//vecLib.framework/vecLib.tbd for architecture x86_64

这个错误与使用的blas有关,默认是使用系统的atlas,然后可能平台的问题。这里改成openblas。参考如下
https://lockxmonk.github.io/14980252836745.html

安装caffe:

git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config
make all -j8
make pycaffe

ERROR:

1、mac 默认c++的编译器是clang,与平时使用的g++和gcc还是有区别的。
编译caffe,需要c++11特性,
错误如下:

n file included from python/caffe/_caffe.cpp:17:
In file included from ./include/caffe/caffe.hpp:7:
In file included from ./include/caffe/blob.hpp:9:
In file included from .build_release/src/caffe/proto/caffe.pb.h:26:
In file included from /usr/local/include/google/protobuf/generated_message_table_driven.h:34:
In file included from /usr/local/include/google/protobuf/map.h:49:
In file included from /usr/local/include/google/protobuf/map_type_handler.h:34:
In file included from /usr/local/include/google/protobuf/parse_context.h:41:
/usr/local/include/google/protobuf/implicit_weak_message.h:66:12: error: incomplete type 'google::protobuf::Arena' named in nested
      name specifier
    return Arena::CreateMessage(arena);
           ^~~~~~~
/usr/local/include/google/protobuf/arena.h:75:7: note: forward declaration of 'google::protobuf::Arena'
class Arena;    // defined below
      ^
In file included from python/caffe/_caffe.cpp:17:
In file included from ./include/caffe/caffe.hpp:7:
In file included from ./include/caffe/blob.hpp:9:
In file included from .build_release/src/caffe/proto/caffe.pb.h:26:
In file included from /usr/local/include/google/protobuf/generated_message_table_driven.h:34:
In file included from /usr/local/include/google/protobuf/map.h:49:
In file included from /usr/local/include/google/protobuf/map_type_handler.h:34:
In file included from /usr/local/include/google/protobuf/parse_context.h:41:
/usr/local/include/google/protobuf/implicit_weak_message.h:66:33: error: 'ImplicitWeakMessage' does not refer to a value
    return Arena::CreateMessage(arena);

如果使用的是cmake则修改:
因此修改CMakeList.txt 文件中一下内容

if(UNIX OR APPLE)
-  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall")
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -std=c++11")
 endif()

如果使用的make:

- CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
+ CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11

(https://stackoverflow.com/questions/52728417/osx-caffe-compilation-fails-with-expected-expression-error)

参考自:
https://stackoverflow.com/questions/52728417/osx-caffe-compilation-fails-with-expected-expression-error

同样,编译pycaffe时同样会出现这个错误。
修改

 - LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) 
 + LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11

2、caffe会使用opencv,编译中可以暂时不使用opencv。
不要通过brew安装opencv的包。默认会安装最新版本的opencv,但是caffe中使用的旧版本的opencv,使用的接口在新版本中发生了变化。
如果必须使用opencv,需要安装旧版本的opencv
opencv=

3、ld: library not found for -lboost_thread-mt
找不到这个boost_thread-mt的动态连接库
修改MakeFile:
将boost_thread-mt 改成boost_thread

4、Could NOT find Boost (missing: python)

Could NOT find Boost (missing: python) (found suitable version "1.70.0", minimum required is "1.46")
-- Python interface is disabled or not all required dependencies found. Building without it...minimum required is "1.46")
CMake Error at CMakeLists.txt:107 (add_dependencies):
  The dependency target "pycaffe" of target "pytest" does not exist.

参考:
https://github.com/BVLC/caffe/issues/6445
解决:
ln -s libboost_python27.dylib libboost_python.dylib

append:

## 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 := 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.
#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 := /Users/gpf/soft/anaconda2
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.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)/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/opencv/include /usr/include 
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opencv/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 ?= @

.zshrc

 1 export MYVIMRC=/Users/gpf/.vimrc
  2 export VIMINIT="let &rtp='/Users/gpf/.vim,' . &rtp
  3 so $MYVIMRC"
  4
  5 export PYTHONPATH=/Users/gpf/soft/anaconda2/lib:$PYTHONPATH
  6
  7 # If you come from bash you might have to change your $PATH.
  8 # export PATH=$HOME/bin:/usr/local/bin:$PATH
  9 export OpenCV_DIR=/usr/local/opencv/shared
 10 export DYLD_LIBRARY_PATH=/usr/local/opencv/shared/lib
 11 export PYTHONPATH=/usr/local/opencv/lib/python2.7/site-packages:$PYTHONPATH
 12
 13 # Path to your oh-my-zsh installation.
 14 export ZSH="/Users/gpf/.oh-my-zsh"
 15
 16 export PATH=/Users/gpf/soft/anaconda2/bin:$PATH
 17 export DYLD_FALLBACK_LIBRARY_PATH=/Users/gpf/soft/anaconda2/lib:/usr/local/lib:/usr/lib
 18
 19
 20 export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/Users/gpf/soft/anaconda2/include/python2.7/
 21 export C_INCLUDE_PATH=$C_INCLUDE_PATH:/Users/gpf/soft/anaconda2/include/python2.7/
 22
 23 export PYTHONPATH=/Users/gpf/soft/caffe/python:$PYTHONPATH

你可能感兴趣的:(mac 安装caffe)