Click here to Skip to main content
15,867,756 members
Articles / Programming Languages / Python

Make Your Own Image Classifier On Android Using Tensorflow Within 5 Steps

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
18 Jun 2019CPOL9 min read 18.2K   5   5
My article is written mainly for the beginners that are new to Machine Learning or who are unfamiliar with technology to make a small project with Tensorflow on Android.

Introduction

Hi there! Today, we are going to discover a concept that maybe you heard about it but don’t understand how exactly it works. That’s Image Classification. In today's world, we are using it a lot because of its effective benefits. If you are a newbie to Image Classification but you would love to make some things using it, this article is good for you. Don’t worry, we code much, just some command lines that I say 5 steps.

Before we go directly to step 1, we should understand some things about Image Classification or Machine Learning.

What Is Machine Learning & Image Classification?

We can search on Google for Machine Learning and get a definition on Wikipedia like this: “Machine Learning is the scientific study of algorithms and statistical models that computer systems use in order to perform a specific task effectively without using explicit instructions, relying on patterns and inference instead”. Ok! We can understand in this way. We’ll teach the computer to do something we want by using a large amount of trained data. Let’s take an instance for imagination! We want the computer to recognize a picture and tell us if the object in the picture is a fan or not. Before that, we need to give the computer a lot of pictures that are fans and a lot of pictures that are not fans. After the computer “learns”, we use this computer to recognize fan. It’s also the way how we make an image classifier.

Not only can the Image Classification classify 1 thing, but also a lot of things as we want.

How the Machine "Learns"?

To make your own image classifier, you’ll start by installing some materials for data training. Then we collect images, train the image classifier and test it! But how can the machine learn?

In this article, I’ll train my image classifier to recognize Papa Smurf. And I use it for this example to take you to the overview of how the machine learns. First of all, we give the computer a great amount of Papa Smurf and divide them into 2 parts, Train data and Test data. With each picture, the computer will handle it by some algorithm to convert the image into lists of numbers. Then, the computer continues to divide these data into small parts through a bunch of layers and formulas. To understand these formulae, I think you have to have a good mind of mathematics. You can go deep into them later. After that, the computer gets the features of the object(s). In this example, the machine can recognize a Papa Smurf by some features like red part in the shape of curve on the top, below that are blue and white parts, especially there are 2 white circles in the blue part. Obviously, there are other features to recognize the edge of the object, etc. But above are the general features. These features are set of numbers. Since then, when we give the computer a picture, it will go through a lot of equations using set of numbers above to return a result of what is the computer thinks the object in the given picture is.

By learning from a large amount of pictures, the accuracy of the machine is increased. Let the machine learn for times to get the highest accuracy as possible, often over 90% is the best.

When we get expected accuracy, we’ll stop the training process and use it!

Image 1

Above all, it is an overview of how the machine learns to classify images. Interesting, right? Let’s go to learn how to make an Image Classifier now!

What Is Tensorflow?

Tensorflow is a big library that provides a lot of tools, APIs for Machine Learning and Deep Learning. This library is developed by Google. In this article, we are using Tensorflow as the library supports us to train data. This library also provides us a great API on the Android, Image Classification API. We can search it on Google and it’ll return you lots of information from history to other APIs.

Let’s Begin

Step 1: Install Android Studio

To create an Image Classifier on Android, we definitely install tools to code and run it on an Android smartphone. Install and play with it!

Firstly, we download the install file from Google through this link.

Please choose the correct version compatible with your PC. I recommend to download an .exe file.

If you download a .zip file, you need to unpack it, copy the android-studio folder into your Program Files folder and open the Android-studio > bin folder and launch studio.exe.

Then, follow the instructions of the Wizard to complete installing Android Studio. If you wonder how to do this step, this link from Google is the best for you.

Before we go to other steps, you should know some command statements. Firstly, if you want to open Command Prompt, go to Start and search for Command Prompt, right click and choose Run As Administrator.

Image 2

Secondly, to navigate the command prompt to a particular folder, just enter ‘cd ’ + directory to the folder. If you would like to navigate to another disk, before running the cd statement, run this statement first, {disk_name}:

Image 3

Ok! It’s fine now!

Step 2: Install Some Materials For Training

Python and Pip

Python and pip need to be installed first. Let’s download Python 3.5 from this link.

It helps us a lot for training data. Don’t forget to download the compatible version with your computer.

You can check your install by this line:

python --version

It’ll return the version of your downloaded python.

Then, download pip. Copy codes in this link and create a text file and paste it. Save the text file as get-pip.py file. Then open the command prompt and go to the folder you saved it. And run with this command statement:

python get-pip.pip

Wait a moment for installing completed. Then you’ve got pip installed.

Tensorflow

Tensorflow is the main part here. With one command line:

pip3 install –upgrade tensorflow

Now, check your download with some code lines:

We firstly need to access python by this statement:

python

Ok! Let’s check it!

>>> import tensorflow as t
>>> a = t.constant(5)
>>> b = t.constant(12)
>>> sess = t.Session()
>>> print(sess.run(a+b))

Image 4

If it returns 17, you’ve just got ready to get to the next step! If you have any errors, just copy the error and search it on Google, there is a great community support for you. I did this when I first learnt about Tensorflow.

Tensorflow Repo for Retraining

