For our task, we require a collection of labeled photos of fruits. As you may recall from Chapter 1, Introduction to Machine Learning, this type of machine learning problem is known as supervised learning. We need our model to take in an image and return the label of what it thinks the image is, also known as multi-class classification.
Go ahead and collect photos of fruits. Create ML allows for multiple ways of organizing your data, but I find that ad hoc collection is easiest done by organizing it in folders, as shown here:
Source: http://www.image-net.org/
Here, we have organized our data into folders, where the folder name is used as a label for its contents. An alternative is labeling each image, where each instance of a specific class has a suffix number, for example banana.0.jpg
, banana.1.jpg
, and so on. Or you can simply pass in a dictionary of labels with their associated list of image URLs.
At this stage, you may be wondering how many images you should get. Apple...
Creating and training a model
Thanks to the great effort by Apple's engineers, the process of creating common machine learning models is incredibly easy and will no doubt spark a new wave of intelligent apps over the coming months.
In this section, you will see just how easy it is as we walk through creating an image classifier for our application using Create ML.
Create ML is accessible using Xcode Playground, so there is a good place to start. Open up Xcode and create a new Playground, ensuring that you select macOS
as the platform, as shown here:
Once in the playground, import CreateML
and Foundation
as follows:
import CreateML
import Foundation
Next, create a URL
that points to the directory that contains your training data:
let trainingDir = URL(fileURLWithPath: "/<PATH TO DIRECTORY WITH TRAINING DATA>")
The only thing left to do is to create an instance of our model, passing in the path to our training data (I did say it was incredibly easy):
let model = try MLImageClassifier(
...
This tool essentially democratizes machine learning by way of allowing anyone (who is able) to create custom models, but there is always a trade-off between simplicity and expressiveness. So, here is a short list of tools you may want to explore:
- Turi create: comes from a firm acquired by Apple in 2016; it provides tight integration with Core ML, allowing for easy deployment and custom models. It also provides a more comprehensive suite of machine learning models such as Style Transfer and segmentation. You can learn more about Turi create here: https://github.com/apple/turicreate.
- IBM Watson Services for Core ML: IBM Watson is IBM's AI platform, exposing an array of common machine learning models as a service. They have recently made available some of these services via Core ML models, allowing your application to leverage IBM Watson's services even when offline.
- ML Kit: Google announced an ML Kit in early 2018 as a platform for common machine learning tasks such as image...
In this chapter, we introduced Create ML, a tool that makes it incredibly easy to train and deploy common machine learning models. We saw how easy it is to create an image classifier using a minimal amount of examples and minimal amount of code. We discussed how this was achieved through the use of transfer learning, and then covered some considerations to keep in mind with regard to your training data and the importance of splitting it for validation and testing.