使用anaconda3环境下的python2.7, 机器macos mojave 10.14

1.安装Xcode

首先现在app store中安装Xcode:

不然会有” framework not found vecLib “的问题出现

2.相应包安装

1.首先要安装homebrew包管理工具,在终端运行下面命令:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2.然后利用该管理工具安装caffe依赖包

brew install -vd snappy leveldb gflags glog szip lmdb
brew install hdf5 opencv
brew install openblas

3.安装boost

brew install boost@1.59 boost-python@1.59
brew link boost@1.59 --force
brew link boost-python@1.59 --force

4. 安装2.6.1版本的protobuf

一开始的是安装3.5.1版本的protobuf,但是后面出现问题,所以还是安装2.6.1版本

下面只是记录一下:

cd ~/Downloads
wget https://github.com/protocolbuffers/protobuf/archive/v3.5.1.zip
unzip v3.5..zip
cd protobuf-3.5.
./autogen.sh
./configure
make
make install

运行./autogen.sh过程出错:

+ autoreconf -f -i -Wall,no-obsolete
./autogen.sh: line : autoreconf: command not found

解决方案是安装protobuf的依赖项:

(deeplearning2) userdeMacBook-Pro:protobuf-3.5. user$ brew install autoconf automake libtool

再重新运行./autogen.sh及之后命令

成功后查看版本:

(deeplearning2) userdeMacBook-Pro:protobuf-3.5. user$ protoc --version
libprotoc 3.5.

这里在下面caffe文件夹中make all之前忘记make clean了,可能是这个原因没成功也不一定,但是2.6.0版本的的确成功了,直接用该版本即可

后面发现还是不行,就去下了2.6.1版本的:

/Library/Developer/CommandLineTools/usr/bin/make  check-TESTS
PASS: protobuf-test
PASS: protobuf-lazy-descriptor-test
PASS: protobuf-lite-test
PASS: google/protobuf/compiler/zip_output_unittest.sh
PASS: google/protobuf/io/gzip_stream_unittest.sh
============================================================================
Testsuite summary for Protocol Buffers 2.6.
============================================================================
# TOTAL:
# PASS:
# SKIP:
# XFAIL:
# FAIL:
# XPASS:
# ERROR:
============================================================================

版本查看:

(deeplearning2) userdeMacBook-Pro:~ user$  protoc --version
libprotoc 2.6.

3.下载caffe源码

(deeplearning2) userdeMacBook-Pro:~ user$ git clone https://github.com/bvlc/caffe.git
Cloning into 'caffe'...
remote: Enumerating objects: , done.

下载好后:

(deeplearning2) userdeMacBook-Pro:~ user$ cd caffe
(deeplearning2) userdeMacBook-Pro:caffe user$ cp Makefile.config.example Makefile.config

4.更改Makefile.config文件

打开CPU_ONLY选项,保存

# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY :=

即去掉前面的#号,表示caffe编译时仅支持CPU,不支持GPU

取消CPU_ONLY前面的注释

取消USE_OPENCV前面的注释

取消USE_HDF5前面的注释,并且改成1

更改CUSTOM_CXX

注释掉CUDA_DIR那一行

将BLAS改成open

设置blas的路径,取消下面两行的注释

# uncomment to disable IO dependencies and corresponding data layers
USE_OPENCV := # This code is taken from https://github.com/sh1r0/caffe-android-lib
USE_HDF5 := # Uncomment if you're using OpenCV 3
OPENCV_VERSION :=
# CUDA directory contains bin/ and lib/ directories that we need.
#CUDA_DIR := /usr/local/cuda
BLAS := open # 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
# 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

下面就是设置路径,使用的是anaconda3,这个根据你自己的路径设置

注释掉:

# 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. \
# /usr/lib/python2./dist-packages/numpy/core/include

更改为:

# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := /anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/envs/deeplearning2/include \
$(ANACONDA_HOME)/envs/deeplearning2/include/python2. \
$(ANACONDA_HOME)/envs/deeplearning2/lib/python2./site-packages/numpy/core/include

下面这个也更改为:

# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/envs/deeplearning2/lib

然后添加一些include和lib路径:

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/opt/boost@1.59/include /usr/local/opt/hdf5/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opt/boost-python@1.59/lib /usr/local/opt/hdf5/lib

最终版本为:

## 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 := # CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := # uncomment to disable IO dependencies and corresponding data layers
USE_OPENCV :=
# USE_LEVELDB :=
# USE_LMDB :=
# This code is taken from https://github.com/sh1r0/caffe-android-lib
USE_HDF5 := # 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 := # Uncomment if you're using OpenCV 3
# OPENCV_VERSION := # 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++ # 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. \
# /usr/lib/python2./dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := /anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/envs/deeplearning2/include \
$(ANACONDA_HOME)/envs/deeplearning2/include/python2. \
$(ANACONDA_HOME)/envs/deeplearning2/lib/python2./site-packages/numpy/core/include # Uncomment to use Python (default is Python )
# PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
# /usr/lib/python3./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)/envs/deeplearning2/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 := # Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/opt/boost@1.59/include /usr/local/opt/hdf5/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opt/boost-python@1.59/lib /usr/local/opt/hdf5/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 := # 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 := # 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 := # The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := # enable pretty build (comment to see full commands)
Q ?= @

