Train a machine learning model to classify images, and add it to your Core ML app.
- Create ML
- Create MLUI
An image classifier is a machine learning model that recognizes images. When you give it an image, it responds with a label for that image.
You train an image classifier by showing it many examples of images you’ve already labeled. For example, you can train an image classifier to recognize safari animals by showing it a variety of photos of elephants, giraffes, lions, and so on.
After the image classifier finishes training, assess its accuracy and if it performs well enough, save it as a Core ML model file. In your Xcode project, you import the Core ML model file to use your image classifier in your app.
Prepare Your Data
Start by preparing the data that you’ll use to train and evaluate the classifier. Create a training data set from about 80% of your images for each label. Create a testing data set from the remaining 20% of images. Make sure that any given image appears in only one data set or the other.
Next, organize your data on disk to be compatible with one of the
MLImage types. One way is to create two folders named
Training Data and
Testing Data. In each folder, you create subfolders using your labels as names. Then sort the images into the appropriate subfolders for each data set.
The exact label strings aren’t important, as long as they make sense to you. For example, you might use the label
Cheetah for all the images of cheetahs. You don’t have to name the image files in any particular way or add metadata to them. You only need to put them into the folder with the right label.
Use at least 10 images per label for the training set, but more is always better. Also, balance the number of images for each label. Don’t use 10 images for
Cheetah and 1000 images for
The images can be in any format whose uniform type identifier conforms to
public. This includes common formats like JPEG and PNG. The images don’t have to be the same size as each other, nor do they have to be any particular size. However, it’s best to use images that are at least 299x299 pixels. If possible, train with images that you collect in a way that’s similar to how you collect the images for prediction. That is, if your app classifies images taken from the device’s camera in an outdoor setting, train your model with images taken outdoors with a similar camera.
Provide images with variety. For example, use images that show animals from many different angles and in different lighting conditions. A classifier trained on nearly identical images for a given label tends to have poorer performance than one trained with a more diverse image set.
Create an Image Classifier Project
Use Create ML to create an image classification project. With Xcode open, Control-click Xcode’s Dock icon and choose Open Developer Tool > Create ML. Or, from the Xcode menu, choose Open Developer Tool > Create ML.
In Create ML, choose File > New to see the list of model templates. Select Image Classifier and click Next.
Give the classifier a meaningful name. Change the default
My to be
Animal by changing the name in the UI. You can also add more information about the model, like the author and a short description.
Configure the Training Input
Training Data folder from the Finder onto the
Training Data section in your project window.
Testng Data folder from the Finder onto the
Testing Data section in your project window.
Optionally, customize the training parameters or add image augmentations.
Train the Image Classifier
Click the Play button in the project window’s toolbar to start the training process. The image classifier shows its progress as it learns from your training images.
Understanding the Training and Validation Process
As a part of the training process, Create ML automatically splits your training data into a training set and a validation set. The model iteratively learns from the training set and with each iteration, the model checks its own accuracy with the validation set. Because Create ML splits the data randomly into these two sets, you might get a different result each time you train the model.
Assess the Classifier’s Accuracy
When training finishes, Create ML shows training and validation accuracy scores. These report how well the trained model classifies images from the corresponding sets. Because the model trained on these images, it typically does a good job classifying them. In this example, the trained model correctly identified all the images in the training set and 97% of the images in the validation set.
The UI shows a graph of the model’s accuracy progress with each training iteration, as well as the precision and recall details for each image class.
The precision is the number of true-positives divided by the sum of true-positives and false-positives. Recall is the number of true-positives divided by the sum of true-positives and false-negatives.
Test the Classifier’s Performance
Create ML tests your trained model’s performance with your testing data set, which contains images the model has never seen before.
The model processes all of the images and makes a prediction for each. Create ML compares each prediction with the correct label and presents the overall testing accuracy in the UI.
If the evaluation performance isn’t good enough, you may need to retrain with more varied training data. For example, you can enable image augmentation options for an image classifier. For information about how to do a more detailed model evaluation, as well as strategies for improving model performance, see Improving Your Model’s Accuracy.
Save the Model
When your model performs well enough, save it so that you can use it in your app. Drag the model icon from the
Output tab to a directory in Finder to save the model to a file in the
Add the Model to Your App
Now add the trained model to an app that uses Core ML. You can use this model to replace the one that comes with the Classifying Images with Vision and Core ML sample code project. If you do this, the sample app works exactly the same as before, except that it recognizes and classifies animals according to the labels you’ve defined.
Open the sample code project in Xcode, and drag your model file into the navigation pane. Once the model is part of the project, Xcode shows you the model metadata, along with other information, like the model class.
To use the new model in code, you change only one line. The project instantiates the MobileNet model in exactly one place in the
Change this one line to use the new model class instead:
The models are interchangeable because both take an image as input and output a label. After your substitution, the sample app classifies images as before, except it uses your model and its associated labels.
Automate the Process of Building an Image Classifier
You can use Create ML to train a useful image classifier with very little code or machine learning expertise, as described in the sections above. However, you can also use an
MLImage instance to script the process of training a model. The general tasks are the same: prepare data, train a model, evaluate performance, and save the result to a Core ML model file. The difference is that you do everything programmatically.
For example, instead of letting Create ML partition your training data to test the model’s performance, you partition the data yourself into its own directory. Then, you initialize an
MLImage instance with that directory and provide it to the image classifier’s
evaluation(on:) method, which generates an