CoreML from Scratch

ObjectGraph
ObjectGraph
Published in
2 min readJul 19, 2017

CoreML is the new framework from Apple to easily incorporate machine learning models in to your iOS and macOS applications. There are a lot of examples of pre-trained models that could be easily incorporated by convering to the coreml format( .mlmodel ). Here I will show how to create a model, then export it to the CoreML format and use it in a Xcode project.

Fisher IRIS Data

Fisher IRIS data is the “Hello, World!” of Machine learning datasets. This dataset contains 50 observations of iris flowers attributes such as sepal length, sepal width, petal length, petal width and the species. You can read about the data set here.

We are going to create a model that will learn from this data and predict the species of iris based on the sepal — petal widths and heights.

Tools Needed

In order to train your model and convert to a format you could use in an Xcode project, you will need the following tools

  • Python (Download a prepackaged one like Anaconda )
  • scikit-learn library conda install scikit-learn
  • coremltools library pip install coremltools

Create IRIS model in SciKit-Learn

The IRIS data is already included in sklearn library. We just load the data, create a RandomForestClassifier and create the model. The following assumes that you know the model works for the type of classification you are doing. Normally you would create a training set, testing set and validate the model using a confusion matrix etc.

Converting

At this point the clf object contains the model , Using coremltools convert this model to a .mlmodel that you could use with in Xcode

Incorporating in Xcode

Create a new iOS Project (A simple single view application is sufficient). Now drag and drop the iris.mlmodel file to Xcode. Xcode will generate the swift stub classes necessary to interact with this model.

You can now simply call the prediction like below

The output will be as below

Sample iOS Application

Conclusion

CoreML makes it very easy to convert your existing models and bring them to iOS and macOS applications. Some of these .mlmodel files could be extremely large depending on what kind of analysis you are peforming.

--

--