Cannot Set Validation Data Directory for Create ML

I want to use MLImageClassifier from Create ML, and set the model parameters to include a directory containing validation data.


A simple example setting only the parameter with the maximum number of iterations works fine:


let trainDirectory = URL(fileURLWithPath: "/Users/gordon/Documents/TrainingData")

let modelParameters = MLImageClassifier.ModelParameters(maxIterations : 100)

let model = try MLImageClassifier(trainingData: .labeledDirectories(at: trainDirectory), parameters: modelParameters)


But if I add the validation directory (for simplicity I have removed the maximum number of iterations) by:


let trainDirectory = URL(fileURLWithPath: "/Users/gordon/Documents/TrainingData")

let validationDirectory = URL(fileURLWithPath: "/Users/gordon/Documents/ValidationData")

let modelParameters = MLImageClassifier.ModelParameters(validationData: validationDirectory) // ERROR HERE

let model = try MLImageClassifier(trainingData: .labeledDirectories(at: trainDirectory), parameters: modelParameters)

I get an error on the third line which is trying to set the validationData parameter:


Cannot convert value of type 'URL' to expected argument type '[String : [URL]]?'


Am I doing something wrong, or is this a bug in the beta?


Thanks in advance.

Replies

UPDATE - same error in Xcode Version 10.0 beta 2 (10L177m), running under macOS Mojave Version 10.14 Beta 2 (18A314h).

Hi Tehoro,


When initializing ImageClassifier ModelParameters, you can pass validation data either in the form of a Dictionary with String keys corresponding to labels and arrays of image URLs corresponding to samples, or with a DataSource. If the URL to your validation data contains images in a hierarchical structure, you can do this via the following:


let modelParameters = MLImageClassifier.ModelParameters(validationData: .labeledDirectories(at: validationDirectory))


Similarly, you could also construct a Dictionary of image labels to arrays of image URLs and pass that directly. The two init methods on ImageClassifier.ModelParameters are pasted below for reference.


/// Describes optional model training parameters to set.

public struct ModelParameters {


public init(featureExtractor: FeatureExtractorType = default, validationData: [String : [URL]]? = default,

maxIterations: Int = default, augmentationOptions: ImageAugmentationOptions = default)


public init(featureExtractor: FeatureExtractorType = default, validationData: DataSource,

maxIterations: Int = default, augmentationOptions: ImageAugmentationOptions = default)

}