In this post, we’re going to train an Image Classifier for the Android Machine Learning demo app. You’ll need to install TensorFlow and you’ll need to understand how to use the command line.
What is Machine Learning? and Why important?
You can think of Machine Learning as a subfield of AI.Machine Learning is the study of algorithms that learn from examples and experience instead of relying on hard-coded rules.So That’s the state-of-the-art.
Can you write code to tell the difference between an apple and an orange? Takes file as input, does some analysis, and outputs the type of fruit.
We’re going to train a classifier.For now, you can think of a classifier as a function.It takes some data as input and assigns a label to It as output.
The technique to write the classifier automatically is called supervised learning.It begins with the example of the problems you want to solve.
To use supervised learning, we’ll follow a few standard steps.
1.Collect training data
We’re going to write a function to classify a piece of fruit Image. For starters, it will take an image of the fruit as input and predict whether it’s an apple or oranges as output.The more training data you have, the better a classifier you can create (at least 30 images of each, more is better).
We will create a
~/tf_files/fruits folder and place each set of jpeg images in subdirectories (such as
A quick way to download multiple images at once is to search something on Google Images, and use a Chrome extension for batch download.
2.Train an Image Classifier with TensorFLow for Poets
You want to build a classifier that can tell the difference between a picture of an Apple and Orange.TensorFlow for Poets This is a great way to get started learning about and working with image classification.
To train our classifier we’ll basically just need to run a couple of scripts.To train an image classifier with TensorFlow for Poets, we’ll only need to provide one thing–training data.
The classifier we’ll be using is called a neural network.At a high level, that’s just another type of classifier, like the nearest neighbor one wrote last lime.The different a neural network can learn more complex functions.
retrain.py script is part of the TensorFlow repo.You need to download it manually, to the current directory(
curl -O https://raw.githubusercontent.com/tensorflow/tensorflow/r1.1/tensorflow/examples/image_retraining/retrain.py
Now, we have a trainer, we have data(Image), so let’s train! We will train the Inception v3 network.
Step2: Before starting the training, active TensorFlow.
Step3: Start your image retraining with one big command.
python retrain.py \
These commands will make TensorFlow download the inception model and retrain it to detect images from
This operation can take several minutes depending on how many images you have and how many training steps you specified.
The script will generate two files: the model in a protobuf file (retrained_graph.pb) and a label list of all the objects it can recognize (retrained_labels.txt).
Clone the Git repository for test model
The following command will clone the Git repository containing the files required for the test model.
git clone https://github.com/googlecodelabs/tensorflow-for-poets-2
The repo contains two directories: android/, and scripts/
1.android/: Directory contains nearly all the files necessary to build a simple Android app that classifies images.
2.scripts/: Directory contains the python scripts. These include scripts to prepare, test and evaluate the model.
Now copy the tf_files directory from the first part, into /tensorflow-for-poets-2 working directory.
Test the Model
The scripts/ directory contains a simple command line script,
label_image.py, to test the network.
python -m scripts.label_image \
Optimize model for Android
TensorFlow installation includes a tool,
optimize_for_inference, that removes all nodes that aren’t needed for a given set of input and output nodes.
python -m tensorflow.python.tools.optimize_for_inference \
It creates a new file at
Make the model compressible
The retrained model is still 84MB in size at this point. That large download size may be a limiting factor for any app that includes it.
Neural network operation requires a bunch of matrix characterizations, which means tons of multiply and add operations.current mobile devices are capable of doing some of them with specialized hardware.
Quantization is one of the techniques to reduce both memory footprint and computer load.Usually, TensorFlow takes a single precision floating value for input and math, and also output as well.As you know, the single precision floating point takes 32 bits each.W can reduce the precision to 16 bits,8 bit, or even less while keeping a good result, just because our learning process involves some noise by nature.Adding some extra noise wouldn’t matter much.So quantized weight is the optimization for storage size, which reduces the precision of the constant node in the graph file.
With the quantized calculations, we can reduce computing precision by using the quantized value directory.This one is good for first memory bandwidth, which is a limiting factor in mobile devices.Also, hardware can handle these precision values faster than single precision floating values.
Now use the
quantize_graph script to apply changes:
python -m scripts.quantize_graph \
It does this without any changes to the structure of the network, it simply quantizes the constants in place.It creates a new file at
Every mobile app distribution system compresses the package before distribution. So test how much the graph can be compressed:
You should see a significant improvement. I get 73% optimize model.