How To Create Your Own Core ML In Xcode

Your Own Core ML

Okay, so if you are following us regularly, then you have already studied Apple’s Core ML. But, if you are not following us regularly which we may tell you should, then let us give you a preview.

Last year, Apple introduced Core ML for its developers to create large codes in a few lines. Core ML has been a dynamic machine learning model for the apps. That’s why Apple launched Core ML 2 in this year Apple’s Worldwide Developers Conference. In which Apple, has given enormous power to the developers as they can know to customize their machine learning model in the Xcode playground.

The latest version of Core ML involves text, images, and tables as data. Which means you can play in these three data fields and can customize them according to your wish. You just need some basic data and you are good to go. Well, in this post, we are going to give you some taste that how can you create your own Core ML using Xcode.

Image Classifier Model

First, we are going to teach you how to implement an image classifier model. You can upload as many as images you want in the classifier, but we are going to take the example of two images. This classifier will show you how image apple can be classified from the image of a banana. Download different images of Apple and Bananas and let’s start the process.

When you open folder, you will see two further folders named as Training Data and Testing Data. In the Training Data folder, 20 images of Apple and Bananas are stored, whereas in Testing Data folder 80 images are stored. The images of Training Data will be used for training and images of Testing Data will be used to measure the accuracy. The keynote here is to maintain the ratio of 20-80% to create your own classifier.

Now, it’s time to open the Xcode playground. This is the important part: under macOS, select the Blank template. Now, with the three magic coding lines all your work will be done:


import CreateMLUI

let builder = MLImageClassifierBuilder()


Text Classifier Model

Okay, so next, we will distinguish between the spam and ham data by creating a Spam Detector with the Core ML. You can for that download the JSON file containing plenty of spam or ham messages or you can pick data according to your wish. But, make sure to pick a small number of data in the beginning phase.

Now, you have to do some coding work. The text classifier model code isn’t simple as image classifier model code, but it isn’t that difficult also. Your code is:

import CreateML

import Foundation


let data = try MLDataTable(contentsOf: URL(fileURLWithPath: “/Users/Path/To/spam.json”))

let (trainingData, testingData) = data.randomSplit(by: 0.8, seed: 5)

let spamClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: “text”, labelColumn: “label”)


let trainingAccuracy = (1.0 – spamClassifier.trainingMetrics.classificationError) * 100

let validationAccuracy = (1.0 – spamClassifier.validationMetrics.classificationError) * 100


let evaluationMetrics = spamClassifier.evaluation(on: testingData)

let evaluationAccuracy = (1.0 – evaluationMetrics.classificationError) * 100


let metadata = MLModelMetadata(author: “LearnCodeOnline”, shortDescription: “A model trained to classify spam messages”, version: “1.0”)

try spamClassifier.write(to: URL(fileURLWithPath:

Table Classifier Model

This is the most interesting form of Core ML model in which different patterns of the data can be classified. It is very important method when you have to deal with the large number of database such as house pricing. When you have to compare house pricing of large number of houses, then this Core ML will be very helpful for you.

So, you see people, with the simple code you can easily create the Core ML model. And, this is just the beginning as Apple will be surely going to introduce new features of Core ML in the near future.

Send a Message