Mac下安裝Anaconda+Caffe
Mac下安裝caffe主要步驟:
- 安裝Anaconda
- 安裝Caffe依賴庫、編譯Caffe
安裝Anaconda
這個從官網下載pkg安裝版一路next很順利就裝好了,默認路徑在~/anconda,安裝器會自動添加路徑到PATH中.
安裝Caffe依賴庫,編譯Caffe
參考:
Mac下安裝caffe(cpu-only) .
mac安裝caffe(避免踩坑的腳本)
安裝依賴庫
brew install --fresh -vd snappy leveldb gflags glog szip lmdb homebrew/science/opencv
brew install --build-from-source --with-python --fresh -vd protobuf
brew install --build-from-source --fresh -vd boost boost-python
brew install openblas
編譯
Makefile.config裏要設置 Anaconda 和 Blas路徑 ,
make all
另外,caffe-master/python文件夾下的requirements.txt中顯示了編譯pycaffe所需的庫列表,需要提前安裝。( 能用anaconda裝的所需modules和libraries就用conda裝,裝不了再試其他的
for req in $(cat python/requirements.txt); do pip install $req; done
make pycaffe
make distribute
最後,設置環境變量PYTHONPATH.
open ~/.bash_profile
# added by Anaconda2 4.4.0 installer
export PATH="/Users/~/anaconda/bin:$PATH"
# caffe
export PATH="/usr/local/Cellar/caffe/build/tools:$PATH"
export PYTHONPATH=/usr/local/Cellar/caffe/python:$PYTHONPATH
Anaconda中import caffe的問題
到此時,在系統的terminal中輸入python會顯示調用Anaconda的python,並且能夠直接import caffe;
在Anaconda的jupyter notebook下也能直接import caffe;
但是在Anaconda的Spyder IDE的ipython console中直接運行import caffe時就會出現找不到caffe的錯誤,據說 pycharm 也會出現這個問題,解決方法是臨時追加caffe/python路徑
import sys
sys.path.append('/usr/local/Cellar/caffe/python')
import caffe
caffe運行測試
還是用mnist數據集:
cd $CAFFE_ROOT
./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh
caffe train --solver=examples/mnist/lenet_solver.prototxt
caffe test -model=examples/mnist/lenet_train_test.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel
寫一個python腳本,輸入自己的測試圖片,使用Caffe訓練的模型進行預測:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import os
import sys
cafferoot='/usr/local/Cellar/caffe'
sys.path.insert(0, cafferoot + '/python')
import caffe
model_file = cafferoot+'/examples/mnist/lenet.prototxt'
pretrained_file = cafferoot+'/examples/mnist/lenet_iter_10000.caffemodel'
net = caffe.Classifier(model_file, pretrained_file, image_dims=(28, 28), raw_scale=255)
img = cafferoot+'/examples/mnist/mytest/1.png'
score = net.predict([caffe.io.load_image(img, color=False)], oversample=False)
print (score)
*順便熟悉一下Mac下的Xcode配置項目的簡單步驟,這裏用一個c++小程序實驗,該程序通過調用protobuf庫來解析caffe的參數配置文件.prototxt,代碼來自《》這裏寫鏈接內容