Use Core ML to integrate machine learning models into your app. Core ML provides a unified representation for all models. Your app uses Core ML APIs and user data to make predictions, and to train or fine-tune models, all on the user’s device.

Core ML optimizes on-device performance by leveraging the CPU, GPU, and Neural Engine while minimizing its memory footprint and power consumption. Running a model strictly on the user’s device removes any need for a network connection, which helps keep the user’s data private and your app responsive.

MLModel Overview

An MLModel encapsulates a Core ML model's prediction methods, configuration, and model description. You can use the coremltools package to convert trained models from a variety of training tools into Core ML models. For the full list of model types, see Model.

The converters in coremltools return a converted model as an MLModel object. You can then save the MLModel as an .mlmodel file, use it to make predictions, modify the input and output descriptions, and set the model's metadata. The MLModel class defines the minimal interface to a Core ML model in Python.

The Core ML specification

A key component of Core ML is the public specification for representing machine learning models. This specification is defined in the Model.proto protobuf (for a more readable version, see Model), which can be created using any language supported by protobuf (such as Python, C++, Java, C#, and Perl).

At a high level, the protobuf specification consists of the following:

  • Model description: The model name and and the types of input and output.
  • Model parameters: The set of parameters required to represent a specific instance of the model.
  • Metadata: Information about the origin, license, and author of the model.

Use cases

The following code snippets demonstrate how to use an MLModel. To use these code snippets, first import coremltools as follows:

import coremltools as ct

Load and save the MLModel

# Load the MLModel
mlmodel = ct.models.MLModel('path/to/the/model.mlmodel')

# Save the MLModel'path/to/the/saved/model.mlmodel')

Use the MLModel for prediction

# Use the MLModel for prediction

Work with the spec object

The spec object is the parsed protobuf object of the Core ML model. The following code snippets show how you can get the spec from the MLModel, print its description, get the type of MLModel, and save the MLModel directly from the spec. It also shows how to convert the spec to MLModel and compile the model in one step:

# Get the spec from the MLModel
spec = mlmodel.get_spec()

# Print the input/output description for the MLModel

# Get the type of MLModel (NeuralNetwork, SupportVectorRegressor, Pipeline etc)

# Save out the MLModel directly from the spec
ct.models.utils.save_spec(spec, 'path/to/the/saved/model.mlmodel')

# Convert spec to MLModel. This step also compiles the model.
mlmodel = ct.models.MLModel(spec)

# Load the spec from the saved .mlmodel file directly
spec = ct.models.utils.load_spec('path/to/the/model.mlmodel')

Update the metadata and input/output descriptions

The following example converts a model from Scikit-learn. The model predicts the price of a house based on three features (bedroom, bath, size).

The example shows how you can use the MLModel object returned by the conversion to update the metadata and input/output descriptions that are displayed in the Xcode preview. (For details about the Xcode preview, see Xcode Model Preview Types.

from sklearn.linear_model import LinearRegression
import pandas as pd

# Load data
data = pd.read_csv('houses.csv')

# Train a model
model = LinearRegression()[["bedroom", "bath", "size"]], data["price"])

# Convert and save the scikit-learn model
import coremltools as ct

model = ct.converters.sklearn.convert(model, ["bedroom", "bath", "size"], "price")

# Set model metadata = 'John Smith'
model.license = 'BSD'
model.short_description = 'Predicts the price of a house in the Seattle area.'
model.version = '1'

# Set feature descriptions manually
model.input_description['bedroom'] = 'Number of bedrooms'
model.input_description['bathrooms'] = 'Number of bathrooms'
model.input_description['size'] = 'Size (in square feet)'

# Set the output descriptions
model.output_description['price'] = 'Price of the house'

# Save the model'HousePricer.mlmodel')

Updated about a month ago

MLModel Overview

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.