Image classification with tensorflow
While working through the Google YouTube series on machine learning I watched episode six Train an Image Classifier with Tensorflow for Poets. Since I create notebooks for every episode I did this here, too. The following text is taken from this notebook and it is a short tutorial on how to implement an image classifier with tensorflow in a very short amount of time. You can transfer this knowledge to every other image classification task without much effort. You can find the original python notebook here and my other notebooks on my github.
This notebook collects the experiments I did after watching the Google YouTube Tutorial Train an Image Classifier with TensorFlow for Poets. You can find the video here. The corresponding tutorial can be found here.
The goal of this notebook is to create a classifier for images of flowers. There are 5 types of flowers in this case: roses, tulips, sunflowers, daisies and dandelions. After the model is created you can download a random flower image from the internet and classify it. It worked very well in my case.
Step 1: Preparing the environment
The first thing I had to do was setting up the environment on my mac. I use Anaconda to manage my Python environments. So I created a new environment (Python 2.7) called tensorflow. To do this I use the Anaconda Navigator, a graphical user interface for Anaconda. I installed the following packages in this new environment: numpy, pandas, matplotlib. Tensorflow however could not be installed with the Navigator. So I opened a Terminal in this environment. I installed tensorflow with the following command:
conda install -c conda-forge tensorflow
The next step was cloning the tensorflow GitHub repository. This repository contains the script that will create our model later. You can clone it by navigating into a folder and calling
git clone https://github.com/tensorflow/tensorflow.git
Theoretically you are finished with your environment here. I however ran into a strange problem where I had to fix a line in the script we are going to call later. I don't know if that had something to do with my environment. If someone knows the answer don't hesitate to write a comment on my blog. I got the error that Python could not import tensorflow.python.framework.graph_util. It seems that the tensorflow team moved the graph_util.py recently, so depending on your tensorflow version you should edit the file retrain.py in the folder tensorflow/examples/image_retraining in the git repository. I changed the line
from tensorflow.python.framework import graph_util
from tensorflow.python.client import graph_util
Remember: only do this if you are getting an module not found error when you execute retrain.py.
Step 2: Downloading the images
I have a data folder in my user folder which contains all the data I experiment with. I navigated there with the terminal and called
curl -O http://download.tensorflow.org/example_images/flower_photos.tgz
After a while the .tgz file with the images was downloaded. I extracted it into the folder flower_photos. After doing that you can see 5 folders containing the images. These 5 folders represent the 5 classes the classifier will use later. You don't have to define these classes. The folder names will automatically be the class names.
Step 3: Training the model
Now, after setting up the environment and downloading the data, we can finally train our model. We won't train a model from scratch. The cool thing about this is, that we will use a model (Inception) that was created by Google and retrain it. Please refer to the video and the original (much longer) tutorial for more information on that.
Please call the following in your terminal and be very careful with the paths. Attention: I added new lines for every parameter for readability. Please note that you have to enter this as one long command.
--how_many_training_steps 500 --model_dir=../data/inception
The following parameters point to files/folders that will be created: bottleneck_dir, output_graph and output_labels. The bottleneck files aren't important for the model itself but they will be reused if you created another model. The graph and the labels contain the model. These two files are important for classifying an image. Please note the paths to these files.
Depending on the parameter how_many_training_steps the runtime of the script can be very high. I let it run over night on my MacBook Air. If you have a more powerful machine the script won't run longer than 30 minutes.
When the script has finished you can use your model to classify images.
Step 4: Classify images
To classify images you can use the following code. Please adjust the variables image_path, labels_path and graph_path to make it work. You can also switch the comments in the first three lines to make the code work as a script in the terminal.
I wanted to classify the following picture:
You can find the code for classifying the image in the notebook. The result of the classification was
roses (score = 0.99737) tulips (score = 0.00177) sunflowers (score = 0.00084) daisy (score = 0.00002) dandelion (score = 0.00001)
As you can see in my example the picture was classified as roses with a probability of 99,737%.