MAC 深度学习环境搭建手记

近期在研究各种模型与coreml之间的转变,每个模型都依赖一堆不同的数据包,配置大量信息,今天决定把我的环境清理一下,从python开始从新搭建,在此记录一下过程。

brew

brew 是 Mac 下的一个包管理工具,可以很方便地进行安装/卸载/更新各种软件包。

ruby -e "$(curl -fsSL (https://raw.githubusercontent.com/Homebrew/install/master/install))"
Python3.7 安装

coreml目前不支持python3.7

brew install python
Python2.7 安装
brew install python@2
PyTorch 1.0 安装
pip install torch_nightly -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html

安装torchsummary

pip install torchsummary
coremltools 安装

coremltools 依赖以下库:

numpy (1.12.1+)

brew install numpy

protobuf (3.1.0+)

brew install protobuf

coremltools 安装

 pip install -U coremltools
onnx 安装
pip install -U onnx
pip install -U onnx-coreml

如转换遇到问题,可尝试使用source安装onnx-coreml

git clone --recursive https://github.com/onnx/onnx-coreml.git
cd onnx-coreml
./install.sh
PyTorch->onnx->coreml转换测试

这里使用touch.torchvision提供的alexnet预训练模型做测试

import torch.onnx

from torch.autograd import Variable
import torch.onnx
import torchvision

import onnx
import onnx_coreml
from coremltools.proto import NeuralNetwork_pb2

dummy_input = Variable(torch.randn(1, 3, 224, 224))
# Obtain your model, it can be also constructed in your script explicitly
model = torchvision.models.alexnet(pretrained=True)
# Invoke export
torch.onnx.export(model, dummy_input, "alexnet.onnx")

# Load the ONNX model
model = onnx.load("alexnet.onnx")
# Check that the IR is well formed
onnx.checker.check_model(model)

cml = onnx_coreml.convert(model,image_input_names=['0'])
cml.save('output/alexnet.mlmodel')
jupyter notebook安装
brew install jupyter
使用OpenBLAS集成编译numpy

首先安装openblas

$ brew install openblas
$ cd ~/github
$ git clone git://github.com/numpy/numpy.git numpy
$ cd numpy
$ cp site.cfg.example site.cfg

编辑site.cfg

[ALL]
library_dirs = /usr/local/opt/openblas/lib
include_dirs = /usr/local/opt/openblas/include
[atlas]
library_dirs = openblas
include_dirs = openblas
[openblas]
libraries = openblas
library_dirs = /usr/local/opt/openblas/lib
include_dirs = /usr/local/opt/openblas/include
runtime_library_dirs = /usr/local/opt/openblas/lib

编辑 ~/.bash_profile 添加

export LDFLAGS=-I/usr/local/opt/openblas/lib
export CPPFLAGS=-L/usr/local/opt/openblas/include
export PKG_CONFIG_PATH=/usr/local/opt/openblas/lib/pkgconfig

执行

python setup.py build && python setup.py install

出现错误提示

fatal error: 'endian.h' file not found

首先需要安装Command Line Tools (macOS 10.X) for XCode 10.X

xcode-select --install

MacOS 10.14之前版本,同时会自动生成"/usr/include"文件, 包括 "/usr/include/machine/endian.h".
MacOS 10.14起,文件不会自动升成,需要手动安装 /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

如果出现权限不足错误

Adding numpy 1.16.0.dev0+11a316e to easy-install.pth file
error: [Errno 13] Permission denied: '/usr/local/lib/python2.7/site-packages/easy-install.pth'

可以改用

sudo python setup.py build && sudo python setup.py install
opencv安装
brew install opencv
matplotlib安装

Matplotlib 是 Python 的绘图库

pip3 install matplotlib
tensorflow安装
pip3 install tensorflow
keras安装
pip3 install keras
PIL安装

PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库

pip3 install -U Pillow
requests
pip3 install requests

你可能感兴趣的:(MAC 深度学习环境搭建手记)