
Over the last few years we have witnessed a huge increase in the development and use of artificial intelligence. Due to the high complexity of the existing algorithms, the computing power needed is also high and costly. This means that they are usually used on the backend side.
As mobile developers with an interest in the field, we wanted to understand what could be done on a device level, without the need for an external server or service.
In this blogpost I will briefly discuss some of the available tools for on-device AI development, as well as provide an overview of our first AI iOS app: a push-up counter!
On-device AI has many advantages over using a server. To name a few:
There are, however, drawbacks to using this approach. The main issues are related to the limitations of the devices, regarding memory, processing power and battery. Consequently, we may need to use smaller models, which translates into less power.
It is then important to consider the specific use cases to make a proper decision on what we should use. Let’s take a closer look at some of the available tools for on-device AI development.
ML Kit is Google’s turnkey solution for on-device artificial intelligence development, for both Android and iOS. It provides users with easy to use APIs for various AI problems in the fields of Vision and Natural Language. The algorithms make use of Google’s pre-trained models, but some of the APIs allow you to use your custom TensorFlow Lite models in case you prefer them.
Apple has developed an AI framework of their own, Core ML. It supports the Vision, Natural Language, Speech and Sound Analysis frameworks which can be used to solve their corresponding ML problems. Alternatively, you can use your own models, which should either be converted to the appropriate format using Core ML Tools or created directly via Create ML. The disadvantage of using Core ML is that it only works on iOS.
TensorFlow is a widely used machine learning framework. TensorFlow Lite is TensorFlow’s solution for developing on-device AI on Android and iOS. It allows you to use models to make inferences on new data. TensorFlow Lite models are smaller in size and faster at making inferences. These can be obtained by converting a previous TensorFlow model to this format or creating a new one using the TensorFlow Lite Model Maker tool. In case you don’t want to create your own models, TensorFlow Lite provides you with pre-trained ones for common ML problems.
Much like TensorFlow, PyTorch is a very popular framework for machine learning development. PyTorch Mobile, its Android and iOS framework, allows you to deploy models and provides APIs for preprocessing data. You can also optimize existing PyTorch models for mobile usage. At the time of writing PyTorch Mobile is, however, still in beta.
We ultimately decided to go with ML Kit to implement our app. It comes with a pose detection algorithm out of the box that is easy to use. It was also important for us that it is available for both iOS and Android. Even though, for now, we only want to build an iOS app, we feel like it is better to gain familiarity with an SDK that is also available for Android in case we want to use it in the future.
The push-up counter app is a simple one minute challenge for users to do as many push-ups as possible. It is based on Google’s push-up counting app for Android using ML Kit. From a high-level perspective, the process is fairly simple: the app uses the phone’s camera to film the user doing push-ups, uses the frames to get the estimated pose of the user, processes it, classifies it as either up or down and then increments the counter if needed. When looking at each of these steps in detail, they can be quite complex. Let’s take a closer look.
Since our goal is to count push-ups we need to know what the user is doing, namely the position of his body, at any moment during the challenge. Google’s ML Kit offers an off the shelf solution for detecting poses in images and video. A pose corresponds to the position of the body and is composed of a set of 33 landmarks. These represent key skeletal points, which can be used to determine if two poses are the same. They are described by a set of 3D coordinates, where the z coordinate corresponds to the position of the landmark relative to the user’s hips.
There are two available SDKs for pose detection: a base one and an accurate one. The first is faster but the poses detected are not as accurate. In our case, since we are processing video frames and performance is a concern, we are using the base one. If the quality of the pose is more important and speed is not as relevant consider using the accurate SDK.
Note: Pose detection will only work if the user’s face is present in the image, but it works for partial body images as well.
In order to count push-ups we need to be able to determine the class of each pose, that is, the category to which it belongs: up or down. To that end, we use a k-nearest neighbors (KNN) algorithm. Given a new sample, it determines the k closest examples — its nearest neighbors — in the training data, using a given distance metric. It then assigns the new sample the majority class among these neighbors. It is a non-parametric, supervised classifier, meaning, respectively, that it does not make assumptions on the data and it uses labeled training data. It is also simple yet powerful, intuitive and easy to implement, without the need of a training step (often called a lazy learner), but it is also sensitive to outliers and computationally demanding and slow for large datasets.
Our classifier uses a value of k=10, and a weighted Euclidean distance for the distance metric, since the value of z is not as relevant in this case.
Simply running the pose detection algorithm and feeding its output to the classifier is not enough to achieve good results. We also needed to implement some additional processing steps, described as follows:
,
Currently there are already some powerful tools available for on-device AI development. We were able to implement a push-up counter app and overall achieve good results. However, before deciding to start building on-device AI apps, consider everything we discussed in this blogpost. It is important to take into account the current limitations and properly evaluate each scenario to determine if the performance tradeoff is worth it.
With AI still on the rise it is also only natural that more and more use cases and tools continue to emerge. It will be interesting to keep following the trends and see what the future unveils for us mobile developers!



