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!
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.
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}
:
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))
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.
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
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}
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:
private static final String MODEL_FILE =
private static final String LABEL_FILE =
Then replace them with the following lines:
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!
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