About

Nov. 12th 2014

In this tutorial, we will learn how to use the TuriCreate API offered by Apple to differentiate between cats and dogs by creating an image recognition app of our own using a custom dataset.

We will be training our own machine learning model using images of cats and dogs, and exporting it for use in IOS devices.

This tutorial is designed with begginers in mind, who are interested in learning how to use the API for image recognition purposes.

Why turicreate?
Turicreate, now owned by Apple, makes the machine learning process extremely simple. It does a lot of laborous work for us, such as resizing images, and training/testing the model. This is perfect for beginners and app developers who do not want to spend time learning the implementation details of machine learning and convolution networks.

Installation

Nov. 12th 2014

For this project, we would be needing:
1. A computer running mac OS.
2. Basic understanding of Python or any programming language
3. Any version of python >= 2.7.14 which can be found here
4. A version of Xcode version >= 9.0
5. An installation of PIP.
PIP is a tool for managing and installing python packages. It can be installed by opening the terminal and typing:

sudo easy_install pip

Lets go ahead and install TuriCreate by typing the following command in your terminal:

pip install turicreate

Now we would need something to make the image downloading process simpler for us. We will be using google-images-download by github user Hardikvasa. To install this, type in the command:

pip install google_images_download

Congratulations! We are done with the installations, so let's go ahead and get started.

Getting our dataset

by sneaky pranjit on Nov. 12th 2014

The first step for us is to gather our dataset. A machine learning model can only be as good as the data it is trained on. If the data is skewed, the output results from the machine learning model also have a high chance of being incorrect.

In your command line, enter the first two lines one after the other, and read the comments (lines starting with #) to understand what each of these lines does.

For further information on how the google image downloader works, click on the link above in orange.

Let's get training

Open a new command line and enter into it:

cd Desktop
python

the first line changes the directory to the desktop so that your file will be saved here.

The second line allows you to write python code in the command line itself.

In your command line, enter the code lines of code one by one (lines not beginning with a #), and to understand what the code does, read the comments (Lines beggining with #)

Just give me the code already!

Okay here you go.

You can ignore those errors you get importing images, some images can be corrupted or of wrong format.

Here is some output to guide you:

Line 17:
Line 24-31:
Line 34:

Great! You have just trained your first machine learning model that can be used in IOS apps. After you have understood what this code does, you can just copy and paste the whole thing at once if you have to reuse it in the future.

To the app!

For testing purposes, we will use the demo app provided by Apple.This demo app can be downloaded here.

unzip the project, and copy the catDog.ml model that we just created into the folder:

vision +ML Example/Model

Now open up the file ending with .xcodeproj

We now have to add our catDog.ml model to the build phases as shown below

Now navigate to ImageClassificationViewController.swift
and change line number 30 to:

let model = try VNCoreMLModel(for: catDog().model)

Run the app on your device, and it should be working.

Congrats on making your first machine-learning IOS app!