The world’s leading publication for data science, AI, and ML professionals.

Create ML: Deploy the model to an iOS App

Let's create a simple Dog vs Cat iOS app!

A couple of days back I’ve published the first of two articles in Create ML section, and that one dealt with the process of gathering data and model training. Today we’ll have much more fun, as by the end of the article you’ll have an app on your iPhone (or a simulator) capable of classifying Dogs and Cats!

Photo by Anthony Choren on Unsplash
Photo by Anthony Choren on Unsplash

Note from the author: I’m not an iOS developer, and this article probably won’t demonstrate the cleanest of solutions. However, it still works flawlessly, and I find it worth sharing with my fellow readers.

Just in case you’ve missed the first article, you can find it here:

Create ML: A GUI for training Neural Networks

Keep in mind that you don’t have to read it entirely, as the model file is available in the official GitHub repo. Nevertheless, I strongly encourage you to do so, as you’ll get an idea of how easy it is to train image classifiers nowadays.

The prerequisites for following along are still harsh:

  1. You’ll need to have a macOS machine (or use MacInCloud.com)
  2. The latest version of Xcode is a must
  3. You should know a bit about iOS development (directory structure, how to run the app…)

It’s cool if you don’t have an iPhone since the app can be run on the simulator. Before jumping into code let’s take a quick look at the final version of the app, just to get you motivated:

The article is structured as follows:

  1. iOS project creation
  2. The layout
  3. The code
  4. Let’s run it
  5. Conclusion

So, without further ado, let’s get started!


iOS project creation

Fire up the Xcode and select Create a new Xcode project. From there, select Single View App:

This will open a new window. Here you can specify the app name – do so and leave everything else as default. Click on Next:

The last step is to store the application somewhere – you decide where’s the optimal location:

The project will now be created. The last step we need to do here is to include our model into the app. To do so, simply drag the .mlmodel file to the project files:

And that’s all for this section. In the next one, we’ll create the layout for the app.


The layout

The layout is literally as simple as it can be – and we’ll be designing it in the Main.storyboard file. My decision was to wrap the view in the Navigation Controller so we can have the camera button in the navigation bar.

Also, the navigation bar has a title – Dog or Cat?

Further, in the content section, we have a basic UIImageView which will display an image, and below it, we have a Text View to display the predicted class. You don’t see it because it is blank by default, and will get populated once we select an image.

Here’s the screenshot of what you should have in the end:

If you have some experience in developing iOS apps, constructing UI simple like this won’t be a problem. I decided not show the process go step by step because the article would get really messy really soon. You are more than welcome to clone the official repo and use it for reference if you get stuck.

Okay, if you’re following along by now you should have the layout part completed. In the next section, we’ll start to write some code and also to manage permissions.


The code

Before writing any Swift code, we’ll need to get Photo Library permission from the user – go to the Info.plist and add it as follows:

That’s it, now the user can give us permission to access his/her images. We are now ready to write some Swift code!

I’m by no means an expert in Swift, just the opposite, I consider myself to be an absolute beginner at it, but I found this code to work perfectly. Maybe it’s not written with having best practices in mind, but it will suit our needs.

Here are the steps we need to perform (well, the main steps):

  1. Import Libraries
  2. Declare variables for our views
  3. Show image picker when the camera button is pressed
  4. Handle image picker
  5. Declare a function for image classification
  6. Link detect function to the image picker controller

The entire code must be written in ViewController.swift file. You’ll find the code in a gist below with steps written as comments:

And that’s pretty much it with regards to the code! In the next section, we’ll run the app (but I guess you already know how it performs – if you’ve watched the video demo).


Let’s run it

Now we can actually pick on which device we’ll run the app. If you don’t have an iPhone you can select any emulator from the dropdown menu:

It will take a minute or so to compile and run once you press the Start button. On the simulator, I’ve gone online and downloaded some royalty-free images of dogs and cats.

Once you do the same, open the app to test it. Here are the obtained classifications from my device:

Yes, we do have a lot of unused whitespaces and the design isn’t perfect – but this is a Data Science-related article, not UI design related one. Anyway, that’s it for today, let’s just quickly summarize everything in the next section.


Conclusion

Am I an iOS developer? Most certainly not. Is there room for improvement, design and code quality-wise? Absolutely.

But, as data scientists we are not obligated to be great at UI design. I mean it’s great is you possess these skills, but it’s not mandatory.

The one and only goal behind this and the previous Create ML articles was to spice things up and get you (and me) outside of the comfort zone. It’s one thing to code out the algorithm and spend weeks doing so – but what’s the purpose if only you know how to use it? What’s the point if there’s no good way to actually present your work?

Take a moment to think about that. It doesn’t have to be a mobile app, as a simple web application with an upload component would be just fine for this project.

Thanks for reading.

P.S. Let me know if the model misclassifies some images for you, as every single one I’ve tested was classified correctly.


Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.

Join Medium with my referral link – Dario Radečić


Related Articles