mac 安裝caffe

http://caffe.berkeleyvision.org/install_osx.html
http://www.itdecent.cn/p/cc16e2977e27
http://www.itdecent.cn/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
默認(rèn)使用的是caffe_root下的Cmakelist.txt 構(gòu)造相應(yīng)的makefile文件,進(jìn)行編譯。

準(zhǔn)備:

1、安裝Homebrew
參見我的另一片文章,mac安裝brew
2、安裝cmake(開始嘗試cmake方式,但是沒成功,改用make方式,所以cmake可裝可不裝)參見我的另一篇文章,安裝cmake
3、python環(huán)境,這里使用anaconda的python環(huán)境
使用anacond-python-2.7.13,設(shè)置成默認(rèn)的python環(huán)境。
安裝boost和caffe都需要。
4、安裝caffe依賴的環(huán)境
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 
# 該命令執(zhí)行不成功,更換成了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安裝如下,會(huì)直接安裝最新版本,這里是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默認(rèn)是有blas的,但是編譯中存在問題

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

這個(gè)錯(cuò)誤與使用的blas有關(guān),默認(rèn)是使用系統(tǒng)的atlas,然后可能平臺(tái)的問題。這里改成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 默認(rèn)c++的編譯器是clang,與平時(shí)使用的g++和gcc還是有區(qū)別的。
編譯caffe,需要c++11特性,
錯(cuò)誤如下:

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 文件中一下內(nèi)容

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時(shí)同樣會(huì)出現(xiàn)這個(gè)錯(cuò)誤。
修改

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

2、caffe會(huì)使用opencv,編譯中可以暫時(shí)不使用opencv。
不要通過brew安裝opencv的包。默認(rèn)會(huì)安裝最新版本的opencv,但是caffe中使用的舊版本的opencv,使用的接口在新版本中發(fā)生了變化。
如果必須使用opencv,需要安裝舊版本的opencv
opencv=

3、ld: library not found for -lboost_thread-mt
找不到這個(gè)boost_thread-mt的動(dòng)態(tài)連接庫
修改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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容