Training the Classifier

After you’ve finished setting up, you’re ready to dive into the meat of this tutorial — actually training your classifier. We’ll be working mostly in Python, but if you’ve never used Python before, that’s okay. I’ll explain each step as we go along, and if you have any questions, don’t hesitate to leave a comment below.

Python File

First, we’ll need to have a place to put down our thoughts (that is, of course, in Python). If you already have an editor that supports Python, such as Atom or an integrated development environment such as PyCharm, you can use them to create a blank file called dog_breeds.py .

If you prefer the more developer-y route, like me, you can use the Terminal to do the same thing. You’ll need to create this file inside your ml_classifier folder, alongside the images folder so that your hierarchy looks like this:

Figure 3: Folder Structure with Python File

To create a new file, first enter into the target directory:

$ cd ml_classifier

Then, create a new file named dog_breeds.py .

$ touch dog_breeds.py

Voilà! Your folders, files, and images are all where they need to be, and you’re ready to continue with the next step. We’ll be using Xcode to open our file, so make sure you have it installed and up-to-date.

Loading Dataset Images

Finally, it’s time to begin telling Turi what it needs to do via the Python file we just created. If you double-click the file, it should open up by default in Xcode, if you have it installed. If not, you can also use another editor or a Python IDE.

1. Import Frameworks

import turicreate

At the top of the file, you’ll need to import the Turi Create framework. If you want, you can create a name for reference by adding as <your name> . For example, if you wanted to refer to it as tc in your code, you could write:

import turicreate as tc

This would allow you to call it tc instead of writing out turicreate . In this tutorial, I’ll be using the full version, calling it turicreate to reduce ambiguity.

You’ll also need to deal with folder names and other os-related tasks in order to classify your images. This will require another Python library called os . To import it, simply add the following:

import os

2. Loading Images

data = turicreate.image_analysis.load_images("images/")

Here, we’re storing all of the images in our dataset into a variable called data . Since our dog_breeds.py file is in the same directory as the images folder, we can simply put “images/” as the path.

3. Defining Labels

Now that Turi Create has all of your images, you need to link the folder names to a label name. These label names are what will be returned in your Core ML model when it’s being used in an iOS or macOS app.

data["label"] = data["path"].apply(lambda path: os.path.basename(os.path.dirname(path)))

This allows you to map all of your folder names to a “label” name, which tells Turi Create that all of the images that are in the “cocker_spaniel” folder are indeed Cocker Spaniels, for example.

4. Save as SFrame

In case you’re not familiar with an SFrame , in simple terms, it’s a dictionary of all of your data (in this case, an image) and all of the labels (in this case, the dog breed). Save your SFrame like this:

data.save("dog_classifier.sframe")

This allows you to store your labeled images for use in the next step. This is a fairly standard data type in the machine learning industry.

Training and Testing

After Turi Create has all of your labeled images in place, it’s time to enter the home stretch and finally train your model. We also need to split the data so that 80% is used for training, and 20% is saved for testing the model once it’s done training — we won’t have to test it manually.

1. Loading SFrame

Now, we need to load the SFrame we just created in the previous step. This is what we’ll use to split into testing and training data later.

data = turicreate.SFrame("dog_classifier.sframe")

This assigns the data variable, which is now of type SFrame to the SFrame that we saved in the previous step. Now, we’ll need to split up the data into testing and training data. As aforementioned, we’ll be doing an 80:20 split of testing to training data.

2. Splitting Data

It’s time to split the data. After your SFrame code, add the following:

testing, training = data.random_split(0.8)

This code randomly splits the data 80–20 and assigns it to two variables, testing and training , respectively. Now, Turi will automatically test your model without you needing to manually supply test images and create an app — if you need to make adjustments, you won’t need to fully implement it first, and instead, you can do them right in your Python file.

3. Training, Testing, and Exporting

Your hard work has finally paid off! In this line of Python code, you’ll just tell Turi Create to train your model, while specifying the architecture that you’d like to use.

classifier = turicreate.image_classifier.create(testing, target="label", model="resnet-50")

You’re simply telling Turi to use your testing data (specified earlier), and use them to predict the labels (based on the folder structure from before), while using resnet-50 , which is one of the most accurate machine learning model architectures.

To use your testing data and ensure that your model is accurate, add this:

testing = classifier.evaluate(training)

print testing["accuracy"]

This uses the training data you specified and stores the results after testing in a variable called (you guessed it) testing . For your information, it prints out the accuracy, but you can print other things as well, given enough time on Turi Create’s APIs.

Last but not least, you can save your model right into your file system with this one-liner after you give it a useful name:

classifier.save("dog_classifier.model")

classifier.export_coreml("dog_classifier.mlmodel")

Of course, you can also save your model in other formats, but for this example, I’ve saved it as a Core ML model.

Running and Output

For all you iOS developers out there — no, this isn’t an Xcode project that keeps compiling automatically and complaining for errors. In order for the code you just wrote to execute, we’ll need to do it via the terminal.

Running the Python File

Running the Python file is easy! Make sure you’re in the correct directory, and all you need to do is enter the following in your terminal window:

python dog_breeds.py

Output

After a couple of minutes of training, your images folder and dog_breeds.py file will be accompanied by an SFrame, a model folder, and a .mlmodel file, which is your Core ML model!

You’ll also be presented with output in your terminal window, which will look something like this:

Figure 4: Output after Running Python

This gives you information about training and training accuracy, the amount of images processed, and other useful information, which you can use to analyze your model without ever even using it.

Conclusion

I hope you enjoyed reading this tutorial as much as I enjoyed making it! Here are some steps on where to go from here. If you want to learn how to use your Core ML model in an iOS app, check out another one of my tutorials:

This tutorial will show you how to take your resulting dog_classifier.mlmodel model and implement it in a real-world iOS app. It will also teach you to parse a live video feed and take individual frames for image classification.

If you have any questions or comments regarding this tutorial, don’t hesitate to ask them down in the comments section below! I’m always eager to hear feedback, questions, or how you used your knowledge from this tutorial.

Discuss this post on Reddit and Hacker News