source on github
source on github
This will be a quick tutorial on how to set up a python environment on your mac to enable you to use the coremltools package to convert models from various formats (Caffe, Keras, Libsvm, etc.) to the coremlmodel specification. Once you have the coremlmodel, you can drag and drop it into Xcode to use in your iOS apps.
First, if you haven't already downloaded XCode 9, you can do so here.
The coremltools package requires python version 2.7. There are a couple ways to set up the python environment. The first way is to use pip and virtualenv. The other way is to install Anaconda to manage your python environments. I would recommend using conda, simply because as someone new to python, I found it very user-friendly to work with.
To start with Anaconda, get the python 2.7 installer from Continuum.
Once installed, in a terminal, run $conda info -e to view your environments. A default environment will be set up for you, but we want to create a new environment to manage all of our specific dependencies needed for coremltools.
Use $conda search <package> to print out package details.
The docs for coremltools list these dependencies:
numpy (1.12.1+)
protobuf (3.1.0+)
I started my environment by adding numpy version 1.13.
$ conda create -n myenv anaconda numpy=1.13
In the terminal, confirm to install all required dependencies for the new package.
To activate the new environment: $source activate myenv.
Any other packages you install will now install into your active environment.
Install coremltools: $pip install coremltools
Install ipykernel: $pip install ipykernel
To set up your Jupyter kernel for your environment: $python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
You can now navigate to the directory where you want to store your Jupyter notebook. To start the notebook server, with your environment active, run: $jupyter notebook
The url of the web app will be printed along with some information about the server. Copy the url into your browser and you should see the new kernel available.
You are ready to use coremltools! The demo presented at WWDC17 converted a caffe model for flower classification into a coremlmodel. You can watch the core ml session here to see the demo: https://developer.apple.com/videos/play/wwdc2017/710/
Here is the model.
You will also need the prototxt and the labels