← Back

Working With Core ML Models in Swift Playgrounds

May 17, 2020

Swift Playgrounds has become an amazing way to try out new ideas without having to worry about the overhead of a real app. One framework that works better in a real project in Xcode though is Core ML. Occasionally you might still need to integrate a Core ML model into a playgrounds book. Here's how to do so:

Generating the Interface

To make interacting with a model easier, Xcode automatically generates interfaces for models. Playgrounds can't generate these interfaces so you'll need to let Xcode generate the interfaces:

Loading the Actual Model File

Since the generated interface expects a compiled version of the model to be in the app bundle, these lines will have to be replaced in the in the playground:

class var urlOfModelInThisBundle: URL {
	let bundle = Bundle(for: ExampleModel.self)
	return bundle.url(forResource: "ExampleModel", withExtension:"mlmodelc")!
}

Replace these lines with the following ones:

class var urlOfModelInThisBundle: URL {
	return try! MLModel.compileModel(at: [file])
}

Replace [file] with a Playground file literal of the original model file.

Unlike Xcode, Playgrounds doesn't automatically compile the Core ML model and while you could add a compiled version of the model (.modelc), the Playgrounds app really doesn't like these files. Instead of pre-compiling it, the playground now compiles the model when Core ML needs a compiled version. This can result in worse performance, but performance isn't a strength of Playgrounds anyways.

Notes

By using the process described in this post, you can add a model to your playground but the entire process tedious. I'd really appreciate an easier way to train and use these models on the iPad.