A Simple Tutorial to Classify Images Using TensorFlow — Step by Step Guide

Figure : 1 → Dog Breeds Dataset from Kaggle

In this tutorial, I am going to show how easily we can train images by categories using Tensorflow deep learning framework. For this tutorial, I have taken a simple use case from Kaggle’s prediction competition “Dog Breed Identification”, to determine the breed of a dog in an image.

My Github URL for this tutorial → https://github.com/RaghavPrabhu/Deep-Learning/tree/master/dogs_breed_classification

Pre-trained Models

There has been many CNNs trained models using various CNN architectures (Earlier in my blog explained these architectures LeNet, AlexNet, VGG, GoogLeNet Inception, ResNet) are created. These CNNs have been trained on the ILSVRC-2012-CLS image classification dataset.

These models, by default it can classify whether an object is a car or a truck or an elephant or an aeroplane or a cat or a dog and so on. To train you own dataset, we need a lot of images by its categorisation to achieve high accuracy. In some cases, we may not get more training dataset from any sources. During that scenarios, Data augmentation technique come in handy to solve this dataset limitation by transform and create new images from existing images to flip, rotate, apply colour variation etc., Refer Stanford’s data augmentation paper for more details.

Dataset Pre-Processing

First, to download train and test dataset along with its labels from Kaggle by using this URL. Refer below screenshot for details

Figure : 2→ Dog Breeds Dataset Site from Kaggle

Step 1 : Setup and Install

  • clone the project from my github repo
git clone git@github.com:RaghavPrabhu/Deep-Learning.git
cd Deep-Learning/dogs_breed_classification/
Figure : 3 → Tutorial Github Repo
  • Create virtual environment, if you want using virtualenv command
  • Install dependent libararies
pip install -r requirements.txt
  • Unzip all folders which we downloaded from Kaggle’s site
unzip train.zip
unzip test.zip
unzip labels.csv.zip

Step 2 : Organise your train folder

  • Run data processing python code to re-arrange folders by dogs breed name
python data_processing.py /Users/raghav/dogs_breed_classification/

The above python script will organise folders by dog breed names. Please refer below screenshot as organised output processed folders by dog breed name. Each dog breed folder contains corresponding dog images inside in it.

Figure : 4 → Organised output folders

Step 3: Train your model using our processed dataset

Run the below command to train your model using CNN architectures. By default, below script will download ‘Google’s inception architecture — ‘inception-2015–12–05.tgz’.

python retrain.py — image_dir=dataset/ — bottleneck_dir=bottleneck/ — how_many_training_steps=500 — output_graph=trained_model/retrained_graph.pb — output_labels=trained_model/retrained_labels.txt — summaries_dir=summaries

Following is the processing output.

Figure : 5 → Training our model

Tensorboard accuracy graph for the above trained model

Figure 6 → TensorFlow Accuracy Graph

To run TensorBoard, run this command

User:/dogs_breed_classification$ tensorboard — logdir summaries/ — host= — port=8888
TensorBoard 1.7.0 at (Press CTRL+C to quit)#

Step 4: Test your model

Run the below python script to classify your test images based on our pre-trained model.

python classify.py
Figure : 6 → Output dogs breed classifier

You can skip step 3 in the above tutorial and directly use my pre-trained model (trained_model/retrained_graph.pb) available in my github repo to test the model.