How to Run and Test Core ML Models in a Swift Playground

So you’ve trained your Core ML model in Create ML or Turi Create. And now, you want to test it out on some real-world data. To make quick and dirty tests, you can leverage Swift Playgrounds and run Core ML models there. If you’re satisfied with the results, you can move over to Xcode project and run on-device or on a simulator.

In this tutorial, we’ll test a Core ML model in Swift Playgrounds.

Before beginning, I have a bit of bad news: You cannot directly use Core ML (.mlmodel) files in the Playground (there is an another option I will mention later in this post). Instead, you have to create its class and compiled mlmodelc files to use it.

Let see how to do that!

Drag and drop you Core ML model into an Xcode project and build your project:

After a successful build, right click on your .app file under the “Products” folder:

Right click your app and select “Show Package Contents”, which will open your bundle as a folder:

Find the mlmodelc folder. It will be named according to your Core ML file.

/// URL of model assuming it was installed in the same bundle as this class
//    class var urlOfModelInThisBundle : URL {
//        let bundle = Bundle(for: ObjectDetectionAccurate.self)
//        return bundle.url(forResource: "ObjectDetectionAccurate", withExtension:"mlmodelc")!
//    }
    class var urlOfModelInThisBundle: URL {
        return try! MLModel.compileModel(at: URL(fileURLWithPath:"/Users/ozgur/Library/Autosave Information/ThermDetector.playground/Resources/ObjectDetectionAccurate.mlmodel"))
    }

Create a Playground and make sure the “Navigator” is shown by clicking the button shown below:

Drag-drop this folder into the “Resources” folder in the Playground:

We also need to provide the Xcode generated Swift model class to the Playground. Open the Xcode project and select your mlmodel file and click the arrow next to it (below) to open this auto-generated class:

After opening this class, locate it in Finder by right clicking and then choose “Navigate->Show in Finder”:

Drag-drop this file into the “Sources” folder in the Playground. This will let us use our model through this auto-generated class:

If you try to create an instance of your model and run inference, you’ll get the error below:

The class definitions in this auto-generated Swift file have internal protection levels for classes and variables. You have to make them public to use them.

Open your model file and add a public keyword to the classes, functions, and variables:

After fixing protection levels, run your model, and you’ll see prediction results in Playgrounds:

Conclusion

In this post, we learned how to run Core ML models in Playgrounds, which make it easy to experiment with pre-trained models. This is yet another example of Apple lowering the ML entrance barrier for iOS developers. We should see a lot announced at the upcoming WWDC (June 22–26, virtual event)!

You can check out some Core ML playground samples shared on Github:

Thanks for reading!

If you liked this story, you can follow me on Medium and Twitter. You can contact me via e-mail.

Fritz

Our team has been at the forefront of Artificial Intelligence and Machine Learning research for more than 15 years and we're using our collective intelligence to help others learn, understand and grow using these new technologies in ethical and sustainable ways.

Comments 0 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

wix banner square