Implementing a Machine Learning Model Using the CoreML Framework – Kevin

Recently, I have been experimenting with CoreML, the machine learning framework for Apple’s mobile and desktop operating systems. Rather than continue my discussion of linear regression, I will detail the implementation of a model with CoreML in this blog post.

What is CoreML?

CoreML is a machine learning framework developed by Apple and implemented on iOS 11, macOS 11.13, watchOS 4, and tvOS 11. It allows developers to integrate trained machine learning models into their mobile applications. CoreML leverages Accelerate, BNNS (Basic neural network subroutines), and MPS (Metal Performance Shaders) to provide the model with near direct access to device hardware. The major benefit of using CoreML is that it enables on-device machine learning, meaning that the apps can remain operational and responsive when reliable Internet connection is absent. It also guarantees user privacy because no data is being sent to a remote server.

Implementing CoreML

The first step is to obtain a CoreML model. The CoreML framework requires the use of a CoreML model (with .mlmodel extension). The Apple Developer website offers a variety of popular trained models that are already in the CoreML format. It is also possible to convert trained model in other formats into the CoreML format using the Python package coremltools available at the Python Package Index. It is important to note that for natural language processing and certain computer vision applications, iOS already offers NSLinguisticTagger and the Vision framework.

In our case, we will use MarsHabitatPricer.mlmodel, a sample regression model trained by Apple to “predict habitat prices on Mars,” (“Integrating a Core ML Model into Your App”). You can download the entire project here. After downloading the CoreML model, it should appear as a file with the extension “.mlmodel”. Create a new Xcode project and drag and drop the file into the project manager view on the left.

Once the file has been added, Xcode will automatically generate a Swift model class and can be used with the following code:

let model = MarsHabitatPricer()

When clicking on the MarsHabitatPricer.mlmodel file from the project manager view on the left, we can see the details about the model which include the input and output parameters. In our case, we can see following input and output parameters:

Screen Shot CoreML
Accordingly, add the following code to initialize the inputs and outputs:

var price = 0.0
var solarPanels = 0.0
var greenhouses = 0.0
var size = 0.0
//Make sure to use “0.0” as opposed to “0” because the inputs have the type “double”

Use the following line to pass the inputs into the model and obtain the output.

guard let MHPOutput = try? model.prediction(solarPanels: solarPanels, greenhouses: greenhouses, size: size) else {

Note that it is always good practice to use the guard statement to catch errors.
Finally, use the following code to readout the output of the model:

price = MHPOutput.price

UI elements and then be added to the storyboard to allow user inputs.

Congratulations, you have created your first CoreML app! In the next blog post, I will resume my discussion of regression models.

Works Cited

Apple Inc. “Converting Trained Models to Core ML.” Apple Developer, Apple, Accessed 16 Oct. 2017.

—. “Core ML.” Apple Developer, Apple, Accessed 16 Oct. 2017.

—. Core ML Logo. Apple Developer, Apple, Accessed 17 Oct. 2017.

—. “Integrating a Core ML Model into Your App.” Apple Developer, Apple, Accessed 16 Oct. 2017.

—. “VNCoreMLRequest.” Apple Developer, Apple, Accessed 16 Oct. 2017.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s