Machine Learning (Vol 2) - Neuroon Networks

Breaking

Monday, February 4, 2019

Machine Learning (Vol 2)

Train a Neural Network for Flower Detection


Today we are going to make an artificial neural network to detect flower types. In this case we are using only five flower types which are daisy, sunflower, rose, tulip and dandelion. And we'll build a small android application so that we can use our mobile to detect flowers.

So basically this a code-lab provided by google and I'm going to explain it in my way. If you are not familiar with ML concepts and TF please refer this link and get a rough idea. So let's begin...

So first of all you have to download the following repository which is provided by tf.
Just open a terminal and make a folder, So it will be easy to find all of your tf projects in one folder. Change your directory to that folder and paste the following command in the terminal. (I hope all of you have installed 'git' in your Ubuntu machine).

git clone https://github.com/googlecodelabs/tensorflow-for-poets-2

And change your directory to that downloaded folder.

cd tensorflow-for-poets-2

Before you start any training, you'll need a set of images to teach the model about the new classes you want to recognize. So basically we need a set of flowers which should be categorized by their name. No need to worry tf has manage it also. Download that image set by pasting following commands in the terminal (Don't change the directory. Stay where you are right now).

curl http://download.tensorflow.org/example_images/flower_photos.tgz \ | tar xz -C tf_files

To check the image directories, 

ls tf_files/flower_photos

And it should display something like following,

daisy/
dandelion/
roses/
sunflowers/
tulip/
LICENSE.txt

So now we only have to retrain the NN (MobileNet :  MobileNet is a a small efficient convolutional neural network) as our need. Most of the coding part has done by the tf. (We will disuse how to change those models as we need in out next tutorial. For now let's use their model). So before retrain the module we need to have some shell variables. Just paste following command in the terminal.

IMAGE_SIZE=224
ARCHITECTURE="mobilenet_0.50_${IMAGE_SIZE}"

So what those shell variables mean???

The MobileNet (the neural network that we user to retrain) is configurable in two ways:
  • Input image resolution: 128,160,192, or 224px. Unsurprisingly, feeding in a higher resolution image takes more processing time, but results in better classification accuracy.
  • The relative size of the model as a fraction of the largest MobileNet: 1.0, 0.75, 0.50, or 0.25.
We will use 224 0.5 for this codelab. So for this we have to use shell variables. So my advice is to open a new terminal inside the 'tensorflow-for-poets-2' folder and paste this command on it as we'll going to use that terminal to train process and the other terminal to start tensorboard. ( tensorboard: TensorBoard is a monitoring and inspection tool included with tensorflow. You will use it to monitor the training progress).

And start tensorboard,

tensorboard --logdir tf_files/training_summaries &

This command will fail with the following error if you already have a tensorboard process running:

ERROR:tensorflow:TensorBoard attempted to bind to port 6006, but it was already in use 

You can kill all existing TensorBoard instances with: 

pkill -f "tensorboard"

So now the tensorboard is monitoring, Let's start training. Paste the following command in the other terminal.

python -m scripts.retrain \ 
--bottleneck_dir=tf_files/bottlenecks \ 
--how_many_training_steps=500 \ 
--model_dir=tf_files/models/ \ 
--summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}" \ 
--output_graph=tf_files/retrained_graph.pb \ 
--output_labels=tf_files/retrained_labels.txt \ 
--architecture="${ARCHITECTURE}" \ 
--image_dir=tf_files/flower_photos

If this is not working paste this one,

python3 -m scripts.retrain \ 
--bottleneck_dir=tf_files/bottlenecks \ 
--how_many_training_steps=500 \ 
--model_dir=tf_files/models/ \ 
--summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}" \ 
--output_graph=tf_files/retrained_graph.pb \ 
--output_labels=tf_files/retrained_labels.txt \ 
--architecture="${ARCHITECTURE}" \ 
--image_dir=tf_files/flower_photos

So for better accuracy you can change the step size in line 3 up to 4000. But it will take some much longer time. So  retrained_graph.pb and retrained_labels.txt are the files that we should take seriously. retrained_labels.txt has the label of the each flower and retrained_graph.pb is the file that holds all the analytic values.

bottleneck is an informal term we often use for the layer just before the final output layer that actually does the classification. "Bottleneck" is not used to imply that the layer is slowing down the network. We use the term bottleneck because near the output, the representation is much more compact than in the main body of the network.

Every image is reused multiple times during training. Calculating the layers behind the bottleneck for each image takes a significant amount of time. Since these lower layers of the network are not being modified their outputs can be cached and reused.

So the script is running the constant part of the network, everything below the node labeled Bottlene... above, and caching the results.

The command you ran saves these files to the bottlenecks/ directory. If you rerun the script, they'll be reused, so you don't have to wait for this part again.

So when the training is finished, to check whether our module is working or not use following method.

python -m scripts.label_image \ 
--graph=tf_files/retrained_graph.pb \ 
--image=tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg

or

python3 -m scripts.label_image \ 
--graph=tf_files/retrained_graph.pb \ 
--image=tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg


Paste above command in the terminal. As you can see this is a image of disy (flower_photos/daisy/21652746_cc379e0eea_m.jpg). You can use different image in a different directory. Then this will give an output like this,

daisy (score = 0.99071) 
sunflowers (score = 0.00595) 
dandelion (score = 0.00252) 
roses (score = 0.00049) 
tulips (score = 0.00032)

As you can see it will give some probabilistic value, describing its label (flower name). So it's over for this article. In the next article we'll see how to build the android application using this retrained module.

1 comment:

  1. Hey, It really is incredibly fantastic and informative post. Good to discover your site Very well article! I’m simply in love with it.

    On Demand Taxi booking app services are in high demand because this kind of platform provides advanced services to society. Want to know how much does it cost to develop a taxi booking app? If yes then contact us.

    ReplyDelete