Flower Image Classifier

Retty George
6 min readMar 25, 2022

--

picture reference 8
picture reference 8

The goal is to build an image classifier for the Kaggle Flower dataset which contains 4242 flower images that belongs to five different categories daisy, dandelion, rose, sunflower and tulip.

Before going deep in to the project Let’s see what is an image classification.

Image classification is a machine learning method used to recognize the category of an image. using different machine learning algorithm when we provide an image as input it predicts the category of the input image. First we will train an image classifier by providing different images that belongs to different categories. Once the image classifier is trained enough and the accuracy is hight it will start predicting the category of images correctly

Here I am using Tensorflow to build the image classifier

What is Tensorflow ?

TensorFlow is a deep learning machine learning library created by Google’s Brain team in 2015. Data is accepted in the form of tensors, which are higher-dimensional arrays. When dealing with enormous amounts of data, multidimensional arrays are quite useful.

First let’s import all the required libraries.

The first steps is to load the data and split the input data into test and train datasets.

Tensorflow consists of a function called image_dataset_from_directory which can be used to split our dataset to test and train [1]

Convolutional Neural Network

The higher performance of convolutional neural networks with picture, speech, or audio signal inputs sets them apart from conventional neural networks. They have three different types of layers:

  • Convolutional layer
  • Pooling layer
  • Fully-connected (FC) layer

A convolutional network’s first layer is called the convolutional layer. While further convolutional layers or pooling layers can be added after the convolutional layers, the fully-connected layer is the last layer. The CNN becomes more complicated with each layer, detecting more areas of the image.

Building Neural Network Model

My contribution involves reading the Tensorflow documentation understanding the different Tensorflow models, layers and implementing them one by one and then comparing the accuracy and performance.

Here is my progress…….

Model 1

First I implemented a basic neural network model using the sequential model in Tensorflow and the dense layer and calculate the accuracy (Referred Tensorflow tutorial [3])

From the above details it is pretty much evident that our initial model has resulted in Overfitting

Overfitting

Overfitting happens when your machine learning model shows high accuracy in the train dataset but very low accuracy in the test dataset.

Above you can clearly see a large difference in the accuracy of model over test and train data. With the test data the model is showing an accuracy of 85% whereas in the train data it is showing an accuracy of 46%. Well…this can be a sign of overfitting. So we need to find ways to prevent overfitting.

Let’s look into the different ways to prevent overfitting

As per the Tensorflow documentation the two effective method to prevent overfitting is to add weight regularization and dropout [7]

After Overfitting Prevention and with tiny sequential model with 2 Dense layers and 10 inputs we were able to achieve an accuracy of 0.48

Compare accuracy before and after handling overfitting

Before Overfitting Prevention
Before Overfitting Prevention
After Overfitting Prevention

Model 2

The next steps is to increase the number of inputs of dense layer and then compare the accuracy [3,6]

Firstly increased the number of inputs to 100

After increasing the inputs to 100 the model accuracy got increased to 51%

Model 3

The next is to increase the number of inputs to 200 and increase one more layer with inputs 200 in dense layer and compare the accuracy [3,6]

After introducing one more layer the accuracy got decreased to 43%. So, it’s evident that increasing the number of inputs does not increase the accuracy everytime

Model 4

Now Let’s experiment using conv2D layer in Tensorflow

Initial step was to train the model using 4 conv2D layers with inputs 16,32,64 and 128 and 4 MaxPooling2D along with 2 Dense layers. Reference [3,6]

After implementing the model with conv2D layer the accuracy got increased to a high 72% which is the highest so far..

Model 5

The next step was to increase the number of inputs in the last model with conv2D layer to 64,128,256 and 256.

This model resulted in an accuracy of 68% which is a good one but not the best so far

Overall it was nice learning Tensorflow and experimenting with different models and from the initial model which had an accuracy of 49% at the end I achieved an accuracy of 72%.

Accuracy of different models

My Contribution

My main contribution was to refer to the Tensorflow documentation and experiment with different hyperparameters and layers and then trying to improve the accuracy. With the initial model I got an accuracy of 49% and at the end i was able to increase the accuracy to 72%.

I have implemented a total of 5 different models

  1. Initial model with 2 dense layers with 10 input — accuracy of 49 %.
  2. Model with inputs increased to 100 as the initial model — accuracy of 51%
  3. Model with inputs layers increased to 3 layers with inputs 200,200 and 5 — accuracy of 43%

It’s evident that adding more layers will not result in a higher accuracy.

5. Implementing 4 conv2d (with inputs 16,32,64 and 168) layers and 2 dense layers — accuracy of 72%

6. Implementing 4 conv2d (with inputs 64,128,256 and 256) layers and 2 dense layers — accuracy of 68%

After Implementing the conv2d the accuracy got increased to almost 72%

My contribution also include trying out various activation models in conv2D layer and also training the model with different epoch and batch size values.

Challenges and Solutions

Initial challenge was to learn a completely new framework for building the image classifier model. For this I have gone through the Tensorflow documentation and after understanding the basic concepts, I started with tiny models and experimented with different parameters and then compared the accuracy.

The other challenge was overfitting. My first tiny model itself resulted in overfitting and then i have referred to Tensorflow overfitting documentation to understand how to prevent overfitting and thus i was able to handle overfitting.

The other challenges were like it was time consuming to train the model and once the model was trained well it started giving high accuracy.

Reference

  1. https://www.tensorflow.org/api_docs/python/tf/keras/utils/image_dataset_from_directory
  2. https://en.wikipedia.org/wiki/TensorFlow
  3. https://www.simplilearn.com/tutorials/deep-learning-tutorial/what-is-tensorflow
  4. https://machinelearningmastery.com/overfitting-and-underfitting-with-machine-learning-algorithms/
  5. https://www.ibm.com/cloud/learn/convolutional-neural-networks
  6. https://www.tensorflow.org/js/guide/models_and_layers
  7. https://www.tensorflow.org/tutorials/images/classification
  8. https://www.tensorflow.org/tutorials/keras/classification
  9. https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense
  10. https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D
  11. https://www.tensorflow.org/tutorials/keras/overfit_and_underfit
  12. https://www.freecodecamp.org/news/how-to-build-the-best-image-classifier-3c72010b3d55/
  13. https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/images/classification.ipynb#scrollTo=jWnopEChMMCn
  14. https://developer.apple.com/documentation/createml/creating_an_image_classifier_model

--

--