MacOS编译安装Caffe


原文链接: MacOS编译安装Caffe

MacOS编译安装Caffe

Caffe是一个清晰而高效的深度学习框架,是纯粹的C++、CUDA架构,支持命令行,Python和MATLAB接口,可以在CPU和GPU直接无缝切换,Caffe的优势:

上手快,模型和相应优化都是以文本形式而非代码形式给出,Caffe给出了模型的定义,最优化设置以及预训练的权重,方便立即上手。

速度快,Caffe与cuDNN结合使用,能够运行最棒的模型和海量的数据。

模块化,方便拓展新的认知和设置.

开源,开放

Caffe在MacOS可以使用Homebrew安装,也是自行编译源代码安装,这里介绍下直接编译源代码安装.

下载Caffe源代码
Caffe的github地址:https://github.com/BVLC/caffe,直接克隆Caffe源代码:

git clone git@github.com:BVLC/caffe.git

复制Makefile.config文件:

cd caffe
cp Makefile.config.example Makefile.config

Makefile.config文件中有些编译选项需要做更改才能在MacOS上面编译通过,后面会介绍,下面先介绍下编译Caff所需要的依赖。

安装Caffe依赖
编译Caffe需要有大量的依赖,开始扁你之前,需要先安装这些依赖。
(1) 使用GPU模式需要安装CUDA,安装CUDA的命令:

brew cask install cuda

也可以不采用GPU模式,只使用CPU,在Makefile.config中做修改:

CPUU_ONLY=1

(2) Boost
Caffe使用的是c++开发,如果要使用python调用Caffe的接口的话,需要安装boot.python:

brew install boost --with-python
brew install boost-python

(3) OpenCV 安装,直接安装OpenCV

brew install opencv

OpenCV安装之后需要在Makefile.config中设置OpenCV的文件头路径,以及lib的路径:

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/Cellar/opencv/2.4.13.2/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/Cellar/opencv/2.4.13.2/lib
(4) 数据库leveldb,lmdb,hdf5安装:

brew install leveldb
brew install lmdb
brew tap homebrew/science
brew install install homebrew/science/hdf5

(5) 日志与数据操作

brew install protobuf
brew install glog
brew install gflags
brew install snappy

安装caffe-python依赖
先要安装 Python依赖库:numpy,h5py以及scikit-image

brew install numpy
pip install h5py
pip install scikit-image

安装完Python的依赖类库之后需要注意以下的4点:

设置Makefile.config中numpy的路径:

PYTHON_INCLUDE := /usr/include/python2.7

在python中使用OpenCV,需要把OpenCV安装目录下../python/site-packages里面的两个文件cv.py和cv2.so拷贝到/usr/local/lib/python2.7/site-packages目录下,这样python才能调用OpenCV。

在Makefile.config设置WITH_PYTHON_LAYER:=1

在Makefile.config中设置PYTHON_LIB:

PYTHON_LIB := /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib

编译Caffe
使用make命令编译Caffe:

make clean
make all
make test
make runtest

编译Caffe-Python:

make pycaffe

以上编译都通过之后将caffe/python添加到python系统路径里fish设置命令:

set -gx PYTHONPATH path/to/caffe/python $PYTHONPATH

bash的设置命令:

export PYTHONPATH=path/to/caffe/python:$PYTHONPATH

使用Caffe
在命令行中直接测试Caffe是否编译成功。

python
import caffe

没有错误出现则表示Caffe安装成功

Install

http://caffe.berkeleyvision.org/installation.html

macos install caffe

#!/bin/sh
# Install brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Apple hides old versions of stuff at https://developer.apple.com/download/more/
# Install the latest XCode (8.0).
#   We used to install the XCode Command Line Tools 7.3 here, but that would just upset the most recent versions of brew.
#   So we're going to install all our brew dependencies first, and then downgrade the tools. You can switch back after
#   you have installed caffe.
# Install CUDA toolkit 8.0 release candidate
#   Register and download from https://developer.nvidia.com/cuda-release-candidate-download
#   or this path from https://developer.nvidia.com/compute/cuda/8.0/rc/local_installers/cuda_8.0.29_mac-dmg
#   Select both the driver and the toolkit, no documentation necessary
# Install the experimental NVIDIA Mac drivers
#   Download from http://www.nvidia.com/download/driverResults.aspx/103826/en-us
# Install cuDNN v5 for 8.0 RC or use the latest when it's available
#   Register and download from https://developer.nvidia.com/rdp/cudnn-download 
#   or this path: https://developer.nvidia.com/rdp/assets/cudnn-8.0-osx-x64-v5.0-ga-tgz
#   extract to the NVIDIA CUDA folder and perform necessary linking
#   into your /usr/local/cuda/lib and /usr/local/cuda/include folders
#   You will need to use sudo because the CUDA folder is owned by root
# sudo tar -xvf ~/Downloads/cudnn-8.0-osx-x64-v5.0-ga.tar /Developer/NVIDIA/CUDA-8.0/
# sudo ln -s /Developer/NVIDIA/CUDA-8.0/lib/libcudnn.dylib /usr/local/cuda/lib/libcudnn.dylib
# sudo ln -s /Developer/NVIDIA/CUDA-8.0/lib/libcudnn.5.dylib /usr/local/cuda/lib/libcudnn.5.dylib
# sudo ln -s /Developer/NVIDIA/CUDA-8.0/lib/libcudnn_static.a /usr/local/cuda/lib/libcudnn_static.a
# sudo ln -s /Developer/NVIDIA/CUDA-8.0/include/cudnn.h /usr/local/cuda/include/cudnn.h
# Install the brew dependencies
#   Do not install python through brew. Only misery lies there
#   We'll use the versions repository to get the right version of boost and boost-python
#   We'll also explicitly upgrade libpng because it's out of date
#   Do not install numpy via brew. Your system python already has it.

brew install -vd snappy leveldb gflags glog szip lmdb
brew install hdf5 opencv wget
brew install openblas

# brew install --build-from-source -vd boost159 boost-python159
brew install boost@1.59 boost-python@1.59
brew link boost@1.59 --force
brew link boost-python@1.59 --force

# brew install --build-from-source --with-python -vd protobuf
cd ~/Downloads
wget https://github.com/protocolbuffers/protobuf/archive/v3.5.1.zip
unzip 3.5.1.zip
cd protobuf-3.5.1
./autogen.sh
./configure
make
make install


# conda https://gist.github.com/mGalarnyk/05e4147b7fdfe4f94863e693644b43d9
cd ~/Downloads
wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh
bash Anaconda3-5.0.1-Linux-x86_64.sh -b -p ~/anaconda
rm Anaconda3-5.0.1-Linux-x86_64.sh
echo 'export PATH="~/anaconda/bin:$PATH"' >> ~/.bashrc 

# Refresh basically
source .bashrc

# py27
conda create --name py27 python=2.7
source activate py27
pip install numpy scipy scikit-image

# Clone the caffe repo
cd ~/Documents
git clone https://github.com/BVLC/caffe.git
# Setup Makefile.config
#   You can download mine directly from here, but I'll explain all the selections
#     For XCode 7.3:
#       https://www.dropbox.com/s/vuy6ha0p7cc5px3/Makefile.config?dl=1
#     For XCode 8.0 and later (Sierra):
#       https://dl.dropboxusercontent.com/u/2891540/caffe_10.12/Makefile.config
#   First, we'll enable cuDNN
#     USE_CUDNN := 1
#   In order to use the built-in Accelerate.framework, you have to reference it.
#   Astonishingly, nobody has written this anywhere on the internet.
#     BLAS := atlas
#     If you use El Capitan (10.11), we'll use the 10.11 sdk path for vecLib:
#       BLAS_INCLUDE := /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/Headers
#     Otherwise (10.12), let's use the 10.12 sdk path:
#       BLAS_INCLUDE := /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/Headers
#     BLAS_LIB := /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A
#   Configure to use system python and system numpy
#     PYTHON_INCLUDE := /System/Library/Frameworks/Python.framework/Headers \
#          /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include
#     PYTHON_LIB := /System/Library/Frameworks/Python.framework/Versions/2.7/lib
#   Configure to enable Python layers. Some projects online need this
#     WITH_PYTHON_LAYER := 1
curl https://gist.githubusercontent.com/vinthony/2d3315cd5b0018594539a1c4ad23756e/raw/3021dab860a670ba7bdda9ca9e74da000ff9478a/Makefile.config -o Makefile.config
# Download the XCode Command Line Tools for 7.3, since NVIDIA does not yet support Xcode 8.0's tools
#   http://adcdownload.apple.com/Developer_Tools/Command_Line_Tools_OS_X_10.11_for_Xcode_7.3/Command_Line_Tools_OS_X_10.11_for_Xcode_7.3.dmg
# Now, choose those tools instead
sudo xcode-select --switch /Library/Developer/CommandLineTools
# Go ahead and build.
cd caffe
make -j8 all
# To get python going, first we need the dependencies
# #   On a super-clean Mac install, you'll need to easy_install pip.
# sudo -H easy_install pip
# #   Now, we'll install the requirements system-wide. You may also muck about with a virtualenv.
# #   Astonishingly, --user is not better known. 
# pip install --user -r python/requirements.txt
# #   Go ahead and run pytest now. Horrible @rpath warnings which can be ignored.
make -j8 pytest
# Now, install the package
#   Make the distribution folder
# make distribute
# #   Install the caffe package into your local site-packages
# cp -r distribute/python/caffe ~/Library/Python/2.7/lib/python/site-packages/
# #   Finally, we have to update references to where the libcaffe libraries are located.
# #   You can see how the paths to libraries are referenced relatively
# #     otool -L ~/Library/Python/2.7/lib/python/site-packages/caffe/_caffe.so
# #   Generally, on a System Integrity Protection -enabled (SIP-enabled) Mac this is no good.
# #   So we're just going to change the paths to be direct
# cp distribute/lib/libcaffe.so.1.0.0-rc3 ~/Library/Python/2.7/lib/python/site-packages/caffe/libcaffe.so.1.0.0-rc3
# install_name_tool -change @rpath/libcaffe.so.1.0.0-rc3 ~/Library/Python/2.7/lib/python/site-packages/caffe/libcaffe.so.1.0.0-rc3 ~/Library/Python/2.7/lib/python/site-packages/caffe/_caffe.so
# # Verify that everything works
#   start python and try to import caffe
python -c 'import caffe'
# If you got this far without errors, congratulations, you installed Caffe on a modern Mac OS X 

`