Initializer

init(trainingData:parameters:)

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

Declaration

init(trainingData: MLImageClassifier.DataSource, parameters: MLImageClassifier.ModelParameters = ModelParameters(validationData: [:])) throws

Parameters

trainingData

The labeled images that you use to train this model, contained in a data source.

parameters

Parameters that you use to configure model training.

Discussion

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])

Use the parameter structure and your training data to build a classifier. The following example uses training data from labeled directories within a directory called Training,which resides in the Downloads directory:

if let downloads = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first {
    let trainingURL = downloads.appendingPathComponent("Training")
    let classifier = try MLImageClassifier(trainingData: .labeledDirectories(at: trainingURL),
                                           parameters: parameters)
}

Training begins immediately.

See Also

Creating and Training an Image Classifier

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

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

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.