An Introduction to Machine Learning on iOS

Machine learning is one of the key components of artificial intelligence. It involves using data analysis to teach software to perform tasks without specifically being programmed. Machine learning is used in many fields and industries to perform tasks such as identifying spam emails, detecting people and objects in images, fraud detection, and financial trading systems, for example.

Apple first introduced machine learning capabilities for iOS and macOS in 2017 with the introduction of the Core ML framework and has continued to add improvements.

Machine learning is a large and complex technology area, so the goal of this and subsequent chapters is to provide an overview of the basics of this topic to get up to speed quickly and provide a knowledge basis on which to build more advanced machine learning skills.

Datasets and Machine Learning Models

Machine learning begins with the gathering of datasets to be used to train and create a learning model. To create a model that can detect happy or sad sentiments expressed in text messages, for example, the model would need to be trained with a dataset containing both types of messages, with each message labeled to indicate whether the message is happy or sad. Similarly, a model intended to identify a particular car model from photos would need to be trained with images of the model of the car in various colors taken from multiple angles and distances.

The model will be built by iterating through the training dataset and learning how to categorize similar data. Once the training is complete, the model is tested by applying it to a dataset containing data not previously included during training. The training results can then be analyzed to check for accuracy, and the model can be improved if necessary by providing additional training data and increasing the number of iterations performed during the training phase. Once a model is complete, it is referred to as a trained model and is ready to be used in real-world situations.

 

You are reading a sample chapter from Building iOS 17 Apps using Xcode Storyboards.

Buy the full book now in eBook or Print format.

The full book contains 96 chapters and 760 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

Machine Learning in Xcode and iOS

The first step in implementing machine learning within an iOS app is to create the model. Once a complex and time-consuming task, this is now made easier through the introduction of Create ML. Create ML (in the form of the CreateMLUI class) can be used from within an Xcode playground to visually create a model by writing a few lines of code, dragging and dropping training and testing datasets into the Assistant Editor, and viewing the results. Alternatively, models may be created programmatically from within an app by using the CreateML class.

Training a model to identify whether an image contains a picture of a dog or cat, for example, can be as simple as creating a training folder containing one subfolder of cat images and another containing dog images. All that is necessary to train the model is to drag the training folder and drop it onto the model builder in the Xcode playground window.

Alternatively, the training dataset can consist of CSV or JSON data containing the training data. The following JSON excerpt, for example, is used to teach a model to distinguish between legitimate and spam email message titles:

[
    {
        "text": "Congratulations, you've won $1000!",
        "label": "spam"
    }, {
        "text": "Meeting reminder",
        "label": "not spam"
    }, {
        "text": "Lose 20lb in one month",
        "label": "spam"
    } ...
]

When dividing data into training and testing sets, the general recommendation is to use 80% of the data for training and 20% for testing.

Once the model has been sufficiently trained, it can be saved to a file and added to an Xcode app project simply by dragging and dropping the file onto the project navigator panel, where it is ready to be used in the app code.

 

You are reading a sample chapter from Building iOS 17 Apps using Xcode Storyboards.

Buy the full book now in eBook or Print format.

The full book contains 96 chapters and 760 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

Figure 83-1

iOS Machine Learning Frameworks

When a trained model has been added to an app project, the classes of the Core ML framework are used to create and initiate requests to be performed using the model. In addition to Core ML, iOS also includes frameworks specifically for working with images (Vision framework), text and natural language (NaturalLangauge framework), and gameplaying (GameplayKit framework).

In a testament to the power in a device the size of an iPhone, machine learning operations run locally on the device using both the CPU and, for more intensive tasks, the GPU, maintaining user data privacy and avoiding reliance on a network connection.

Summary

This chapter has provided a basic introduction to machine learning in iOS, covered the creation of machine learning models using Create ML, and outlined the frameworks available for implementing machine learning within an iOS app. The next chapter will demonstrate the use of Create ML to build a machine learning model for image analysis, which will then be used in an iOS app in the An iOS Vision and Core ML Image Classification Tutorial chapter.


Categories