Android TensorFlow Machine Learning

TensorFlow is an open source software library for machine learning, developed by Google and currently used in many of their projects.

In this article, we will create an Android app that can recognize five types of fruits.

Machine Learning

There have been so many buzzwords such as AI, Machine Learning, Neural Network, or Deep Learning.What’s the difference?

AI or Artificial Intelligence–you can say that is a science for making smart things like building an autonomous driving car or having a computer drawing a beautiful picture or composing music.One way to realize that vision of AI is in Machine Learning.Machine learning is a technology where you can a computer train itself, rather than having human programmers instructing every step, to process the data by itself.

One of the many different algorithms in MI is a neural network.Since around 2012, Google has been seeing a big breakthrough in the world of the neural network, especially for image recognition, voice recognition, or natural language processing and many other applications.

Neural Network

You can think of it just like a function in mathematics or a function in the programming language.So you can put any kind of data as an input and do some matrix operation or calculations inside neural networks. You would get an output vector which has the many labels or speculative values.

Neural Network

For example, if you have a bunch of images, you can train the neural network to classify which one is the image of a cat or the image of a dog, this is just one example of the use cases of neural networks.You can apply the technology to solve any kind of business problems you have.
There are so many possible use cases for the combination between ML and mobile applications, starting from image recognition, OCR, speech-to-text, and text-to-speech, translation.You can apply machine learning to mobile-specification applications such as motion detection or GPS location tracking.

Why do you want to run machine learning model inside your mobile applications?

By using the machine learnings, you can reduce the significant amount of traffic, and you can get much faster responses from your cloud services.Because you can extract the meaning from the raw data.For example, if you are using machine learning for image recognition, the easiest way to implement that is to send all the raw image data taken by the camera to the server. But instead, you can have the machine learning model running inside your mobile application so that your mobile application can recognize what kind of object is in each image.So that you can just send the label, such as a flower or human face, to the server.That can reduce the traffic to 1/10 or 1/100 It’s a significant amount of saving of traffic.

Build an application that is powered by machine learning

The starting point could be the TensorFlow, which is the open-source library for machine intelligence from Google.TensorFlow is the latest framework for building machine learning or AI-based service developed in Google.Google open source it in November 2015. TensorFlow is the most popular framework for building neural networks or deep learning in the world.One benefit you could get with TensorFlow is easy of development.So It’s really easy to get started.You can just write a few lines of Python code.

TensorFlow is very valuable for people like me because I don’t have any sophisticated mathematical background.So when you started reading the textbook on neural networks, you found many mathematical equations on the textbook, like differentiation backpropagation and gradient descent.You really didn’t want to implement everything by yourself.Instead, now you can just download TensorFlow, Where you can write a single line of Python code, like GradientDescentOptimizer.That single line of code can encapsulate all these obfuscated algorithms such as gradient descent, backpropagation, or any other latest algorithm implemented by the Google Engineers.So you yourself don’t have to have the skill set to implement the neural network technologies from scratch.The main benefits of the TensorFlow is the portability and scalability.

Implement TensorFlow in Android

Android just added a JSON integration, which makes step a lot, a lot easier.Just add one line to the build.gradle, and the Gradle take care or the rest of steps.Under the library archive, holding TensorFlow shared object is downloaded from JCenter, linked against the application automatically.

Android release inference library to integrate TensorFlow for Java Application.

Add your model to the project

We need the pre-trained model and label file.In the previous tutorial, we train model.which does the object detection on a given image.You can download the model from here.Unzip this zip file, we will get retrained_labels.txt(label for objects) and rounded_graph.pb (pre-trained model).

Put retrained_labels.txt and rounded_graph.pb into android/assets directory.


At first, create TensorFlow inference interface, opening the model file from the asset in the APK.Then, Set up the input feed using Feed API.On mobile, the input feed tends to be retrieved from various sensors like a camera, accelerometer, Then run the interface, finally, you can fetch the results using fetch method over there.You would notice that those calls are all blocking calls.So you’d want to run them in a worker thread, rather than the main thread because API would take a long time.This one is Java can use regular C++ API as well.

Download this project from GitHub


Related Past

Image Classify Using TensorFlow Lite

Google Cloud Vision API in Android APP

Introduction TensorFlow Machine Learning Library

TenserFlow Lite

Train Image classifier with TensorFlow

Train your Object Detection model locally with TensorFlow

Speech Recognition Using TensorFlow




5 Replies to “Android TensorFlow Machine Learning”

  1. When I run the project it gives me this error:

    Error:(5, 0) assert file(project.ext.ASSET_DIR + “/rounded_graph.pb”).exists()
    | | | | | |
    | | | | | false
    | | | | C:\Users\User\AndroidStudioProjects\ImageClassifier-master/assets/rounded_graph.pb
    | | | C:\Users\User\AndroidStudioProjects\ImageClassifier-master/assets
    | | org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@5c34be05
    | root project ‘ImageClassifier-master’
    Open File

    1. It’s because gradle couldn’t find android/assets/rounded_graph.pb,
      or android/assets/retrained_labels.txt. You can download the model from here.Unzip this zip file, you will get retrained_labels.txt(label for objects) and rounded_graph.pb (pre-trained model).

      Put retrained_labels.txt and rounded_graph.pb into android/assets directory.

    1. if i change change the buildToolsVersion it gives the this error:
      Gradle sync failed: This Gradle plugin requires Studio 3.0 minimum

  2. @Maalik Did some changes to the following files and then it compiled and ran without an error


    classpath ‘’ -> classpath ‘’

    buildToolsVersion ‘26.0.0 rc2’ -> buildToolsVersion ‘26.0.1’

    dependencies {
    implementation fileTree(include: [‘*.jar’], dir: ‘libs’)
    androidTestImplementation(‘’, {
    exclude group: ‘’, module: ‘support-annotations’
    testImplementation ‘junit:junit:4.12’
    implementation ‘’
    compile ‘org.tensorflow:tensorflow-android:1.2.0-preview’
    implementation ‘’

    dependencies {
    compile fileTree(include: [‘*.jar’], dir: ‘libs’)
    androidTestCompile(‘’, {
    exclude group: ‘’, module: ‘support-annotations’
    testCompile ‘junit:junit:4.12’
    compile ‘’
    compile ‘org.tensorflow:tensorflow-android:1.2.0-preview’
    compile ‘’

    And also make sure to get make a copy of the asserts folder with all its contents as mentioned in the earlier comments

    /ImageClassifier/asserts -> /ImageClassifier/app/src/main/assets

Leave a Reply

Your email address will not be published. Required fields are marked *