Deep Learning with Tensorflow: Part 2 — Image classification | by Matteo Kofler | Towards Data Science

Hi everybody, welcome back to my Tenserflow series, this is part 2. I already described the logic and functionality of neural networks and Tenserflow in the first part as well as I showed you how to set up your coding environment. If you haven’t checked it out already, find it here.
Image recognition and classification are the topics of this part. Using the Inception-v3 model, we’ll start classifying images using Google’s pre-trained ImageNet dataset and later move on to build our own classifier. Let’s begin.
The Inception v3 model is a deep convolutional neural network, which has been pre-trained for the ImageNet Large Visual Recognition Challenge using data from 2012, and it can differentiate between 1,000 different classes, like “cat”, “dishwasher” or “plane”.
The TensorFlow team already prepared a tutorial on how to execute the image classification on your machine. Nevertheless, I’ll show you as well.
Well, as we aren’t starting from scratch, start by cloning the Tensorflow models repository from GitHub. Run the following commands:
git clone https://github.com/tensorflow/models.gitcd models/tutorials/image/imagenetpython classify_image.py
If you haven’t installed Git yet, download it here.
classify_image.py downloads the trained model from Google’s backend, when the program runs the first time. You'll need about 200 MB of free space available on your hard disk.
The above commands will classify a supplied image of a panda bear.
If the model runs correctly, the script will produce the following output:
giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca (score = 0.88493)indri, indris, Indri indri, Indri brevicaudatus (score = 0.00878)lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens (score = 0.00317)custard apple (score = 0.00149)earthstar (score = 0.00127)
If you wish to supply other images, you may do so by editing the --image_file argument:
python classify_image.py --iAmAdifferentImage.jpg
That wasn’t that hard, was it? But don’t worry, it will get more challenging. The above script allows us to classify images on pre-trained classes by the Google team. But what if you wanted to retrain Inception for our own classes?
We’ll add four new classes to the Inception model, called “circle”, “square”, “plus” and “triangle”.
Start by cloning my repository from GitHub, type:
git clone https://github.com/koflerm/tensorflow-image-classifier.git
After you have cloned the mentioned repository, we have to somehow tell Inception what the correct label for each image is.
Since we’re classifying if an object is a triangle, square, plus or circle, we need to add a ‘training_dataset’ directory and fill it with four subfolders, named after the class’ label. Those folders will contain data sets (images) for the subjects, for whom the classification is to be performed.
/--- /training_dataset|    ||    --- /circle|    |    circle1.jpg|    |    circle_small_red.png|    |    ...|    ||    --- /square|         square.jpg|         square3.jpg|         ...
Next, we have to actually get the data set used for the training process. Since downloading every single image manually would be very time-consuming, we use a Chrome Extension named ‘ Fatkun Batch Download Image’. It allows us to automatically download the first 50 images from the Google images search. Simply copy them into the folders (image format is not important!)
After setting up all directories and data sets, let the training begin! Execute the train.sh script by double clicking it. The script installs the Inception model (if not already installed) and initiates the re-training process for the specified image data sets.
Once the process is complete, it will return a training accuracy somewhere around 90 percent.
The retrained labels, -graphs and the training summary will be saved in a folder named tf_files , in case you want to take a look at it.
After re-training the model, it’s now time to test the model on our own images. Download or draw another image and copy it into the root directory. I downloaded another “triangle” image. Execute the test by typing:
python classify.py downloadedImage.jpg
Great, my triangle is actually classified as a triangle!
(Optional): If you have added some images or subfolders for new classes, but you don’t want to call the train.sh and then the classify.sh , you can combine the input by typing:
python retrain.py — bottleneck_dir=tf_files/bottlenecks — how_many_training_steps=500 — model_dir=inception — summaries_dir=tf_files/training_summaries/basic — output_graph=tf_files/retrained_graph.pb — output_labels=tf_files/retrained_labels.txt — image_dir=training_dataset — eval_step_interval=100 & python classify.py image.jpg
Note: image.jpg is the image you want to test
Although the classification does work most of the time, there are some issues:
Inception is trained for single-label image classification, which means that multi-label classification is not possible. If i still wanted the above images to be classified as circle, I simply had to use a more accurate training set for the class. That is in fact a little more time-consuming, the Chrome Extension alone simply can’t provide you with all the best results.
Basically, the script loads the pre-trained Inception v3 model, removes the old top layer, and trains a new one on the geometric shapes classes you wanted to add. This is called transfer learning. The re-training works in two phases — Bottleneck and Training:
The first phase analyzes all the images on disk and calculates the bottleneck values for each of them. ‘Bottleneck’ is an informal term used for the layer just before the final output layer that actually does the classification. It processes the images to a meaningful and compact summary of the images.
The second phase is the actual training of the top layer of the network. You could see a series of step outputs, each one showing training accuracy, validation accuracy, and the cross entropy. The training accuracy shows what percent of the images used in the training were labeled with the correct class. The validation accuracy is the precision on a randomly-selected group of images from a different set. Cross entropy is a loss function which gives a glimpse into how well the learning process is progressing.
If you want to dig in deeper, go this way
Congratulations, you are now able to classify images using Tensorflow. In the third part, I’ll show you some other use cases for Tensorflow. So stay tuned till the next post!
__
Link to Part 3: https://medium.com/towards-data-science/deep-learning-with-tensorflow-part-3-music-and-text-generation-8a3fbfdc5e9b