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,代码来自《》这里写链接内容