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


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



The labeled images that you use to train the model. The keys of this dictionary represent the labels, while the values hold arrays of images that correspond to the given label.


Parameters that you use to configure model training.


When you create an MLImageClassifier instance, initialize it with an MLImageClassifier.ModelParameters structure. This allows you to configure the image classifier training process. For example, you can explicitly define the validation data set instead of allowing the model to choose a random selection of your training data. Alternatively, as shown in the following example, set validationData to nil to allow the classifier to choose the validation data for you from among your training data. This lets you set other parameters—like maximum iterations and augmentation options—to non-default values:

let parameters = MLImageClassifier.ModelParameters(featureExtractor: .scenePrint(revision: 1),
                                                   validationData: nil,
                                                   maxIterations: 20,
                                                   augmentationOptions: [.crop])

For this particular initialization method—there’s another with the same signature but a different training data type—represent your training data with a dictionary that uses labels as keys. The corresponding values are arrays of URLs that indicate the images associated with that label. In this example, if you have elephant and giraffe images stored in a directory called Training within your Downloads directory, you can construct a dictionary with the URL of each image:

// Get the URL of the directory that holds the validation data.
guard let downloadsURL = FileManager.default.urls(for: .downloadsDirectory, 
                                                  in: .userDomainMask).first
    else { fatalError("Can't find Downloads directory") }
let url = downloadsURL.appendingPathComponent("Training")

// For a real classifier, use at least 10 images per label. More is better.
let trainingData = ["Elephant": [url.appendingPathComponent("Elephant.1.jpg"),
                    "Giraffe": [url.appendingPathComponent("Giraffe.1.jpg"),

Use the parameter structure and training data to initialize the classifier. Training begins immediately.

let classifier = try MLImageClassifier(trainingData: trainingData,
                                       parameters: parameters)

See Also

Creating and Training an Image Classifier

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.