Skip to main content

Get Started with TensorFlow Lite Examples Using Android Studio

In this beginner's article, we explore a step-by-step guide for running example apps on your phone using Android Studio, TensorFlow Lite, and USB debugging.
Created on September 9|Last edited on July 28
Object DetectionClassificationPose 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 is all great and everything, but why'd go with TF Lite to do on-device machine learning in the first place? We'll explore in this article.
Here's what we'll be covering:

Table of Contents



Let's get going!

Advantages of TensorFlow Lite


Now, let me go over some of my favorite things about the framework:
  • Optimized for inference speed running on mobile devices
  • Support for quantization and model pruning, which can drastically reduce the model sizes (which is a big deal since we care about the precious storage on our phones)
  • The ability to put models directly into your APKs by loading them from assets (For instance, with OpenCV4Java, I'd have to download models from the web on the first run of the app)
An excellent place to learn a bit more about TF Lite would be this introductory video.



Android Studio

So, where do we actually write and run the code?
Awesome question, glad you asked! The go-to for Google's official integrated development environment (IDE) for Android development is Android Studio. If you don't have it, just click the link to 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 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:


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.
ExamplesTF Lite ExamplesChoice of PlatformAndroid Project

Now, let's open it with Android Studio.
ExamplesTF Lite Examples

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


Making the App

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

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


Running the Example TensorFlow Lite App on Your Phone

So, in order to accomplish this point, we are going to need two things:
  • A USB cable
  • An Android phone (or some other Android device)
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.

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.


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:



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.

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




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 you wanna use USB to do (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 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!

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!


Conclusion

In this article, 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 article, you may find useful the video version of it I made:


Iterate on AI agents and models faster. Try Weights & Biases today.