CoreML3: Easy Image classification model training using Xcode Create ML

Rizal Hilman
The Startup
Published in
5 min readMay 22, 2020

--

https://developer.apple.com/documentation/createml

Create ML released by Apple in WWDC19. It helps you to train your CoreML model easily. Before we have Create ML we usually using Create MLUI inside the Xcode playground to train our core ml model.

In Create ML you can train models to perform tasks like recognizing images, extracting meaning from text, or finding relationships between numerical values.

There are two types of image recognizing in Core ML which are Image Classification and Object Detection. In this tutorial, we gonna focus on Image Classification.

What do we need to prepare?

  1. Xcode 11 +
  2. Dataset

What kind of dataset do we need to prepare?

it depends on what you want to recognize. In this tutorial, we will try to recognized Indonesian Traditional Foods:

  1. Nasi Padang
  2. Sate
  3. Gado-Gado
  4. Bakso
  5. Empal Gentong

So we need to find images for each label (food name) for our dataset.

⬇️ Download: Indonesian Foods Dataset.

How many images per label?

In image classification at least you need to prepare 10 images per label for the training. But you also need to prepare images for the testing part. Here is what you need to prepare:

  1. 80% for training
  2. 20% for testing

So let’s say you have 10 images for training. Then you need 2 images for testing. In total it’s 12 different images for each label.

Note: More is always better. Also, balance the number of images for each label. Don’t use 10 images for Cheetah and 1000 images for Elephant.

How is the dataset structure?

You need to create two folders named Training Data and Testing Data. Inside the testing and training folder, create subfolders using your labels as names.

This is how my dataset folder structure looks like. Inside the testing and training folder has the same folder structure with different files.

⬇️ Download: open this dropbox link to see how my dataset looks like.

Does size matter?

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.

But, the bigger your image, the more time will be spent to train your model. Because there is a process called Normalization. It will take time to normalize big images.

💡Tutorial: How to resize multiple images on a MacOS at once using Preview.

Does extension matter?

Yes, use JPEG and PNG formats.

How to train CoreML model using a custom dataset?

Open Create ML

Open Xcode > Developer Tool > Create ML.

New Document

Click “New Document” then select “Image Classifier” then click “Next”.

Fill Your ML Project Detail

Fill project details > Click Next > Create.

Drop Training Data

Drop your training data into Create ML training data input.

Drop Testing Data

Drop your training data into Create ML testing data input.

Set Training Iteration

The default iteration number is 25. It means the iteration of the training process will take 25 times. I will put 100 iterations.

💡 Tips: More iteration. More accurate. More longer training time.

Improve Model Accuracy

Create ML let us apply image augmentations such as noising, blurring, cropping, exposing, flipping, and rotating to make our model more accurate.

developer.apple.com/documentation/createml/improving_your_model_s_accuracy

📝 Note: it will affect the training time.

At the moment I just use two augmentations: Add Noise and Rotate.

Time to Train!

To start the training you just need to click “Train” and wait until the training process is done.

Training Result

Here is my training result:

It says the training accuracy is 100% and validation accuracy is 89%. What does it mean? it means we need more data to improve our validation accuracy. But that's good enough actually!

Already add more data but the validation accuracy still lower than the training accuracy?

Yes, it is possible for you to have lots of data but the validation accuracy still significantly lower than training accuracy.

It is called overfitting and happens because your model learning too many specific details about your training set that don’t generally apply to other examples.

⁉️ How to handle that case?

Reduce training iterations to prevent the model from learning too much about your training data.

How to manually test my model?

It was so easy to test our model. Just click on the Output (top right) then drag your images into create ml.

How to export the core ml model?

Just drag it into your Xcode project!

References:

CoreML Image Classifier + Vision Example Xcode Project

Clone it here: https://github.com/drawrs/CoreMLVisionExample

That’s all! Thank you!

Let get connected

Have questions? or want to have a private lesson with me about iOS development? Let’s connect!

Instagram: https://www.instagram.com/rz.khilman/

Linkedin: https://www.linkedin.com/in/rizal-hilman/

--

--

Rizal Hilman
The Startup

Tech Mentor at Apple Developer Academy - Batam | Apple Swift Certified Trainer | Apple Professional Learning Specialist | Apple Teacher | WWDC19 Winner