init(trainingData:parameters:)
Creates an image classifier with a training dataset represented by a data source.
Declaration
init(trainingData: MLImageClassifier.DataSource, parameters: MLImageClassifier.ModelParameters = ModelParameters(
validation: .split(strategy: .automatic),
augmentation: [],
algorithm: .transferLearning(
featureExtractor: .scenePrint(revision: 1),
classifier: .logisticRegressor
)
)) throwsParameters
- trainingData:
A set of labeled images the task uses to train the image classifier model, contained in a data source.
- parameters:
An Modelparameters Swift.struct instance you use to configure the model for the training session.
Mentioned in
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 dataset 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 values other than the default.
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.