Training a model in an iOS application

Omar M’Haimdat
Jul 13 · 3 min read
Final result of the application

Yes, that’s right. I built an iOS app that looks at a picture and decides whether it’s couscous or not couscous.


Let’s Start With the App Layout

We need to create an iOS project with a single view app:

Create a single view app

Now you have your project. I don’t like using Storyboard, so the app is made programmatically, which means no buttons to toggle or switches — just pure code.

You have to delete the main.storyboard and set your AppDelegate.swift file like this:

Make sure that you have removed the storyboard “Main” from the deployment info.

Main ViewController

Now let’s set our ViewController with the buttons and logo. I used some custom buttons. First, you need to inherit from UIButton and create your own custom button:

Button.swift
BtnPlein.swift
BtnPleinLarge.swift

BtnPleinLarge is our new button, we use it to create our main two buttons for ViewController.swift, our main view.

Set the layout and buttons with some logic as well:

Buttons: Upload a Picture and Take a Picture

Button Upload

We now need to set some logic. It’s important to change the Info.plist file and add a property so that an explanation of why we need access to the camera and the library is given to the user. Add some text to the “Privacy — Camera Usage Description”:

Setting the image picker controller to access the photo library
Setting the image picker controller to access the camera and take a picture

Of course, you need to set up the layout and add the subviews to the view too. I’ve added a logo on top of the view as well:

Setup the view layout

Secondary ViewController: Where We Show Our Results

We need three things:

  1. Our original image
Original image

2. A button to dismiss the view:

Button to dismiss the view with the logic

3. And a picture that either says “Couscous” or “Not Couscous”:

Image that says “Couscous” or “Not Couscous”

Don’t forget to add the subviews to the main view and set up the layout too.

Train the Model

There are two ways to train the model. The first is through Xcode and the second is through a custom Python script (use the turicreate package). The most important thing is to get a .mlmodel file that can be parsed by Xcode. We can take advantage of the way Xcode uses the .mlmodel file to obtain confidence and use it to update our secondary ViewController with the appropriate “Couscous” or “Not Couscous” image.

Since our model is binary, we need to train the model based on pictures that are clearly couscous (in this case I used pictures with Moroccan couscous) and pictures that are not couscous as well.

I used 51 pictures for “Couscous” and 68 pictures for “Not Couscous”. You can find the images I used in the repository.

Let’s Start

Open playground and write this code:

Create a model

When you run it, you should see something similar to this picture:

Drag to the folder that contains the folders “couscous” and “NotCouscous”. Now let the builder classify the images.

You’ll have to save the .mlmodel file and drop it into the Xcode project, Xcode will do the rest (parsing the file, etc.)

Some Classifier Logic

Now we need to add the delegate for the ImagePickerController in our main ViewController:

Creating an app and training a model is that simple!

Final Result

Final result

This project is available to download from my Github account.

Better Programming

Advice for programmers.

Omar M’Haimdat

Written by

Software Engineering Student.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade