mac配置caffe2

Posted by Kriz on 2017-05-17

应要求写个基于macOS Sierra 10.12.4的caffe2配置教程【

caffe2的安装真的是比caffe不知道良心到哪里去了

安装

首先保证已装好AnacondaHomebrew。Anaconda建议装附带GUI的那个,依赖包版本混乱的时候面对清新的绿色界面还是会心情好一点【其实并不是这样

打开终端,使用brew安装以下依赖包:

1
2
3
4
5
6
brew install \
automake \
cmake \
git \
glog \
protobuf

如果已经安装某些依赖但版本比较老导致之后无法成功make install,请使用brew upgrade命令进行升级。

随后使用conda安装:

1
2
3
4
5
conda install -y --channel https://conda.anaconda.org/conda-forge \
gflags \
glog \
numpy \
protobuf

顺便说,嫌conda慢的话国内有镜像,戳这里。不过我没换channel,不知道行不行【

全部安装完毕后打开终端,运行:

1
2
3
4
5
cd
git clone --recursive https://github.com/caffe2/caffe2.git && cd caffe2
mkdir build && cd build
cmake -DUSE_CUDA=OFF ..
sudo make install

卑微的CPU-only MBP用户等待到运行结束就可以撒花了。

可以测试一下:

1
python -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"

问题

make install这一步可能遇到的两个问题:

1
2
#error This file was generated by an older version of protoc which is incompatible with your Protocol Buffer headers.
#error Please regenerate this file with a newer version of protoc.

我碰到这个问题是因为protobuf官网的建议安装版本是3.2.0(至少目前还是这样),但是用3.2.0在后面make install的时候会报版本问题,所以我在上面教程里conda安装的地方把conda installprotobuf=3.2.0改成了protobuf,还是直接装最新版吧。

如果怕其他依赖失效而实在不想更新的话,也可以试试把导致error的部分注释掉(不太建议)。在/path/to/your/caffe2/build/caffe2/proto/目录下,打开报错的xxx.pb.h,把下面这部分注释,重新执行cmake和make install就可以了:

1
2
3
4
5
6
7
8
9
10
#if GOOGLE_PROTOBUF_VERSION < 3002000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers. Please update
#error your headers.
#endif
#if 3002000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers. Please
#error regenerate this file with a newer version of protoc.
#endif

如果碰到

1
2
error: implicit instantiation of undefined template 'std::__1::array<int, 2>'
std::array<int32_t, 2> temp_params = {{tiles_, axis_}};

打开/path/to/your/caffe2/caffe2/operators/tile_op.h,在开头添加#include <array>就可以了。

版本参考

我成功安装后brew里的各依赖包版本:

homebrew

  • automake 1.15
  • cmake 3.7.2 3.8.1
  • git 2.12.1 2.12.2 2.13.0
  • glog 0.3.4_1 0.3.5_1
  • protobuf 3.2.0 3.3.0

conda

  • gflags 2.2.0
  • glog 0.3.4
  • numpy 1.11.3
  • protobuf 3.3.0

参考文献

https://caffe2.ai/docs/getting-started.html

http://www.jianshu.com/p/9c1962d2851f