Image Classification using Tensorflow (on Docker + Windows)

Using Google’s Tensorflow to build an image classifier.

teavanist
7 min readJan 19, 2019

Edit: If you would like to get in touch with me, feel free to mail me at teavanist [at] gmail [dot] com ; Medium is not very conducive to conversations

Introduction

I started working on a Tensorflow based Image Classifier after watching one of Siraj Raval’s videos. Siraj is a great blogger who has encouraged a lot of people to start learning and experimenting with deep learning. His short videos are generally fun to watch for newbie ML enthusiasts.

I have based this exercise on his video — Build a TensorFlow Image Classifier in 5 Min which shows to build an image classifier for Darth Vader using Tensorflow.

But the title of his video can be misleading because, if you are new to Docker or Tensorflow or Linux, it’s going to take you way more than 5 minutes. It took me 2 weeks of after-office hours tinkering to finally nail this. But for someone familiar with these technologies, the whole thing might just be 5 minutes of work. Also, the original video is 2 years old; so a lot of links had changed.

This article is a lengthier complement to his video, addressing some of the challenges that I faced.

The whole exercise can be completed in 7 steps:

  • Step 1: Install Docker
  • Step 2: Install Tensorflow
  • Step 3: Install Tensorflow Hub
  • Step 4: Download your training images
  • Step 5: Download the scripts
  • Step 6: Retrain the model for roses and sunflowers
  • Step 7: Test your model

Basics

Google created a deep learning model called Inception which their engineers trained to recognize 1000+ categories of pictures. This model is now publicly available and using a technique called transfer learning, we can re-train this model to identify specific images.

There is a good tutorial about this technique on the Tensorflow site:

In this exercise, I will be using this model to classify sunflowers and roses.

Step 1: Install Docker

The first step is to install Docker. You can always go to the docker site for the latest version:

https://hub.docker.com/editions/community/docker-ce-desktop-windows

If you are going to run this on Windows, you will need Windows 10 Professional or Enterprise for Docker to work.

You will need to sign up
Login with your username and password
Click on the Get Docker link

Once you have downloaded the file, start the installation and you will come across the following windows:

After the installation is complete, you will see the Docker whale icon on your taskbar.

When you right click on it, following pop-up will appear:

Login with your Docker ID

Once you login and right click on the whale icon, you will see the Docker Desktop pop-up window:

This means that the installation is successful and that docker is running. Open a command prompt window and type the following:

docker ps

This lists your containers and as you can see, it’s empty:

Step 2: Install Tensorflow

We need to download the Tensorflow container. To do that, first we need to login to docker using the command below:

docker login

Use your Docker ID to login

Next, you need to download Tensorflow Docker container:

docker run -it tensorflow/tensorflow:latest-devel

Now when you type docker ps , you will find that the Tensorflow container is now listed:

Connect to your container using the container ID:

docker exec -it container_ID bash

Step 3: Install Tensorflow Hub

Once inside the container, install Tensorflow Hub:

pip install tensorflow_hub

Step 4: Download your training images

Organize your folders as shown in the picture below

The flower images can be downloaded from the following site:

http://download.tensorflow.org/example_images/flower_photos.tgz

flower_photos.tgz actually contains 5 folders.

Original flower_photos folder

I have used only two folders for this exercise —the roses and sunflowers folders.

Roses folder has 641 pictures while the sunflowers folder has 699 images. You can also use your own set of images (dogs and cats for example). My final flowerpics folder looks like this:

Step 5: Download the scripts

There are two scripts needed for this task. You can download them using the following links and save them to the scripts sub-folder.

retrain.py https://github.com/tensorflow/hub/raw/master/examples/image_retraining/retrain.py

If you are using your own set of images, please be aware that the retrain.py script needs at least 2 folders containing images to work. Below is a guidance (taken from the script itself) on how the images sub folders need to be organized:

label_image.py

https://github.com/tensorflow/tensorflow/raw/master/tensorflow/examples/label_image/label_image.py

Your scripts folder will look like this:

Now, copy the image_classification folder on the your Windows PC to your Tensorflow container. I usually open a new command prompt and type the following command:

docker cp .\image_classification\ container_ID:/image_classification
Do this on a new command prompt

Step 6: Retrain the model for roses and sunflowers

You can now re-train the Inception model for roses and sunflowers.

On the Tensorflow container, navigate to the image_classification directory. Once you inside the directory, type the command below:

python scripts/retrain.py --bottleneck_dir=/bottleneck/ --model_dir=/inception --output_labels=/retrained_labels.txt --output_graph=/retrained_graph.pb --image_dir=flowerpics/

The script will start retraining on the two image folders. This took around 15 minutes on my pc and may vary depending on your pc configuration.

Once the training is complete, you will get the following message.

Time to test your model.

Step 7: Test your model

Since the image set was from google, I downloaded a rose picture from bing.com and saved it as dublin_rose.jpg in the same image_classification folder on my Windows machine.

Just make sure whatever picture you choose, it has the .JPG extension.

This is what the picture looks like.

You need to now transfer this file to your container’s image_classification folder. You can use the docker cp command to do this.

Open a new command prompt, navigate to the image_classification folder and type the command below

docker cp dublin-rose.jpg container_ID:/image_classification/dublin-rose.jpg

My image_classification folder was on the Desktop. So here is how I did it.

In your Tensorflow container, if you type ls -al you will find the dublin_rose.jpg file.

We can run the test using the following command:

python scripts/label_image.py --graph=/retrained_graph.pb --labels=/retrained_labels.txt --input_layer=Placeholder --output_layer=final_result --image=dublin-rose.jpg

99% confidence that its a rose !

--

--

teavanist

Tea lover, ML hobbyist, dabbler, tinkerer, learning by doing