In previous posts, we explored some of the compelling applications for Machine Learning on mobile apps, and considered whether the learning/computation should happen on-device or in the cloud. In this post, we’ll explore what are the options for implementing AI based applications on Android and iOS.
Setting Up ML Application on Andriod and iOS Platforms
The good news is that both Android and iOS platforms have announced first-class support for Machine Learning applications. Google announced Neural Networks API to accelerate on-device machine intelligence on Android 8.1 and Tensorflow Light for using in mobile and embedded devices. Apple announced the CoreML framework at their developer conference, WWDC 2017.
Now we will list the steps involved in setting up a simple machine learning application on mobile platforms: Andriod and iOS. For better understanding, we’ll consider the example of an app with Image Recognition using a neural network.
(Note: the below steps are more on the technical side, but just goes on to show the simple high-level steps required to get machine learning setup on your apps.)
Setup in Android
Below are the steps to be followed in setting up Andriod Machine Learning platform:
- Add TensorFlow lib as a dependency to your project.
- Identify the suitable pre-trained model. Model will be “.pb” file. It will come with a label file (.txt format). Drop them into the assets folder.
- Use the TensorFlowInferenceInterface that comes with the TF library. This class encapsulates the logic required to process the image and give the output label with confidence level.
- Process/display the label and confidence level. It'll be a value between 0 and 1.
In Android, the machine learning computation is completely based on pre-trained Tensorflow models, combined with a standard SDK. There is no platform level API to get output from a model (unlike Apple). However, Android has an advanced Neural Net API, which lets you train models on the device, on the go. Pretty powerful and advanced, but not many devices supported (as of the writing of this blog, at least).
Setup in iOS
Below is the list of steps to be followed in setting up iOS Machine Learning platform:
- Here, we will have to use models in the .mlmodel format. This is an Apple-prescribed format. As this is a non-standard format, Apple actually provides some tools to convert other formats in to .mlmodel format, including from TensorFlow models.
- Identify a suitable model, drop it into Project Navigator and assign the target ownership correctly.
- If you click on the model object, it will directly show all details of the model like Input & output name.
- Now XCode will auto-generate a Swift class representing this model. This class will be your interface to provide input and get output from the model.
- Now, all we need to do is pass the input to the model object, and get the output.
We see that both Android and iOS platforms are adopting Machine Learning in a big way, by providing built-in support for them. They’ve made it easy for developers to drag-drop trained models and use them on the fly in few simple steps. And there are several tools available to convert and reuse these models across the platforms.