Overview

Adding a Model to Your Xcode Project

Add the model to your Xcode project by dragging the model into the project navigator.

You can see information about the model—including the model type and its expected inputs and outputs—by opening the model in Xcode. The inputs to the model are the number of solar panels and greenhouses, as well as the lot size of the habitat (in acres). The output of the model is the predicted price of the habitat.

Creating the Model in Code

Xcode also uses information about the model’s inputs and outputs to automatically generate a custom programmatic interface to the model, which you use to interact with the model in your code. For MarsHabitatPricer.mlmodel, Xcode generates interfaces to represent the model (MarsHabitatPricer), the model’s inputs (MarsHabitatPricerInput), and the model’s output (MarsHabitatPricerOutput).

Use the generated MarsHabitatPricer class’s initializer to create the model:

let model = MarsHabitatPricer()

Getting Input Values to Pass to the Model

This sample app uses a UIPickerView to get the model’s input values from the user:

Using the Model to Make Predictions

The MarsHabitatPricer class has a generated prediction(solarPanels:greenhouses:size:) method that’s used to predict a price from the model’s input values—in this case, the number of solar panels, the number of greenhouses, and the size of the habitat (in acres). The result of this method is a MarsHabitatPricerOutput instance, marsHabitatPricerOutput.

The generated prediction(solarPanels:greenhouses:size:) method can throw an error. The most common type of errors you’ll encounter in working with Core ML occur when the type of input data you pass to the method doesn’t match the type of input the model is expecting—for example, an image in the wrong format. In this sample app, the inputs are of type Double. Any type mismatches are caught at compile time, and the sample app raises a fatal error if something goes wrong.

Building and Running a Core ML App

Xcode compiles the Core ML model into a resource that’s been optimized to run on a device. This optimized representation of the model is included in your app bundle and is what’s used to make predictions while the app is running on a device.

See Also

First Steps

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.