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<ImplicitWeakMessage>(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<ImplicitWeakMessage>(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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,014评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,796评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,484评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,830评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,946评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,114评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,182评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,927评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,369评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,678评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,832评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,533评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,166评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,885评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,128评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,659评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,738评论 2 351