As of this year, there are more than two billion active Android devices. The rapid adoption of Android phones has largely been due to the variety of smart apps, ranging from maps to photo editors. With the emergence of Deep Learning, our mobile apps are destined to become smarter. The next generation of mobile apps powered by Deep Learning will learn and customize their functionality specifically for you. An excellent example of this is “Microsoft SwiftKey”, a keyboard app that helps you type faster by learning the common words and phrases you use.

Computer Vision, Natural Language Processing, Speech Recognition, and Speech Synthesis can greatly improve the overall user experience in mobile applications. Fortunately, there are a number of tools that have been developed to ease the process of deploying and managing deep learning models in mobile applications. In this post, I’ll explain how to deploy both PyTorch and Keras models to mobile devices, using TensorFlow mobile.

Deploying models to Android with TensorFlow Mobile involves three steps:

Convert your trained model to TensorFlow

Add TensorFlow Mobile as a dependency in your Android app

Write Java code to perform inference in your app with the TensorFlow model.

In this post, I’ll take you through the entire process and conclude with a working Android app infused with Image Recognition.

Setup

We’ll walk through this tutorial using both PyTorch and Keras—follow the instructions for your preferred machine learning framework. Your setup depends on your framework of choice.

First, install TensorFlow:

pip3 install tensorflow

If you’re a PyTorch developer, ensure you have the latest version of PyTorch installed. For instructions on installing PyTorch, check out my previous article.

If you’re a Keras developer, install it using the following commands:

pip3 install keras pip3 install h5py

Android Studio (Minimum version of 3.0)

Converting PyTorch Models to Keras

This section is only for PyTorch developers. If you’re using Keras, you can skip ahead to the section Converting Keras Models to TensorFlow.

The first thing we need to do is transfer the parameters of our PyTorch model into its equivalent in Keras. To simplify this process, I’ve created a script to automate this conversion. In this tutorial, we’ll be using SqueezeNet, a mobile architecture that’s extremely small with a reasonable level of accuracy. Download the pre-trained model here (just 5mb!).

Before converting the weights, we need to define the SqueezeNet model in both PyTorch and Keras.

Define SqueezeNet in both frameworks and transfer the weights from PyTorch to Keras, as below.

Create a convert.py file, include the code below and run the script.