iOS Image Classifier in 5 Minutes

By Carlos Gonzalez

Cat or Dog?

Let’s take 5 minutes to make an iOS image classifier to classify cats and dogs. Naturally, this can be extended to classify anything, but come on, cats and dogs are a cute start.

The first thing we’re going to do is make two folders on our desktop: one titled “Training Data” and the other titled “Testing Data”. Make sure that the spelling on these two folders, and all of the following folders, match exactly mine or you will encounter some cryptic error messages.

Make sure these match exactly

Inside both folders make two folders — one titled “Cat” and one titled “Dog”. Now here’s the fun part: fill both “Cat” folders with cat pictures from google and both “Dog” folders with dog pictures. Try to have at least twice as many pictures in your Training Data than in your Testing Data. I had 20 cats in my Training Data Cats folder and 10 cats in my Test Data Cats folder, and likewise for the Dog folders.

What the folder structure should look like

Once we have the fun part done, open up a playground on Xcode, select Mac OS, start a blank swift file, and write the only 3 lines of code (seriously) that are needed to start training the classifier.

import CreateMLUI
let imageBuilder = MLImageClassifierBuilder()

Once you run the code, open the assistant editor and you should get something similar to this:

3 lines of code…

Drag and drop the “Training Data” folder into the box titled “Drop Images To Begin Training.” You’ll see the images you had in your training folder start to flash as the classifier trains. Once the classifier finishes training, drop in your “Test Data” folder to test it out!

I got 95% right 😏

And that’s it! If you want to export your model just click on the down arrow right above your evaluation score and you can use it on any iOS or MacOS project.

If you have any questions, let me know below!