We’ll need some code with complicated algorithms to retrain our model that can recognize Papa Smurf. But those codes have written for us in this file, retrain.py. Download it from this link.

TensorFlow Image Classifier

We’ll use Image Classifier example of Tensorflow to deploy our model to it. It’ll save a great time to program but give you a clear idea, a run app that uses Machine Learning. This will be a foundation for you. Let’s download this Tensorflow repo to clone an app for Image Classification. Check out this link.

Step 3: Collect Images

Now, try your best to get as many images as possible. I had collected 300 images of Papa Smurf. The images used in this training must be .jpg files and about 300x300 is fine. If the resolution is bigger, the time for training will be longer.

Image 5

Don’t forget to save them in the folder that contains your images with the object name (don’t include the space in the name). Take this example:

…images/papa_smurf

Image 6

Ok! This step is easy, right? Go to the next step!

Step 4: Retrain the Data

Start From Scratch?

To train a machine to recognize an object doesn’t only take a long time, but also a strong CPU. So we can’t start from scratch. Fortunately, we can reuse the model that has been trained. I mean we will continue with the set of numbers that has been trained through a large data. These models have been used to recognize many objects like people, bottle, mouse, monitor, etc. We can use these features to train our own object. There are many models, but we need Mobile_net to train our model as we need a small but fast model to deploy it on Android which will be used in realtime. Although the Mobile_net is fast and small, it's accuracy isn’t high as other models like Inception, Resnet.

We can download the model Mobile_net V1 from this link.

I chose the MobileNet_v1_1.0_224 model and saved it in the folder with the name of mobilenet.

Download and save it to a folder that can be easily accessed.

Let’s Retrain

Ok! It seems to be ended after this step. Navigate the command to the folder you save retrain.py file. I should mention that in my example, I save the Mobilenet, training images folder in the same folder with the retrain.py file so that I don’t need to type the long directory again. See my example if you don’t understand. We need a phase to analyze all the images on disk and prepare for the retraining process. We call this Bottlenecks. So that the first statement is point out where to save the bottlenecks. Now, let’s start retrain!

Run these statements:

python retrain.py
--bottleneck_dir=/{directory_to_bottlenecks }/bottlenecks        
--how_many_training_steps 1000
--model_dir=/{directory_to_folder_you_saved_the_mobilenet}/mobileNet
--output_graph=/{directory_to_folder_you_want_to_save_the_model}/papa_graph.pb
--output_labels=/{directory}/papa_labels.txt
--image_dir /{directory_to_images_folder}

Image 7

The outputs are the retrained_graph.pb and retrained_labels.txt files that will be used in the final step. This .pb file is our model recognizing Papa Smurf.

Step 5: Deploy the Graph Into Android

To use model on Android, we just open Android Studio and launch the ‘android’ project from the Tensorflow Image Classifier following the directory tensor-master/tensorflow/examples/android.

We should copy retrained_graph.pb and retrained_labels.txt to the asset folder of the project in order to use them easily on other devices. Copy those files to this directory tensorflow-master\tensorflow\examples\android\assets.

Then find the ClassifierActivity.java file in the Project pane on the left or in the directory tensorflow/examples/android/src/org/tensorflow/demo. Open it, if you go directly to the folder, right click and open with Notepad. Search in that .java file:

Java
private static final String MODEL_FILE =
private static final String LABEL_FILE =

Then replace them with the following lines:

Java
private static final String MODEL_FILE = "file:///android_asset/papa_graph.pb";
private static final String LABEL_FILE = "file:///android_asset/papa_labels.pb";

Congratulations!

It finished, just connect your Android smartphone to the computer and run the project. Open Image Classifier app and play with it!!!

Here is my app!

Image 8

Conclusion

Above all, here are some instructions for those who are unfamiliar with the Machine Learning or newbies that would love to make something crazy for first lessons. It’s really a great way to start learning new things by starting with a project. It leads you to many questions and you can discover all of them in a step by step manner. When you start doing a project, you may come across many problems, don’t be shy, it’s normal. As I mentioned, just copy the error description and paste it on the Google search bar, then Enter. It’ll return a bunch of results of how to solve it. Believe in yourself. It can take time but you’ll surely finish. Now, it’s your turn. Go make your own Image Classifier!!!

History

  • 18th June, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionall went well until i try to build android app. Pin
Member 146483507-Nov-19 10:54
Member 146483507-Nov-19 10:54 
when i try to build the app, i got these errors,

error: cannot find symbol class Fill
where T is a type-variable:
T extends Object declared in class Zeros

error: cannot find symbol class Fill
where T is a type-variable:
T extends Object declared in class Zeros

error: cannot find symbol variable Fill
where T is a type-variable:
T extends Object declared in class Zeros

please give me the solution,Thanks.
QuestionI cannot see the download-link for the App! Pin
Creamcatcher5-Oct-19 1:11
Creamcatcher5-Oct-19 1:11 
QuestionTrain Pin
Prilvesh K7-Aug-19 20:44
professionalPrilvesh K7-Aug-19 20:44 
QuestionClassify multiple objects Pin
prunus718-Jun-19 18:52
prunus718-Jun-19 18:52 
AnswerRe: Classify multiple objects Pin
ron8526219-Jun-19 22:41
professionalron8526219-Jun-19 22:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.