Structure

MLImageClassifier

A model you train to classify images.

Declaration

struct MLImageClassifier

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 elephants and giraffes, in two folders labeled Elephant and Giraffe, 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 elephant and giraffe classifier mistakes a giraffe for an elephant. 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 an Image Classifier

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 an image classification model.

let modelParameters: MLImageClassifier.ModelParameters

The configuration parameters that the image classifier used for training during initialization.

Assessing Model Accuracy

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.

Evaluating an Image Classifier

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

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

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

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

Testing an Image Classifier

func prediction(from: CGImage) -> String

Generates a prediction for given image.

func prediction(from: URL) -> String

Generates a prediction for the image at the given URL.

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

Generates predictions for given the given array of images.

Saving an Image Classifier

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

Exports the image classifier as a Core ML model file at the given URL.

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

Exports the image classifier as a Core ML model file at the given file path.

Describing an Image Classifier

var model: MLModel

The underlying Core ML model of the image classifier stored in memory.

var description: String

A text representation of the image classifier.

var debugDescription: String

A text representation of the image classifier that’s suitable for output during debugging.

var playgroundDescription: Any

A description of the image classifier shown in a playground.

Building an Image Classifier in a Playground

class MLImageClassifierBuilder

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

Deprecated

See Also

Computer Vision

Creating an Image Classifier Model

Train a machine learning model to classify images, and add it to your Core ML app.

struct MLObjectDetector

A model you train to detect objects within an image.

Beta