MacOS安装caffe

1.需要安装CUDA,不管有没有N卡
2.安装OpenBLAS,并在caffe设置文件中设置为:

# 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 := /opt/OpenBLAS/include
BLAS_LIB := /opt/OpenBLAS/lib

3.安装依赖库,可用brew list检查安装是否完全
4.添加路径:

export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:/usr/local/lib  

export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:$HOME/anaconda/lib:/usr/local/lib:/usr/lib:/opt/OpenBLAS/lib:/opt/OpenBLAS

5.下载caffe源文件,编译:make all时报错:

    PROTOC src/caffe/proto/caffe.proto  
    make: protoc: No such file or directory 

解决: 

    sudo chown yourname /usr/local  

    brew link yourlibpackage

6.Build: 继续编译:make testmake runtest

添加路径:

    export DYLD_LIBRARY_PATH=/usr/local/cuda/lib

    #python
    for req in $(cat python/requirements.txt); do pip install $req; done
    make pycaffe
    export PYTHONPATH=~/technologies/caffe/python/:$PYTHONPATH
    cd ..

make runtest的结果:

[----------] 10 tests from PowerLayerTest/0, where TypeParam = N5caffe9CPUDeviceIfEE
[ RUN      ] PowerLayerTest/0.TestPower
[       OK ] PowerLayerTest/0.TestPower (2 ms)
[ RUN      ] PowerLayerTest/0.TestPowerZeroGradient
[       OK ] PowerLayerTest/0.TestPowerZeroGradient (1 ms)
[ RUN      ] PowerLayerTest/0.TestPowerTwoGradient
...
[----------] 10 tests from PowerLayerTest/0 (16 ms total)

Bugs:

    library not found for -lboost_python

解决: 

    brew install boost-python

测试

MNIST

下载mnist数据,如下代码,报错,需安装brew install wget

./data/mnist/get_mnist.sh 

转数据:

./examples/mnist/create_mnist.sh
Creating lmdb...
Done.

./examples/mnist/train_lenet.sh

问题:

Cannot use GPU in CPU-only Caffe: check mode.

解决: 修改lenet_solver.prototxt 最后一行的GPU改为CPU,继续执行./examples/mnist/train_lenet.sh

I0407 00:51:59.023721 2035871744 caffe.cpp:178] Use CPU.
I0407 00:51:59.024705 2035871744 solver.cpp:48] Initializing solver from parameters: 
test_iter: 100
test_interval: 500
base_lr: 0.01
display: 100
max_iter: 10000
lr_policy: "inv"
gamma: 0.0001
power: 0.75
momentum: 0.9
weight_decay: 0.0005
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"
solver_mode: CPU
net: "examples/mnist/lenet_train_test.prototxt"
I0407 00:51:59.024927 2035871744 solver.cpp:91] Creating training net from net file: examples/mnist/lenet_train_test.prototxt
I0407 00:51:59.026877 2035871744 net.cpp:313] The NetState phase (0) differed from the phase (1) specified by a rule in layer mnist
I0407 00:51:59.026897 2035871744 net.cpp:313] The NetState phase (0) differed from the phase (1) specified by a rule in layer accuracy

...结果...

I0407 00:57:52.664399 2035871744 solver.cpp:317] Iteration 10000, loss = 0.0032766
I0407 00:57:52.664429 2035871744 solver.cpp:337] Iteration 10000, Testing net (#0)
I0407 00:57:54.761032 2035871744 solver.cpp:404]     Test net output #0: accuracy = 0.9901
I0407 00:57:54.761072 2035871744 solver.cpp:404]     Test net output #1: loss = 0.0290336 (* 1 = 0.0290336 loss)
I0407 00:57:54.761081 2035871744 solver.cpp:322] Optimization Done.
I0407 00:57:54.761087 2035871744 caffe.cpp:222] Optimization Done.