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

Prabhu Raghav
3 min readApr 28, 2018

--

Figure: 1 → Dog Breeds Dataset from Kaggle

In this tutorial, I am going to show how easily we can train images by categories using the 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 have 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 airplane or a cat or a dog and so on. To train your own dataset, we need a lot of images by its categorization to achieve high accuracy. In some cases, we may not get more training datasets from any sources. During that scenarios, the Data augmentation technique comes in handy to solve this dataset limitation by transform and create new images from existing images to flip, rotate, apply color variation, etc., Refer Stanford’s data augmentation paper for more details.

Dataset Pre-Processing

First, to download the 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 a virtual environment
  • Install dependent libraries
* virtualenv -p python3 env
* source env/bin/activate
* 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
python3 data_processing.py

The above python script will organize folders by dog breed names. Please refer below screenshot as an organized output processed folders by dog breed name. Each dog breed folder contains corresponding dog images inside 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, the below script will download ‘Google’s inception architecture — ‘inception-2015–12–05.tgz’.

python3 retrain.py --image_dir=build/dataset/ --bottleneck_dir=build/bottleneck/ --how_many_training_steps=500 --output_graph=build/trained_model/retrained_graph.pb --output_lables=build/trained_model/retrained_labels.txt --summaries_dir=build/summaries

The 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=0.0.0.0 — port=8888TensorBoard 1.7.0 at http://0.0.0.0:8888 (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.

--

--

Prabhu Raghav

Co-Founder & CTO — Decisionfacts.ai - Data Scientist, Architect, Full Stack Developer — Tech Enthusiast, Learner