【深度学习】在Mac下安装深度学习框架Caffe并测试Mnist数据集

来源:互联网 发布:xlsx软件 编辑:程序博客网 时间:2024/05/16 19:13

本文的重点内容是在Mac OS下安装深度学习框架Caffe,对于深度学习、安装过程中使用的命令/方法等原理不做过多介绍。

首先,给出本人使用的Mac信息,不同的系统信息需要配置不同的工具。由于本人也属于小白,在安装过程中并没有使用GPU,因此相关内容不做介绍,后续会在本文的下方给出补充。


在介绍适合自己的配置方法之前,列车本文参考的两篇博客,供参考。

1、http://blog.csdn.net/taigw/article/details/50683289

2、http://www.linuxidc.com/Linux/2016-09/135026.htm

下面给出安装步骤:

准备阶段

1、进入终端,安装Homebrew,brew是一个很好的工具,在后期的工作中会长期使用,而且大部分的软件也可以通过brew在终端直接安装。

2、在brew下安装OpenCV,具体的安装方法在之前的博文中有提及,故不再累述。这里安装的OpenCV版本是opencv 2.4.13。

接下来安装caffe

3、安装caffe相关依赖,这里使用的都是brew安装,在进行安装之前可以先对brew进行一个update:

brew update

之后安装必要依赖,这些依赖与官网的安装指导教程是一致的,当然你也可以根据自己的需要进行选择安装。

brew install -vd snappy leveldb gflags glog ship lmdvbrew tap homebrew/sciencebrew install hdf5

需要声明的是,官网上给出了opencv的安装,由于我的电脑在之前安装Xcode时候,已经安装过opencv,这里就没必要继续安装了,依个人情况而定。

brew install protobuf boost

protobuf与boost也是caffe的依赖。

4、下载并安装caffe,将下载之后的caffe放在一个指定的位置,因为后期会经常用到其路径;这里我讲下载的caffe-master文件夹放在桌面上。假设caffe在Mac中的路径是:/Users/admin/Desktop/caffe-master。

5、修改配置文件:在caffe-master中复制Makefile.config.example,并将其副本命名为“Makefile.config”。这里我建议用文本编辑器将其打开浏览一遍,其内容是caffe的一些相关配置与方法,前面的“#”是注释的意思。安装自己电脑的安装情况,修改Makefile.config中的部分内容。这里的修改项主要有:

CPU_ONLY:=1的注释,因为目前还没有涉及到GPU的使用。

OPENCV_VERSION:=2.4,我这里的版本是2.4.13,根据自己电脑的情况决定。

CUSTOM_CXX:=clang++的注释。

修改后的Makefile.config如下:(其中的一些修改会在下文进行说明)

