North Indian Food or South Indian Food ,Deep Learning Knows it all !!:Part 1

When you travel India from Kashmir to Kanyakumari you’ll experience different culture,different religion but what important aspect for me is the varieties of food available in different parts of my country from Samosa or Khaman Dhokla in North India to Idli or Dosa in South India ,this is some among many varieties of food you can have or rather enjoy tasting in India.

South Indian and North Indian food

You’ll must be wondering what deep learning has to do with Indian foods, well let me go back to the past to explain you the present .Ever since I started learning deep learning the first thing I learnt is how to make a image classifier using TensorFlow , Keras and last but not the least Scikit-learn . The image classifier I build were used to classify the dogs vs cats images, the individuals wearing glasses and not wearing glasses images and I even worked on a classification of seedlings out of 12 possible categories using fast.ai library,but I didn’t made anything which classify foods.

So,I thought why not this time make a fast and simple image classifier for Indian foods .Therefore in this article I will take you all on a journey of how I made a food classifier using TensorFlow Codelab to classify different images of North and South Indian foods.

But before moving towards the execution part I need to share some Software information which I used and this information stand very important with respect to the image classifier standpoint.

Software(look closely to the version)

  1. Anaconda with Python version 3.6.3.
  2. TensorFlow version 1.6.0.[make sure you have atleast version 1.2]
  3. Bash on Ubuntu on Windows[if you do not have Linux installed in your system].

Step 1: Creating a Dataset

In deep learning the most important part is the creation of dataset .So,in order to create dataset of North Indian food like Samosa, Khaman Dhokla and South Indian food like Idli, Dosa I followed this wonderful post “How to create a deep learning dataset using Google Images” from none other than the OpenCV guru Adrian Rosebrock in which he has shown how through few lines of code you can download the bulk of images from Google within no time.

First of all I created a folder named “Indian_foods_photos”. Under this folder I started creating sub-folders of different categories for which I was trying to build a Image classifier and then following the above mentioned post I started downloading the images of Idli, Samosa, Khaman Dhokla and Dosa. I never restricted myself in terms of number of images in a particular folder related to a particular category I collected as much images as possible of every food category.

Feeling hungry,not more than me

You’ll all must be thinking why i didn’t put every category of food for which I was building the classifier into one folder why i created sub-folders of each category,well the explanation is simple the classification script uses the folder names as label names, and the images inside each folder should be pictures that correspond to that label.

Oh my God !,that was a long explanation but from this point forward the explanation and step will be crisp and short.

Step 2: Clone the Git repository:

git clone https://github.com/googlecodelabs/tensorflow-for-poets-2

cd tensorflow-for-poets-2

Step 3:Move folder “Indian_foods_photos” under “tf_files”

This step is helpful for those who are using Bash on Ubuntu on Windows like me ,a Linux subsystem with windows 10 operating system,and those who have Linux installed in their system I don’t think so, they need any guidance on moving files from one directory to the other.

cp -r /mnt/c/Users/ADMIN/Documents/Indian_foods_photos tensorflow-for-poets-2/tf_files/

in order to see whether the folder is copied properly or not ,i gave these command

cd tf_files/Indian_foods_photos/

ls

Step 4:Passing the variable

This step is mother of all step in this step I tried to train the network with MobileNet architecture. MobileNet are optimized to be small and efficient, at the cost of some accuracy and require less processing time.

but before training my network I also set two things, first the “Input image resolution” to 224px .Unsurprisingly, feeding in a higher resolution image takes more processing time, but results in better classification accuracy.Then I gave the relative size of the model as 0.75.Both of these configuration options can be seen in the command below.

IMAGE_SIZE=224

ARCHITECTURE="mobilenet_0.75_${IMAGE_SIZE}"

KNOWLEDGE SECTION I [not a step]

So,after passing these above mentioned variable under tensorflow-for-poets-2 directory .It the perfect moment where I want to share one significant information with all of you .While building this classifier I used a model trained on the ImageNet Large Visual Recognition Challenge dataset and along with it I used transfer learning because doing deep learning from scratch can take days but by the use of transfer learning everything can be done in short orders.

KNOWLEDGE SECTION II [not a step]

As I mentioned in the Knowledge section I above that, I used Imagenet models which consists of networks with millions of parameters that can differentiate a large number of classes,adding to it I only trained the final layer of that network, so that’s why training ended in a reasonable amount of time.

ImageNet does not include any of these Indian foods I was training on here. However, the kinds of information that make it possible for ImageNet to differentiate among 1,000 classes are also useful for distinguishing other objects. By using this pre-trained network, I used that information as input to the final classification layer that distinguishes my Indian Food classes.

Step 5 :Running the training network

Now I passed these commands ,just after declaring the IMAGE_SIZE and ARCHITECTURE variable

python -m scripts.retrain \
--bottleneck_dir=tf_files/bottlenecks \
--model_dir=tf_files/models/"${ARCHITECTURE}" \
--summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}" \
--output_graph=tf_files/retrained_graph.pb \
--output_labels=tf_files/retrained_labels.txt \
--architecture="${ARCHITECTURE}" \
--image_dir=tf_files/Indian_foods_photos

Before I show you what happened when I passed the above command ,let me tell you what I experienced after running these few lines of code

By default, this script runs 4,000 training steps. Each step chooses 10 images at random from the training set, finds their bottlenecks [A bottleneck is an informal term we often use for the layer just before the final output layer that actually does the classification] from the cache, and feeds them into the final layer to get predictions. Those predictions are then compared against the actual labels to update the final layer’s weights through a backpropagation process.

As the process continues, I saw the reported accuracy improved. After all the training steps were completed, the script ran a final test accuracy evaluation on a set of images that are kept separate from the training and validation pictures. This test evaluation provided me the best estimate of how the trained model performed on the classification task.

Let’s just observe all the images describe through the words above

Downloading the mobilenet Architecture
creating bottleneck layers and then determining various parameters
The Final Accuracy

The Final Accuracy of my Model was 88.1%.

Step 6:Classifying the image

This is the final step in which by giving the below mentioned command,I determined how my food classifier was performing on different set of images of various Indian food

python -m scripts.label_image \
--graph=tf_files/retrained_graph.pb \
--image=tf_files/Indian_foods_photos/samosa/00000422.jpg

So,by these results I conclude my article .But my journey still hasn’t finished yet, this was only the first part of my journey ,my journey will end when I’ll explain you how using these results on my custom dataset I went on and created a food classifier Android App using custom TensorFlow app with Android Studio,therefore gear up for the part 2 of this article which will be released soon.

REFERENCES:

  1. The above article wouldn’t been possible without this TensorFlow for Poets Codelab,do refer it for any further clarification.
  2. In order to know more about deep learning you can check here.
  3. In order to know more about transfer learning you can check here.

One last thing…
If you liked this article, click the? below, and share it with other people so they can enjoy it as well.

Also, follow me on Medium if you want to! I would love that.


North Indian Food or South Indian Food ,Deep Learning Knows it all !!:Part 1 was originally published in Towards Data Science on Medium, where people are continuing the conversation by highlighting and responding to this story.

Source: Deep Learning on Medium