Structure

MLImageClassifier

A model you train to classify images.

Overview

Use an image classifier to train a machine learning model that you can include in your app to categorize images.

When you create the model, you give it a training data set made up of labeled images, along with parameters that control the training process. For example, you can provide the model with images of dogs and cats, in two folders labeled dogs and cats, to train it to recognize these animals.

After training completes, you evaluate the trained model by showing the model a testing data set containing labeled images that the model hasn’t seen before. The metrics that come from this evaluation tell you whether the model performs well enough. For example, you can see how often the cat and dog classifier mistakes a cat for a dog. When the model makes too many mistakes, you can add more or better training data, or change the parameters, and try again.

When your model does perform well enough, you save it as a Core ML model file with the mlmodel extension. You can then import this model file into an app—like the Classifying Images with Vision and Core ML sample code project—that uses a Core ML model file to classify images.

Topics

Creating and Training a Model

init(trainingData: [String : [URL]], parameters: MLImageClassifier.ModelParameters)

Creates a classifier from a training set represented by a dictionary.

init(trainingData: MLImageClassifier.DataSource, parameters: MLImageClassifier.ModelParameters)

Creates a classifier from a training set represented by a data source.

enum MLImageClassifier.DataSource

A data source for an image classifier.

struct MLImageClassifier.ModelParameters

Parameters that affect the process of training a model.

Evaluating a Model

var trainingMetrics: MLClassifierMetrics

Measurements of the classifier’s performance on the training data set.

var validationMetrics: MLClassifierMetrics

Measurements of the classifier’s performance on the validation data set.

func evaluation(on: [String : [URL]]) -> MLClassifierMetrics

Returns metrics describing the classifier’s performance on labeled data provided in a dictionary.

func evaluation(on: MLImageClassifier.DataSource) -> MLClassifierMetrics

Returns metrics describing the classifier’s performance on labeled data provided in a data source.

Testing a Model with Unlabeled Data

func prediction(from: CGImage) -> String

Classifies the given image.

func prediction(from: URL) -> String

Classifies the image at the given URL.

func predictions(from: [URL]) -> [String]

Classifies a collection of images.

Saving a Model

func write(to: URL, metadata: MLModelMetadata?)

Exports a Core ML model file for use in your app.

func write(toFile: String, metadata: MLModelMetadata?)

Exports a Core ML model file for use in your app.

struct MLModelMetadata

Information about a model that’s stored in a Core ML model file.

Inspecting a Model

var model: MLModel

The underlying Core ML model stored in memory.

let modelParameters: MLImageClassifier.ModelParameters

The configuration parameters that were used to train the model during initialization.

var description: String

A text representation of this instance.

var debugDescription: String

A text representation of this instance that’s suitable for output during debugging.

var playgroundDescription: Any

A description of an image classifier shown in a playground.

See Also

Computer Vision

Creating an Image Classifier Model

Train a machine learning model to classify images.

class MLImageClassifierBuilder

An Xcode playground UI that you use to train a model to classify images.

Beta

Beta Software

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.

Learn more about using Apple's beta software