## Refer to http://caffe.berkeleyvision.org/installation.html# Contributions simplifying and improving our build system are welcome!# cuDNN acceleration switch (uncomment to build with cuDNN).# USE_CUDNN := 1# CPU-only switch (uncomment to build without GPU support).CPU_ONLY := 1# uncomment to disable IO dependencies and corresponding data layers# USE_OPENCV := 0# USE_LEVELDB := 0# USE_LMDB := 0# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)#You should not set this flag if you will be reading LMDBs with any#possibility of simultaneous read and write# ALLOW_LMDB_NOLOCK := 1# Uncomment if you're using OpenCV 3OPENCV_VERSION := 2.4# To customize your choice of compiler, uncomment and set the following.# N.B. the default for Linux is g++ and the default for OSX is clang++CUSTOM_CXX := clang++# CUDA directory contains bin/ and lib/ directories that we need.CUDA_DIR := /usr/local/cuda# On Ubuntu 14.04, if cuda tools are installed via# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:# CUDA_DIR := /usr# CUDA architecture setting: going with all of them.# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \-gencode arch=compute_20,code=sm_21 \-gencode arch=compute_30,code=sm_30 \-gencode arch=compute_35,code=sm_35 \-gencode arch=compute_50,code=sm_50 \-gencode arch=compute_52,code=sm_52 \-gencode arch=compute_60,code=sm_60 \-gencode arch=compute_61,code=sm_61 \-gencode arch=compute_61,code=compute_61# BLAS choice:# atlas for ATLAS (default)# mkl for MKL# open for OpenBlasBLAS := 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 := /usr/local/Cellar/openblas/0.2.18_2/includeBLAS_LIB := /usr/local/Cellar/openblas/0.2.18_2/lib# Homebrew puts openblas in a directory that is not on the standard search path# BLAS_INCLUDE := $(shell brew --prefix openblas)/include# BLAS_LIB := $(shell brew --prefix openblas)/lib# This is required only if you will compile the matlab interface.# MATLAB directory should contain the mex binary in /bin.# MATLAB_DIR := /usr/local# MATLAB_DIR := /Applications/MATLAB_R2012b.app# NOTE: this is required only if you will compile the python interface.# We need to be able to find Python.h and numpy/arrayobject.h.PYTHON_INCLUDE := /usr/include/python2.7 \/usr/lib/python2.7/dist-packages/numpy/core/include# Anaconda Python distribution is quite popular. Include path:# Verify anaconda location, sometimes it's in root.# ANACONDA_HOME := $(HOME)/anaconda# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \# $(ANACONDA_HOME)/include/python2.7 \# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include# Uncomment to use Python 3 (default is Python 2)# PYTHON_LIBRARIES := boost_python3 python3.5m# PYTHON_INCLUDE := /usr/include/python3.5m \#                 /usr/lib/python3.5/dist-packages/numpy/core/include# We need to be able to find libpythonX.X.so or .dylib.PYTHON_LIB := /usr/lib# PYTHON_LIB := $(ANACONDA_HOME)/lib# Homebrew installs numpy in a non standard path (keg only)# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include# PYTHON_LIB += $(shell brew --prefix numpy)/lib# Uncomment to support layers written in Python (will link against Python libs)# WITH_PYTHON_LAYER := 1# Whatever else you find you need goes here.INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/includeLIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies# INCLUDE_DIRS += $(shell brew --prefix)/include# LIBRARY_DIRS += $(shell brew --prefix)/lib# NCCL acceleration switch (uncomment to build with NCCL)# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)# USE_NCCL := 1# Uncomment to use `pkg-config` to specify OpenCV library paths.# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)# USE_PKG_CONFIG := 1# N.B. both build and distribute dirs are cleared on `make clean`BUILD_DIR := buildDISTRIBUTE_DIR := distribute# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171# DEBUG := 1# The ID of the GPU that 'make runtest' will use to run unit tests.TEST_GPUID := 0# enable pretty build (comment to see full commands)Q ?= @

6、理论上此时可以进入caffe的根目录下进行make测试了:

cd /Users/admin/Desktop/caffe-mastermake all

注意:我在这一步出现的一个Error“cblas.h” file is not found

解决方法是安装BLAS(之前没有安装是因为Mac自带一个不同版本的BLAS),既然报错了那么干脆换一个其他的openblas,安装命令

brew install openblas

之后,修改Makefile.config中的openblas路径,将其设置为自己电脑的路径(见上面的配置列表)。在安装完openblas之后,再makeall就没有问题了。

这里需要说明的是,如果在make all之前报错,那么很可能是Makefile.config文件中的路径问题,一定要根据自己电脑的安装路径进行修改。

7、在caffe-master根目录下运行

make allmake testmake runtest

这几个命令可能都需要几分钟来执行,稍等即可。而且在这个过程中会出现10几个Warnings,自动忽略即可。

最后终端出现这样的显示,表示caffe安装成功。



以上的过程是最简单的caffe安装方法,并未涉及到Python、Matlab等其他工具的安装与配置,相关的安装方法可在本文开始给出的两篇博客中找到。

安装成功之后,接下来就要测试一下caffe的效果。这里使用的是MNIST数据集。

1、首先还是需要在caffe-master的根目录下进行操作,在进行测试之前,可能需要安装wget:

brew install wget
2、在caffe-master根目录下执行:

./data/mnist/get_mnist.sh./examples/mnist/create_mnist.sh

此时可以发现./examples/mnist/路径下会有mnist_test_lmdb与mnist_train_lmdb两个文件夹,即为测试集与训练集。

3、为了保证在=训练过程在CPU上运行,应修改./examples/mnist/lenet_solver.prototxt中的最后一句话为:

solver_mode:CPU
4、最后执行训练命令:
cd caffe-master./examples/mnist/train_lenet.sh

在迭代10000次之后,便可以看到运行结果,准确率约为0.99。

  

以上便是MacOS下caffe的安装与配置过程。

更多功能的配置与使用会在后续的实验过程中补充。

0 0