如果你在运行make test命令出下面类似的错误:

In file included from /usr/local/include/leveldb/iterator.h::
/usr/local/include/leveldb/status.h::: error: expected ';' at end of declaration list
Status() noexcept : state_(nullptr) {}
^
;
/usr/local/include/leveldb/status.h::: warning: rvalue references are a C++ extension [-Wc++-extensions]
Status(Status&& rhs) noexcept : state_(rhs.state_) { rhs.state_ = nullptr; }
^
/usr/local/include/leveldb/status.h::: error: expected ';' at end of declaration list
Status(Status&& rhs) noexcept : state_(rhs.state_) { rhs.state_ = nullptr; }
^
;
/usr/local/include/leveldb/status.h::: error: 'Status' is missing exception specification 'throw()'
inline Status::Status(const Status& rhs) {
^
throw()
/usr/local/include/leveldb/status.h::: note: previous declaration is here
class LEVELDB_EXPORT Status {
^
/usr/local/include/leveldb/status.h::: warning: rvalue references are a C++ extension [-Wc++-extensions]
inline Status& Status::operator=(Status&& rhs) noexcept {
^
/usr/local/include/leveldb/status.h::: error: expected function body after function declarator
inline Status& Status::operator=(Status&& rhs) noexcept {
^
In file included from src/caffe/util/db.cpp::

之前出这样的内容以为是leveldb版本的问题,但是后面发现设置该为:

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

需要添加-std=c++11才能正常编译,就没有上面的问题了,跟着上面的配置走应该就不会有问题

想要读取python写的layer,就要取消下面行的注释:

# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER :=

5.make编译:

make all
make test
make runtest
make pycaffe

⚠️注意:过程中可能会出现很多warning,只要不出现error,那都可以忽略

下面的问题基本上都使用更改配置的方法解决了,所以如果你跟着我上面的配置走,下面的一些问题你应该不会遇见

下面只是记录一下

⚠️运行make命令时,出了任何错,更改过后都要记得make clean一次,然后再从头make all进行编译

make all问题:

bool    OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue,
^
src/caffe/net.cpp::: error: use of undeclared identifier 'H5Fis_hdf5'
if (H5Fis_hdf5(trained_filename.c_str())) {
^
warnings and error generated.
make: *** [.build_release/src/caffe/net.o] Error

解决,在配置中添加boost路径:

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/opt/boost@1.59/include /usr/local/opt/hdf5/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opt/boost-python@1.59/lib /usr/local/opt/hdf5/lib

make runtest问题:

(deeplearning2) userdeMacBook-Pro:caffe user$ make runtest
.build_release/tools/caffe
dyld: Library not loaded: /usr/local/opt/gcc/lib/gcc//libgfortran..dylib
Referenced from: /usr/local/opt/openblas/lib/libopenblasp-r0.3.5.dylib
Reason: image not found
make: *** [runtest] Abort trap:

原因:

因为我本地安装的gcc是9版本,只有/usr/local/opt/gcc/lib/gcc/9,没有8的路径

解决办法就是安装了个8的版本:

(deeplearning2) userdeMacBook-Pro:caffe user$ brew install gcc@
==> Downloading https://homebrew.bintray.com/bottles/gcc@8-8.3.0.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/b1/b1e150c72b4c3b7f3493371d71cdb668f691bfee2e998e5b0bf570eed28254d6?__gda__=exp=1565346562~hmac=81794cd252808740593fa500900f13dbd
######################################################################## 100.0%
==> Pouring gcc@-8.3..mojave.bottle.tar.gz

最新文章

  1. Android中访问sdcard路径的几种方式
  2. ASP.NET中Ajax的用法
  3. 免费SSL-HTTS 申请与配置 NGINX配置
  4. 资源搜集:Git精品文章推荐,常年更新
  5. (转)四种常见的 POST 提交数据方式
  6. Revit如何设置尺寸标注的箭头样式
  7. OpenGL 4.0 GLSL 实现 投影纹理映射(Projective Texture Mapping) (转)
  8. HDU 4893 线段树
  9. ListView 使用
  10. Spring 与 mybatis整合 Error parsing Mapper XML. Cause: java.lang.NullPointerException
  11. Java正则表达式细节1
  12. laravel中with()方法,has()方法和whereHas()方法的区别
  13. python并发编程之多线程二
  14. node基础篇一:node介绍、node http、node event 课堂(持续)
  15. 【BZOJ2998】Problem A(动态规划)
  16. Adaptive Placeholders
  17. 鼠标hover元素scale/zoom中心点放大效果实例页面
  18. Apollo分布式配置中心部署以及使用
  19. Scrapy学习篇(八)之settings
  20. Illegal access: this web application instance has been stopped already. could not load **

热门文章

  1. MySQL5.7数据库的基本操作命令
  2. LOJ#2764. 「JOI 2013 Final」JOIOI 塔
  3. vue中显示markdown文件为html
  4. AtomicIntegerFieldUpdater字段原子更新类
  5. php技能树---大神的进阶之路
  6. C++ 如何进阶?
  7. nvarchar(MAX) 、ntext的差别
  8. Java 合并PDF文件
  9. CF1204E Natasha, Sasha and the Prefix Sums(组合数学)
  10. (持续更新)vs2012,2013,2015,2017,2019 常用的插件 与 开发中常用的工具