Mac下安裝caffe並執行MNIST
阿新 • • 發佈:2019-02-14
1,首先安裝homebrew包管理工具
$ ruby -e "$(curl-fsSL\http://raw.githubusercontent.com/Homebrew/install/master/install)"
2,安裝成功後,利用brew安裝依賴包
$ brew install -vd snappy leveldb gflags blogs zip lmdv
$ brew tap homebrew/science
$ brew install protobuf boost wget
此過程中可能有些依賴包會裝不上,部分可參考下面的解決辦法:
glog安裝
$ wget https: //google-glog.googlecode.com/files/glog-0.3.3.tar.gz
$ tar zxvf glog-0.3.3.tar.gz
$ cd glog-0.3.3
$ ./configure
$ make -stdlib = libstdc++
$ make install
flags安裝
$ wget http://github.com/schuhschuh/gflags/archive/master.zip
$ unzip master.zip
$ cd gulags-master
$ mkdir build && cd build
$ export CXXFLAGS ="-fPIC" &&cmake ..&& make VERBOSE=1
$ make && make install
lmdb安裝
$ git clone https://github.com/LMDB/lmdb
$ cd lmdb/libraries/liblmdb
$ make && make install
3,下載caffe原始碼
$ git clone https://github.com/bvlc/caffe.git
$ cd caffe/
$ mv Makefile.config.example Makefile.config
$ vi Makefile.config
//修改 CPU_ONLY:=1
4,編譯
$ make all
$ make test
$ make runtest
編譯過程中,可能會出現很多warning,不過不用擔心,沒報錯就行。
5, 執行MNIST
獲取並生成訓練集和資料集
$ cd caffe
$ ./data/mnist/get_mnist.sh
$ ./examples/mnist/create_mnist.sh
執行完後在mnist資料夾下會多出兩個資料夾:mnist_train_lmdb,mnist_test_lmdb,分別是訓練集和測試集。
因為Mac上面沒有GPU,所以將./examples/mnist/lenet_solver.prototxt檔案中最後一行改為:
solver_mode: CPU
執行minst:
$ cd caffe
$ ./examples/mnist/train_lenet.sh
成功後,會出現如下資訊:
yuanfeideMacBook-Pro:caffe figo$ ./examples/mnist/train_lenet.sh
I1107 16:53:39.949373 2009157632 caffe.cpp:210] Use CPU.
I1107 16:53:39.950474 2009157632 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"
train_state {
level: 0
stage: ""
}
...//中間太長就省略了。。。
I1107 16:53:39.981427 2009157632 net.cpp:228] label_mnist_1_split does not need backward computation.
I1107 16:53:39.981441 2009157632 net.cpp:228] mnist does not need backward computation.
I1107 16:53:39.981448 2009157632 net.cpp:270] This network produces output accuracy
I1107 16:53:39.981456 2009157632 net.cpp:270] This network produces output loss
I1107 16:53:39.981469 2009157632 net.cpp:283] Network initialization done.
I1107 16:53:39.981591 2009157632 solver.cpp:60] Solver scaffolding done.
I1107 16:53:39.981662 2009157632 caffe.cpp:251] Starting Optimization
I1107 16:53:39.981693 2009157632 solver.cpp:279] Solving LeNet
I1107 16:53:39.981703 2009157632 solver.cpp:280] Learning Rate Policy: inv
I1107 16:53:39.983294 2009157632 solver.cpp:337] Iteration 0, Testing net (#0)
I1107 16:53:42.728090 2009157632 solver.cpp:404] Test net output #0: accuracy = 0.1544
I1107 16:53:42.728124 2009157632 solver.cpp:404] Test net output #1: loss = 2.32693 (* 1 = 2.32693 loss)
I1107 16:53:42.773195 2009157632 solver.cpp:228] Iteration 0, loss = 2.33117
I1107 16:53:42.773283 2009157632 solver.cpp:244] Train net output #0: loss = 2.33117 (* 1 = 2.33117 loss)
I1107 16:53:42.773350 2009157632 sgd_solver.cpp:106] Iteration 0, lr = 0.01
I1107 16:53:46.592530 2009157632 solver.cpp:228] Iteration 100, loss = 0.18131
I1107 16:53:46.592572 2009157632 solver.cpp:244] Train net output #0: loss = 0.18131 (* 1 = 0.18131 loss)
I1107 16:53:46.592582 2009157632 sgd_solver.cpp:106] Iteration 100, lr = 0.00992565
I1107 16:53:50.332984 2009157632 solver.cpp:228] Iteration 200, loss = 0.125354
I1107 16:53:50.333026 2009157632 solver.cpp:244] Train net output #0: loss = 0.125354 (* 1 = 0.125354 loss)
I1107 16:53:50.333035 2009157632 sgd_solver.cpp:106] Iteration 200, lr = 0.00985258
生成訓練集和測試集的時候出現瞭如下錯誤:
Library not loaded: /usr/local/opt/protobuf/lib/libprotobuf.9.dylib
Referenced from: /Users/figo/caffe/build/examples/mnist/convert_mnist_data.bin
Reason: image not found
出現此錯誤主要是因為之前安裝了caffe,後來安裝tensorflow的時候將protobuf更新到了最新的版本,要解決此問題,只需要將caffe重新編譯下就可以了。
重新編譯的過程中,又出現瞭如下錯誤:
.build_release/src/caffe/proto/caffe.pb.h:17:2: error: This file was generated by an older version of protoc
需要clean一下,再編譯就行了
$ make clean
$ make all
$ make test
$ make runtest