WWDC18: Create ML-Complete Machine Learning in Swift

Apple announced Core ML framework for the machine learning in last year WWDC17. The idea behind the Core ML framework was to get the trained machine learning model which was usually in the format of .mlmodel  and integrate with any app using the Core ML framework. We can drag those modules in Xcode and start using those model using the Core ML framework. However, Apple didn’t announce the technology to create or train those models. This creates dependency on third-party machine learning frameworks like TensorFlow, IBM Watson etc for developers to get the trained models before they can use it in the apps. This didn’t clearly end to end workflow of machine learning in Apple platform. Luckily, Apple announced the Create ML framework to train the models using native Apple technologies like Xcode, Swift and Apple developer tools.

Create ML

Create ML is a machine learning framework in Swift which can be used to train the machine learning models using native Apple technologies like Swift, Xcode, and Other Apple frameworks. This means machine learning is possible within Apple ecosystem without any third-party service. Create ML technology has following features

  • A simple approach for training the model using images, texts, and tabular data
  • Models still can be trained using complex algorithm if you are expert
  • Machine learning models can be created using Xcode Playground, Swift, and native Apple framework
  • Create ML models can be easily integrated into apps, just by drag and drop
  • Create ML is powered by Mac and its available to use from macOS Mojave
  • The process of creating and training module can be automated using Swift scripts.

Machine Learning Workflow

The machine learning process starts by identifying the problem where machine learning can be useful. We collect the data in any suitable format e.g images, texts, CSV file, tables, code etc. Once we have enough data, we can start training the machine learning model. We can evaluate the machine learning model until we get satisfactory results. Once we happy with results then we write the model in the form of .mlmodel  that can be used in the apps.

This workflow can be satisfied by the Apple frameworks and native tools like Swift, Xcode, and other native frameworks

You can see the entire workflow in the above diagram which describes that

  • We can collect data using the data source, Swift primitives, and huge data tables. We can store data in the folders as well
  • Training the model is one line of Swift code with Create ML. It will automatically analyze and train data. It also applies the synthesis to optimize the model under training.
  • The process of evaluating the model is inbuilt in the CreateML, we don’t need to know complex algorithms or expert in the machine learning to evaluate the modules. Create ML will evaluate module and tell the accuracy of the module.
  • Once happy with the module then we can write the module to the specific location which is in the .mlmode format.

We can then use the fully trained model in the apps using the Core ML framework which is not that complex.

Create ML Models in Swift

With Core ML technology, we can easily train and evaluate machine learning models using Swift. Apple has already created frameworks for Core ML and Create ML. We just need to use them in the Swift Playground or inside our app for creating the models. The process of creating the machine learning models in Swift playground has following main steps.

Import Create ML

The first step to create the machine learning model is to import the Core ML library in the Swift Playground. We might also need some other Apple frameworks like Foundation if we are using URL. It can be simply done by using

Now, we will get access to all the methods in the Create ML framework to train, evaluate and write the machine learning model.

Specify Data

The next thing is to specify the data to train our model. It can be images, texts, CSV files or Tables or even Swift code. The easiest thing to do is put all the training data inside of the one directory and test data in another directory so that we can evaluate the model for accuracy. Let’s define those directories

We have to make sure that data in the Fruits and TestFruits directory is in the correct order and format.

Create Model

Once, we got the data in the respective directories, we can create a machine learning model. This model can be then trained for accuracy. Depending on what type of data we need to train, we can access different methods e.g for image data we can use MLImageClassifier

There are other formats supported as well and we need to use respective methods from CreateML

  • Text Data: We have to use MLTextClassifier
  • Tabular Data: We have to use multiple inbuilt algorithms but if we want Create ML to choose the best we can use MLRegressor

Evaluate Model

Now that, we have created our machine learning model. The next step is to evaluate the model against test data. We already created test directory where we have data that is not known to the model. We can evaluate the model using Create ML as below

This will evaluate the model against the test data and gives the results. We can always improve accuracy by re-evaluating the model and feeding the right data.

Save Model

Once the desired level of accuracy has been achieved then we are good to save the model. The model can be saved anywhere on the disk. We can save the created model using Swift

This will save the model on the desktop and we can use this model in the apps by dragging into the Xcode.

 

Automating the Model Training

Now that, we have created Swift playground to create, train and evaluate the model. Swift playground is great for giving the instance results in the sidebar as well as console output at the bottom. In the later iteration, we will be only interested in the accuracy percentage not the result of each prediction. It would be the very slow process if we want to re-iterate the model every time from the playground. We can write a script in the swift and execute anytime from the terminal. Our script will look like this

We can save this file as train.swift  or whatever and change the mode of the file to be executable.

Now that we can able to run this script from command line and evaluate the model much faster.

Conclusion

With the launch of Create ML, Apple has made machine learning a native technology within Apple ecosystem. There won’t be a simpler way to create and train machine learning model other than Create ML. With Create ML, we can use core Apple technologies like Swift and Xcode to create fully trained machine learning models that can be supplied to the apps. We don’t need to be machine learning expert or know other tools like TensorFlow to create machine learning models. AI/ML wave is everywhere and this is a huge announcement that opens doors to many machine learning tools in Apple ecosystem. What you think of Create ML and how you are planning to use CreateML in your apps. Waive in the comment below.