Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Machine Learning with Core ML

You're reading from  Machine Learning with Core ML

Product type Book
Published in Jun 2018
Publisher Packt
ISBN-13 9781788838290
Pages 378 pages
Edition 1st Edition
Languages

Table of Contents (16) Chapters

Title Page
Packt Upsell
Contributors
Preface
Introduction to Machine Learning Introduction to Apple Core ML Recognizing Objects in the World Emotion Detection with CNNs Locating Objects in the World Creating Art with Style Transfer Assisted Drawing with CNNs Assisted Drawing with RNNs Object Segmentation Using CNNs An Introduction to Create ML Other Books You May Enjoy Index

Chapter 10. An Introduction to Create ML

The intention of this book has been to explore ways to apply machine learning on the iPhone, specifically focusing on computer vision tasks. Even with this narrow focus, we have only scratched the surface of what is currently possible. But, hopefully, we've covered enough to spark your curiosity and provided enough intuition behind the details of machine learning models to help you on your journey to build intelligent apps. 

This chapter is intended as a primer into continuing that journey by introducing Create ML, a tool released with Core ML 2 that provides an easy way to create some common models using custom data. Even though we only provide a high-level introduction, specifically around computer vision, it still should be enough to help you make use of it in your own applications. 

By the end of this chapter, you will have:

  • Revised the machine learning workflow 
  • Appreciated the importance of splitting your data into sets for training and validation...

A typical workflow 


As with any project, you enter the process with some understanding of what you are trying to build. The better you understand this (the problem), the better you are able to solve it. 

After understanding what it is that you're trying to do, your next question (in the context of building a machine learning model) is what data do I need? This includes an exploration into what data is available and what data you may need to generate yourself. 

Once you've understood what you're trying to do and what data you need, your next question/task is to decide on what algorithm (or model) is needed. This is obviously dependent on your task and the data you have; in some instances, you may be required to create your own model, but more often than not, there will be an adequate model available for you to use, or at least an architecture you can use with your own data. The following table shows some typical computer vision tasks and their related machine learning counterparts:

Preparing the data


For our task, we require a collection of labeled photos of fruits. As you may recall from Chapter 1Introduction 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(
   ...

Closing thoughts


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...

Summary


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.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Machine Learning with Core ML
Published in: Jun 2018 Publisher: Packt ISBN-13: 9781788838290
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}

Task

Machine...