Tuesday, February 6, 2018

CoreML Simple Linear Regressor


I started learning basic Machine Learning from last 6 months from Udemy Course.

Machine Learning A-Z™: Hands-On Python & R In Data Science

I am a friend of mine, started to do MLTokyo study session every Saturday. I remember we were so obsessed with it that once we were working for 3 continuous days in Silver Week holidays from morning 10AM to almost 7PM.

I could grasp the fundamentals of Machine Learning algorithms but I still wanted to connect it to my job.
It was just that last year Apple announced the CoreML Framework in WWDC2017 talks.
So it was the perfect timing to apply the studied machine learning.

CoreML is a great Framework.
For those who are new, here's a quick explanation, the image which I have in mind about CoreML.

  • Traditionally, we train ML models on the server side. We need to have good specification server, keeping in mind the time, processing power, and data size required to train the model.
  • Once we are finished with the training, we want to use it from the mobile app. To achieve this, we generally build a wrapper API around the model.
  • The app captures the data and sends it to the API for processing. The model processes the data and returns the results via API back to the app.

So generally a Machine Learning App is built around this architecture.

The problem with above process is that the app constantly needs to be connected to the Internet to be able to process the data.
Also, there's huge network overhead involved with it.

CoreML to the rescue:

CoreML improves this process to a quite extent.

  • The same already trained model can now be exported to a file format (.mlmodel) which can be easily imported into Xcode Project. This integrates the model into your app directly removing the need for overhead API and also making your ML App offline workable.
  • According to the documentation of CoreML, it does supports following Caffe, Keras, scikit-learn, XGBoost, LIBSVM. 
So I thought why not to convert the models trained in the Udemy Course one by one.

My first CoreML integration.

Udemy Course:
- Part 2 - Regression
 - Section 4 - Simple Linear Regression
   Github source code

Xcode Model

APP Preview in Simulator

Since it was a simple Linear regressor, it was quite simple to integrate.
But as I proceeded to integrate Polynomial Regression, I started facing problems of data preprocessing.
I am still stuck on translating PolynomialFeatures into swift code.
It seems like all the data preprocessing has to be re-written in the app code to be able to provide inputs to the trained model.

Although it looks all promising and interesting to integrate CoreML, there are some things which should be taken care of.

  1. App Size will increase depending on the size of the trained model. CNN model trained with 4000 images each for 2 classes yields a 15MB .mlmodel file. This model gets packed with almost same size as .mlmodelc file inside the IPA.
  2. As far as my understanding of the topic is that it is not possible to train the model on the device. So the only prediction can be done from the model. I think there is a provision to update the model from in background of the app, but have not yet explored it.

If you like this post and have any comments please let me know.

Happy Machine Learning,
Happy iOS

No comments:

Post a Comment

Learning Japanese with my Wife (日本語の勉強、妻と一緒!)

It was just a casual talk with a friend on a Social Language Learning App which turned out to be so nice that I will be marrying her soon...