Get Started with TensorFlow Lite Examples Using Android Studio

A step-by-step guide towards running example apps on your phone using Android Studio, TensorFlow Lite, and USB debugging. Made by Ivan Goncharov using W&B
Ivan Goncharov

Introduction

Object Detection Classification Pose Estimation

It is SURPRISINGLY easy to get started with TensorFlow Lite on Android.

I mean, really, in my experience of putting neural nets on Android devices, the transition from nothing to an app running on your phone is the smoothest here thanks to the amazing examples that TensorFlow Lite team provides for people to get started with.

Now, this all great and everything but why'd go with TF Lite to do on-device machine learning in the first place?

Advantages of TensorFlow Lite

image.png

Now, let me go over some of my favorite things about the framework:

An excellent place to learn a bit more about TF Lite would be this introductory video. IMAGE ALT TEXT HERE

Android Studio

So, where do we actually write and run the code?

Awesome question, glad you asked! The go-to Google's official integrated development environment (IDE) for Android development is Android Studio. If you don't have it, just click the link install it.

TensorFlow Lite Examples

Now, the reason why it's so easy to get started here is that the TensorFlow Lite team actually provides us with numerous examples of working projects, including object detection, gesture recognition, pose estimation & much, much more. And trust me, that is a big deal and helps a lot with getting started.

These example projects are essentially folders with specially-arranged Android files with the Java code and instructions on how to make our app in the studio. Once we open the projects in Android Studio and make them, the TensorFlow Lite .lite model files will be downloaded to the assets folder, which is where you keep the files that'll be embedded in the APKs.

And it will look something like this: image.png

Get the TF Lite Examples

Now, we have to go and download the examples.

So, the folders with the code we're looking for comfortably reside in Tensorflow's GitHub examples repository. And we need to go and download it to our local machine, so we can open the examples with Android Studio.

There are two ways to download the examples:

Open Example Projects in Android Studio

Once we download and unzip the examples, we'll find that there are all sorts of things there.

Since we're interested in TF Lite examples, specifically Android, we navigate to lite/examples. There we can see all sorts of available awesome example projects. You can choose the one you like. In this tutorial, I'll stick with Object Detection as an example.

So once we open lite/examples/object_detection, we get to choose the platform. We're going to pivot this whole tutorial now and go with raspberry_pi...

Okay, okay... Just kidding. Pick Android.

And there we go! Familiar filenames for anybody who's put together Android apps! We're inside an Android project folder.

Examples TF Lite Examples Choice of Platform Android Project

Now, let's open it with Android Studio.

Examples TF Lite Examples

Now, check this out, we've opened the Object Detection example! image.png

Making the App

Once we open the project, it will automatically start downloading files, including the .lite models and configuring other things. image.png

After it has finished downloading, we can check out the model and click "Make Project" button to make sure it is ready to be run on mobile. image.png

Running the Example TensorFlow Lite App on Your Phone

So, in order to accomplish this point, we are going to need two things:

However, before we do it, we need to make sure that we have an Android SDK (Software Development Kit) - which is essentially a selection of tools required to build Android apps - downloaded for our version of Android.

So first, we navigate to tools/SDK Manager.

image.png

I have an Android 10 phone, and the SDK for this version came pre-installed with Android Studio. For instance, if you want to build your app for Android 9, select that (or any other version you are interested in), click Apply and then download those.

image.png

Enabling Developer Options

Now, there is one more thing we need to configure on our Android phones. You see, Android does not want everyday users stumbling into Developer Options settings, breaking their phones, and then writing long-list complaints to the head office, so they decided to hide them.

Here is my brief guide on enabling Developer Options and check out the official one from Google as well.

Essentially, enabling Developer Options comes down to finding a device's Build Number tab and tapping on it 7 times.

In my case, I navigated to About Phone/Version, and there it was.

Because those are already enabled on my device, we see this: image.png

Enabling USB Debugging

Now that we have enabled Developer Options on our phones, we can navigate to them in the settings. In my case, they are located at Setting/Additional Settings/Developer Options. Just tap your way there.

image.png

Then, scroll down to Debugging and check USB Debugging. And you are good to go. image.png

Running the Apps on Our Devices

Now, you can actually go ahead and connect your phone to your PC via the USB cable.

Here's the first important step: when it asks you what do you wanna use USB to (Charge Only/Transfer Files/Transfer Photos) go ahead and select Transfer Files.

Note: On my older (Android 6) phone debugging worked with Charge Only and, as far as I remember, didn't with Transfer Files, so you may wanna play around with the two in case you encounter of any problems.

So, we have the Android Studio project open, and, having done all the steps above, it should detect the connected debugging-ready device. And we're actually now ready to run the app on our phones.

Now, go ahead and click Run/Run App and the app will start getting installed on your phone (you'll be able to run it without the cable later), and you can start having fun with it!

image.png

Also, for your information, Debugging the app is essentially the same as running it but you also get the error messages linking you to specific lines of code where something went wrong in Logcat. Super useful when you actually start writing your own code!

And there we have it! image.png

Conclusion

In this report we've learned about running TensorFlow Lite examples on Android with Android Studio. And I hope it helps you, and you're able to have fun with it, and build something cool! And definitely feel free to leave your questions in the comment section down below :)

Also, if you enjoyed this report, you may find useful the video version of it I made:

IMAGE ALT TEXT HERE