Introduction to Machine Learning on Mobile

Dennis Hills
6 min readApr 23, 2018

--

Photo by jarmoluk on Pixabay.

Machine learning and AI technology are taking mobile app development to a new level. Apps utilizing machine learning can now recognize speech, images, and gestures, and translate voices with extraordinary success rates! Why is this important? It gives us new and compelling ways to engage and interact with objects, people, and the world around us. Machine learning is truly making our smart devices smart. And as a mobile developer, you create the bridge between the technology and the device. How cool is that?!

I’m passionate about this because I’m a mobile developer and a cloud solutions architect. This article is intended for mobile developers who want an understanding of machine learning and how to use it in a mobile application without understanding the complexities of algorithms and how they train models and learn. Let the data scientists worry about that!

In this article, I’ll briefly define AI and machine learning, explain why machine learning is so hot, pinpoint the parts of machine learning most relevant to mobile development, and provide tips for getting started.

Here we go!

Definitions

What is AI? Artificial Intelligence (AI) is a broad term for applying ANY technique that enables computers to mimic human intelligence, using logic, if-then rules, decision trees, and machine learning (including deep learning). — think chatbots, robots, and KITT from Knight Rider!

What is Machine Learning? A subset of AI, Machine Learning (ML) is a set of methods that can automatically detect patterns in data, and then use the uncovered patterns to predict future data, or to perform other kinds of decision making under uncertainty.

Machine Learning is all about using data to answer questions. First, data (e.g., images, text, or voice) is provided along with answers (labels) to that data. Then the computer (model) is trained on this data so it can “learn” and later make predictions (aka inference) on the mobile device.

Mobile apps use pre-trained models to make predictions. These models are first trained outside of the app — typically in the cloud — and then brought into the app to accomplish the task of answering questions based on the data you provide.

Why all the hype?

Several developments in the world of ML are creating an exciting playing field for mobile developers:

1. Advances in neural networks (algorithms) have dramatically improved accuracy in recognizing images and speech. Accuracy rates matter. So, what seemed like science fiction not so long ago is here today.

2. Advances in cloud computing have greatly reduced the time it takes to train these models. I’m talking weeks to hours! That means fewer resources, reduced capital expenses, and faster to market.

3. There’s also been a flood of third-party API-driven machine learning services hitting the market that do a lot of the heavy lifting for you. Build your own or let someone else do it.

For these reasons and more, engineers, data scientists, and developers are jumping in to help build tools and frameworks to stoke this momentum, thus putting AI and ML in the hands of every mobile developer.

Three Types of Machine Learning

There are three (3) types of machine learning:

1. Supervised Learning
2. Unsupervised Learning
3. Reinforcement Learning

Supervised learning is a system of all labeled data and a predictive model. We use labeled data to train a “model” and then use that “model” to make predictions (inference) on new unlabeled data. The trained model is the core of our discussion when it comes to machine learning on a mobile device.

Scene Detection Example: Photo by Robert Calin on Unsplash

Supervised Learning covers more than 90% of practical uses of Machine Learning for mobile apps and will be the focus of this article.

Supervised learning consists of two problem-solving tasks: Regression and Classification.

Regression: Based on data input, this ML predicts a continuous numerical value where the output is a real value, such as “dollars” or “pounds”. For example, I have a 2009 Honda Civic EX with 200,000 miles in fair condition in Seattle. What will my car be worth in two years?

Classification: This ML takes mined data to deliver a categorical output variable, such as “cat” or “dog.” For example, when you take a picture of your neighbor’s pet, the ML on your phone can tell you it’s a dog and what the dog is doing.

Photo by Pezibear on Pexel

Getting Started Considerations

So, we now understand machine learning, why it’s being used, and what problems it can solve, but how do you start playing with it as a mobile developer?

There are a variety of machine learning tools available for iOS and Android developers; however, most of these tools are unfriendly for the mobile environment, often require significant work and/or may not be proven to work on mobile devices. So, currently only a few GOOD choices exist for AI/ML on-device tools for mobile development. It boils down to this:

For iOS, use Core ML (read more on Core ML in 5 minutes).

Apple’s Core ML Model

For Android, use TensorFlow Lite.

Google’s TensorFlow Lite

For iOS developers, Apple provides machine learning tools like Core ML, Vision, and NLP. Core ML is the framework used across Apple products for providing optimized on-device ML inference and is fully integrated with the Vision and NLP frameworks. In this case, the fully-trained model is trained in the cloud, converted to Core ML format and then added to your Xcode project.

For Android development, use TensorFlow Lite (in preview), which is the lightweight version of the TensorFlow open source library. TensorFlow Lite is a small binary for mobile and embedded devices to provide on-device, low latency machine learning inference using a pre-trained TensorFlow Lite model file.

What about cross-platform machine learning?!?!

Good question. The TensorFlow framework DOES support both Android and iOS, but I’ll answer by saying Apple builds the hardware and software. The Core ML framework is optimized for iOS and the integrated ML toolset from Apple just doesn’t make sense for developers to use the TensorFlow framework on iOS. However, the simple solution, if you are committed to TensorFlow, is to use the TensorFlow ML framework to train and build the ML model in the cloud and then convert the TensorFlow model file (.tflite) to Core ML model type (.mlmodel) using this TensorFlow to CoreML Converter tool.

This is the best of both worlds as the ML model is really the core of ML and you are simply interfacing with the same trained model on the device but using the best framework for each platform.

Closing Thoughts

With all the latest machine learning frameworks and tools available, we can now make our mobile apps smarter with very little effort. ML and AI functionality in mobile apps will soon be the norm. And your customers will begin to expect and demand these solutions (even if they don’t know it’s AI/ML under the hood).

Think about what problems you want to solve with your app, but also play around, have fun, and maybe you’ll even identify a huge gap and disrupt an entire industry!?

To learn more, check out my other article on machine learning on the edge using Core ML.

I hope you enjoy!

--

--

Dennis Hills

Mobile Developer. I do mobile stuff. Opinions are always my own.